Global: better handling locales

This commit is contained in:
Carter Li
2024-06-07 10:00:30 +08:00
parent 896468195a
commit 5df5bbf85b
3 changed files with 14 additions and 10 deletions
+8 -1
View File
@@ -8,9 +8,9 @@
#include <stdlib.h>
#include <unistd.h>
#include <locale.h>
#ifdef _WIN32
#include <windows.h>
#include <locale.h>
#include "util/windows/unicode.h"
#else
#include <signal.h>
@@ -51,6 +51,13 @@ void ffInitInstance(void)
#ifdef WIN32
//https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setlocale-wsetlocale?source=recommendations&view=msvc-170#utf-8-support
setlocale(LC_ALL, ".UTF8");
#else
// Never use `setlocale(LC_ALL, "")`
setlocale(LC_TIME, "");
setlocale(LC_NUMERIC, "");
#ifdef LC_MESSAGES
setlocale(LC_MESSAGES, "");
#endif
#endif
initState(&instance.state);
+4 -5
View File
@@ -12,12 +12,11 @@ void ffDetectLocale(FFstrbuf* result)
if(result->length > 0)
return;
setlocale(LC_ALL, "");
#ifdef LC_MESSAGES
ffStrbufAppendS(result, setlocale(LC_MESSAGES, NULL));
if(result->length == 0)
ffStrbufAppendS(result, setlocale(LC_ALL, NULL));
if(result->length > 0)
return;
#endif
setlocale(LC_ALL, "C");
ffStrbufAppendS(result, setlocale(LC_TIME, NULL));
}
+2 -4
View File
@@ -33,8 +33,7 @@ static inline uint32_t getWcsWidth(const FFstrbuf* mbstr, wchar_t* wstr, mbstate
void ffPrintSeparator(FFSeparatorOptions* options)
{
setlocale(LC_ALL, "");
setlocale(LC_CTYPE, "");
mbstate_t state = {};
bool fqdn = instance.config.modules.title.fqdn;
const FFPlatform* platform = &instance.state.platform;
@@ -90,8 +89,7 @@ void ffPrintSeparator(FFSeparatorOptions* options)
}
}
putchar('\n');
setlocale(LC_ALL, "C");
setlocale(LC_CTYPE, "C");
}
bool ffParseSeparatorCommandOptions(FFSeparatorOptions* options, const char* key, const char* value)