Friday, October 21, 2011

Using non ASCII symbols in Boost file names

The boost filesystem library has a lot of goodies to make the life of the cross-platform developer easier and looks like a natural choice of the contemporary C++ developer.

One of the things that's been bugging me recently though is how to use non ASCII file names in paths on Windows. The problem that I encountered was that boost would throw this exception:

boost::filesystem::create_directory: The filename, directory name, or volume label syntax is incorrect:


every time I try to e.g. create a directory with a non-ASCII name (e.g. something in cyrillic). After digging the net for a while I've found this in the Boost filesystem documentation

The default imbued locale provides a codecvt facet that invokes Windows MultiByteToWideChar or WideCharToMultiByte API's with a codepage of CP_THREAD_ACP if Windows AreFileApisANSI()is true, otherwise codepage CP_OEMCP.
This was what I was looking for. Setting which code page to use for the conversion is then done with:
#ifdef OS_WINDOWS
SetFileApisToOEM();
#endif

Also you have to use a wstring instead of a regular string to construct the boost::filesystem::path object. A very useful page that explains the internationalization aspects of char and wchar_t is this excellent answer.

No comments:

Post a Comment