Skip to content

Commit d5dd075

Browse files
committed
读取语言文件时,总是使用utf8编码
1 parent 6ce91c6 commit d5dd075

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

TrafficMonitor/IniHelper.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "IniHelper.h"
33
#include "Common.h"
44

5-
CIniHelper::CIniHelper(const wstring& file_path)
5+
CIniHelper::CIniHelper(const wstring& file_path, bool force_utf8)
66
{
77
m_file_path = file_path;
88
ifstream file_stream{ file_path };
@@ -19,17 +19,24 @@ CIniHelper::CIniHelper(const wstring& file_path)
1919
// 检查并添加末尾的空行
2020
if (!ini_str.empty() && ini_str.back() != L'\n')
2121
ini_str.push_back(L'\n');
22-
//判断文件是否是utf8编码
2322
bool is_utf8;
24-
if (ini_str.size() >= 3 && ini_str[0] == -17 && ini_str[1] == -69 && ini_str[2] == -65)
23+
if (force_utf8)
2524
{
26-
//如果有UTF8的BOM,则删除BOM
2725
is_utf8 = true;
28-
ini_str = ini_str.substr(3);
2926
}
3027
else
3128
{
32-
is_utf8 = false;
29+
//判断文件是否是utf8编码
30+
if (ini_str.size() >= 3 && ini_str[0] == -17 && ini_str[1] == -69 && ini_str[2] == -65)
31+
{
32+
//如果有UTF8的BOM,则删除BOM
33+
is_utf8 = true;
34+
ini_str = ini_str.substr(3);
35+
}
36+
else
37+
{
38+
is_utf8 = false;
39+
}
3340
}
3441
//转换成Unicode
3542
m_ini_str = CCommon::StrToUnicode(ini_str.c_str(), is_utf8);

TrafficMonitor/IniHelper.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
class CIniHelper
88
{
99
public:
10-
CIniHelper(const wstring& file_path);
10+
// 从磁盘加载ini文件
11+
// file_path:文件路径
12+
// force_utf8:如果为true,则强制以UTF8编码解析,否则,仅当含有UTF8 BOM时才以UTF8编码解析
13+
CIniHelper(const wstring& file_path, bool force_utf8 = false);
1114
// 从资源文件加载ini (只能读取)
1215
CIniHelper(UINT id, bool is_utf8 = true);
1316
CIniHelper();

TrafficMonitor/StrTable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void CStrTable::Init()
9191
for (const wstring& file_name : files)
9292
{
9393
std::wstring file_path{ language_dir + file_name };
94-
CIniHelper ini_file(file_path);
94+
CIniHelper ini_file(file_path, true);
9595
LanguageInfo language_info;
9696
LanguageInfoFromIni(language_info, ini_file);
9797
language_info.language_id = LocaleNameToLCID(language_info.bcp_47.c_str(), 0); //根据语言bcp-47代码获取语言id

0 commit comments

Comments
 (0)