blob: 4fade848d4c131f10b989f155991e0eb05db0c9e [file] [log] [blame]
Elliott Hughes5363a452014-04-08 14:34:12 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <locale.h>
30#include <pthread.h>
31#include <stdlib.h>
32
Elliott Hughesc4936e22014-04-08 17:05:05 -070033// We currently support a single locale, the "C" locale (also known as "POSIX").
34
35struct __locale_t {
36 // Because we only support one locale, these are just tokens with no data.
37};
38
39static pthread_once_t gLocaleOnce = PTHREAD_ONCE_INIT;
40static lconv gLocale;
41
42static pthread_once_t gUselocaleKeyOnce = PTHREAD_ONCE_INIT;
43static pthread_key_t gUselocaleKey;
Elliott Hughes5363a452014-04-08 14:34:12 -070044
45static void __locale_init() {
Elliott Hughesc4936e22014-04-08 17:05:05 -070046 gLocale.decimal_point = const_cast<char*>(".");
Elliott Hughes5363a452014-04-08 14:34:12 -070047
48 char* not_available = const_cast<char*>("");
Elliott Hughesc4936e22014-04-08 17:05:05 -070049 gLocale.thousands_sep = not_available;
50 gLocale.grouping = not_available;
51 gLocale.int_curr_symbol = not_available;
52 gLocale.currency_symbol = not_available;
53 gLocale.mon_decimal_point = not_available;
54 gLocale.mon_thousands_sep = not_available;
55 gLocale.mon_grouping = not_available;
56 gLocale.positive_sign = not_available;
57 gLocale.negative_sign = not_available;
Elliott Hughes5363a452014-04-08 14:34:12 -070058
Elliott Hughesc4936e22014-04-08 17:05:05 -070059 gLocale.int_frac_digits = CHAR_MAX;
60 gLocale.frac_digits = CHAR_MAX;
61 gLocale.p_cs_precedes = CHAR_MAX;
62 gLocale.p_sep_by_space = CHAR_MAX;
63 gLocale.n_cs_precedes = CHAR_MAX;
64 gLocale.n_sep_by_space = CHAR_MAX;
65 gLocale.p_sign_posn = CHAR_MAX;
66 gLocale.n_sign_posn = CHAR_MAX;
67 gLocale.int_p_cs_precedes = CHAR_MAX;
68 gLocale.int_p_sep_by_space = CHAR_MAX;
69 gLocale.int_n_cs_precedes = CHAR_MAX;
70 gLocale.int_n_sep_by_space = CHAR_MAX;
71 gLocale.int_p_sign_posn = CHAR_MAX;
72 gLocale.int_n_sign_posn = CHAR_MAX;
73}
74
75static void __uselocale_key_init() {
76 pthread_key_create(&gUselocaleKey, NULL);
77}
78
79static bool __is_supported_locale(const char* locale) {
80 return (strcmp(locale, "") == 0 || strcmp(locale, "C") == 0 || strcmp(locale, "POSIX") == 0);
81}
82
83static locale_t __new_locale() {
84 return reinterpret_cast<locale_t>(malloc(sizeof(__locale_t)));
Elliott Hughes5363a452014-04-08 14:34:12 -070085}
86
87lconv* localeconv() {
Elliott Hughesc4936e22014-04-08 17:05:05 -070088 pthread_once(&gLocaleOnce, __locale_init);
89 return &gLocale;
Elliott Hughes5363a452014-04-08 14:34:12 -070090}
91
Elliott Hughesc4936e22014-04-08 17:05:05 -070092locale_t duplocale(locale_t l) {
93 locale_t clone = __new_locale();
94 if (clone != NULL && l != LC_GLOBAL_LOCALE) {
95 *clone = *l;
96 }
97 return clone;
98}
99
100void freelocale(locale_t l) {
101 free(l);
102}
103
104locale_t newlocale(int category_mask, const char* locale_name, locale_t /*base*/) {
105 // Is 'category_mask' valid?
106 if ((category_mask & ~LC_ALL_MASK) != 0) {
107 errno = EINVAL;
108 return NULL;
109 }
110
111 if (!__is_supported_locale(locale_name)) {
112 errno = ENOENT;
113 return NULL;
114 }
115
116 return __new_locale();
117}
118
119char* setlocale(int category, char const* locale_name) {
120 // Is 'category' valid?
121 if (category < LC_CTYPE || category > LC_IDENTIFICATION) {
122 errno = EINVAL;
123 return NULL;
124 }
125
126 // Caller just wants to query the current locale?
127 if (locale_name == NULL) {
128 return const_cast<char*>("C");
129 }
130
131 // Caller wants one of the mandatory POSIX locales?
132 if (__is_supported_locale(locale_name)) {
133 return const_cast<char*>("C");
134 }
135
136 // We don't support any other locales.
137 errno = ENOENT;
Elliott Hughes5363a452014-04-08 14:34:12 -0700138 return NULL;
139}
Elliott Hughesc4936e22014-04-08 17:05:05 -0700140
141locale_t uselocale(locale_t new_locale) {
142 pthread_once(&gUselocaleKeyOnce, __uselocale_key_init);
143
144 locale_t old_locale = static_cast<locale_t>(pthread_getspecific(gUselocaleKey));
145
146 // If this is the first call to uselocale(3) on this thread, we return LC_GLOBAL_LOCALE.
147 if (old_locale == NULL) {
148 old_locale = LC_GLOBAL_LOCALE;
149 }
150
151 if (new_locale != NULL) {
152 pthread_setspecific(gUselocaleKey, new_locale);
153 }
154
155 return old_locale;
156}