手头的任务
我正在 Windows 上从 UTF-8 编码的 XML 解析文件名。我需要将该文件名传递给我无法更改的函数。它在内部使用不支持 Unicode 字符串的 _fsopen()
。
当前方法
我目前的方法是将文件名转换为用户的字符集,希望文件名可以用该编码表示。然后我使用 boost::locale::conv::from_utf()
从 UTF-8 转换,我使用 boost::locale::util::get_system_locale()
获取当前语言环境的名称。
生活好吗?
我在使用代码页 Windows-1252 的德国系统上,因此 get_system_locale()
正确产生 de_DE.windows-1252。如果我使用包含变音符号的文件名测试该方法,一切都会按预期工作。
问题
只是为了确保我 switched my system locale 到使用代码页 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;
}
问题
std::locale("").name()
产生 German_Germany.1252,使用它会导致 boost::locale::conv::invalid_charset_error
异常。 打印精美
最佳答案
ANSI 已被弃用,所以不要理会它。
Windows 使用 UTF16,您必须使用 MultiByteToWideChar
从 UTF8 转换为 UTF16。这种转换是安全的。
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;
}
编辑,使用
GetACP()
从 UTF8 获取 ANSIstd::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;
}
关于c++ - 获取 boost::locale::conv 中函数的用户代码页名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38107847/