]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/locid.cpp
ICU-62108.0.1.tar.gz
[apple/icu.git] / icuSources / common / locid.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b75a7d8f
A
3/*
4 **********************************************************************
2ca993e8 5 * Copyright (C) 1997-2016, International Business Machines
b75a7d8f
A
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
8*
9* File locid.cpp
10*
11* Created by: Richard Gillam
12*
13* Modification History:
14*
15* Date Name Description
374ca955 16* 02/11/97 aliu Changed gLocPath to fgDataDirectory and added
b75a7d8f 17* methods to get and set it.
374ca955 18* 04/02/97 aliu Made operator!= inline; fixed return value
b75a7d8f
A
19* of getName().
20* 04/15/97 aliu Cleanup for AIX/Win32.
21* 04/24/97 aliu Numerous changes per code review.
22* 08/18/98 stephen Changed getDisplayName()
23* Added SIMPLIFIED_CHINESE, TRADITIONAL_CHINESE
24* Added getISOCountries(), getISOLanguages(),
25* getLanguagesForCountry()
26* 03/16/99 bertrand rehaul.
27* 07/21/99 stephen Added U_CFUNC setDefault
28* 11/09/99 weiv Added const char * getName() const;
29* 04/12/00 srl removing unicodestring api's and cached hash code
30* 08/10/01 grhoten Change the static Locales to accessor functions
31******************************************************************************
32*/
33
34
35#include "unicode/locid.h"
f3c0d7a5 36#include "unicode/strenum.h"
b75a7d8f 37#include "unicode/uloc.h"
4388f060 38#include "putilimp.h"
51004dcb 39#include "mutex.h"
b75a7d8f
A
40#include "umutex.h"
41#include "uassert.h"
42#include "cmemory.h"
43#include "cstring.h"
b331163b 44#include "uassert.h"
b75a7d8f
A
45#include "uhash.h"
46#include "ucln_cmn.h"
4388f060 47#include "ustr_imp.h"
f3c0d7a5 48#include "charstr.h"
b75a7d8f 49
51004dcb
A
50U_CDECL_BEGIN
51static UBool U_CALLCONV locale_cleanup(void);
52U_CDECL_END
53
54U_NAMESPACE_BEGIN
55
b331163b
A
56static Locale *gLocaleCache = NULL;
57static UInitOnce gLocaleCacheInitOnce = U_INITONCE_INITIALIZER;
51004dcb
A
58
59// gDefaultLocaleMutex protects all access to gDefaultLocalesHashT and gDefaultLocale.
60static UMutex gDefaultLocaleMutex = U_MUTEX_INITIALIZER;
61static UHashtable *gDefaultLocalesHashT = NULL;
62static Locale *gDefaultLocale = NULL;
63
f3c0d7a5
A
64/**
65 * \def ULOC_STRING_LIMIT
66 * strings beyond this value crash in CharString
67 */
68#define ULOC_STRING_LIMIT 357913941
69
51004dcb
A
70U_NAMESPACE_END
71
b75a7d8f
A
72typedef enum ELocalePos {
73 eENGLISH,
74 eFRENCH,
75 eGERMAN,
76 eITALIAN,
77 eJAPANESE,
78 eKOREAN,
79 eCHINESE,
80
81 eFRANCE,
82 eGERMANY,
83 eITALY,
84 eJAPAN,
85 eKOREA,
86 eCHINA, /* Alias for PRC */
87 eTAIWAN,
88 eUK,
89 eUS,
90 eCANADA,
91 eCANADA_FRENCH,
729e4ab9 92 eROOT,
b75a7d8f
A
93
94
95 //eDEFAULT,
96 eMAX_LOCALES
97} ELocalePos;
98
374ca955
A
99U_CFUNC int32_t locale_getKeywords(const char *localeID,
100 char prev,
101 char *keywords, int32_t keywordCapacity,
102 char *values, int32_t valuesCapacity, int32_t *valLen,
103 UBool valuesToo,
104 UErrorCode *status);
105
374ca955
A
106U_CDECL_BEGIN
107//
108// Deleter function for Locales owned by the default Locale hash table/
109//
110static void U_CALLCONV
111deleteLocale(void *obj) {
4388f060 112 delete (icu::Locale *) obj;
374ca955 113}
b75a7d8f 114
374ca955 115static UBool U_CALLCONV locale_cleanup(void)
b75a7d8f
A
116{
117 U_NAMESPACE_USE
118
b331163b
A
119 delete [] gLocaleCache;
120 gLocaleCache = NULL;
121 gLocaleCacheInitOnce.reset();
374ca955
A
122
123 if (gDefaultLocalesHashT) {
124 uhash_close(gDefaultLocalesHashT); // Automatically deletes all elements, using deleter func.
125 gDefaultLocalesHashT = NULL;
b75a7d8f 126 }
b331163b 127 gDefaultLocale = NULL;
b75a7d8f
A
128 return TRUE;
129}
b331163b
A
130
131
132static void U_CALLCONV locale_init(UErrorCode &status) {
133 U_NAMESPACE_USE
134
135 U_ASSERT(gLocaleCache == NULL);
136 gLocaleCache = new Locale[(int)eMAX_LOCALES];
137 if (gLocaleCache == NULL) {
138 status = U_MEMORY_ALLOCATION_ERROR;
139 return;
140 }
141 ucln_common_registerCleanup(UCLN_COMMON_LOCALE, locale_cleanup);
142 gLocaleCache[eROOT] = Locale("");
143 gLocaleCache[eENGLISH] = Locale("en");
144 gLocaleCache[eFRENCH] = Locale("fr");
145 gLocaleCache[eGERMAN] = Locale("de");
146 gLocaleCache[eITALIAN] = Locale("it");
147 gLocaleCache[eJAPANESE] = Locale("ja");
148 gLocaleCache[eKOREAN] = Locale("ko");
149 gLocaleCache[eCHINESE] = Locale("zh");
150 gLocaleCache[eFRANCE] = Locale("fr", "FR");
151 gLocaleCache[eGERMANY] = Locale("de", "DE");
152 gLocaleCache[eITALY] = Locale("it", "IT");
153 gLocaleCache[eJAPAN] = Locale("ja", "JP");
154 gLocaleCache[eKOREA] = Locale("ko", "KR");
155 gLocaleCache[eCHINA] = Locale("zh", "CN");
156 gLocaleCache[eTAIWAN] = Locale("zh", "TW");
157 gLocaleCache[eUK] = Locale("en", "GB");
158 gLocaleCache[eUS] = Locale("en", "US");
159 gLocaleCache[eCANADA] = Locale("en", "CA");
160 gLocaleCache[eCANADA_FRENCH] = Locale("fr", "CA");
161}
162
374ca955 163U_CDECL_END
b75a7d8f
A
164
165U_NAMESPACE_BEGIN
51004dcb
A
166
167Locale *locale_set_default_internal(const char *id, UErrorCode& status) {
168 // Synchronize this entire function.
169 Mutex lock(&gDefaultLocaleMutex);
b331163b 170
374ca955
A
171 UBool canonicalize = FALSE;
172
173 // If given a NULL string for the locale id, grab the default
174 // name from the system.
175 // (Different from most other locale APIs, where a null name means use
176 // the current ICU default locale.)
177 if (id == NULL) {
51004dcb 178 id = uprv_getDefaultLocaleID(); // This function not thread safe? TODO: verify.
374ca955
A
179 canonicalize = TRUE; // always canonicalize host ID
180 }
181
374ca955
A
182 char localeNameBuf[512];
183
184 if (canonicalize) {
185 uloc_canonicalize(id, localeNameBuf, sizeof(localeNameBuf)-1, &status);
186 } else {
187 uloc_getName(id, localeNameBuf, sizeof(localeNameBuf)-1, &status);
b75a7d8f 188 }
374ca955
A
189 localeNameBuf[sizeof(localeNameBuf)-1] = 0; // Force null termination in event of
190 // a long name filling the buffer.
191 // (long names are truncated.)
51004dcb
A
192 //
193 if (U_FAILURE(status)) {
194 return gDefaultLocale;
46f4442e
A
195 }
196
51004dcb
A
197 if (gDefaultLocalesHashT == NULL) {
198 gDefaultLocalesHashT = uhash_open(uhash_hashChars, uhash_compareChars, NULL, &status);
374ca955 199 if (U_FAILURE(status)) {
51004dcb 200 return gDefaultLocale;
374ca955 201 }
51004dcb
A
202 uhash_setValueDeleter(gDefaultLocalesHashT, deleteLocale);
203 ucln_common_registerCleanup(UCLN_COMMON_LOCALE, locale_cleanup);
374ca955
A
204 }
205
374ca955 206 Locale *newDefault = (Locale *)uhash_get(gDefaultLocalesHashT, localeNameBuf);
51004dcb 207 if (newDefault == NULL) {
374ca955
A
208 newDefault = new Locale(Locale::eBOGUS);
209 if (newDefault == NULL) {
51004dcb
A
210 status = U_MEMORY_ALLOCATION_ERROR;
211 return gDefaultLocale;
374ca955
A
212 }
213 newDefault->init(localeNameBuf, FALSE);
51004dcb
A
214 uhash_put(gDefaultLocalesHashT, (char*) newDefault->getName(), newDefault, &status);
215 if (U_FAILURE(status)) {
216 return gDefaultLocale;
374ca955
A
217 }
218 }
51004dcb
A
219 gDefaultLocale = newDefault;
220 return gDefaultLocale;
b75a7d8f 221}
51004dcb 222
b75a7d8f
A
223U_NAMESPACE_END
224
225/* sfb 07/21/99 */
226U_CFUNC void
227locale_set_default(const char *id)
228{
229 U_NAMESPACE_USE
51004dcb
A
230 UErrorCode status = U_ZERO_ERROR;
231 locale_set_default_internal(id, status);
b75a7d8f
A
232}
233/* end */
234
235U_CFUNC const char *
236locale_get_default(void)
237{
238 U_NAMESPACE_USE
b75a7d8f
A
239 return Locale::getDefault().getName();
240}
241
242
243U_NAMESPACE_BEGIN
244
46f4442e
A
245UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Locale)
246
b75a7d8f
A
247/*Character separating the posix id fields*/
248// '_'
249// In the platform codepage.
250#define SEP_CHAR '_'
251
252Locale::~Locale()
374ca955 253{
b331163b
A
254 if (baseName != fullName) {
255 uprv_free(baseName);
256 }
257 baseName = NULL;
b75a7d8f 258 /*if fullName is on the heap, we free it*/
374ca955 259 if (fullName != fullNameBuffer)
b75a7d8f
A
260 {
261 uprv_free(fullName);
262 fullName = NULL;
263 }
264}
265
266Locale::Locale()
374ca955 267 : UObject(), fullName(fullNameBuffer), baseName(NULL)
b75a7d8f 268{
374ca955 269 init(NULL, FALSE);
b75a7d8f
A
270}
271
374ca955
A
272/*
273 * Internal constructor to allow construction of a locale object with
274 * NO side effects. (Default constructor tries to get
275 * the default locale.)
276 */
277Locale::Locale(Locale::ELocaleType)
278 : UObject(), fullName(fullNameBuffer), baseName(NULL)
b75a7d8f
A
279{
280 setToBogus();
281}
282
283
374ca955
A
284Locale::Locale( const char * newLanguage,
285 const char * newCountry,
286 const char * newVariant,
287 const char * newKeywords)
288 : UObject(), fullName(fullNameBuffer), baseName(NULL)
b75a7d8f
A
289{
290 if( (newLanguage==NULL) && (newCountry == NULL) && (newVariant == NULL) )
291 {
374ca955 292 init(NULL, FALSE); /* shortcut */
b75a7d8f
A
293 }
294 else
295 {
f3c0d7a5 296 UErrorCode status = U_ZERO_ERROR;
b75a7d8f
A
297 int32_t size = 0;
298 int32_t lsize = 0;
299 int32_t csize = 0;
300 int32_t vsize = 0;
374ca955 301 int32_t ksize = 0;
b75a7d8f
A
302
303 // Calculate the size of the resulting string.
304
305 // Language
306 if ( newLanguage != NULL )
307 {
308 lsize = (int32_t)uprv_strlen(newLanguage);
f3c0d7a5
A
309 if ( lsize < 0 || lsize > ULOC_STRING_LIMIT ) { // int32 wrap
310 setToBogus();
311 return;
312 }
b75a7d8f
A
313 size = lsize;
314 }
315
f3c0d7a5
A
316 CharString togo(newLanguage, lsize, status); // start with newLanguage
317
b75a7d8f
A
318 // _Country
319 if ( newCountry != NULL )
320 {
321 csize = (int32_t)uprv_strlen(newCountry);
f3c0d7a5
A
322 if ( csize < 0 || csize > ULOC_STRING_LIMIT ) { // int32 wrap
323 setToBogus();
324 return;
325 }
b75a7d8f
A
326 size += csize;
327 }
328
329 // _Variant
330 if ( newVariant != NULL )
331 {
332 // remove leading _'s
333 while(newVariant[0] == SEP_CHAR)
334 {
335 newVariant++;
336 }
374ca955 337
b75a7d8f
A
338 // remove trailing _'s
339 vsize = (int32_t)uprv_strlen(newVariant);
f3c0d7a5
A
340 if ( vsize < 0 || vsize > ULOC_STRING_LIMIT ) { // int32 wrap
341 setToBogus();
342 return;
343 }
b75a7d8f
A
344 while( (vsize>1) && (newVariant[vsize-1] == SEP_CHAR) )
345 {
346 vsize--;
347 }
348 }
349
350 if( vsize > 0 )
351 {
352 size += vsize;
353 }
354
355 // Separator rules:
356 if ( vsize > 0 )
357 {
374ca955 358 size += 2; // at least: __v
b75a7d8f
A
359 }
360 else if ( csize > 0 )
361 {
374ca955
A
362 size += 1; // at least: _v
363 }
364
365 if ( newKeywords != NULL)
366 {
367 ksize = (int32_t)uprv_strlen(newKeywords);
f3c0d7a5
A
368 if ( ksize < 0 || ksize > ULOC_STRING_LIMIT ) {
369 setToBogus();
370 return;
371 }
374ca955 372 size += ksize + 1;
b75a7d8f
A
373 }
374
375 // NOW we have the full locale string..
b75a7d8f 376 // Now, copy it back.
f3c0d7a5
A
377
378 // newLanguage is already copied
b75a7d8f
A
379
380 if ( ( vsize != 0 ) || (csize != 0) ) // at least: __v
381 { // ^
f3c0d7a5 382 togo.append(SEP_CHAR, status);
b75a7d8f
A
383 }
384
385 if ( csize != 0 )
374ca955 386 {
f3c0d7a5 387 togo.append(newCountry, status);
b75a7d8f
A
388 }
389
390 if ( vsize != 0)
391 {
f3c0d7a5
A
392 togo.append(SEP_CHAR, status)
393 .append(newVariant, vsize, status);
b75a7d8f
A
394 }
395
374ca955
A
396 if ( ksize != 0)
397 {
398 if (uprv_strchr(newKeywords, '=')) {
f3c0d7a5 399 togo.append('@', status); /* keyword parsing */
374ca955
A
400 }
401 else {
f3c0d7a5 402 togo.append('_', status); /* Variant parsing with a script */
374ca955 403 if ( vsize == 0) {
f3c0d7a5 404 togo.append('_', status); /* No country found */
374ca955
A
405 }
406 }
f3c0d7a5 407 togo.append(newKeywords, status);
374ca955
A
408 }
409
f3c0d7a5
A
410 if (U_FAILURE(status)) {
411 // Something went wrong with appending, etc.
412 setToBogus();
413 return;
414 }
b75a7d8f
A
415 // Parse it, because for example 'language' might really be a complete
416 // string.
f3c0d7a5 417 init(togo.data(), FALSE);
b75a7d8f
A
418 }
419}
420
421Locale::Locale(const Locale &other)
374ca955 422 : UObject(other), fullName(fullNameBuffer), baseName(NULL)
b75a7d8f
A
423{
424 *this = other;
425}
426
427Locale &Locale::operator=(const Locale &other)
428{
429 if (this == &other) {
430 return *this;
431 }
432
b75a7d8f 433 /* Free our current storage */
b331163b
A
434 if (baseName != fullName) {
435 uprv_free(baseName);
436 }
437 baseName = NULL;
b75a7d8f
A
438 if(fullName != fullNameBuffer) {
439 uprv_free(fullName);
440 fullName = fullNameBuffer;
441 }
442
443 /* Allocate the full name if necessary */
444 if(other.fullName != other.fullNameBuffer) {
445 fullName = (char *)uprv_malloc(sizeof(char)*(uprv_strlen(other.fullName)+1));
46f4442e
A
446 if (fullName == NULL) {
447 return *this;
448 }
b75a7d8f 449 }
b75a7d8f
A
450 /* Copy the full name */
451 uprv_strcpy(fullName, other.fullName);
452
b331163b
A
453 /* Copy the baseName if it differs from fullName. */
454 if (other.baseName == other.fullName) {
455 baseName = fullName;
456 } else {
457 if (other.baseName) {
458 baseName = uprv_strdup(other.baseName);
459 }
374ca955
A
460 }
461
b75a7d8f
A
462 /* Copy the language and country fields */
463 uprv_strcpy(language, other.language);
374ca955 464 uprv_strcpy(script, other.script);
b75a7d8f
A
465 uprv_strcpy(country, other.country);
466
729e4ab9 467 /* The variantBegin is an offset, just copy it */
b75a7d8f
A
468 variantBegin = other.variantBegin;
469 fIsBogus = other.fIsBogus;
470 return *this;
471}
472
374ca955
A
473Locale *
474Locale::clone() const {
475 return new Locale(*this);
476}
477
b75a7d8f
A
478UBool
479Locale::operator==( const Locale& other) const
480{
481 return (uprv_strcmp(other.fullName, fullName) == 0);
482}
483
4388f060
A
484#define ISASCIIALPHA(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
485
b75a7d8f 486/*This function initializes a Locale from a C locale ID*/
374ca955 487Locale& Locale::init(const char* localeID, UBool canonicalize)
b75a7d8f
A
488{
489 fIsBogus = FALSE;
490 /* Free our current storage */
b331163b
A
491 if (baseName != fullName) {
492 uprv_free(baseName);
493 }
494 baseName = NULL;
b75a7d8f
A
495 if(fullName != fullNameBuffer) {
496 uprv_free(fullName);
497 fullName = fullNameBuffer;
498 }
499
500 // not a loop:
501 // just an easy way to have a common error-exit
502 // without goto and without another function
503 do {
374ca955
A
504 char *separator;
505 char *field[5] = {0};
506 int32_t fieldLen[5] = {0};
507 int32_t fieldIdx;
508 int32_t variantField;
b75a7d8f
A
509 int32_t length;
510 UErrorCode err;
511
512 if(localeID == NULL) {
513 // not an error, just set the default locale
514 return *this = getDefault();
515 }
516
374ca955
A
517 /* preset all fields to empty */
518 language[0] = script[0] = country[0] = 0;
519
b75a7d8f
A
520 // "canonicalize" the locale ID to ICU/Java format
521 err = U_ZERO_ERROR;
374ca955
A
522 length = canonicalize ?
523 uloc_canonicalize(localeID, fullName, sizeof(fullNameBuffer), &err) :
524 uloc_getName(localeID, fullName, sizeof(fullNameBuffer), &err);
525
526 if(err == U_BUFFER_OVERFLOW_ERROR || length >= (int32_t)sizeof(fullNameBuffer)) {
b75a7d8f
A
527 /*Go to heap for the fullName if necessary*/
528 fullName = (char *)uprv_malloc(sizeof(char)*(length + 1));
529 if(fullName == 0) {
530 fullName = fullNameBuffer;
531 break; // error: out of memory
532 }
533 err = U_ZERO_ERROR;
374ca955
A
534 length = canonicalize ?
535 uloc_canonicalize(localeID, fullName, length+1, &err) :
536 uloc_getName(localeID, fullName, length+1, &err);
b75a7d8f
A
537 }
538 if(U_FAILURE(err) || err == U_STRING_NOT_TERMINATED_WARNING) {
539 /* should never occur */
540 break;
541 }
542
374ca955 543 variantBegin = length;
b75a7d8f 544
374ca955
A
545 /* after uloc_getName/canonicalize() we know that only '_' are separators */
546 separator = field[0] = fullName;
547 fieldIdx = 1;
2ca993e8 548 while ((separator = uprv_strchr(field[fieldIdx-1], SEP_CHAR)) && fieldIdx < UPRV_LENGTHOF(field)-1) {
374ca955 549 field[fieldIdx] = separator + 1;
73c04bcf 550 fieldLen[fieldIdx-1] = (int32_t)(separator - field[fieldIdx-1]);
374ca955
A
551 fieldIdx++;
552 }
553 // variant may contain @foo or .foo POSIX cruft; remove it
554 separator = uprv_strchr(field[fieldIdx-1], '@');
555 char* sep2 = uprv_strchr(field[fieldIdx-1], '.');
556 if (separator!=NULL || sep2!=NULL) {
557 if (separator==NULL || (sep2!=NULL && separator > sep2)) {
558 separator = sep2;
b75a7d8f 559 }
73c04bcf 560 fieldLen[fieldIdx-1] = (int32_t)(separator - field[fieldIdx-1]);
b75a7d8f 561 } else {
374ca955
A
562 fieldLen[fieldIdx-1] = length - (int32_t)(field[fieldIdx-1] - fullName);
563 }
564
4388f060 565 if (fieldLen[0] >= (int32_t)(sizeof(language)))
374ca955 566 {
4388f060 567 break; // error: the language field is too long
374ca955
A
568 }
569
4388f060 570 variantField = 1; /* Usually the 2nd one, except when a script or country is also used. */
374ca955
A
571 if (fieldLen[0] > 0) {
572 /* We have a language */
573 uprv_memcpy(language, fullName, fieldLen[0]);
574 language[fieldLen[0]] = 0;
575 }
4388f060
A
576 if (fieldLen[1] == 4 && ISASCIIALPHA(field[1][0]) &&
577 ISASCIIALPHA(field[1][1]) && ISASCIIALPHA(field[1][2]) &&
578 ISASCIIALPHA(field[1][3])) {
374ca955
A
579 /* We have at least a script */
580 uprv_memcpy(script, field[1], fieldLen[1]);
581 script[fieldLen[1]] = 0;
4388f060 582 variantField++;
374ca955 583 }
4388f060
A
584
585 if (fieldLen[variantField] == 2 || fieldLen[variantField] == 3) {
586 /* We have a country */
587 uprv_memcpy(country, field[variantField], fieldLen[variantField]);
588 country[fieldLen[variantField]] = 0;
589 variantField++;
590 } else if (fieldLen[variantField] == 0) {
591 variantField++; /* script or country empty but variant in next field (i.e. en__POSIX) */
374ca955 592 }
4388f060
A
593
594 if (fieldLen[variantField] > 0) {
374ca955
A
595 /* We have a variant */
596 variantBegin = (int32_t)(field[variantField] - fullName);
b75a7d8f
A
597 }
598
b331163b
A
599 err = U_ZERO_ERROR;
600 initBaseName(err);
601 if (U_FAILURE(err)) {
602 break;
603 }
604
b75a7d8f
A
605 // successful end of init()
606 return *this;
73c04bcf 607 } while(0); /*loop doesn't iterate*/
b75a7d8f
A
608
609 // when an error occurs, then set this object to "bogus" (there is no UErrorCode here)
610 setToBogus();
611
612 return *this;
613}
614
b331163b
A
615/*
616 * Set up the base name.
617 * If there are no key words, it's exactly the full name.
618 * If key words exist, it's the full name truncated at the '@' character.
619 * Need to set up both at init() and after setting a keyword.
620 */
621void
622Locale::initBaseName(UErrorCode &status) {
623 if (U_FAILURE(status)) {
624 return;
625 }
626 U_ASSERT(baseName==NULL || baseName==fullName);
627 const char *atPtr = uprv_strchr(fullName, '@');
628 const char *eqPtr = uprv_strchr(fullName, '=');
629 if (atPtr && eqPtr && atPtr < eqPtr) {
630 // Key words exist.
631 int32_t baseNameLength = (int32_t)(atPtr - fullName);
632 baseName = (char *)uprv_malloc(baseNameLength + 1);
633 if (baseName == NULL) {
634 status = U_MEMORY_ALLOCATION_ERROR;
635 return;
636 }
637 uprv_strncpy(baseName, fullName, baseNameLength);
638 baseName[baseNameLength] = 0;
639
640 // The original computation of variantBegin leaves it equal to the length
641 // of fullName if there is no variant. It should instead be
642 // the length of the baseName.
643 if (variantBegin > baseNameLength) {
644 variantBegin = baseNameLength;
645 }
646 } else {
647 baseName = fullName;
648 }
649}
650
651
b75a7d8f 652int32_t
374ca955 653Locale::hashCode() const
b75a7d8f 654{
4388f060 655 return ustr_hashCharsN(fullName, uprv_strlen(fullName));
b75a7d8f
A
656}
657
374ca955 658void
b75a7d8f 659Locale::setToBogus() {
374ca955 660 /* Free our current storage */
b331163b
A
661 if(baseName != fullName) {
662 uprv_free(baseName);
663 }
664 baseName = NULL;
374ca955
A
665 if(fullName != fullNameBuffer) {
666 uprv_free(fullName);
667 fullName = fullNameBuffer;
668 }
669 *fullNameBuffer = 0;
670 *language = 0;
671 *script = 0;
672 *country = 0;
673 fIsBogus = TRUE;
f3c0d7a5 674 variantBegin = 0;
b75a7d8f
A
675}
676
374ca955
A
677const Locale& U_EXPORT2
678Locale::getDefault()
b75a7d8f 679{
51004dcb
A
680 {
681 Mutex lock(&gDefaultLocaleMutex);
682 if (gDefaultLocale != NULL) {
683 return *gDefaultLocale;
684 }
b75a7d8f 685 }
51004dcb
A
686 UErrorCode status = U_ZERO_ERROR;
687 return *locale_set_default_internal(NULL, status);
b75a7d8f
A
688}
689
374ca955
A
690
691
692void U_EXPORT2
693Locale::setDefault( const Locale& newLocale,
694 UErrorCode& status)
b75a7d8f 695{
374ca955 696 if (U_FAILURE(status)) {
b75a7d8f 697 return;
374ca955
A
698 }
699
700 /* Set the default from the full name string of the supplied locale.
701 * This is a convenient way to access the default locale caching mechanisms.
702 */
703 const char *localeID = newLocale.getName();
51004dcb 704 locale_set_default_internal(localeID, status);
b75a7d8f
A
705}
706
374ca955 707Locale U_EXPORT2
b75a7d8f
A
708Locale::createFromName (const char *name)
709{
710 if (name) {
374ca955
A
711 Locale l("");
712 l.init(name, FALSE);
b75a7d8f
A
713 return l;
714 }
715 else {
716 return getDefault();
717 }
718}
719
374ca955
A
720Locale U_EXPORT2
721Locale::createCanonical(const char* name) {
722 Locale loc("");
723 loc.init(name, TRUE);
724 return loc;
725}
b75a7d8f
A
726
727const char *
728Locale::getISO3Language() const
729{
730 return uloc_getISO3Language(fullName);
731}
732
733
734const char *
735Locale::getISO3Country() const
736{
737 return uloc_getISO3Country(fullName);
738}
739
740/**
741 * Return the LCID value as specified in the "LocaleID" resource for this
742 * locale. The LocaleID must be expressed as a hexadecimal number, from
743 * one to four digits. If the LocaleID resource is not present, or is
744 * in an incorrect format, 0 is returned. The LocaleID is for use in
745 * Windows (it is an LCID), but is available on all platforms.
746 */
374ca955 747uint32_t
b75a7d8f
A
748Locale::getLCID() const
749{
750 return uloc_getLCID(fullName);
751}
752
374ca955 753const char* const* U_EXPORT2 Locale::getISOCountries()
b75a7d8f
A
754{
755 return uloc_getISOCountries();
756}
757
374ca955 758const char* const* U_EXPORT2 Locale::getISOLanguages()
b75a7d8f
A
759{
760 return uloc_getISOLanguages();
761}
762
374ca955 763// Set the locale's data based on a posix id.
b75a7d8f
A
764void Locale::setFromPOSIXID(const char *posixID)
765{
374ca955 766 init(posixID, TRUE);
b75a7d8f
A
767}
768
729e4ab9
A
769const Locale & U_EXPORT2
770Locale::getRoot(void)
771{
772 return getLocale(eROOT);
773}
774
374ca955 775const Locale & U_EXPORT2
b75a7d8f
A
776Locale::getEnglish(void)
777{
778 return getLocale(eENGLISH);
779}
780
374ca955 781const Locale & U_EXPORT2
b75a7d8f
A
782Locale::getFrench(void)
783{
784 return getLocale(eFRENCH);
785}
786
374ca955 787const Locale & U_EXPORT2
b75a7d8f
A
788Locale::getGerman(void)
789{
790 return getLocale(eGERMAN);
791}
792
374ca955 793const Locale & U_EXPORT2
b75a7d8f
A
794Locale::getItalian(void)
795{
796 return getLocale(eITALIAN);
797}
798
374ca955 799const Locale & U_EXPORT2
b75a7d8f
A
800Locale::getJapanese(void)
801{
802 return getLocale(eJAPANESE);
803}
804
374ca955 805const Locale & U_EXPORT2
b75a7d8f
A
806Locale::getKorean(void)
807{
808 return getLocale(eKOREAN);
809}
810
374ca955 811const Locale & U_EXPORT2
b75a7d8f
A
812Locale::getChinese(void)
813{
814 return getLocale(eCHINESE);
815}
816
374ca955 817const Locale & U_EXPORT2
b75a7d8f
A
818Locale::getSimplifiedChinese(void)
819{
820 return getLocale(eCHINA);
821}
822
374ca955 823const Locale & U_EXPORT2
b75a7d8f
A
824Locale::getTraditionalChinese(void)
825{
826 return getLocale(eTAIWAN);
827}
828
829
374ca955 830const Locale & U_EXPORT2
b75a7d8f
A
831Locale::getFrance(void)
832{
833 return getLocale(eFRANCE);
834}
835
374ca955 836const Locale & U_EXPORT2
b75a7d8f
A
837Locale::getGermany(void)
838{
839 return getLocale(eGERMANY);
840}
841
374ca955 842const Locale & U_EXPORT2
b75a7d8f
A
843Locale::getItaly(void)
844{
845 return getLocale(eITALY);
846}
847
374ca955 848const Locale & U_EXPORT2
b75a7d8f
A
849Locale::getJapan(void)
850{
851 return getLocale(eJAPAN);
852}
853
374ca955 854const Locale & U_EXPORT2
b75a7d8f
A
855Locale::getKorea(void)
856{
857 return getLocale(eKOREA);
858}
859
374ca955 860const Locale & U_EXPORT2
b75a7d8f
A
861Locale::getChina(void)
862{
863 return getLocale(eCHINA);
864}
865
374ca955 866const Locale & U_EXPORT2
b75a7d8f
A
867Locale::getPRC(void)
868{
869 return getLocale(eCHINA);
870}
871
374ca955 872const Locale & U_EXPORT2
b75a7d8f
A
873Locale::getTaiwan(void)
874{
875 return getLocale(eTAIWAN);
876}
877
374ca955 878const Locale & U_EXPORT2
b75a7d8f
A
879Locale::getUK(void)
880{
881 return getLocale(eUK);
882}
883
374ca955 884const Locale & U_EXPORT2
b75a7d8f
A
885Locale::getUS(void)
886{
887 return getLocale(eUS);
888}
889
374ca955 890const Locale & U_EXPORT2
b75a7d8f
A
891Locale::getCanada(void)
892{
893 return getLocale(eCANADA);
894}
895
374ca955 896const Locale & U_EXPORT2
b75a7d8f
A
897Locale::getCanadaFrench(void)
898{
899 return getLocale(eCANADA_FRENCH);
900}
901
902const Locale &
903Locale::getLocale(int locid)
904{
905 Locale *localeCache = getLocaleCache();
73c04bcf 906 U_ASSERT((locid < eMAX_LOCALES)&&(locid>=0));
b75a7d8f
A
907 if (localeCache == NULL) {
908 // Failure allocating the locale cache.
909 // The best we can do is return a NULL reference.
910 locid = 0;
911 }
73c04bcf 912 return localeCache[locid]; /*operating on NULL*/
b75a7d8f
A
913}
914
915/*
916This function is defined this way in order to get around static
917initialization and static destruction.
918 */
919Locale *
920Locale::getLocaleCache(void)
921{
b331163b
A
922 UErrorCode status = U_ZERO_ERROR;
923 umtx_initOnce(gLocaleCacheInitOnce, locale_init, status);
b75a7d8f
A
924 return gLocaleCache;
925}
926
374ca955
A
927class KeywordEnumeration : public StringEnumeration {
928private:
929 char *keywords;
930 char *current;
931 int32_t length;
932 UnicodeString currUSKey;
933 static const char fgClassID;/* Warning this is used beyond the typical RTTI usage. */
934
935public:
936 static UClassID U_EXPORT2 getStaticClassID(void) { return (UClassID)&fgClassID; }
937 virtual UClassID getDynamicClassID(void) const { return getStaticClassID(); }
938public:
939 KeywordEnumeration(const char *keys, int32_t keywordLen, int32_t currentIndex, UErrorCode &status)
940 : keywords((char *)&fgClassID), current((char *)&fgClassID), length(0) {
941 if(U_SUCCESS(status) && keywordLen != 0) {
942 if(keys == NULL || keywordLen < 0) {
943 status = U_ILLEGAL_ARGUMENT_ERROR;
944 } else {
945 keywords = (char *)uprv_malloc(keywordLen+1);
946 if (keywords == NULL) {
947 status = U_MEMORY_ALLOCATION_ERROR;
948 }
949 else {
950 uprv_memcpy(keywords, keys, keywordLen);
951 keywords[keywordLen] = 0;
952 current = keywords + currentIndex;
953 length = keywordLen;
954 }
955 }
956 }
957 }
958
4388f060 959 virtual ~KeywordEnumeration();
374ca955
A
960
961 virtual StringEnumeration * clone() const
962 {
963 UErrorCode status = U_ZERO_ERROR;
964 return new KeywordEnumeration(keywords, length, (int32_t)(current - keywords), status);
965 }
966
967 virtual int32_t count(UErrorCode &/*status*/) const {
968 char *kw = keywords;
969 int32_t result = 0;
970 while(*kw) {
971 result++;
972 kw += uprv_strlen(kw)+1;
973 }
974 return result;
975 }
976
977 virtual const char* next(int32_t* resultLength, UErrorCode& status) {
978 const char* result;
979 int32_t len;
980 if(U_SUCCESS(status) && *current != 0) {
981 result = current;
73c04bcf 982 len = (int32_t)uprv_strlen(current);
374ca955
A
983 current += len+1;
984 if(resultLength != NULL) {
985 *resultLength = len;
986 }
987 } else {
988 if(resultLength != NULL) {
989 *resultLength = 0;
990 }
991 result = NULL;
992 }
993 return result;
994 }
995
996 virtual const UnicodeString* snext(UErrorCode& status) {
997 int32_t resultLength = 0;
998 const char *s = next(&resultLength, status);
999 return setChars(s, resultLength, status);
1000 }
1001
1002 virtual void reset(UErrorCode& /*status*/) {
1003 current = keywords;
1004 }
1005};
1006
1007const char KeywordEnumeration::fgClassID = '\0';
1008
4388f060
A
1009KeywordEnumeration::~KeywordEnumeration() {
1010 uprv_free(keywords);
1011}
1012
374ca955
A
1013StringEnumeration *
1014Locale::createKeywords(UErrorCode &status) const
1015{
1016 char keywords[256];
1017 int32_t keywordCapacity = 256;
1018 StringEnumeration *result = NULL;
1019
1020 const char* variantStart = uprv_strchr(fullName, '@');
1021 const char* assignment = uprv_strchr(fullName, '=');
1022 if(variantStart) {
1023 if(assignment > variantStart) {
1024 int32_t keyLen = locale_getKeywords(variantStart+1, '@', keywords, keywordCapacity, NULL, 0, NULL, FALSE, &status);
1025 if(keyLen) {
1026 result = new KeywordEnumeration(keywords, keyLen, 0, status);
1027 }
1028 } else {
1029 status = U_INVALID_FORMAT_ERROR;
1030 }
1031 }
1032 return result;
1033}
1034
1035int32_t
1036Locale::getKeywordValue(const char* keywordName, char *buffer, int32_t bufLen, UErrorCode &status) const
1037{
1038 return uloc_getKeywordValue(fullName, keywordName, buffer, bufLen, &status);
1039}
1040
729e4ab9
A
1041void
1042Locale::setKeywordValue(const char* keywordName, const char* keywordValue, UErrorCode &status)
1043{
1044 uloc_setKeywordValue(keywordName, keywordValue, fullName, ULOC_FULLNAME_CAPACITY, &status);
b331163b
A
1045 if (U_SUCCESS(status) && baseName == fullName) {
1046 // May have added the first keyword, meaning that the fullName is no longer also the baseName.
1047 initBaseName(status);
1048 }
729e4ab9
A
1049}
1050
374ca955 1051const char *
b331163b 1052Locale::getBaseName() const {
374ca955
A
1053 return baseName;
1054}
1055
b75a7d8f
A
1056//eof
1057U_NAMESPACE_END