MinGW下带有boost :: filestream的UTF-8名称 [英] UTF-8 names with boost::filestream under MinGW

查看:153
本文介绍了MinGW下带有boost :: filestream的UTF-8名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了boost文件流的问题:我需要在Windows下的用户目录中创建和修改文件.但是,用户名包含一个变音符号,当在MinGW下编译时,该名称会失败,因为标准缺少用于提升使用率的文件流的wide_char open()API.参见使用unicode文件名读取/写入文件普通C ++/Boost 符合UTF-8的IOstream https://svn.boost.org/trac10/ticket/9968

I ran into a problem with the boost filestreams: I need to create and modify files in the user directory under windows. However the username contains an umlaut which makes this fail when compiled under MinGW as the standard is missing the wide_char open() API for filestreams which boost uses. see Read/Write file with unicode file name with plain C++/Boost, UTF-8-compliant IOstreams and https://svn.boost.org/trac10/ticket/9968

但是,我碰巧碰到了这个问题,主要是在尝试在系统代码页之外使用字符时发生的.就我而言,我仅使用系统代码页中的字符,因为显然用户目录存在.这使我认为,如果我可以告诉boost :: path期望所有std::string都是蜜蜂UTF8,但在调用string()成员函数时将它们转换为系统编码(在boost::fstream::open中发生),则这应该可行)

However I ran across the line, that this problem mainly occurs when trying to use a character outside the systems codepage. In my case I'm using only characters from the systems codepage as obviously the users directory exists. This makes me think, that this should work, if I could tell boost::path to expect all std::strings as beeing UTF8 but converting them to the system encoding when calling the string() member function (which happens in boost::fstream::open)

所以基本上:有什么方法可以使用boost(和boost Locale)自动进行这种转换(UTF8->系统编码)吗?

So basically: Is there any way to do that conversion (UTF8->system encoding) automatically using boost (and boost Locale)?

为了完整起见,这是我设置语言环境的代码:

To be complete here is my code for setting the locale:

#ifdef _WIN32
        // On windows we want to enforce the encoding (mostly UTF8). Also using "" would use the default which uses "wrong" separators
        std::locale::global(boost::locale::generator().generate("C"));
#else
        // In linux / OSX this suffices
        std::locale::global(std::locale::classic());
#endif // _WIN32
        // Use also the encoding (mostly UTF8) for bfs paths
        bfs::path::imbue(std::locale());

推荐答案

我发现有2个使用另一个库的解决方案,两者都有其缺点.

I found 2 solutions using another library where both have their drawbacks.

  1. Pathie ( Boost.NoWide (
  1. Pathie (Docu) It looks like a full replacement of boost::filesystem providing UTF8 aware streams and path handling as well as symlink creation and other file/folder operations. Really cool is the builtin support for getting special directories (temp, HOME, programs folder and many more)
    Drawback: Only works as a dynamic library as the static build has bugs. Also might be overkill if you already use boost.
  2. Boost.NoWide (Docu) Provides alternatives to almost all file and stream handlers to support UTF8 on windows and falls back to standard functions on others. The filestreams accept UTF8 encoded values (for the name) and it uses boost itself.
    Drawback: No path handling and does not accept bfs::path or wide strings (bfs::path internal format on Windows is UTF16) so a patch would be required, although it is simple. Also requires a build for windows if you want to use std::cout etc with UTF8 strings (yes that works directly!)
    Another cool thing: It provides a class to convert the argc/argv to UTF8 on windows.

这篇关于MinGW下带有boost :: filestream的UTF-8名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆