aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/GrAutoLocaleSetter.h
blob: f6a10e5b7d909ca7d5e4c118109b6ffddd5535b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef GrAutoLocaleSetter_DEFINED
#define GrAutoLocaleSetter_DEFINED

#include "GrTypes.h"
#include "SkNoncopyable.h"

#if defined(SK_BUILD_FOR_WIN)
#include "SkString.h"
#endif

#if !defined(SK_BUILD_FOR_ANDROID)
#include <locale.h>
#endif

#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
#include <xlocale.h>
#include <cstring>
#define HAVE_XLOCALE 1
#else
#define HAVE_XLOCALE 0
#endif

#if defined(SK_BUILD_FOR_ANDROID) || defined(__UCLIBC__) || defined(_NEWLIB_VERSION)
#define HAVE_LOCALE_T 0
#else
#define HAVE_LOCALE_T 1
#endif

/**
 * Helper class for ensuring that we don't use the wrong locale when building shaders. Android
 * doesn't support locale in the NDK, so this is a no-op there.
 */
class GrAutoLocaleSetter : public SkNoncopyable {
public:
    GrAutoLocaleSetter (const char* name) {
#if defined(SK_BUILD_FOR_WIN)
        fOldPerThreadLocale = _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
        char* oldLocale = setlocale(LC_ALL, name);
        if (oldLocale) {
            fOldLocale = oldLocale;
            fShouldRestoreLocale = true;
        } else {
            fShouldRestoreLocale = false;
        }
#elif HAVE_LOCALE_T
#if HAVE_XLOCALE
        // In xlocale nullptr means the C locale.
        if (0 == strcmp(name, "C")) {
            name = nullptr;
        }
#endif
        fLocale = newlocale(LC_ALL_MASK, name, nullptr);
        if (fLocale) {
            fOldLocale = uselocale(fLocale);
        } else {
            fOldLocale = static_cast<locale_t>(nullptr);
        }
#else
        (void) name; // suppress unused param warning.
#endif
    }

    ~GrAutoLocaleSetter () {
#if defined(SK_BUILD_FOR_WIN)
        if (fShouldRestoreLocale) {
            setlocale(LC_ALL, fOldLocale.c_str());
        }
        _configthreadlocale(fOldPerThreadLocale);
#elif HAVE_LOCALE_T
        if (fLocale) {
             uselocale(fOldLocale);
             freelocale(fLocale);
        }
#endif
    }

private:
#if defined(SK_BUILD_FOR_WIN)
    int fOldPerThreadLocale;
    bool fShouldRestoreLocale;
    SkString fOldLocale;
#elif HAVE_LOCALE_T
    locale_t fOldLocale;
    locale_t fLocale;
#endif
};

#undef HAVE_LOCALE_T
#undef HAVE_XLOCALE

#endif