| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef GrAutoLocaleSetter_DEFINED | 8 #ifndef GrAutoLocaleSetter_DEFINED |
| 9 #define GrAutoLocaleSetter_DEFINED | 9 #define GrAutoLocaleSetter_DEFINED |
| 10 | 10 |
| 11 #include "GrTypes.h" | 11 #include "GrTypes.h" |
| 12 | 12 |
| 13 #if defined(SK_BUILD_FOR_WIN) | 13 #if defined(SK_BUILD_FOR_WIN) |
| 14 #include "SkString.h" | 14 #include "SkString.h" |
| 15 #endif | 15 #endif |
| 16 | 16 |
| 17 #if !defined(SK_BUILD_FOR_ANDROID) | 17 #if !defined(SK_BUILD_FOR_ANDROID) |
| 18 #include <locale.h> | 18 #include <locale.h> |
| 19 #endif | 19 #endif |
| 20 | 20 |
| 21 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) | 21 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
| 22 #include <xlocale.h> | 22 #include <xlocale.h> |
| 23 #endif | 23 #endif |
| 24 | 24 |
| 25 #if defined(SK_BUILD_FOR_ANDROID) || defined(__UCLIBC__) || defined(_NEWLIB_VERS
ION) |
| 26 #define HAVE_LOCALE_T 0 |
| 27 #else |
| 28 #define HAVE_LOCALE_T 1 |
| 29 #endif |
| 30 |
| 25 /** | 31 /** |
| 26 * Helper class for ensuring that we don't use the wrong locale when building sh
aders. Android | 32 * Helper class for ensuring that we don't use the wrong locale when building sh
aders. Android |
| 27 * doesn't support locale in the NDK, so this is a no-op there. | 33 * doesn't support locale in the NDK, so this is a no-op there. |
| 28 */ | 34 */ |
| 29 class GrAutoLocaleSetter : public SkNoncopyable { | 35 class GrAutoLocaleSetter : public SkNoncopyable { |
| 30 public: | 36 public: |
| 31 GrAutoLocaleSetter (const char* name) { | 37 GrAutoLocaleSetter (const char* name) { |
| 32 #if defined(SK_BUILD_FOR_WIN) | 38 #if defined(SK_BUILD_FOR_WIN) |
| 33 fOldPerThreadLocale = _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
| 39 fOldPerThreadLocale = _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
|
| 34 char* oldLocale = setlocale(LC_ALL, name); | 40 char* oldLocale = setlocale(LC_ALL, name); |
| 35 if (oldLocale) { | 41 if (oldLocale) { |
| 36 fOldLocale = oldLocale; | 42 fOldLocale = oldLocale; |
| 37 fShouldRestoreLocale = true; | 43 fShouldRestoreLocale = true; |
| 38 } else { | 44 } else { |
| 39 fShouldRestoreLocale = false; | 45 fShouldRestoreLocale = false; |
| 40 } | 46 } |
| 41 #elif !defined(SK_BUILD_FOR_ANDROID) && !defined(__UCLIBC__) | 47 #elif HAVE_LOCALE_T |
| 42 fLocale = newlocale(LC_ALL, name, 0); | 48 fLocale = newlocale(LC_ALL, name, 0); |
| 43 if (fLocale) { | 49 if (fLocale) { |
| 44 fOldLocale = uselocale(fLocale); | 50 fOldLocale = uselocale(fLocale); |
| 45 } else { | 51 } else { |
| 46 fOldLocale = static_cast<locale_t>(0); | 52 fOldLocale = static_cast<locale_t>(0); |
| 47 } | 53 } |
| 48 #else | 54 #else |
| 49 (void) name; // suppress unused param warning. | 55 (void) name; // suppress unused param warning. |
| 50 #endif | 56 #endif |
| 51 } | 57 } |
| 52 | 58 |
| 53 ~GrAutoLocaleSetter () { | 59 ~GrAutoLocaleSetter () { |
| 54 #if defined(SK_BUILD_FOR_WIN) | 60 #if defined(SK_BUILD_FOR_WIN) |
| 55 if (fShouldRestoreLocale) { | 61 if (fShouldRestoreLocale) { |
| 56 setlocale(LC_ALL, fOldLocale.c_str()); | 62 setlocale(LC_ALL, fOldLocale.c_str()); |
| 57 } | 63 } |
| 58 _configthreadlocale(fOldPerThreadLocale); | 64 _configthreadlocale(fOldPerThreadLocale); |
| 59 #elif !defined(SK_BUILD_FOR_ANDROID) && !defined(__UCLIBC__) | 65 #elif HAVE_LOCALE_T |
| 60 if (fLocale) { | 66 if (fLocale) { |
| 61 uselocale(fOldLocale); | 67 uselocale(fOldLocale); |
| 62 freelocale(fLocale); | 68 freelocale(fLocale); |
| 63 } | 69 } |
| 64 #endif | 70 #endif |
| 65 } | 71 } |
| 66 | 72 |
| 67 private: | 73 private: |
| 68 #if defined(SK_BUILD_FOR_WIN) | 74 #if defined(SK_BUILD_FOR_WIN) |
| 69 int fOldPerThreadLocale; | 75 int fOldPerThreadLocale; |
| 70 bool fShouldRestoreLocale; | 76 bool fShouldRestoreLocale; |
| 71 SkString fOldLocale; | 77 SkString fOldLocale; |
| 72 #elif !defined(SK_BUILD_FOR_ANDROID) && !defined(__UCLIBC__) | 78 #elif HAVE_LOCALE_T |
| 73 locale_t fOldLocale; | 79 locale_t fOldLocale; |
| 74 locale_t fLocale; | 80 locale_t fLocale; |
| 75 #endif | 81 #endif |
| 76 }; | 82 }; |
| 77 | 83 |
| 84 #undef HAVE_LOCALE_T |
| 85 |
| 78 #endif | 86 #endif |
| 79 | 87 |
| OLD | NEW |