3

手头的任务

我正在从 Windows 上的 UTF-8 编码 XML 解析文件名。我需要将该文件名传递给我无法更改的函数。它在内部使用_fsopen()不支持 Unicode 字符串的。

目前的方法

我目前的方法是将文件名转换为用户的字符集,希望文件名可以在该编码中表示。然后我boost::locale::conv::from_utf()用来从 UTF-8 转换,我boost::locale::util::get_system_locale()用来获取当前语言环境的名称。

生活很好?

我在使用代码页Windows-1252的德语系统上,因此get_system_locale()正确生成de_DE.windows-1252。如果我使用包含变音符号的文件名测试该方法,那么一切都会按预期工作。

问题

只是为了确保我将系统区域设置切换为使用代码页Windows-1251的乌克兰语。在文件名中使用一些西里尔字母我的方法失败了。原因是get_system_locale()仍然产生de_DE.windows-1252现在不正确。

另一方面GetACP(),德国语言环境正确产生 1252,乌克兰语言环境正确产生 1251。我也知道 Boost.Locale 可以转换为给定的语言环境,因为这个小型测试程序可以按我的预期工作:

#include <boost/locale.hpp>
#include <iostream>
#include <string>
#include <windows.h>

int main()
{
    std::cout << "Codepage: " << GetACP() << std::endl;
    std::cout << "Boost.Locale: " << boost::locale::util::get_system_locale() << std::endl;

    namespace blc = boost::locale::conv;
    // Cyrillic small letter zhe -> \xe6 (ш on 1251, æ on 1252)
    std::string const test1251 = blc::from_utf(std::string("\xd0\xb6"), "windows-1251");
    std::cout << "1251: " << static_cast<int>(test1251.front()) << std::endl;
    // Latin small letter sharp s -> \xdf (Я on 1251, ß on 1252)
    auto const test1252 = blc::from_utf(std::string("\xc3\x9f"), "windows-1252");
    std::cout << "1252: " << static_cast<int>(test1252.front()) << std::endl;

}

问题

  • 如何以 Boost.Locale 支持的格式查询用户语言环境的名称?使用yield German_Germany.1252std::locale("").name(),使用它会导致异常。boost::locale::conv::invalid_charset_error

  • 尽管我应该将其更改为本地管理员,但系统区域设置是否可能仍然是de_DE.windows-1252 ?同样,系统语言是德语,尽管我的帐户的语言是英语。(在我登录之前,登录屏幕是德语)

  • 我应该坚持使用短文件名吗?虽然似乎不能可靠地工作。

印刷精美

  • 编译器是 MSVC18
  • Boost 是 1.56.0 版本,后端据说是 winapi
  • 系统是Win7,系统语言是德语,用户语言是英语
4

2 回答 2

2

ANSI 已被弃用,所以不要理会它。

Windows 使用 UTF16,您必须使用MultiByteToWideChar. 这种转换是安全的。

std::wstring getU16(const std::string &str)
{
    if (str.empty()) return std::wstring();
    int sz = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), 0, 0);
    std::wstring res(sz, 0);
    MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &res[0], sz);
    return res;
}

然后,您使用_wfsopen(从您提供的链接)打开具有 UTF16 名称的文件。

int main()
{
    //UTF8 source:
    std::string filename_u8;

    //This line works in VS2015 only
    //For older version comment out the next line, obtain UTF8 from another source
    filename_u8 = u8"c:\\test\\__ελληνικά.txt";

    //convert to UTF16
    std::wstring filename_utf16 = getU16(filename_u8);

    FILE *file = NULL;
    _wfopen_s(&file, filename_utf16.c_str(), L"w");
    if (file)
    {
        //Add BOM, optional...

        //Write the file name in to file, for testing...
        fwrite(filename_u8.data(), 1, filename_u8.length(), file);

        fclose(file);
    }
    else
    {
        cout << "access denined, or folder doesn't exits...
    }

    return 0;
}


编辑,从 UTF8 获取 ANSI,使用GetACP()

std::wstring string_to_wstring(const std::string &str, int codepage)
{
    if (str.empty()) return std::wstring();
    int sz = MultiByteToWideChar(codepage, 0, &str[0], (int)str.size(), 0, 0);
    std::wstring res(sz, 0);
    MultiByteToWideChar(codepage, 0, &str[0], (int)str.size(), &res[0], sz);
    return res;
}

std::string wstring_to_string(const std::wstring &wstr, int codepage)
{
    if (wstr.empty()) return std::string();
    int sz = WideCharToMultiByte(codepage, 0, &wstr[0], (int)wstr.size(), 0, 0, 0, 0);
    std::string res(sz, 0);
    WideCharToMultiByte(codepage, 0, &wstr[0], (int)wstr.size(), &res[0], sz, 0, 0);
    return res;
}

std::string get_ansi_from_utf8(const std::string &utf8, int codepage)
{
    std::wstring utf16 = string_to_wstring(utf8, CP_UTF8);
    std::string ansi = wstring_to_string(utf16, codepage);
    return ansi;
}
于 2016-06-30T00:29:14.447 回答
2

Barmak 的方法是最好的方法。

要清除语言环境的内容,该过程总是从“C”语言环境开始。您可以使用setlocale 函数将语言环境设置为系统默认值或任意语言环境。

#include <clocale>

// Get the current locale
setlocale(LC_ALL,NULL);

// Set locale to system default
setlocale(LC_ALL,"");

// Set locale to German
setlocale(LC_ALL,"de-DE");
于 2016-06-30T04:27:42.080 回答