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