File indexing completed on 2024-06-09 04:26:46

0001 // SPDX-FileCopyrightText: 2013-2018 INRIA
0002 // SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
0005 #include <windows.h>
0006 #endif // defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
0007 #include <locale.h>
0008 #ifdef __APPLE__
0009 #include <xlocale.h>
0010 #endif
0011 
0012 #ifndef KIS_CONTEXT_THREAD_LOCALE_H
0013 #define KIS_CONTEXT_THREAD_LOCALE_H
0014 
0015 // Helper class to set the C locale when doing OCIO calls.
0016 //
0017 // See https://github.com/AcademySoftwareFoundation/OpenColorIO/issues/297#issuecomment-505636123
0018 //
0019 // Source: https://github.com/NatronGitHub/openfx-io/commit/27a13e00db8bae1a31308d66b4039a3542c14805
0020 class AutoSetAndRestoreThreadLocale
0021 {
0022 public:
0023     AutoSetAndRestoreThreadLocale()
0024     {
0025 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
0026         // set locale will only change locale on the current thread
0027         previousThreadConfig = _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
0028 
0029         // get and store current locale
0030         ssaLocale.assign(setlocale(LC_ALL, NULL));
0031 
0032         // set to "C" locale
0033         setlocale(LC_ALL, "C");
0034 #else
0035         // set to C locale, saving the old one (returned from useLocale)
0036         currentLocale = newlocale(LC_ALL_MASK, "C", NULL);
0037         oldLocale = uselocale(currentLocale);
0038 #endif
0039     }
0040 
0041     ~AutoSetAndRestoreThreadLocale()
0042     {
0043 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
0044         // thread specific
0045         setlocale(LC_ALL, ssaLocale.c_str());
0046 
0047         // set back to global settings]
0048         _configthreadlocale(previousThreadConfig);
0049 #else
0050         // restore the previous locale and freeing the created locale
0051         uselocale(oldLocale);
0052         freelocale(currentLocale);
0053 #endif
0054     }
0055 
0056 private:
0057 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
0058     std::string ssaLocale;
0059     int previousThreadConfig;
0060 #else
0061     locale_t oldLocale;
0062     locale_t currentLocale;
0063 #endif
0064 };
0065 
0066 #endif // KIS_CONTEXT_THREAD_LOCALE_H