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 
13 #if !defined(SK_BUILD_FOR_ANDROID)
14 #include <locale.h>
15 #endif
16 
17 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
18 #include <xlocale.h>
19 #endif
20 
21 /**
22  * Helper class for ensuring that we don't use the wrong locale when building shaders. Android
23  * doesn't support locale in the NDK, so this is a no-op there.
24  */
25 class GrAutoLocaleSetter {
26 public:
GrAutoLocaleSetter(const char * name)27     GrAutoLocaleSetter (const char* name) {
28 #if defined(SK_BUILD_FOR_WIN)
29         fOldPerThreadLocale = _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
30         fOldLocale = setlocale(LC_ALL, name);
31 #elif !defined(SK_BUILD_FOR_ANDROID)
32         fLocale = newlocale(LC_ALL, name, 0);
33         if (fLocale) {
34             fOldLocale = uselocale(fLocale);
35         }
36 #else
37         (void) name; // suppress unused param warning.
38 #endif
39     }
40 
~GrAutoLocaleSetter()41     ~GrAutoLocaleSetter () {
42 #if defined(SK_BUILD_FOR_WIN)
43         setlocale(LC_ALL, fOldLocale);
44         _configthreadlocale(fOldPerThreadLocale);
45 #elif !defined(SK_BUILD_FOR_ANDROID)
46         if (fLocale) {
47              uselocale(fOldLocale);
48              freelocale(fLocale);
49         }
50 #endif
51     }
52 
53 private:
54 #if defined(SK_BUILD_FOR_WIN)
55     int fOldPerThreadLocale;
56     const char* fOldLocale;
57 #elif !defined(SK_BUILD_FOR_ANDROID)
58     locale_t fOldLocale;
59     locale_t fLocale;
60 #endif
61 };
62 
63 #endif
64 
65