### Naive rm Program Example Source: https://www.boost.org/doc/libs/latest/libs/nowide/index A basic C++ implementation of the 'rm' command for removing files, illustrating how POSIX systems handle file paths as null-terminated strings that are independent of locale encoding. ```cpp #include int main(int argc,char **argv) { for(int i=1;i ``` -------------------------------- ### Standalone Nowide Library Usage Example Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/index Demonstrates the usage of the standalone Nowide library for handling UTF-8 encoded arguments and file streams. This example shows how to use `nowide::args`, `nowide::cerr`, `nowide::cout`, and `nowide::ifstream` for Unicode-aware console I/O and file operations. ```c++ #include #include #include int main(int argc, char **argv) { nowide::args a(argc, argv); // Fix arguments - make them UTF-8 if (argc != 2) { nowide::cerr << "Usage: file_name" << std::endl; // Unicode aware console return 1; } nowide::ifstream f(argv[1]); // argv[1] - is UTF-8 if (!f) { // the console can display UTF-8 nowide::cerr << "Can't open a file " << argv[1] << std::endl; return 1; } int total_lines = 0; while (f) { if (f.get() == '\n') total_lines++; } f.close(); // the console can display UTF-8 nowide::cout << "File " << argv[1] << " has " << total_lines << " lines" << std::endl; return 0; } ``` -------------------------------- ### Standalone Nowide Example: File Processing and Argument Handling Source: https://www.boost.org/doc/libs/latest/libs/nowide/index Demonstrates how to use the standalone Nowide library for UTF-8 aware command-line argument processing and file input/output. It shows argument fixing, Unicode-aware console output, and reading from files with UTF-8 encoded paths. ```cpp #include #include #include int main(int argc,char **argv) { nowide::args a(argc,argv); // Fix arguments - make them UTF-8 if(argc!=2) { nowide::cerr << "Usage: file_name" << std::endl; // Unicode aware console return 1; } nowide::ifstream f(argv[1]); // argv[1] - is UTF-8 if(!f) { // the console can display UTF-8 nowide::cerr << "Can't open a file " << argv[1] << std::endl; return 1; } int total_lines = 0; while(f) { if(f.get() == '\n') total_lines++; } f.close(); // the console can display UTF-8 nowide::cout << "File " << argv[1] << " has " << total_lines << " lines" << std::endl; return 0; } ``` -------------------------------- ### Boost.Nowide args Class Usage Example (C++) Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/classboost_1_1nowide_1_1args Demonstrates the usage of the `boost::nowide::args` class to ensure UTF-8 encoded command-line arguments and environment variables are available within the `main` function on Windows. The `args` object manages the conversion and restoration of these values. ```cpp int main(int argc, char** argv, char** env) { boost::nowide::args _(argc, argv, env); // Note the _ as a "don't care" name for the instance // Use argv and env as usual, they are now UTF-8 encoded on Windows return 0; // Memory held by args is released } ``` -------------------------------- ### Build Boost.Nowide with CMake Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/index Build the Boost.Nowide library using the experimental Boost CMake build system. This command configures CMake to build only the nowide library from the Boost root directory. Replace with the actual path to your Boost installation and add any additional CMake options as needed. ```bash cmake -DBOOST_INCLUDE_LIBRARIES=nowide ``` -------------------------------- ### Naive 'rm' implementation on POSIX Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/index A basic C++ implementation of the 'rm' command that iterates through command-line arguments and removes files. This example highlights how POSIX systems treat strings as null-terminated sequences and how altering them based on locale could lead to incorrect behavior. ```c++ #include int main(int argc, char **argv) { for (int i = 1; i < argc; i++) std::remove(argv[i]); return 0; } ``` -------------------------------- ### Buffer Initialization and Configuration in C++ Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/filebuf_8hpp_source Provides make_buffer and set_unbuffered_read functions for stream buffer setup. make_buffer allocates character buffer if size is specified; set_unbuffered_read determines buffering mode based on binary flag and buffer size, disabling buffering for text mode due to seek/position tracking limitations. ```cpp void make_buffer() { if(buffer_) return; if(buffer_size_ > 0) { buffer_ = new char[buffer_size_]; owns_buffer_ = true; } } void set_unbuffered_read() { unbuffered_read_ = !(mode_ & std::ios_base::binary) || buffer_size_ == 0u; } ``` -------------------------------- ### Integrate UTF-8 with Boost.Filesystem and std::filesystem Source: https://www.boost.org/doc/libs/latest/libs/nowide/index This snippet demonstrates how to integrate Boost.Nowide with Boost.Filesystem and std::filesystem to handle UTF-8 encoded paths. It includes examples for writing and reading paths from streams using `boost::nowide::quoted`. This is particularly useful on Windows where the default narrow encoding may not be UTF-8. ```c++ #include #include #include std::string write(const std::filesystem::path& path) { std::ostringstream s; s << boost::nowide::quoted(path); return s.str(); } std::experimental::path read(std::istream& is) { std::filesystem::path path; is >> boost::nowide::quoted(path); return path; } ``` -------------------------------- ### Get basic_filebuf Internal Buffer Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/fstream_8hpp_source Returns a pointer to the internal buffer of the basic_filebuf object. This method is const and returns a non-const pointer by casting, allowing modification of the internal buffer. ```C++ internal_buffer_type* rdbuf() const { return const_cast(&buf_); } ``` -------------------------------- ### Boost.Nowide Main Function Arguments (argc, argv, env) Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/index A class that makes the `argc`, `argv`, and `env` parameters of the `main` function UTF-8 aware on Windows. This utility simplifies processing command-line arguments and environment variables that may contain Unicode characters, ensuring they are correctly interpreted as UTF-8 without manual conversion to the Wide API. ```cpp #include #include #include int main(int argc, char **argv) { // Initialize Boost.Nowide argument handling boost::nowide::args args(argc, argv); // Now, args.get_argv() returns a vector of strings interpreted as UTF-8 on Windows // Example: Printing all command-line arguments for (const char* arg : args.get_argv()) { boost::nowide::cout << "Argument: " << arg << std::endl; } // Example: Accessing environment variables in a UTF-8 aware manner boost::nowide::cout << "User variable: " << boost::nowide::getenv("USERNAME") << std::endl; return 0; } ``` -------------------------------- ### Build Boost.Nowide with b2 (Command Line) Source: https://www.boost.org/doc/libs/latest/libs/nowide/index Instructions for building the Boost.Nowide library as part of the Boost build system using `b2`. This snippet highlights the requirement for C++11 features and how to explicitly enable C++11 mode if the compiler defaults to an older standard. ```bash # Build Boost.Nowide, ensuring C++11 mode b2 cxxstd=11 ``` -------------------------------- ### Get Converted String Pointer Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/classboost_1_1nowide_1_1basic__stackstring Member function that returns a pointer to the converted, NULL-terminated string stored in the internal buffer. Returns NULL if no string was converted or if a NULL pointer was passed to the constructor or convert method. ```cpp output_char* get() const output_char* get() const ``` -------------------------------- ### Get string length using basic_stackstring in C++ Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/classboost_1_1nowide_1_1basic__stackstring Returns the current length of the string stored in the basic_stackstring, excluding the NULL terminator. If NULL is stored, it returns NULL. This is a const method of the boost::nowide::basic_stackstring class. ```C++ template size_t boost::nowide::basic_stackstring< CharOut, CharIn, BufferSize >::length() const ``` -------------------------------- ### Handle Buffer Underflow in C++ Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/filebuf_8hpp_source Refills the get area when empty during read operations. Supports both buffered and unbuffered read modes. Returns EOF on file end or error, otherwise returns the next character as integer. ```cpp int underflow() override { if(!(mode_ & std::ios_base::in) || !stop_writing()) return EOF; if(unbuffered_read_) { const int c = std::fgetc(file_); if(c == EOF) return EOF; last_char_[0] = Traits::to_char_type(c); setg(last_char_, last_char_, last_char_ + 1); } else { make_buffer(); const size_t n = std::fread(buffer_, 1, buffer_size_, file_); setg(buffer_, buffer_, buffer_ + n); if(n == 0) return EOF; } return Traits::to_int_type(*gptr()); } ``` -------------------------------- ### Boost.Nowide basic_ofstream Constructors Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/classboost_1_1nowide_1_1basic__ofstream-members Demonstrates the different ways to construct a `basic_ofstream` object in Boost.Nowide. Supports initialization with C-style strings, `std::string`, and a `Path` object, along with specifying open modes. Copy construction is deleted, while move construction and assignment are supported. ```cpp namespace boost { namespace nowide { template class basic_ofstream : public std::basic_ofstream { public: // Default constructor basic_ofstream(); // Constructor with C-style string file name explicit basic_ofstream(const char *file_name, std::ios_base::openmode mode = std::ios_base::out); // Constructor with std::string file name explicit basic_ofstream(const std::string &file_name, std::ios_base::openmode mode = std::ios_base::out); // Constructor with Path object template explicit basic_ofstream(const Path &file_name, detail::enable_if_path_t mode = std::ios_base::out); // Deleted copy constructor basic_ofstream(const basic_ofstream &) = delete; // Move constructor basic_ofstream(basic_ofstream &&other) noexcept; // Deleted copy assignment operator basic_ofstream &operator=(const basic_ofstream &) = delete; // Move assignment operator basic_ofstream &operator=(basic_ofstream &&rhs); }; } // namespace nowide } // namespace boost ``` -------------------------------- ### Boost.Nowide Includes and Namespace Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/config_8hpp Standard include directives for Boost.Nowide and Boost.Config, along with the declaration of the boost::nowide namespace. This namespace contains implementations for UTF-8 string compatibility on Windows. ```c++ #include #include #include namespace boost::nowide { // ... implementations for UTF-8 string handling on Windows } ``` -------------------------------- ### basic_stackstring Constructors and Assignments (C++) Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/stackstring_8hpp_source Details the constructors and assignment operators for the basic_stackstring class. These allow for creating stackstrings from NULL, copying existing stackstrings, and initializing them with character ranges or C-style strings. Proper initialization is key to using the class effectively. ```cpp basic_stackstring() basic_stackstring(const basic_stackstring &other) basic_stackstring(const input_char *input) basic_stackstring(const input_char *begin, const input_char *end) basic_stackstring & operator=(const basic_stackstring &other) ``` -------------------------------- ### Construct boost::nowide::basic_fstream Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/classboost_1_1nowide_1_1basic__fstream Demonstrates various ways to construct a `boost::nowide::basic_fstream` object. These constructors accept different types of file name arguments, including C-style strings, `std::string`, and path objects, along with optional open modes. ```cpp boost::nowide::basic_fstream file1("example.txt", std::ios_base::in | std::ios_base::out); boost::nowide::basic_fstream file2(std::string("another_example.txt")); // Example with a Path object (requires Boost.Filesystem or similar) // Assuming Path is a type compatible with boost::nowide // boost::nowide::basic_fstream file3(Path("path/to/file.txt")); // Move constructor example boost::nowide::basic_fstream file4(std::move(file1)); ``` -------------------------------- ### Stop Reading and Writing Stream Operations in C++ Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/filebuf_8hpp_source Implements stop_reading and stop_writing helper functions for managing stream buffer state. stop_reading clears the get area and seeks to proper position; stop_writing flushes the put area to file. Both handle edge cases with pragma directives for compiler warnings. ```cpp bool stop_reading() { if(!gptr()) return true; const auto off = gptr() - egptr(); setg(nullptr, nullptr, nullptr); if(!off) return true; #if defined(__clang__) #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare" #endif if(off < std::numeric_limits::min()) return false; #if defined(__clang__) #pragma clang diagnostic pop #endif return detail::fseek(file_, static_cast(off), SEEK_CUR) == 0; } bool stop_writing() { if(pptr()) { const char* const base = pbase(); const size_t n = pptr() - base; setp(nullptr, nullptr); if(n && std::fwrite(base, 1, n, file_) != n) return false; } return true; } ``` -------------------------------- ### Include Boost.Nowide configuration and UTF headers Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/console__buffer_8hpp_source Includes necessary headers for Boost.Nowide console buffer implementation: config.hpp for platform-specific configuration, utf.hpp for UTF character traits, standard streambuf for stream buffering, and vector for dynamic buffer management. ```cpp #include #include #include #include ``` -------------------------------- ### Include Boost.Nowide fstream Header Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/classboost_1_1nowide_1_1basic__fstream This header file provides the necessary definitions for the `boost::nowide::basic_fstream` class template. Ensure this include statement is present in your C++ source files when using this class. ```cpp #include ``` -------------------------------- ### Boost.Nowide basic_ofstream Constructor Reference Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/classboost_1_1nowide_1_1basic__ofstream This section details the constructors for the boost::nowide::basic_ofstream class. These constructors allow for the creation of output file streams, accepting file names as C-style strings, std::string, or generic path types. They also support specifying the open mode for the file. ```cpp #include // Constructor accepting a C-style string for the file name basic_ofstream(const char* file_name, std::ios_base::openmode mode = std::ios_base::out); // Constructor accepting a std::string for the file name basic_ofstream(const std::string& file_name, std::ios_base::openmode mode = std::ios_base::out); // Template constructor accepting a generic path type template basic_ofstream(const Path& file_name, detail::enable_if_path_t mode = std::ios_base::out); // Deleted copy constructor basic_ofstream(const basic_ofstream&) = delete; // Move constructor basic_ofstream(basic_ofstream&& other) noexcept; ``` -------------------------------- ### Legacy Compatibility Header Structure - C++ Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/detail_2convert_8hpp_source Demonstrates the header guard and namespace structure that provides backward compatibility for code using the detail namespace. This header re-exports functions from boost::nowide::utf to boost::nowide::detail for legacy compatibility. ```C++ #ifndef BOOST_NOWIDE_DETAIL_CONVERT_HPP_INCLUDED #define BOOST_NOWIDE_DETAIL_CONVERT_HPP_INCLUDED #include namespace boost { namespace nowide { namespace detail { using boost::nowide::utf::convert_buffer; using boost::nowide::utf::convert_string; using boost::nowide::utf::strlen; } // namespace detail } // namespace nowide } // namespace boost #endif ``` -------------------------------- ### Boost.Nowide basic_filebuf Constructor and Destructor Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/classboost_1_1nowide_1_1basic__filebuf_3_01char_01_4 Provides the default constructor for creating a new basic_filebuf object. Copy and move constructors/assignment operators are deleted or defined as required for proper object management. ```cpp boost::nowide::basic_filebuf< char >::basic_filebuf (); boost::nowide::basic_filebuf< char >::basic_filebuf (const basic_filebuf &) = delete; basic_filebuf & boost::nowide::basic_filebuf< char >::operator= (const basic_filebuf &) = delete; boost::nowide::basic_filebuf< char >::basic_filebuf (basic_filebuf &&other) noexcept; basic_filebuf & boost::nowide::basic_filebuf< char >::operator= (basic_filebuf &&other) noexcept; ``` -------------------------------- ### Configure UTF-8 Filesystem Locale in Boost.Nowide Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/filesystem_8hpp_source Creates and applies a UTF-8 codecvt locale to Boost.Filesystem paths for cross-platform wide character support. This function constructs a new locale with UTF-8 codec conversion and imbu it into the filesystem path, ensuring proper encoding handling. Dependencies include boost/nowide/utf8_codecvt.hpp and boost/filesystem/path.hpp. ```cpp namespace boost { namespace nowide { inline std::locale nowide_filesystem() { std::locale tmp = std::locale(std::locale(), new boost::nowide::utf8_codecvt()); return boost::filesystem::path::imbue(tmp); } } // namespace nowide } // namespace boost ``` -------------------------------- ### Boost.Nowide UTF-8 Aware main Function Arguments Source: https://www.boost.org/doc/libs/latest/libs/nowide/index A class that enables the `argc`, `argv`, and `env` parameters of the `main` function to use UTF-8 encoding on Windows. This simplifies handling command-line arguments and environment variables that may contain non-ASCII characters, by converting them from the system's native Wide API format (UTF-16) to UTF-8. ```cpp #include #include int main(int argc, char **argv) { boost::nowide::args args(argc, argv); // args.argc and args.argv are now UTF-8 aware on Windows for (int i = 0; i < args.argc; ++i) { boost::nowide::cout << "Arg " << i << ": " << args.argv[i] << std::endl; } return 0; } ``` -------------------------------- ### Build Boost.Nowide with b2 in C++11 Mode Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/index Build the Boost.Nowide library using the b2 build system with C++11 standard support. This command passes the cxxstd=11 parameter to ensure the compiler uses C++11 features, which are required by the library. If the library fails to build, check configuration output for missing C++11 features. ```bash b2 cxxstd=11 ``` -------------------------------- ### Boost.Nowide basic_filebuf File Opening Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/classboost_1_1nowide_1_1basic__filebuf_3_01char_01_4 These functions open files using UTF-8 encoded paths. They are similar to std::filebuf::open but specifically handle string and C-style string inputs for the file name, accepting standard open modes. ```cpp basic_filebuf * open (const char *s, std::ios_base::openmode mode); basic_filebuf * open (const std::string &s, std::ios_base::openmode mode); template detail::enable_if_path_t< Path, basic_filebuf * > open (const Path &file_name, std::ios_base::openmode mode); ``` -------------------------------- ### basic_stackstring Conversion Functions (C++) Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/stackstring_8hpp_source Documents the conversion functions within basic_stackstring, enabling the transformation of input character sequences into the internal output character format. These functions are essential for handling different UTF encodings and ensuring correct string representation. ```cpp output_char * convert(const input_char *input) output_char * convert(const input_char *begin, const input_char *end) ``` -------------------------------- ### Boost.Nowide Args Class Constructor (Windows) Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/args_8hpp_source This constructor for the 'args' class on Windows initializes and modifies the command-line arguments. It uses Windows API functions like CommandLineToArgvW and GetCommandLineW to retrieve wide character arguments, converts them to a suitable format, and updates the provided argc and argv pointers. It also handles environment variable conversion if an environment pointer is provided. ```cpp #include #ifdef BOOST_WINDOWS #include #include #include #include namespace boost { namespace nowide { class args { public: args(int& argc, char**& argv) : old_argc_(argc), old_argv_(argv), old_env_(0), old_argc_ptr_(&argc), old_argv_ptr_(&argv), old_env_ptr_(0) { fix_args(argc, argv); } args(int& argc, char**& argv, char**& env) : old_argc_(argc), old_argv_(argv), old_env_(env), old_argc_ptr_(&argc), old_argv_ptr_(&argv), old_env_ptr_(&env) { fix_args(argc, argv); fix_env(env); } ~args() { if(old_argc_ptr_) *old_argc_ptr_ = old_argc_; if(old_argv_ptr_) *old_argv_ptr_ = old_argv_; if(old_env_ptr_) *old_env_ptr_ = old_env_; } private: class wargv_ptr { wchar_t** p; int argc; public: wargv_ptr() { p = CommandLineToArgvW(GetCommandLineW(), &argc); } ~wargv_ptr() { if(p) LocalFree(p); } wargv_ptr(const wargv_ptr&) = delete; wargv_ptr& operator=(const wargv_ptr&) = delete; int size() const { return argc; } operator bool() const { return p != nullptr; } const wchar_t* operator[](size_t i) const { return p[i]; } }; class wenv_ptr { wchar_t* p; public: wenv_ptr() : p(GetEnvironmentStringsW()) {} ~wenv_ptr() { if(p) FreeEnvironmentStringsW(p); } wenv_ptr(const wenv_ptr&) = delete; wenv_ptr& operator=(const wenv_ptr&) = delete; operator const wchar_t*() const { return p; } }; void fix_args(int& argc, char**& argv) { const wargv_ptr wargv; if(!wargv) throw std::runtime_error("Could not get command line!"); args_.resize(wargv.size() + 1, 0); arg_values_.resize(wargv.size()); for(int i = 0; i < wargv.size(); i++) args_[i] = arg_values_[i].convert(wargv[i]); argc = wargv.size(); argv = &args_[0]; } void fix_env(char**& env) { const wenv_ptr wstrings; if(!wstrings) throw std::runtime_error("Could not get environment strings!"); const wchar_t* wstrings_end = 0; int count = 0; for(wstrings_end = wstrings; *wstrings_end; wstrings_end += wcslen(wstrings_end) + 1) count++; env_.convert(wstrings, wstrings_end); envp_.resize(count + 1, 0); char* p = env_.get(); int pos = 0; for(int i = 0; i < count; i++) { if(*p != '=') envp_[pos++] = p; p += strlen(p) + 1; } env = &envp_[0]; } std::vector args_; std::vector arg_values_; stackstring env_; std::vector envp_; int old_argc_; char** old_argv_; char** old_env_; int* old_argc_ptr_; char*** old_argv_ptr_; char*** old_env_ptr_; }; } // namespace nowide } // namespace boost #endif ``` -------------------------------- ### Boost.Nowide Args Constructors and Destructor (C++) Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/classboost_1_1nowide_1_1args-members This snippet shows the inline member declarations for the `boost::nowide::args` class in C++. It includes the constructors that take argument counts and pointers, and the destructor. These are fundamental to initializing and cleaning up the arguments handled by the library. ```cpp namespace boost { namespace nowide { class args { public: inline args(int &argc, char **&argv); inline args(int &argc, char **&argv, char **&env); ~args(); }; } } ``` -------------------------------- ### Boost.Nowide utf8_codecvt Class Members Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/classboost_1_1nowide_1_1utf8__codecvt_3_01CharType_00_014_01_4-members This section lists the complete members for boost::nowide::utf8_codecvt< CharType, 4 >, including all inherited members. ```APIDOC ## Class boost::nowide::utf8_codecvt< CharType, 4 > ### Description This class provides a `codecvt` facet that converts between UTF-8 and a specified `CharType`. ### Members #### Protected Functions - **do_always_noconv**() const noexcept override - Description: Checks if the facet always operates without conversion. - Method: `const noexcept` - **do_encoding**() const noexcept override - Description: Returns the maximum number of bytes for the narrow encoding. - Method: `const noexcept` - **do_in**(std::mbstate_t &, const char *from, const char *from_end, const char *&from_next, uchar *to, uchar *to_end, uchar *&to_next) const override - Description: Converts a sequence of narrow characters to wide characters. - Method: `const override` - **do_length**(std::mbstate_t &, const char *from, const char *from_end, size_t max) const override - Description: Determines the length of the next character sequence in the narrow encoding. - Method: `const override` - **do_max_length**() const noexcept override - Description: Returns the maximum length of an encoding sequence. - Method: `const noexcept` - **do_out**(std::mbstate_t &, const uchar *from, const uchar *from_end, const uchar *&from_next, char *to, char *to_end, char *&to_next) const override - Description: Converts a sequence of wide characters to narrow characters. - Method: `const override` - **do_unshift**(std::mbstate_t &, char *from, char *, char *&next) const override - Description: Returns the sequence of shift characters. - Method: `const override` #### Protected Typedefs - **uchar** - Description: An alias for the character type used in conversions. #### Public Functions - **utf8_codecvt**(size_t refs=0) - Description: Constructor for the `utf8_codecvt` facet. - Method: `inline` ``` -------------------------------- ### Conditional fopen implementation for Windows and POSIX Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/index Boost.Nowide provides platform-specific implementations where Windows offers UTF-8 aware fopen, while POSIX systems use standard library aliases. The Windows version interprets file_name and mode parameters as UTF-8 strings. ```cpp namespace boost { namespace nowide { #ifdef BOOST_WINDOWS inline FILE *fopen(const char* name, const char* mode) { ... } #else using std::fopen; #endif } } ``` -------------------------------- ### Boost.Nowide basic_filebuf Protected Member Functions Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/classboost_1_1nowide_1_1basic__filebuf_3_01char_01_4 These protected member functions are overrides from the base class and handle the core stream buffer operations such as setting buffers, synchronizing streams, managing overflow, reading/writing data, and seeking positions within the stream. ```cpp std::streambuf * setbuf (char *s, std::streamsize n) override; int sync () override; int overflow (int c = EOF) override; std::streamsize xsputn (const char *s, std::streamsize n) override; int underflow () override; std::streamsize xsgetn (char *s, std::streamsize n) override; int pbackfail (int c = EOF) override; std::streampos seekoff (std::streamoff off, std::ios_base::seekdir seekdir, std::ios_base::openmode = std::ios_base::in | std::ios_base::out) override; std::streampos seekpos (std::streampos pos, std::ios_base::openmode m = std::ios_base::in | std::ios_base::out) override; void imbue (const std::locale &loc) override; ``` -------------------------------- ### Initialize basic_filebuf Constructor with Default Settings Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/filebuf_8hpp_source Default constructor for basic_filebuf that initializes all member variables to safe default states. Sets file pointer to nullptr, buffer to nullptr with default BUFSIZ, and initializes stream buffer pointers using setg() and setp() methods. This constructor supports move semantics and is used when creating new file buffer objects. ```cpp basic_filebuf() : file_(nullptr), buffer_(nullptr), buffer_size_(BUFSIZ), owns_buffer_(false), unbuffered_read_(false), last_char_(), mode_(std::ios_base::openmode(0)) { setg(nullptr, nullptr, nullptr); setp(nullptr, nullptr); } ``` -------------------------------- ### UTF-8 Aware fopen on Windows Source: https://www.boost.org/doc/libs/latest/libs/nowide/index This code illustrates how Boost.Nowide provides a UTF-8 aware version of `fopen` on Windows systems. On POSIX systems, it acts as an alias to the standard `fopen`. This ensures that file names and modes are correctly interpreted as UTF-8 strings. ```c++ namespace boost { namespace nowide { #ifdef BOOST_WINDOWS inline FILE *fopen(const char* name, const char* mode) { ... } #else using std::fopen #endif } // nowide } // boost ``` -------------------------------- ### Configure File Buffer Replacement Strategy Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/config_8hpp_source Enables BOOST_NOWIDE_USE_FILEBUF_REPLACEMENT on Windows platforms or when explicitly defined for documentation. Falls back to the wide character overloads setting on non-Windows platforms if not explicitly configured. ```cpp #if defined(BOOST_WINDOWS) || defined(BOOST_NOWIDE_DOXYGEN) #ifdef BOOST_NOWIDE_USE_FILEBUF_REPLACEMENT #undef BOOST_NOWIDE_USE_FILEBUF_REPLACEMENT #endif #define BOOST_NOWIDE_USE_FILEBUF_REPLACEMENT 1 #elif !defined(BOOST_NOWIDE_USE_FILEBUF_REPLACEMENT) #define BOOST_NOWIDE_USE_FILEBUF_REPLACEMENT BOOST_NOWIDE_USE_WCHAR_OVERLOADS #endif ``` -------------------------------- ### Boost.Nowide UTF-8 Aware cstdlib Functions Source: https://www.boost.org/doc/libs/latest/libs/nowide/index Offers UTF-8 aware replacements for standard C library functions such as system, getenv, setenv, unsetenv, and putenv. Similar to cstdio functions, these operate with UTF-8 on Windows, converting to UTF-16 for native APIs, while acting as direct aliases on POSIX systems. ```cpp #include #include int main() { // Example usage on Windows (UTF-8 aware) // On POSIX, this is std::getenv const char* env_var = boost::nowide::getenv("USER_HOME"); if (env_var) { std::cout << "User Home: " << env_var << std::endl; } // Example for system command boost::nowide::system("echo Hello World"); return 0; } ``` -------------------------------- ### Include Header for basic_stackstring Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/classboost_1_1nowide_1_1basic__stackstring Header file inclusion directive required to use the basic_stackstring class template. This header is part of the Boost.Nowide library and provides UTF string conversion utilities. ```cpp #include ``` -------------------------------- ### Boost.Nowide args Constructor Overloads (C++) Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/classboost_1_1nowide_1_1args Overloaded constructors for the `boost::nowide::args` class. The first overload handles the conversion of `argc` and `argv`, while the second overload additionally handles `env`. These constructors are responsible for obtaining and converting Unicode-encoded values using Windows API calls. ```cpp boost::nowide::args(int &argc, char **&argv) boost::nowide::args(int &argc, char **&argv, char **&env) ``` -------------------------------- ### Conditional Compilation for Platform Support Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/cstdlib_8hpp_source Uses preprocessor directives to provide platform-specific implementations. On non-Windows systems, uses standard library functions directly via 'using' declarations. On Windows, uses UTF-8 aware wrapper implementations defined in the Boost.Nowide library. ```cpp #if !defined(BOOST_WINDOWS) && !defined(BOOST_NOWIDE_DOXYGEN) using std::getenv; using std::system; #else BOOST_NOWIDE_DECL char* getenv(const char* key); BOOST_NOWIDE_DECL int system(const char* cmd); #endif ``` -------------------------------- ### Boost.Nowide Args Class Constructor (Non-Windows) Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/args_8hpp_source This is a placeholder constructor for the 'args' class on non-Windows systems. It does not perform any argument manipulation, assuming standard C-style arguments are already compatible. This ensures the 'args' class interface remains consistent across different platforms. ```cpp #include #if !defined(BOOST_WINDOWS) && !defined(BOOST_NOWIDE_DOXYGEN) namespace boost { namespace nowide { class args { public: args(int&, char**&) {} args(int&, char**&, char**&) {} }; } // namespace nowide } // namespace boost #endif ``` -------------------------------- ### basic_stackstring Conversion Methods Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/stackstring_8hpp_source Provides methods to convert input character strings to the internal representation of basic_stackstring. It attempts to use stack memory first and falls back to heap allocation if necessary. ```cpp output_char* convert(const input_char* input) { if(input) return convert(input, input + utf::strlen(input)); clear(); return get(); } output_char* convert(const input_char* begin, const input_char* end) { clear(); if(begin) { const size_t input_len = end - begin; const size_t min_output_size = input_len + 1; if(min_output_size <= buffer_size && utf::convert_buffer(buffer_, buffer_size, begin, end)) data_ = buffer_; else { const size_t max_output_size = input_len * utf::utf_traits::max_width + 1; data_ = new output_char[max_output_size]; const bool success = utf::convert_buffer(data_, max_output_size, begin, end) == data_; assert(success); (void)success; } } return get(); } ``` -------------------------------- ### basic_ofstream Constructors (C++) Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/fstream_8hpp_source Constructs a basic_ofstream object, optionally opening a file. Supports construction from std::string and a templated Path type, with a default open mode of std::ios_base::out. ```cpp template> class basic_ofstream : public detail::fstream_impl { public: basic_ofstream() = default; explicit basic_ofstream(const std::string& file_name, std::ios_base::openmode mode = std::ios_base::out) { open(file_name, mode); } template explicit basic_ofstream(const Path& file_name, detail::enable_if_path_t mode = std::ios_base::out) { open(file_name, mode); } // ... other members }; ``` -------------------------------- ### Unicode-Aware Line Counter Using Boost.Nowide in C++ Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/index An enhanced version of the line counter that uses Boost.Nowide functions to provide proper Unicode support. The program uses boost::nowide::args for UTF-8 argument conversion, boost::nowide::ifstream for file access, and boost::nowide::cout/cerr for console output with UTF-8 encoding. ```cpp #include #include #include int main(int argc,char **argv) { boost::nowide::args a(argc,argv); // Fix arguments - make them UTF-8 if(argc!=2) { boost::nowide::cerr << "Usage: file_name" << std::endl; // Unicode aware console return 1; } boost::nowide::ifstream f(argv[1]); // argv[1] - is UTF-8 if(!f) { // the console can display UTF-8 boost::nowide::cerr << "Can't open " << argv[1] << std::endl; return 1; } int total_lines = 0; while(f) { if(f.get() == '\n') total_lines++; } f.close(); // the console can display UTF-8 boost::nowide::cout << "File " << argv[1] << " has " << total_lines << " lines" << std::endl; return 0; } ``` -------------------------------- ### Copy Constructor for basic_stackstring Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/classboost_1_1nowide_1_1basic__stackstring Copy constructor that creates a new basic_stackstring instance by copying the converted string from another basic_stackstring object. Enables creation of independent copies with the same converted content. ```cpp basic_stackstring(const basic_stackstring& other) ``` -------------------------------- ### Declare Boost.Nowide Namespace Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/config_8hpp_source Establishes the boost::nowide namespace as the primary namespace for all Boost.Nowide library components and utilities. ```cpp namespace boost { namespace nowide {} } // namespace boost ``` -------------------------------- ### Boost.Nowide cstdlib Functions Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/index Offers UTF-8 aware replacements for standard C library functions related to system commands and environment variables. Similar to cstdio functions, on Windows, narrow string arguments are interpreted as UTF-8 and converted to UTF-16. On POSIX systems, these are aliases to their standard library counterparts. This ensures consistent handling of strings with special characters across platforms. ```cpp #include #include int main() { // Example: Executing a system command with a path containing non-ASCII characters // On Windows, "My Documents\File Folder\Run This.exe" will be handled correctly if UTF-8 encoded. boost::nowide::system("echo Hello World"); // Example: Getting an environment variable const char* user_home = boost::nowide::getenv("HOME"); if (user_home) { std::cout << "User home: " << user_home << std::endl; } // Example: Setting an environment variable boost::nowide::setenv("MY_VAR", "My Value With Symbols ©", 1); return 0; } ``` -------------------------------- ### UTF-8 Conversion Length and Unshift Methods Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/classboost_1_1nowide_1_1utf8__codecvt_3_01CharType_00_012_01_4-members do_length calculates the number of complete multibyte sequences that fit within a maximum byte count from the input buffer. do_unshift finalizes any pending conversion state, ensuring proper termination of partial sequences in stateful encodings. ```cpp int do_length(std::mbstate_t &std_state, const char *from, const char *from_end, size_t max) const override; result do_unshift(std::mbstate_t &s, char *from, char *, char *&next) const override; ``` -------------------------------- ### Enable UTF-8 encoding in Boost.Filesystem Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/index Call nowide_filesystem() at program startup to imbue a locale with UTF-8 conversion facet for Boost.Filesystem. This converts narrow strings to wide strings using UTF-8 encoding on Windows, with minimal effect on POSIX systems. ```cpp boost::nowide::nowide_filesystem(); ``` -------------------------------- ### Construct basic_stackstring from Character Range Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/classboost_1_1nowide_1_1basic__stackstring Constructor that accepts a character range defined by begin and end pointers and converts the sequence [begin, end) into the internal buffer. If begin is NULL, nothing is stored. This constructor enables conversion of non-null-terminated character sequences. ```cpp basic_stackstring(const input_char* begin, const input_char* end) ``` -------------------------------- ### UTF-8 Encoding Conversion do_in Method Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/classboost_1_1nowide_1_1utf8__codecvt_3_01CharType_00_012_01_4-members Converts multibyte UTF-8 sequences to wide characters. This protected virtual method implements the core decoding logic, consuming bytes from the input range [from, from_end) and producing characters in the output range [to, to_end). It maintains conversion state and updates iterators to indicate progress. ```cpp result do_in(std::mbstate_t &std_state, const char *from, const char *from_end, const char *&from_next, uchar *to, uchar *to_end, uchar *&to_next) const override; ``` -------------------------------- ### basic_stackstring Getters (C++) Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/stackstring_8hpp_source Illustrates the getter functions for basic_stackstring, which provide access to the converted, NULL-terminated string. These functions are vital for retrieving the string data after potential conversions and ensuring it's properly null-terminated for standard string operations. ```cpp const output_char * get() const output_char * get() ``` -------------------------------- ### Unicode Aware File Line Counter using Boost.Nowide (C++) Source: https://www.boost.org/doc/libs/latest/libs/nowide/index A Unicode-aware version of the file line counter using Boost.Nowide. It replaces standard streams and argument handling with their Boost.Nowide equivalents to correctly handle UTF-8 filenames and console output on Windows. ```cpp #include #include #include int main(int argc, char **argv) { boost::nowide::args a(argc, argv); // Fix arguments - make them UTF-8 if (argc != 2) { boost::nowide::cerr << "Usage: file_name" << std::endl; // Unicode aware console return 1; } boost::nowide::ifstream f(argv[1]); // argv[1] - is UTF-8 if (!f) { // the console can display UTF-8 boost::nowide::cerr << "Can't open " << argv[1] << std::endl; return 1; } int total_lines = 0; while (f) { if (f.get() == '\n') total_lines++; } f.close(); // the console can display UTF-8 boost::nowide::cout << "File " << argv[1] << " has " << total_lines << " lines" << std::endl; return 0; } ``` -------------------------------- ### Open File with basic_filebuf (char*) Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/fstream_8hpp_source Opens a file using a C-style string (char*) as the file name. It handles the underlying file opening operation and sets the stream state accordingly. This function requires the internal buffer to be available. ```C++ void open(const char* file_name, std::ios_base::openmode mode = T_StreamType::mode()) { if(!rdbuf()->open(file_name, mode | T_StreamType::mode_modifier())) setstate(std::ios_base::failbit); else clear(); } ``` -------------------------------- ### Define basic_stackstring Template with Character Type Parameters Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/classboost_1_1nowide_1_1basic__stackstring Template class declaration for basic_stackstring with three template parameters: CharOut (output character type, default wchar_t), CharIn (input character type, default char), and BufferSize (stack buffer size, default 256). This template enables flexible UTF string conversion between different character encodings. ```cpp template class boost::nowide::basic_stackstring ``` -------------------------------- ### Configure Auto-Linking for Boost.Nowide Library Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/config_8hpp_source Sets up automatic library linking configuration by defining BOOST_LIB_NAME and conditionally setting BOOST_DYN_LINK before including auto_link.hpp. This enables automatic linking to the correct build variant when not explicitly disabled. ```cpp #if !defined(BOOST_ALL_NO_LIB) && !defined(BOOST_NOWIDE_NO_LIB) && !defined(BOOST_NOWIDE_SOURCE) #define BOOST_LIB_NAME boost_nowide #if defined(BOOST_ALL_DYN_LINK) || defined(BOOST_NOWIDE_DYN_LINK) #define BOOST_DYN_LINK #endif #include #endif // auto-linking disabled ``` -------------------------------- ### basic_stackstring Class Definition and Constructors Source: https://www.boost.org/doc/libs/latest/libs/nowide/doc/html/stackstring_8hpp_source Defines the basic_stackstring class template, which can use either stack or heap memory for storage. It includes default, copy, and conversion constructors to initialize the string from C-style strings or iterators. ```cpp #include #include #include #include namespace boost { namespace nowide { template class basic_stackstring { public: static const size_t buffer_size = BufferSize; using output_char = CharOut; using input_char = CharIn; basic_stackstring() { buffer_[0] = 0; } explicit basic_stackstring(const input_char* input) { convert(input); } basic_stackstring(const input_char* begin, const input_char* end) { convert(begin, end); } basic_stackstring(const basic_stackstring& other) { *this = other; } // ... other members ... }; } // namespace nowide } // namespace boost ```