Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_context_thread_locale.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2013-2018 INRIA
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
5#include <windows.h>
6#endif // defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
7#include <locale.h>
8#ifdef __APPLE__
9#include <xlocale.h>
10#endif
11
12#ifndef KIS_CONTEXT_THREAD_LOCALE_H
13#define KIS_CONTEXT_THREAD_LOCALE_H
14
15// Helper class to set the C locale when doing OCIO calls.
16//
17// See https://github.com/AcademySoftwareFoundation/OpenColorIO/issues/297#issuecomment-505636123
18//
19// Source: https://github.com/NatronGitHub/openfx-io/commit/27a13e00db8bae1a31308d66b4039a3542c14805
21{
22public:
24 {
25#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
26 // set locale will only change locale on the current thread
27 previousThreadConfig = _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
28
29 // get and store current locale
30 ssaLocale.assign(setlocale(LC_ALL, NULL));
31
32 // set to "C" locale
33 setlocale(LC_ALL, "C");
34#else
35 // set to C locale, saving the old one (returned from useLocale)
36 currentLocale = newlocale(LC_ALL_MASK, "C", NULL);
37 oldLocale = uselocale(currentLocale);
38#endif
39 }
40
42 {
43#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
44 // thread specific
45 setlocale(LC_ALL, ssaLocale.c_str());
46
47 // set back to global settings]
48 _configthreadlocale(previousThreadConfig);
49#else
50 // restore the previous locale and freeing the created locale
51 uselocale(oldLocale);
52 freelocale(currentLocale);
53#endif
54 }
55
56private:
57#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
58 std::string ssaLocale;
59 int previousThreadConfig;
60#else
61 locale_t oldLocale;
62 locale_t currentLocale;
63#endif
64};
65
66#endif // KIS_CONTEXT_THREAD_LOCALE_H