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