10 # include <unicode/ucsdet.h>
11 # include <unicode/ucnv.h>
14 # error MSVC builds require ICU
19 # define WIN32_LEAN_AND_MEAN
25 # ifndef LCF_SUPPORT_ICU
47 if (codepage == 932) {
48 #ifdef LCF_SUPPORT_ICU
49 return "ibm-943_P130-1999";
54 if (codepage == 949) {
55 #ifdef LCF_SUPPORT_ICU
56 return "ibm-949_P110-1999";
61 std::ostringstream out;
62 #ifdef LCF_SUPPORT_ICU
63 out <<
"windows-" << codepage;
65 out <<
"CP" << codepage;
69 std::string outs = out.str();
75 #ifdef LCF_SUPPORT_ICU
76 std::ostringstream text;
118 if (text.str().size() > 33)
120 UErrorCode status = U_ZERO_ERROR;
121 UCharsetDetector* detector = ucsdet_open(&status);
123 std::string s = text.str();
124 ucsdet_setText(detector, s.c_str(), s.length(), &status);
126 const UCharsetMatch* match = ucsdet_detect(detector, &status);
129 encoding = ucsdet_getName(match, &status);
131 ucsdet_close(detector);
134 if (encoding ==
"Shift_JIS")
136 encoding =
"ibm-943_P130-1999";
138 else if (encoding ==
"EUC-KR")
140 encoding =
"ibm-949_P110-1999";
142 else if (encoding ==
"ISO-8859-1" || encoding ==
"windows-1252")
144 encoding =
"ibm-5348_P100-1997";
146 else if (encoding ==
"ISO-8859-2" || encoding ==
"windows-1250")
148 encoding =
"ibm-5346_P100-1998";
150 else if (encoding ==
"ISO-8859-5" || encoding ==
"windows-1251")
152 encoding =
"ibm-5347_P100-1998";
154 else if (encoding ==
"ISO-8859-6" || encoding ==
"windows-1256")
156 encoding =
"ibm-9448_X100-2005";
158 else if (encoding ==
"ISO-8859-7" || encoding ==
"windows-1253")
160 encoding =
"ibm-5349_P100-1998";
162 else if (encoding ==
"ISO-8859-8" || encoding ==
"windows-1255")
164 encoding =
"ibm-9447_P100-2002";
175 std::string encoding = ini.
Get(
"EasyRPG",
"Encoding", std::string());
176 if (!encoding.empty()) {
180 return std::string();
185 int codepage = GetACP();
189 std::locale loc = std::locale(
"");
191 std::string loc_full = loc.name().substr(0, loc.name().find_first_of(
"@."));
193 std::string loc_lang = loc.name().substr(0, loc.name().find_first_of(
"_"));
195 if (loc_lang ==
"th") codepage = 874;
196 else if (loc_lang ==
"ja") codepage = 932;
197 else if (loc_full ==
"zh_CN" ||
198 loc_full ==
"zh_SG") codepage = 936;
199 else if (loc_lang ==
"ko") codepage = 949;
200 else if (loc_full ==
"zh_TW" ||
201 loc_full ==
"zh_HK") codepage = 950;
202 else if (loc_lang ==
"cs" ||
208 loc_lang ==
"sl") codepage = 1250;
209 else if (loc_lang ==
"ru") codepage = 1251;
210 else if (loc_lang ==
"ca" ||
222 loc_lang ==
"eu") codepage = 1252;
223 else if (loc_lang ==
"el") codepage = 1253;
224 else if (loc_lang ==
"tr") codepage = 1254;
225 else if (loc_lang ==
"he") codepage = 1255;
226 else if (loc_lang ==
"ar") codepage = 1256;
227 else if (loc_lang ==
"et" ||
229 loc_lang ==
"lv") codepage = 1257;
230 else if (loc_lang ==
"vi") codepage = 1258;
236 std::string
ReaderUtil::Recode(
const std::string& str_to_encode,
const std::string& source_encoding) {
241 const std::string& src_enc,
242 const std::string& dst_enc) {
243 std::string encoding_str = src_enc;
245 if (src_enc.empty()) {
246 return str_to_encode;
248 if (atoi(src_enc.c_str()) > 0) {
251 #ifdef LCF_SUPPORT_ICU
252 UErrorCode status = U_ZERO_ERROR;
253 int size = str_to_encode.size() * 4;
254 UChar* unicode_str =
new UChar[size];
257 std::string result_str;
259 conv = ucnv_open(encoding_str.c_str(), &status);
261 if (status != U_ZERO_ERROR && status != U_AMBIGUOUS_ALIAS_WARNING) {
262 return std::string();
264 status = U_ZERO_ERROR;
266 length = ucnv_toUChars(conv, unicode_str, size, str_to_encode.c_str(), -1, &status);
268 if (status != U_ZERO_ERROR)
return std::string();
270 char* result =
new char[length * 4];
272 conv = ucnv_open(dst_enc.data(), &status);
273 ucnv_fromUChars(conv, result, length * 4, unicode_str, -1, &status);
275 if (status != U_ZERO_ERROR)
return std::string();
279 delete[] unicode_str;
282 return std::string(result_str);
284 iconv_t cd = iconv_open(dst_enc.c_str(), encoding_str.c_str());
285 if (cd == (iconv_t)-1)
286 return str_to_encode;
287 char *src =
const_cast<char *
>(str_to_encode.c_str());
288 size_t src_left = str_to_encode.size();
289 size_t dst_size = str_to_encode.size() * 5 + 10;
290 char *dst =
new char[dst_size];
291 size_t dst_left = dst_size;
293 char ICONV_CONST *p = src;
298 size_t status = iconv(cd, &p, &src_left, &q, &dst_left);
300 if (status == (
size_t) -1 || src_left > 0) {
302 return std::string();
305 std::string result(dst);
std::string DetectEncoding(const std::string &database_file)
std::string exit_game_message
std::string CodepageToEncoding(int codepage)
std::string Recode(const std::string &str_to_encode, const std::string &source_encoding)
std::string spirit_points
std::string normal_status
std::string health_points
std::string GetLocaleEncoding()
bool Load(const std::string &filename, const std::string &encoding)
std::string GetEncoding(const std::string &ini_file)
std::string save_game_message
std::string Get(std::string section, std::string name, std::string default_value)
std::string load_game_message