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