bsalomon | 6f7f201 | 2015-03-16 14:00:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef GrAutoLocaleSetter_DEFINED |
| 9 | #define GrAutoLocaleSetter_DEFINED |
| 10 | |
| 11 | #include "GrTypes.h" |
Ben Wagner | d5148e3 | 2018-07-16 17:44:06 -0400 | [diff] [blame] | 12 | #include "SkNoncopyable.h" |
bsalomon | 6f7f201 | 2015-03-16 14:00:52 -0700 | [diff] [blame] | 13 | |
kkinnunen | aab16e5 | 2015-12-13 23:12:31 -0800 | [diff] [blame] | 14 | #if defined(SK_BUILD_FOR_WIN) |
| 15 | #include "SkString.h" |
| 16 | #endif |
| 17 | |
bsalomon | 6f7f201 | 2015-03-16 14:00:52 -0700 | [diff] [blame] | 18 | #if !defined(SK_BUILD_FOR_ANDROID) |
| 19 | #include <locale.h> |
| 20 | #endif |
| 21 | |
| 22 | #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
| 23 | #include <xlocale.h> |
Ben Wagner | 29f2eaf | 2018-06-15 13:58:41 -0400 | [diff] [blame] | 24 | #include <cstring> |
Brian Salomon | 4d5e65d | 2017-01-05 10:37:24 -0500 | [diff] [blame] | 25 | #define HAVE_XLOCALE 1 |
| 26 | #else |
| 27 | #define HAVE_XLOCALE 0 |
bsalomon | 6f7f201 | 2015-03-16 14:00:52 -0700 | [diff] [blame] | 28 | #endif |
| 29 | |
smklein | db284c5 | 2015-12-15 10:52:51 -0800 | [diff] [blame] | 30 | #if defined(SK_BUILD_FOR_ANDROID) || defined(__UCLIBC__) || defined(_NEWLIB_VERSION) |
| 31 | #define HAVE_LOCALE_T 0 |
| 32 | #else |
| 33 | #define HAVE_LOCALE_T 1 |
| 34 | #endif |
| 35 | |
bsalomon | 6f7f201 | 2015-03-16 14:00:52 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Helper class for ensuring that we don't use the wrong locale when building shaders. Android |
| 38 | * doesn't support locale in the NDK, so this is a no-op there. |
| 39 | */ |
kkinnunen | 4222e19 | 2015-11-19 08:45:30 -0800 | [diff] [blame] | 40 | class GrAutoLocaleSetter : public SkNoncopyable { |
bsalomon | 6f7f201 | 2015-03-16 14:00:52 -0700 | [diff] [blame] | 41 | public: |
| 42 | GrAutoLocaleSetter (const char* name) { |
| 43 | #if defined(SK_BUILD_FOR_WIN) |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 44 | fOldPerThreadLocale = _configthreadlocale(_ENABLE_PER_THREAD_LOCALE); |
kkinnunen | aab16e5 | 2015-12-13 23:12:31 -0800 | [diff] [blame] | 45 | char* oldLocale = setlocale(LC_ALL, name); |
| 46 | if (oldLocale) { |
| 47 | fOldLocale = oldLocale; |
| 48 | fShouldRestoreLocale = true; |
| 49 | } else { |
| 50 | fShouldRestoreLocale = false; |
| 51 | } |
smklein | db284c5 | 2015-12-15 10:52:51 -0800 | [diff] [blame] | 52 | #elif HAVE_LOCALE_T |
Brian Salomon | 4d5e65d | 2017-01-05 10:37:24 -0500 | [diff] [blame] | 53 | #if HAVE_XLOCALE |
| 54 | // In xlocale nullptr means the C locale. |
| 55 | if (0 == strcmp(name, "C")) { |
| 56 | name = nullptr; |
| 57 | } |
| 58 | #endif |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 59 | fLocale = newlocale(LC_ALL_MASK, name, nullptr); |
bsalomon | 6f7f201 | 2015-03-16 14:00:52 -0700 | [diff] [blame] | 60 | if (fLocale) { |
| 61 | fOldLocale = uselocale(fLocale); |
kkinnunen | 4222e19 | 2015-11-19 08:45:30 -0800 | [diff] [blame] | 62 | } else { |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 63 | fOldLocale = static_cast<locale_t>(nullptr); |
bsalomon | 6f7f201 | 2015-03-16 14:00:52 -0700 | [diff] [blame] | 64 | } |
| 65 | #else |
| 66 | (void) name; // suppress unused param warning. |
| 67 | #endif |
| 68 | } |
| 69 | |
| 70 | ~GrAutoLocaleSetter () { |
| 71 | #if defined(SK_BUILD_FOR_WIN) |
kkinnunen | aab16e5 | 2015-12-13 23:12:31 -0800 | [diff] [blame] | 72 | if (fShouldRestoreLocale) { |
| 73 | setlocale(LC_ALL, fOldLocale.c_str()); |
bsalomon | ea75053 | 2015-12-04 07:27:36 -0800 | [diff] [blame] | 74 | } |
bsalomon | 6f7f201 | 2015-03-16 14:00:52 -0700 | [diff] [blame] | 75 | _configthreadlocale(fOldPerThreadLocale); |
smklein | db284c5 | 2015-12-15 10:52:51 -0800 | [diff] [blame] | 76 | #elif HAVE_LOCALE_T |
bsalomon | 6f7f201 | 2015-03-16 14:00:52 -0700 | [diff] [blame] | 77 | if (fLocale) { |
| 78 | uselocale(fOldLocale); |
| 79 | freelocale(fLocale); |
| 80 | } |
| 81 | #endif |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | #if defined(SK_BUILD_FOR_WIN) |
| 86 | int fOldPerThreadLocale; |
kkinnunen | aab16e5 | 2015-12-13 23:12:31 -0800 | [diff] [blame] | 87 | bool fShouldRestoreLocale; |
| 88 | SkString fOldLocale; |
smklein | db284c5 | 2015-12-15 10:52:51 -0800 | [diff] [blame] | 89 | #elif HAVE_LOCALE_T |
bsalomon | 6f7f201 | 2015-03-16 14:00:52 -0700 | [diff] [blame] | 90 | locale_t fOldLocale; |
| 91 | locale_t fLocale; |
| 92 | #endif |
| 93 | }; |
| 94 | |
smklein | db284c5 | 2015-12-15 10:52:51 -0800 | [diff] [blame] | 95 | #undef HAVE_LOCALE_T |
Brian Salomon | 4d5e65d | 2017-01-05 10:37:24 -0500 | [diff] [blame] | 96 | #undef HAVE_XLOCALE |
smklein | db284c5 | 2015-12-15 10:52:51 -0800 | [diff] [blame] | 97 | |
bsalomon | 6f7f201 | 2015-03-16 14:00:52 -0700 | [diff] [blame] | 98 | #endif |