What I did:
struct vpath_traits;
typedef boost::filesystem::basic_path<std::string, vpath_traits> vpath;
// utf8
struct vpath_traits
{
typedef std::string internal_string_type;
typedef std::wstring external_string_type;
static external_string_type to_external(
const vpath &, const internal_string_type &src);
static internal_string_type to_internal(const external_string_type &src);
};
namespace boost {
namespace filesystem {
template<> struct is_basic_path<vpath>
{ BOOST_STATIC_CONSTANT( bool, value = true ); };
}
}
// convenient functions for converting vpath to and from
// whatever API you may be using.
std::string vpathToWin32Byte(const vpath &src);
vpath vpathFromWin32Byte(const std::string &str);
vpath vpathFromWin32Byte(const char *str);
std::wstring vpathToWin32Wide(const vpath &src);
vpath vpathFromWin32Wide(const std::wstring &str);
vpath vpathFromWin32Wide(const wchar_t *str);
QDir vpathToQDir(const vpath &src);
vpath vpathFromQDir(const QDir &qdir);
Note:
You must somehow implement the methods vpath_traits::to_external
and
vpath_traits::to_internal
. There are probably some utf8-converter in
boost. However I implemented the utf8 to ucs-16 and back myself, but it is
neither pretty nor complete, so you are better off using some standard
implementation.
It works in our code. E g:
namespace fs = boost::filesystem;
...
vpath dstFile = dstDir / filePath;
bool success = true;
try { fs::remove(dstFile); }
catch (fs::basic_filesystem_error<vpath> & /* e */) { success = false;}
if (success) {
try { fs::copy_file(srcFile, dstFile); }
catch (fs::basic_filesystem_error<vpath> & /* e */) { success = false;}
}
...