diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2023-02-27 16:40:59 +0100 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2023-03-10 12:18:54 +0100 |
commit | c5f209de71d37f725ce691a60fcec436be4547d6 (patch) | |
tree | ffd6dce3db4acf128d296d3d59612bd07c6766d2 /src | |
parent | 77006658310f647a58648f07409ff3a4e980f3d9 (diff) |
Tidy up QCoreApplicationPrivate::initLocale()'s #if-ery
Explain each exception cleanly and as itself, thereby avoiding the
need for long and tangled #if-ery conditions.
Make sure to setlocale(LC_ALL, "") everywhere we think we have
initialized the locale, including Integrity - it plainly has
setlocale(), since we call it for LC_CTYPE - since we should at least
give it the chance to set its implementation-defined default locale,
instead of the standard-defined POSIX locale in use on entry to
main().
Done-with: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Change-Id: Iab00984ba45dfc9a324b6a3c12e3d330b655a5a9
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
(cherry picked from commit 91bea4470ea25c1e6fe7e46559a37bdd7b703b63)
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/kernel/qcoreapplication.cpp | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index 82406c150a..46d5474506 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -588,22 +588,30 @@ QString qAppName() void QCoreApplicationPrivate::initLocale() { -#if defined(Q_OS_UNIX) && !defined(QT_BOOTSTRAPPED) +#if defined(QT_BOOTSTRAPPED) + // Don't try to control bootstrap library locale or encoding. +#elif defined(Q_OS_UNIX) static bool qt_locale_initialized = false; if (qt_locale_initialized) return; qt_locale_initialized = true; -# ifdef Q_OS_INTEGRITY + // By default the portable "C"/POSIX locale is selected and active. + // Apply the locale from the environment, via setlocale(), which will + // read LC_ALL, LC_<category>, and LANG, in order (for each category). + const char *locale = setlocale(LC_ALL, ""); + + // Next, let's ensure that LC_CTYPE is UTF-8, since QStringConverter's + // QLocal8Bit hard-codes this, and we need to be consistent. +# if defined(Q_OS_INTEGRITY) setlocale(LC_CTYPE, "UTF-8"); +# elif defined(Q_OS_QNX) + // QNX has no nl_langinfo, so we can't check. + // FIXME: Shouldn't we still setlocale("UTF-8")? +# elif defined(Q_OS_ANDROID) && __ANDROID_API__ < __ANDROID_API_O__ + // Android 6 still lacks nl_langinfo(), so we can't check. + // FIXME: Shouldn't we still setlocale("UTF-8")? # else -# if defined(Q_OS_QNX) || (defined(Q_OS_ANDROID) && __ANDROID_API__ < __ANDROID_API_O__) - // Android 6 still lacks nl_langinfo(), as does QNX, so we just assume it's - // always UTF-8 on these platforms. - auto nl_langinfo = [](int) { return "UTF-8"; }; -# endif // QNX or Android NDK < 26, "O". - - const char *locale = setlocale(LC_ALL, ""); const char *codec = nl_langinfo(CODESET); if (Q_UNLIKELY(qstricmp(codec, "UTF-8") != 0 && qstricmp(codec, "utf8") != 0)) { QByteArray oldLocale = locale; @@ -615,8 +623,8 @@ void QCoreApplicationPrivate::initLocale() newLocale += ".UTF-8"; newLocale = setlocale(LC_CTYPE, newLocale); - // if locale doesn't exist, try some fallbacks -# ifdef Q_OS_DARWIN + // If that locale doesn't exist, try some fallbacks: +# if defined(Q_OS_DARWIN) if (newLocale.isEmpty()) newLocale = setlocale(LC_CTYPE, "UTF-8"); # endif @@ -630,7 +638,7 @@ void QCoreApplicationPrivate::initLocale() "reconfigure your locale. See the locale(1) manual for more information.", codec, oldLocale.constData(), newLocale.constData()); } -# endif // Integrity +# endif // Platform choice #endif // Unix } |