Locale (Windows): reports the code page used by the console

This commit is contained in:
李通洲
2026-08-14 11:01:22 +08:00
parent 965ed4ba79
commit 7b2b3405ae
4 changed files with 72 additions and 2 deletions
+4
View File
@@ -31,6 +31,10 @@ typedef struct FFPlatform {
FFstrbuf userShell;
FFPlatformSysinfo sysinfo;
#if _WIN32
uint32_t initCP; // The code page used by the console when the program started
#endif
} FFPlatform;
void ffPlatformInit(FFPlatform* platform);
+1
View File
@@ -18,6 +18,7 @@ void ffPlatformInit(FFPlatform* platform) {
#ifdef _WIN32
ffStrbufInit(&platform->sid);
platform->initCP = 0;
#endif
FFPlatformSysinfo* info = &platform->sysinfo;
+5 -1
View File
@@ -39,7 +39,7 @@ static void defaultConfig(void) {
}
#ifdef _WIN32
static volatile UINT oldCp = CP_UTF8;
static UINT oldCp = CP_UTF8;
void resetConsoleCP(void) {
if (oldCp != CP_UTF8) {
SetConsoleOutputCP(oldCp);
@@ -69,6 +69,10 @@ void ffInitInstance(void) {
defaultConfig();
initState(&instance.state);
#ifdef _WIN32
instance.state.platform.initCP = oldCp;
#endif
}
static volatile bool ffDisableLinewrap = false;
+62 -1
View File
@@ -19,8 +19,69 @@ const char* ffDetectLocale(FFstrbuf* result) {
if (size <= 1) { // including '\0'
return "GetUserDefaultLocaleName() failed";
}
ffStrbufAppendNWS(result, (uint32_t) size - 1, name);
if (result->length > 2 && result->chars[2] == '-') { // Windows uses '-' instead of '_'
result->chars[2] = '_';
}
ffStrbufSetNWS(result, (uint32_t) size - 1, name);
uint32_t codePage = instance.state.platform.initCP;
if (__builtin_expect(codePage != 0, true)) {
ffStrbufAppendC(result, '.');
switch (codePage) {
case CP_UTF8:
ffStrbufAppendS(result, "UTF-8");
break;
case CP_UTF7:
ffStrbufAppendS(result, "UTF-7");
break;
case 1200:
ffStrbufAppendS(result, "UTF-16LE");
break;
case 1201:
ffStrbufAppendS(result, "UTF-16BE");
break;
case 936:
ffStrbufAppendS(result, "GBK");
break;
case 54936:
ffStrbufAppendS(result, "GB18030");
break;
case 950:
ffStrbufAppendS(result, "BIG5");
break;
case 932:
ffStrbufAppendS(result, "Shift_JIS");
break;
case 949:
ffStrbufAppendS(result, "EUC-KR");
break;
case 20932:
ffStrbufAppendS(result, "EUC-JP");
break;
case 20866:
ffStrbufAppendS(result, "KOI8-R");
break;
case 21866:
ffStrbufAppendS(result, "KOI8-U");
break;
case 20127:
ffStrbufAppendS(result, "US-ASCII");
break;
case 874:
case 1250 ... 1258:
ffStrbufAppendS(result, "Windows-");
ffStrbufAppendUInt(result, codePage);
break;
default:
ffStrbufAppendS(result, "CP");
ffStrbufAppendUInt(result, codePage);
break;
}
}
return nullptr;
}