2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include "xlocale_private.h"
31 #define C_LOCALE_INITIALIZER { \
35 1, 0, 0, 0, 0, 0, 1, 1, \
37 &_DefaultRuneXLocale, \
40 static char C
[] = "C";
41 static const struct _xlocale __c_locale
= C_LOCALE_INITIALIZER
;
42 const locale_t _c_locale
= (const locale_t
)&__c_locale
;
43 __private_extern__
struct _xlocale __global_locale
= C_LOCALE_INITIALIZER
;
44 __private_extern__ pthread_key_t __locale_key
= (pthread_key_t
)-1;
46 extern int __collate_load_tables(const char *, locale_t
);
47 extern int __detect_path_locale(void);
48 extern const char *__get_locale_env(int);
49 extern int __messages_load_locale(const char *, locale_t
);
50 extern int __monetary_load_locale(const char *, locale_t
);
51 extern int __numeric_load_locale(const char *, locale_t
);
52 extern int __setrunelocale(const char *, locale_t
);
53 extern int __time_load_locale(const char *, locale_t
);
56 * check that the encoding is the right size, isn't . or .. and doesn't
59 static inline __attribute__((always_inline
)) int
60 _checkencoding(const char *encoding
)
62 return (encoding
&& (strlen(encoding
) > ENCODING_LEN
63 || (encoding
[0] == '.' && (encoding
[1] == 0
64 || (encoding
[1] == '.' && encoding
[2] == 0)))
65 || strchr(encoding
, '/') != NULL
)) ? -1 : 0;
69 * check that the locale has the right magic number
71 static inline __attribute__((always_inline
)) int
72 _checklocale(const locale_t loc
)
76 return (loc
== LC_GLOBAL_LOCALE
|| loc
->__magic
== XMAGIC
) ? 0 : -1;
80 * copy a locale_t except anything before the magic value
82 static inline __attribute__((always_inline
)) void
83 _copylocale(locale_t dst
, const locale_t src
)
85 memcpy(&dst
->__magic
, &src
->__magic
, sizeof(*dst
) - offsetof(struct _xlocale
, __magic
));
89 * Make a copy of a locale_t, locking/unlocking the source as determined
90 * by the lock flag. A NULL locale_t means to make a copy of the current
91 * locale while LC_GLOBAL_LOCALE means to copy the global locale. If
92 * &__c_locale is passed (meaning a C locale is desired), just make
96 _duplocale(locale_t loc
)
100 if ((new = (locale_t
)malloc(sizeof(struct _xlocale
))) == NULL
)
103 loc
= __current_locale();
104 else if (loc
== LC_GLOBAL_LOCALE
)
105 loc
= &__global_locale
;
106 else if (loc
== &__c_locale
) {
110 _copylocale(new, loc
);
111 /* __mbs_mblen is the first of NMBSTATET mbstate_t buffers */
112 bzero(&new->__mbs_mblen
, NMBSTATET
* sizeof(new->__mbs_mblen
));
114 XL_RETAIN(new->__lc_collate
);
116 XL_RETAIN(new->__lc_ctype
);
118 XL_RETAIN(new->__lc_messages
);
120 XL_RETAIN(new->__lc_monetary
);
122 XL_RETAIN(new->__lc_numeric
);
124 XL_RETAIN(new->__lc_time
);
126 XL_RETAIN(new->__lc_localeconv
);
132 * Modify a locale_t, setting the parts specified in the mask
133 * to the locale specified by the string. If the string is NULL, the C
134 * locale is used. If the string is empty, the value is determined from
135 * the environment. -1 is returned on error, and loc is in a partial state.
138 _modifylocale(locale_t loc
, int mask
, __const
char *locale
)
141 const char *enc
= NULL
;
147 ret
= __detect_path_locale();
155 for(m
= 1; m
<= _LC_LAST_MASK
; m
<<= 1) {
158 case LC_COLLATE_MASK
:
160 enc
= __get_locale_env(LC_COLLATE
);
161 if (_checkencoding(enc
) < 0) {
166 oenc
= (loc
->__collate_load_error
? C
: loc
->__lc_collate
->__encoding
);
167 if (strcmp(enc
, oenc
) != 0 && __collate_load_tables(enc
, loc
) == _LDP_ERROR
)
172 enc
= __get_locale_env(LC_CTYPE
);
173 if (_checkencoding(enc
) < 0) {
178 if (strcmp(enc
, loc
->__lc_ctype
->__ctype_encoding
) != 0 && (ret
= __setrunelocale(enc
, loc
)) != 0) {
183 case LC_MESSAGES_MASK
:
185 enc
= __get_locale_env(LC_MESSAGES
);
186 if (_checkencoding(enc
) < 0) {
191 oenc
= (loc
->_messages_using_locale
? loc
->__lc_messages
->_messages_locale_buf
: C
);
192 if (strcmp(enc
, oenc
) != 0 && __messages_load_locale(enc
, loc
) == _LDP_ERROR
)
195 case LC_MONETARY_MASK
:
197 enc
= __get_locale_env(LC_MONETARY
);
198 if (_checkencoding(enc
) < 0) {
203 oenc
= (loc
->_monetary_using_locale
? loc
->__lc_monetary
->_monetary_locale_buf
: C
);
204 if (strcmp(enc
, oenc
) != 0 && __monetary_load_locale(enc
, loc
) == _LDP_ERROR
)
207 case LC_NUMERIC_MASK
:
209 enc
= __get_locale_env(LC_NUMERIC
);
210 if (_checkencoding(enc
) < 0) {
215 oenc
= (loc
->_numeric_using_locale
? loc
->__lc_numeric
->_numeric_locale_buf
: C
);
216 if (strcmp(enc
, oenc
) != 0 && __numeric_load_locale(enc
, loc
) == _LDP_ERROR
)
221 enc
= __get_locale_env(LC_TIME
);
222 if (_checkencoding(enc
) < 0) {
227 oenc
= (loc
->_time_using_locale
? loc
->__lc_time
->_time_locale_buf
: C
);
228 if (strcmp(enc
, oenc
) != 0 && __time_load_locale(enc
, loc
) == _LDP_ERROR
)
238 * release all the memory objects (the memory will be freed when the refcount
242 _releaselocale(locale_t loc
)
245 XL_RELEASE(loc
->__lc_collate
);
247 XL_RELEASE(loc
->__lc_ctype
);
249 XL_RELEASE(loc
->__lc_messages
);
251 XL_RELEASE(loc
->__lc_monetary
);
253 XL_RELEASE(loc
->__lc_numeric
);
255 XL_RELEASE(loc
->__lc_time
);
257 XL_RELEASE(loc
->__lc_localeconv
);
261 * EXTERNAL: Duplicate a (non-NULL) locale_t. LC_GLOBAL_LOCALE means the
262 * global locale, while NULL means the current locale. NULL is returned
266 duplocale(locale_t loc
)
268 if (_checklocale(loc
) < 0) {
272 return _duplocale(loc
);
276 * EXTERNAL: Free a locale_t, releasing all memory objects. Don't free
277 * illegal locale_t's or the global locale.
280 freelocale(locale_t loc
)
282 if (!loc
|| _checklocale(loc
) < 0 || loc
== &__global_locale
283 || loc
== LC_GLOBAL_LOCALE
) {
293 * EXTERNAL: Create a new locale_t, based on the base locale_t, and modified
294 * by the mask and locale string. If the base is NULL, the current locale
295 * is used as the base. If locale is NULL, changes are made from the C locale
296 * for categories set in mask.
299 newlocale(int mask
, __const
char *locale
, locale_t base
)
302 int lcmask
= (mask
& LC_ALL_MASK
);
304 if (_checkencoding(locale
) < 0) {
308 if (lcmask
== LC_ALL_MASK
)
309 base
= (locale_t
)&__c_locale
;
310 else if (_checklocale(base
) < 0) {
314 new = _duplocale(base
);
317 if (lcmask
== 0 || (lcmask
== LC_ALL_MASK
&& locale
== NULL
))
319 if (_modifylocale(new, lcmask
, locale
) < 0) {
327 * EXTERNAL: Returns the locale string for the part specified in mask. The
328 * least significant bit is used. If loc is NULL, the current per-thread
332 querylocale(int mask
, locale_t loc
)
336 if (_checklocale(loc
) < 0 || (mask
& LC_ALL_MASK
) == 0) {
341 loc
= __current_locale();
342 else if (loc
== LC_GLOBAL_LOCALE
)
343 loc
= &__global_locale
;
344 for(m
= 1; m
<= _LC_LAST_MASK
; m
<<= 1) {
347 case LC_COLLATE_MASK
:
348 return (loc
->__collate_load_error
? C
: loc
->__lc_collate
->__encoding
);
350 return loc
->__lc_ctype
->__ctype_encoding
;
351 case LC_MESSAGES_MASK
:
352 return (loc
->_messages_using_locale
? loc
->__lc_messages
->_messages_locale_buf
: C
);
353 case LC_MONETARY_MASK
:
354 return (loc
->_monetary_using_locale
? loc
->__lc_monetary
->_monetary_locale_buf
: C
);
355 case LC_NUMERIC_MASK
:
356 return (loc
->_numeric_using_locale
? loc
->__lc_numeric
->_numeric_locale_buf
: C
);
358 return (loc
->_time_using_locale
? loc
->__lc_time
->_time_locale_buf
: C
);
362 /* should never get here */
368 * EXTERNAL: Set the thread-specific locale. The previous locale is returned.
369 * Use LC_GLOBAL_LOCALE to set the global locale. LC_GLOBAL_LOCALE
370 * may also be returned if there was no previous thread-specific locale in
371 * effect. If loc is NULL, the current locale is returned, but no locale
372 * chance is made. NULL is returned on error.
375 uselocale(locale_t loc
)
380 orig
= (locale_t
)pthread_getspecific(__locale_key
);
382 if (_checklocale(loc
) < 0) {
386 if (loc
== &__global_locale
) /* should never happen */
387 loc
= LC_GLOBAL_LOCALE
;
388 orig
= pthread_getspecific(__locale_key
);
389 pthread_setspecific(__locale_key
, loc
== LC_GLOBAL_LOCALE
? NULL
: loc
);
391 return (orig
? orig
: LC_GLOBAL_LOCALE
);
395 * EXTERNAL: Used by the MB_CUR_MAX macro to determine the thread-specific
401 return __current_locale()->__lc_ctype
->__mb_cur_max
;
405 * EXTERNAL: Used by the MB_CUR_MAX_L macro to determine the thread-specific
406 * value, from the given locale_t.
409 ___mb_cur_max_l(locale_t loc
)
411 return __locale_ptr(loc
)->__lc_ctype
->__mb_cur_max
;
415 * Called from the Libc initializer to setup the thread-specific key.
417 __private_extern__
void
420 if (__locale_key
== (pthread_key_t
)-1)
421 pthread_key_create(&__locale_key
, NULL
);