intl/icu/source/common/locid.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/common/locid.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,1037 @@
     1.4 +/*
     1.5 + **********************************************************************
     1.6 + *   Copyright (C) 1997-2012, International Business Machines
     1.7 + *   Corporation and others.  All Rights Reserved.
     1.8 + **********************************************************************
     1.9 +*
    1.10 +* File locid.cpp
    1.11 +*
    1.12 +* Created by: Richard Gillam
    1.13 +*
    1.14 +* Modification History:
    1.15 +*
    1.16 +*   Date        Name        Description
    1.17 +*   02/11/97    aliu        Changed gLocPath to fgDataDirectory and added
    1.18 +*                           methods to get and set it.
    1.19 +*   04/02/97    aliu        Made operator!= inline; fixed return value
    1.20 +*                           of getName().
    1.21 +*   04/15/97    aliu        Cleanup for AIX/Win32.
    1.22 +*   04/24/97    aliu        Numerous changes per code review.
    1.23 +*   08/18/98    stephen     Changed getDisplayName()
    1.24 +*                           Added SIMPLIFIED_CHINESE, TRADITIONAL_CHINESE
    1.25 +*                           Added getISOCountries(), getISOLanguages(),
    1.26 +*                           getLanguagesForCountry()
    1.27 +*   03/16/99    bertrand    rehaul.
    1.28 +*   07/21/99    stephen     Added U_CFUNC setDefault
    1.29 +*   11/09/99    weiv        Added const char * getName() const;
    1.30 +*   04/12/00    srl         removing unicodestring api's and cached hash code
    1.31 +*   08/10/01    grhoten     Change the static Locales to accessor functions
    1.32 +******************************************************************************
    1.33 +*/
    1.34 +
    1.35 +
    1.36 +#include "unicode/locid.h"
    1.37 +#include "unicode/uloc.h"
    1.38 +#include "putilimp.h"
    1.39 +#include "mutex.h"
    1.40 +#include "umutex.h"
    1.41 +#include "uassert.h"
    1.42 +#include "cmemory.h"
    1.43 +#include "cstring.h"
    1.44 +#include "uhash.h"
    1.45 +#include "ucln_cmn.h"
    1.46 +#include "ustr_imp.h"
    1.47 +
    1.48 +#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
    1.49 +
    1.50 +U_CDECL_BEGIN
    1.51 +static UBool U_CALLCONV locale_cleanup(void);
    1.52 +U_CDECL_END
    1.53 +
    1.54 +U_NAMESPACE_BEGIN
    1.55 +
    1.56 +static Locale *gLocaleCache = NULL;
    1.57 +
    1.58 +// gDefaultLocaleMutex protects all access to gDefaultLocalesHashT and gDefaultLocale.
    1.59 +static UMutex gDefaultLocaleMutex = U_MUTEX_INITIALIZER;
    1.60 +static UHashtable *gDefaultLocalesHashT = NULL;
    1.61 +static Locale *gDefaultLocale = NULL;
    1.62 +
    1.63 +U_NAMESPACE_END
    1.64 +
    1.65 +typedef enum ELocalePos {
    1.66 +    eENGLISH,
    1.67 +    eFRENCH,
    1.68 +    eGERMAN,
    1.69 +    eITALIAN,
    1.70 +    eJAPANESE,
    1.71 +    eKOREAN,
    1.72 +    eCHINESE,
    1.73 +
    1.74 +    eFRANCE,
    1.75 +    eGERMANY,
    1.76 +    eITALY,
    1.77 +    eJAPAN,
    1.78 +    eKOREA,
    1.79 +    eCHINA,      /* Alias for PRC */
    1.80 +    eTAIWAN,
    1.81 +    eUK,
    1.82 +    eUS,
    1.83 +    eCANADA,
    1.84 +    eCANADA_FRENCH,
    1.85 +    eROOT,
    1.86 +
    1.87 +
    1.88 +    //eDEFAULT,
    1.89 +    eMAX_LOCALES
    1.90 +} ELocalePos;
    1.91 +
    1.92 +U_CFUNC int32_t locale_getKeywords(const char *localeID,
    1.93 +            char prev,
    1.94 +            char *keywords, int32_t keywordCapacity,
    1.95 +            char *values, int32_t valuesCapacity, int32_t *valLen,
    1.96 +            UBool valuesToo,
    1.97 +            UErrorCode *status);
    1.98 +
    1.99 +U_CDECL_BEGIN
   1.100 +//
   1.101 +// Deleter function for Locales owned by the default Locale hash table/
   1.102 +//
   1.103 +static void U_CALLCONV
   1.104 +deleteLocale(void *obj) {
   1.105 +    delete (icu::Locale *) obj;
   1.106 +}
   1.107 +
   1.108 +static UBool U_CALLCONV locale_cleanup(void)
   1.109 +{
   1.110 +    U_NAMESPACE_USE
   1.111 +
   1.112 +    if (gLocaleCache) {
   1.113 +        delete [] gLocaleCache;
   1.114 +        gLocaleCache = NULL;
   1.115 +    }
   1.116 +
   1.117 +    if (gDefaultLocalesHashT) {
   1.118 +        uhash_close(gDefaultLocalesHashT);   // Automatically deletes all elements, using deleter func.
   1.119 +        gDefaultLocalesHashT = NULL;
   1.120 +        gDefaultLocale = NULL;
   1.121 +    }
   1.122 +
   1.123 +    return TRUE;
   1.124 +}
   1.125 +U_CDECL_END
   1.126 +
   1.127 +U_NAMESPACE_BEGIN
   1.128 +
   1.129 +Locale *locale_set_default_internal(const char *id, UErrorCode& status) {
   1.130 +    // Synchronize this entire function.
   1.131 +    Mutex lock(&gDefaultLocaleMutex);
   1.132 +    
   1.133 +    UBool canonicalize = FALSE;
   1.134 +
   1.135 +    // If given a NULL string for the locale id, grab the default
   1.136 +    //   name from the system.
   1.137 +    //   (Different from most other locale APIs, where a null name means use
   1.138 +    //    the current ICU default locale.)
   1.139 +    if (id == NULL) {
   1.140 +        id = uprv_getDefaultLocaleID();   // This function not thread safe? TODO: verify.
   1.141 +        canonicalize = TRUE; // always canonicalize host ID
   1.142 +    }
   1.143 +
   1.144 +    char localeNameBuf[512];
   1.145 +
   1.146 +    if (canonicalize) {
   1.147 +        uloc_canonicalize(id, localeNameBuf, sizeof(localeNameBuf)-1, &status);
   1.148 +    } else {
   1.149 +        uloc_getName(id, localeNameBuf, sizeof(localeNameBuf)-1, &status);
   1.150 +    }
   1.151 +    localeNameBuf[sizeof(localeNameBuf)-1] = 0;  // Force null termination in event of
   1.152 +                                                 //   a long name filling the buffer.
   1.153 +                                                 //   (long names are truncated.)
   1.154 +                                                 //
   1.155 +    if (U_FAILURE(status)) {
   1.156 +        return gDefaultLocale;
   1.157 +    }
   1.158 +
   1.159 +    if (gDefaultLocalesHashT == NULL) {
   1.160 +        gDefaultLocalesHashT = uhash_open(uhash_hashChars, uhash_compareChars, NULL, &status);
   1.161 +        if (U_FAILURE(status)) {
   1.162 +            return gDefaultLocale;
   1.163 +        }
   1.164 +        uhash_setValueDeleter(gDefaultLocalesHashT, deleteLocale);
   1.165 +        ucln_common_registerCleanup(UCLN_COMMON_LOCALE, locale_cleanup);
   1.166 +    }
   1.167 +
   1.168 +    Locale *newDefault = (Locale *)uhash_get(gDefaultLocalesHashT, localeNameBuf);
   1.169 +    if (newDefault == NULL) {
   1.170 +        newDefault = new Locale(Locale::eBOGUS);
   1.171 +        if (newDefault == NULL) {
   1.172 +            status = U_MEMORY_ALLOCATION_ERROR;
   1.173 +            return gDefaultLocale;
   1.174 +        }
   1.175 +        newDefault->init(localeNameBuf, FALSE);
   1.176 +        uhash_put(gDefaultLocalesHashT, (char*) newDefault->getName(), newDefault, &status);
   1.177 +        if (U_FAILURE(status)) {
   1.178 +            return gDefaultLocale;
   1.179 +        }
   1.180 +    }
   1.181 +    gDefaultLocale = newDefault;
   1.182 +    return gDefaultLocale;
   1.183 +}
   1.184 +
   1.185 +U_NAMESPACE_END
   1.186 +
   1.187 +/* sfb 07/21/99 */
   1.188 +U_CFUNC void
   1.189 +locale_set_default(const char *id)
   1.190 +{
   1.191 +    U_NAMESPACE_USE
   1.192 +    UErrorCode status = U_ZERO_ERROR;
   1.193 +    locale_set_default_internal(id, status);
   1.194 +}
   1.195 +/* end */
   1.196 +
   1.197 +U_CFUNC const char *
   1.198 +locale_get_default(void)
   1.199 +{
   1.200 +    U_NAMESPACE_USE
   1.201 +    return Locale::getDefault().getName();
   1.202 +}
   1.203 +
   1.204 +
   1.205 +U_NAMESPACE_BEGIN
   1.206 +
   1.207 +UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Locale)
   1.208 +
   1.209 +/*Character separating the posix id fields*/
   1.210 +// '_'
   1.211 +// In the platform codepage.
   1.212 +#define SEP_CHAR '_'
   1.213 +
   1.214 +Locale::~Locale()
   1.215 +{
   1.216 +    /*if fullName is on the heap, we free it*/
   1.217 +    if (fullName != fullNameBuffer)
   1.218 +    {
   1.219 +        uprv_free(fullName);
   1.220 +        fullName = NULL;
   1.221 +    }
   1.222 +    if (baseName && baseName != baseNameBuffer) {
   1.223 +        uprv_free(baseName);
   1.224 +        baseName = NULL;
   1.225 +    }
   1.226 +}
   1.227 +
   1.228 +Locale::Locale()
   1.229 +    : UObject(), fullName(fullNameBuffer), baseName(NULL)
   1.230 +{
   1.231 +    init(NULL, FALSE);
   1.232 +}
   1.233 +
   1.234 +/*
   1.235 + * Internal constructor to allow construction of a locale object with
   1.236 + *   NO side effects.   (Default constructor tries to get
   1.237 + *   the default locale.)
   1.238 + */
   1.239 +Locale::Locale(Locale::ELocaleType)
   1.240 +    : UObject(), fullName(fullNameBuffer), baseName(NULL)
   1.241 +{
   1.242 +    setToBogus();
   1.243 +}
   1.244 +
   1.245 +
   1.246 +Locale::Locale( const   char * newLanguage,
   1.247 +                const   char * newCountry,
   1.248 +                const   char * newVariant,
   1.249 +                const   char * newKeywords)
   1.250 +    : UObject(), fullName(fullNameBuffer), baseName(NULL)
   1.251 +{
   1.252 +    if( (newLanguage==NULL) && (newCountry == NULL) && (newVariant == NULL) )
   1.253 +    {
   1.254 +        init(NULL, FALSE); /* shortcut */
   1.255 +    }
   1.256 +    else
   1.257 +    {
   1.258 +        MaybeStackArray<char, ULOC_FULLNAME_CAPACITY> togo;
   1.259 +        int32_t size = 0;
   1.260 +        int32_t lsize = 0;
   1.261 +        int32_t csize = 0;
   1.262 +        int32_t vsize = 0;
   1.263 +        int32_t ksize = 0;
   1.264 +        char    *p;
   1.265 +
   1.266 +        // Calculate the size of the resulting string.
   1.267 +
   1.268 +        // Language
   1.269 +        if ( newLanguage != NULL )
   1.270 +        {
   1.271 +            lsize = (int32_t)uprv_strlen(newLanguage);
   1.272 +            size = lsize;
   1.273 +        }
   1.274 +
   1.275 +        // _Country
   1.276 +        if ( newCountry != NULL )
   1.277 +        {
   1.278 +            csize = (int32_t)uprv_strlen(newCountry);
   1.279 +            size += csize;
   1.280 +        }
   1.281 +
   1.282 +        // _Variant
   1.283 +        if ( newVariant != NULL )
   1.284 +        {
   1.285 +            // remove leading _'s
   1.286 +            while(newVariant[0] == SEP_CHAR)
   1.287 +            {
   1.288 +                newVariant++;
   1.289 +            }
   1.290 +
   1.291 +            // remove trailing _'s
   1.292 +            vsize = (int32_t)uprv_strlen(newVariant);
   1.293 +            while( (vsize>1) && (newVariant[vsize-1] == SEP_CHAR) )
   1.294 +            {
   1.295 +                vsize--;
   1.296 +            }
   1.297 +        }
   1.298 +
   1.299 +        if( vsize > 0 )
   1.300 +        {
   1.301 +            size += vsize;
   1.302 +        }
   1.303 +
   1.304 +        // Separator rules:
   1.305 +        if ( vsize > 0 )
   1.306 +        {
   1.307 +            size += 2;  // at least: __v
   1.308 +        }
   1.309 +        else if ( csize > 0 )
   1.310 +        {
   1.311 +            size += 1;  // at least: _v
   1.312 +        }
   1.313 +
   1.314 +        if ( newKeywords != NULL)
   1.315 +        {
   1.316 +            ksize = (int32_t)uprv_strlen(newKeywords);
   1.317 +            size += ksize + 1;
   1.318 +        }
   1.319 +
   1.320 +
   1.321 +        //  NOW we have the full locale string..
   1.322 +
   1.323 +        /*if the whole string is longer than our internal limit, we need
   1.324 +        to go to the heap for temporary buffers*/
   1.325 +        if (size >= togo.getCapacity())
   1.326 +        {
   1.327 +            // If togo_heap could not be created, initialize with default settings.
   1.328 +            if (togo.resize(size+1) == NULL) {
   1.329 +                init(NULL, FALSE);
   1.330 +            }
   1.331 +        }
   1.332 +
   1.333 +        togo[0] = 0;
   1.334 +
   1.335 +        // Now, copy it back.
   1.336 +        p = togo.getAlias();
   1.337 +        if ( lsize != 0 )
   1.338 +        {
   1.339 +            uprv_strcpy(p, newLanguage);
   1.340 +            p += lsize;
   1.341 +        }
   1.342 +
   1.343 +        if ( ( vsize != 0 ) || (csize != 0) )  // at least:  __v
   1.344 +        {                                      //            ^
   1.345 +            *p++ = SEP_CHAR;
   1.346 +        }
   1.347 +
   1.348 +        if ( csize != 0 )
   1.349 +        {
   1.350 +            uprv_strcpy(p, newCountry);
   1.351 +            p += csize;
   1.352 +        }
   1.353 +
   1.354 +        if ( vsize != 0)
   1.355 +        {
   1.356 +            *p++ = SEP_CHAR; // at least: __v
   1.357 +
   1.358 +            uprv_strncpy(p, newVariant, vsize);  // Must use strncpy because
   1.359 +            p += vsize;                          // of trimming (above).
   1.360 +            *p = 0; // terminate
   1.361 +        }
   1.362 +
   1.363 +        if ( ksize != 0)
   1.364 +        {
   1.365 +            if (uprv_strchr(newKeywords, '=')) {
   1.366 +                *p++ = '@'; /* keyword parsing */
   1.367 +            }
   1.368 +            else {
   1.369 +                *p++ = '_'; /* Variant parsing with a script */
   1.370 +                if ( vsize == 0) {
   1.371 +                    *p++ = '_'; /* No country found */
   1.372 +                }
   1.373 +            }
   1.374 +            uprv_strcpy(p, newKeywords);
   1.375 +            p += ksize;
   1.376 +        }
   1.377 +
   1.378 +        // Parse it, because for example 'language' might really be a complete
   1.379 +        // string.
   1.380 +        init(togo.getAlias(), FALSE);
   1.381 +    }
   1.382 +}
   1.383 +
   1.384 +Locale::Locale(const Locale &other)
   1.385 +    : UObject(other), fullName(fullNameBuffer), baseName(NULL)
   1.386 +{
   1.387 +    *this = other;
   1.388 +}
   1.389 +
   1.390 +Locale &Locale::operator=(const Locale &other)
   1.391 +{
   1.392 +    if (this == &other) {
   1.393 +        return *this;
   1.394 +    }
   1.395 +
   1.396 +    if (&other == NULL) {
   1.397 +        this->setToBogus();
   1.398 +        return *this;
   1.399 +    }
   1.400 +
   1.401 +    /* Free our current storage */
   1.402 +    if(fullName != fullNameBuffer) {
   1.403 +        uprv_free(fullName);
   1.404 +        fullName = fullNameBuffer;
   1.405 +    }
   1.406 +
   1.407 +    /* Allocate the full name if necessary */
   1.408 +    if(other.fullName != other.fullNameBuffer) {
   1.409 +        fullName = (char *)uprv_malloc(sizeof(char)*(uprv_strlen(other.fullName)+1));
   1.410 +        if (fullName == NULL) {
   1.411 +            return *this;
   1.412 +        }
   1.413 +    }
   1.414 +    /* Copy the full name */
   1.415 +    uprv_strcpy(fullName, other.fullName);
   1.416 +
   1.417 +    /* baseName is the cached result of getBaseName.  if 'other' has a
   1.418 +       baseName and it fits in baseNameBuffer, then copy it. otherwise set
   1.419 +       it to NULL, and let the user lazy-create it (in getBaseName) if they
   1.420 +       want it. */
   1.421 +    if(baseName && baseName != baseNameBuffer) {
   1.422 +        uprv_free(baseName);
   1.423 +    }
   1.424 +    baseName = NULL;
   1.425 +
   1.426 +    if(other.baseName == other.baseNameBuffer) {
   1.427 +        uprv_strcpy(baseNameBuffer, other.baseNameBuffer);
   1.428 +        baseName = baseNameBuffer;
   1.429 +    }
   1.430 +
   1.431 +    /* Copy the language and country fields */
   1.432 +    uprv_strcpy(language, other.language);
   1.433 +    uprv_strcpy(script, other.script);
   1.434 +    uprv_strcpy(country, other.country);
   1.435 +
   1.436 +    /* The variantBegin is an offset, just copy it */
   1.437 +    variantBegin = other.variantBegin;
   1.438 +    fIsBogus = other.fIsBogus;
   1.439 +    return *this;
   1.440 +}
   1.441 +
   1.442 +Locale *
   1.443 +Locale::clone() const {
   1.444 +    return new Locale(*this);
   1.445 +}
   1.446 +
   1.447 +UBool
   1.448 +Locale::operator==( const   Locale& other) const
   1.449 +{
   1.450 +    return (uprv_strcmp(other.fullName, fullName) == 0);
   1.451 +}
   1.452 +
   1.453 +#define ISASCIIALPHA(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
   1.454 +
   1.455 +/*This function initializes a Locale from a C locale ID*/
   1.456 +Locale& Locale::init(const char* localeID, UBool canonicalize)
   1.457 +{
   1.458 +    fIsBogus = FALSE;
   1.459 +    /* Free our current storage */
   1.460 +    if(fullName != fullNameBuffer) {
   1.461 +        uprv_free(fullName);
   1.462 +        fullName = fullNameBuffer;
   1.463 +    }
   1.464 +
   1.465 +    if(baseName && baseName != baseNameBuffer) {
   1.466 +        uprv_free(baseName);
   1.467 +        baseName = NULL;
   1.468 +    }
   1.469 +
   1.470 +    // not a loop:
   1.471 +    // just an easy way to have a common error-exit
   1.472 +    // without goto and without another function
   1.473 +    do {
   1.474 +        char *separator;
   1.475 +        char *field[5] = {0};
   1.476 +        int32_t fieldLen[5] = {0};
   1.477 +        int32_t fieldIdx;
   1.478 +        int32_t variantField;
   1.479 +        int32_t length;
   1.480 +        UErrorCode err;
   1.481 +
   1.482 +        if(localeID == NULL) {
   1.483 +            // not an error, just set the default locale
   1.484 +            return *this = getDefault();
   1.485 +        }
   1.486 +
   1.487 +        /* preset all fields to empty */
   1.488 +        language[0] = script[0] = country[0] = 0;
   1.489 +
   1.490 +        // "canonicalize" the locale ID to ICU/Java format
   1.491 +        err = U_ZERO_ERROR;
   1.492 +        length = canonicalize ?
   1.493 +            uloc_canonicalize(localeID, fullName, sizeof(fullNameBuffer), &err) :
   1.494 +            uloc_getName(localeID, fullName, sizeof(fullNameBuffer), &err);
   1.495 +
   1.496 +        if(err == U_BUFFER_OVERFLOW_ERROR || length >= (int32_t)sizeof(fullNameBuffer)) {
   1.497 +            /*Go to heap for the fullName if necessary*/
   1.498 +            fullName = (char *)uprv_malloc(sizeof(char)*(length + 1));
   1.499 +            if(fullName == 0) {
   1.500 +                fullName = fullNameBuffer;
   1.501 +                break; // error: out of memory
   1.502 +            }
   1.503 +            err = U_ZERO_ERROR;
   1.504 +            length = canonicalize ?
   1.505 +                uloc_canonicalize(localeID, fullName, length+1, &err) :
   1.506 +                uloc_getName(localeID, fullName, length+1, &err);
   1.507 +        }
   1.508 +        if(U_FAILURE(err) || err == U_STRING_NOT_TERMINATED_WARNING) {
   1.509 +            /* should never occur */
   1.510 +            break;
   1.511 +        }
   1.512 +
   1.513 +        variantBegin = length;
   1.514 +
   1.515 +        /* after uloc_getName/canonicalize() we know that only '_' are separators */
   1.516 +        separator = field[0] = fullName;
   1.517 +        fieldIdx = 1;
   1.518 +        while ((separator = uprv_strchr(field[fieldIdx-1], SEP_CHAR)) && fieldIdx < (int32_t)(sizeof(field)/sizeof(field[0]))-1) {
   1.519 +            field[fieldIdx] = separator + 1;
   1.520 +            fieldLen[fieldIdx-1] = (int32_t)(separator - field[fieldIdx-1]);
   1.521 +            fieldIdx++;
   1.522 +        }
   1.523 +        // variant may contain @foo or .foo POSIX cruft; remove it
   1.524 +        separator = uprv_strchr(field[fieldIdx-1], '@');
   1.525 +        char* sep2 = uprv_strchr(field[fieldIdx-1], '.');
   1.526 +        if (separator!=NULL || sep2!=NULL) {
   1.527 +            if (separator==NULL || (sep2!=NULL && separator > sep2)) {
   1.528 +                separator = sep2;
   1.529 +            }
   1.530 +            fieldLen[fieldIdx-1] = (int32_t)(separator - field[fieldIdx-1]);
   1.531 +        } else {
   1.532 +            fieldLen[fieldIdx-1] = length - (int32_t)(field[fieldIdx-1] - fullName);
   1.533 +        }
   1.534 +
   1.535 +        if (fieldLen[0] >= (int32_t)(sizeof(language)))
   1.536 +        {
   1.537 +            break; // error: the language field is too long
   1.538 +        }
   1.539 +
   1.540 +        variantField = 1; /* Usually the 2nd one, except when a script or country is also used. */
   1.541 +        if (fieldLen[0] > 0) {
   1.542 +            /* We have a language */
   1.543 +            uprv_memcpy(language, fullName, fieldLen[0]);
   1.544 +            language[fieldLen[0]] = 0;
   1.545 +        }
   1.546 +        if (fieldLen[1] == 4 && ISASCIIALPHA(field[1][0]) &&
   1.547 +                ISASCIIALPHA(field[1][1]) && ISASCIIALPHA(field[1][2]) &&
   1.548 +                ISASCIIALPHA(field[1][3])) {
   1.549 +            /* We have at least a script */
   1.550 +            uprv_memcpy(script, field[1], fieldLen[1]);
   1.551 +            script[fieldLen[1]] = 0;
   1.552 +            variantField++;
   1.553 +        }
   1.554 +
   1.555 +        if (fieldLen[variantField] == 2 || fieldLen[variantField] == 3) {
   1.556 +            /* We have a country */
   1.557 +            uprv_memcpy(country, field[variantField], fieldLen[variantField]);
   1.558 +            country[fieldLen[variantField]] = 0;
   1.559 +            variantField++;
   1.560 +        } else if (fieldLen[variantField] == 0) {
   1.561 +            variantField++; /* script or country empty but variant in next field (i.e. en__POSIX) */
   1.562 +        }
   1.563 +
   1.564 +        if (fieldLen[variantField] > 0) {
   1.565 +            /* We have a variant */
   1.566 +            variantBegin = (int32_t)(field[variantField] - fullName);
   1.567 +        }
   1.568 +
   1.569 +        // successful end of init()
   1.570 +        return *this;
   1.571 +    } while(0); /*loop doesn't iterate*/
   1.572 +
   1.573 +    // when an error occurs, then set this object to "bogus" (there is no UErrorCode here)
   1.574 +    setToBogus();
   1.575 +
   1.576 +    return *this;
   1.577 +}
   1.578 +
   1.579 +int32_t
   1.580 +Locale::hashCode() const
   1.581 +{
   1.582 +    return ustr_hashCharsN(fullName, uprv_strlen(fullName));
   1.583 +}
   1.584 +
   1.585 +void
   1.586 +Locale::setToBogus() {
   1.587 +    /* Free our current storage */
   1.588 +    if(fullName != fullNameBuffer) {
   1.589 +        uprv_free(fullName);
   1.590 +        fullName = fullNameBuffer;
   1.591 +    }
   1.592 +    if(baseName && baseName != baseNameBuffer) {
   1.593 +        uprv_free(baseName);
   1.594 +        baseName = NULL;
   1.595 +    }
   1.596 +    *fullNameBuffer = 0;
   1.597 +    *language = 0;
   1.598 +    *script = 0;
   1.599 +    *country = 0;
   1.600 +    fIsBogus = TRUE;
   1.601 +}
   1.602 +
   1.603 +const Locale& U_EXPORT2
   1.604 +Locale::getDefault()
   1.605 +{
   1.606 +    {
   1.607 +        Mutex lock(&gDefaultLocaleMutex);
   1.608 +        if (gDefaultLocale != NULL) {
   1.609 +            return *gDefaultLocale;
   1.610 +        }
   1.611 +    }
   1.612 +    UErrorCode status = U_ZERO_ERROR;
   1.613 +    return *locale_set_default_internal(NULL, status);
   1.614 +}
   1.615 +
   1.616 +
   1.617 +
   1.618 +void U_EXPORT2
   1.619 +Locale::setDefault( const   Locale&     newLocale,
   1.620 +                            UErrorCode&  status)
   1.621 +{
   1.622 +    if (U_FAILURE(status)) {
   1.623 +        return;
   1.624 +    }
   1.625 +
   1.626 +    /* Set the default from the full name string of the supplied locale.
   1.627 +     * This is a convenient way to access the default locale caching mechanisms.
   1.628 +     */
   1.629 +    const char *localeID = newLocale.getName();
   1.630 +    locale_set_default_internal(localeID, status);
   1.631 +}
   1.632 +
   1.633 +Locale U_EXPORT2
   1.634 +Locale::createFromName (const char *name)
   1.635 +{
   1.636 +    if (name) {
   1.637 +        Locale l("");
   1.638 +        l.init(name, FALSE);
   1.639 +        return l;
   1.640 +    }
   1.641 +    else {
   1.642 +        return getDefault();
   1.643 +    }
   1.644 +}
   1.645 +
   1.646 +Locale U_EXPORT2
   1.647 +Locale::createCanonical(const char* name) {
   1.648 +    Locale loc("");
   1.649 +    loc.init(name, TRUE);
   1.650 +    return loc;
   1.651 +}
   1.652 +
   1.653 +const char *
   1.654 +Locale::getISO3Language() const
   1.655 +{
   1.656 +    return uloc_getISO3Language(fullName);
   1.657 +}
   1.658 +
   1.659 +
   1.660 +const char *
   1.661 +Locale::getISO3Country() const
   1.662 +{
   1.663 +    return uloc_getISO3Country(fullName);
   1.664 +}
   1.665 +
   1.666 +/**
   1.667 + * Return the LCID value as specified in the "LocaleID" resource for this
   1.668 + * locale.  The LocaleID must be expressed as a hexadecimal number, from
   1.669 + * one to four digits.  If the LocaleID resource is not present, or is
   1.670 + * in an incorrect format, 0 is returned.  The LocaleID is for use in
   1.671 + * Windows (it is an LCID), but is available on all platforms.
   1.672 + */
   1.673 +uint32_t
   1.674 +Locale::getLCID() const
   1.675 +{
   1.676 +    return uloc_getLCID(fullName);
   1.677 +}
   1.678 +
   1.679 +const char* const* U_EXPORT2 Locale::getISOCountries()
   1.680 +{
   1.681 +    return uloc_getISOCountries();
   1.682 +}
   1.683 +
   1.684 +const char* const* U_EXPORT2 Locale::getISOLanguages()
   1.685 +{
   1.686 +    return uloc_getISOLanguages();
   1.687 +}
   1.688 +
   1.689 +// Set the locale's data based on a posix id.
   1.690 +void Locale::setFromPOSIXID(const char *posixID)
   1.691 +{
   1.692 +    init(posixID, TRUE);
   1.693 +}
   1.694 +
   1.695 +const Locale & U_EXPORT2
   1.696 +Locale::getRoot(void)
   1.697 +{
   1.698 +    return getLocale(eROOT);
   1.699 +}
   1.700 +
   1.701 +const Locale & U_EXPORT2
   1.702 +Locale::getEnglish(void)
   1.703 +{
   1.704 +    return getLocale(eENGLISH);
   1.705 +}
   1.706 +
   1.707 +const Locale & U_EXPORT2
   1.708 +Locale::getFrench(void)
   1.709 +{
   1.710 +    return getLocale(eFRENCH);
   1.711 +}
   1.712 +
   1.713 +const Locale & U_EXPORT2
   1.714 +Locale::getGerman(void)
   1.715 +{
   1.716 +    return getLocale(eGERMAN);
   1.717 +}
   1.718 +
   1.719 +const Locale & U_EXPORT2
   1.720 +Locale::getItalian(void)
   1.721 +{
   1.722 +    return getLocale(eITALIAN);
   1.723 +}
   1.724 +
   1.725 +const Locale & U_EXPORT2
   1.726 +Locale::getJapanese(void)
   1.727 +{
   1.728 +    return getLocale(eJAPANESE);
   1.729 +}
   1.730 +
   1.731 +const Locale & U_EXPORT2
   1.732 +Locale::getKorean(void)
   1.733 +{
   1.734 +    return getLocale(eKOREAN);
   1.735 +}
   1.736 +
   1.737 +const Locale & U_EXPORT2
   1.738 +Locale::getChinese(void)
   1.739 +{
   1.740 +    return getLocale(eCHINESE);
   1.741 +}
   1.742 +
   1.743 +const Locale & U_EXPORT2
   1.744 +Locale::getSimplifiedChinese(void)
   1.745 +{
   1.746 +    return getLocale(eCHINA);
   1.747 +}
   1.748 +
   1.749 +const Locale & U_EXPORT2
   1.750 +Locale::getTraditionalChinese(void)
   1.751 +{
   1.752 +    return getLocale(eTAIWAN);
   1.753 +}
   1.754 +
   1.755 +
   1.756 +const Locale & U_EXPORT2
   1.757 +Locale::getFrance(void)
   1.758 +{
   1.759 +    return getLocale(eFRANCE);
   1.760 +}
   1.761 +
   1.762 +const Locale & U_EXPORT2
   1.763 +Locale::getGermany(void)
   1.764 +{
   1.765 +    return getLocale(eGERMANY);
   1.766 +}
   1.767 +
   1.768 +const Locale & U_EXPORT2
   1.769 +Locale::getItaly(void)
   1.770 +{
   1.771 +    return getLocale(eITALY);
   1.772 +}
   1.773 +
   1.774 +const Locale & U_EXPORT2
   1.775 +Locale::getJapan(void)
   1.776 +{
   1.777 +    return getLocale(eJAPAN);
   1.778 +}
   1.779 +
   1.780 +const Locale & U_EXPORT2
   1.781 +Locale::getKorea(void)
   1.782 +{
   1.783 +    return getLocale(eKOREA);
   1.784 +}
   1.785 +
   1.786 +const Locale & U_EXPORT2
   1.787 +Locale::getChina(void)
   1.788 +{
   1.789 +    return getLocale(eCHINA);
   1.790 +}
   1.791 +
   1.792 +const Locale & U_EXPORT2
   1.793 +Locale::getPRC(void)
   1.794 +{
   1.795 +    return getLocale(eCHINA);
   1.796 +}
   1.797 +
   1.798 +const Locale & U_EXPORT2
   1.799 +Locale::getTaiwan(void)
   1.800 +{
   1.801 +    return getLocale(eTAIWAN);
   1.802 +}
   1.803 +
   1.804 +const Locale & U_EXPORT2
   1.805 +Locale::getUK(void)
   1.806 +{
   1.807 +    return getLocale(eUK);
   1.808 +}
   1.809 +
   1.810 +const Locale & U_EXPORT2
   1.811 +Locale::getUS(void)
   1.812 +{
   1.813 +    return getLocale(eUS);
   1.814 +}
   1.815 +
   1.816 +const Locale & U_EXPORT2
   1.817 +Locale::getCanada(void)
   1.818 +{
   1.819 +    return getLocale(eCANADA);
   1.820 +}
   1.821 +
   1.822 +const Locale & U_EXPORT2
   1.823 +Locale::getCanadaFrench(void)
   1.824 +{
   1.825 +    return getLocale(eCANADA_FRENCH);
   1.826 +}
   1.827 +
   1.828 +const Locale &
   1.829 +Locale::getLocale(int locid)
   1.830 +{
   1.831 +    Locale *localeCache = getLocaleCache();
   1.832 +    U_ASSERT((locid < eMAX_LOCALES)&&(locid>=0));
   1.833 +    if (localeCache == NULL) {
   1.834 +        // Failure allocating the locale cache.
   1.835 +        //   The best we can do is return a NULL reference.
   1.836 +        locid = 0;
   1.837 +    }
   1.838 +    return localeCache[locid]; /*operating on NULL*/
   1.839 +}
   1.840 +
   1.841 +/*
   1.842 +This function is defined this way in order to get around static
   1.843 +initialization and static destruction.
   1.844 + */
   1.845 +Locale *
   1.846 +Locale::getLocaleCache(void)
   1.847 +{
   1.848 +    umtx_lock(NULL);
   1.849 +    UBool needInit = (gLocaleCache == NULL);
   1.850 +    umtx_unlock(NULL);
   1.851 +
   1.852 +    if (needInit) {
   1.853 +        Locale *tLocaleCache = new Locale[(int)eMAX_LOCALES];
   1.854 +        if (tLocaleCache == NULL) {
   1.855 +            return NULL;
   1.856 +        }
   1.857 +	tLocaleCache[eROOT]          = Locale("");
   1.858 +        tLocaleCache[eENGLISH]       = Locale("en");
   1.859 +        tLocaleCache[eFRENCH]        = Locale("fr");
   1.860 +        tLocaleCache[eGERMAN]        = Locale("de");
   1.861 +        tLocaleCache[eITALIAN]       = Locale("it");
   1.862 +        tLocaleCache[eJAPANESE]      = Locale("ja");
   1.863 +        tLocaleCache[eKOREAN]        = Locale("ko");
   1.864 +        tLocaleCache[eCHINESE]       = Locale("zh");
   1.865 +        tLocaleCache[eFRANCE]        = Locale("fr", "FR");
   1.866 +        tLocaleCache[eGERMANY]       = Locale("de", "DE");
   1.867 +        tLocaleCache[eITALY]         = Locale("it", "IT");
   1.868 +        tLocaleCache[eJAPAN]         = Locale("ja", "JP");
   1.869 +        tLocaleCache[eKOREA]         = Locale("ko", "KR");
   1.870 +        tLocaleCache[eCHINA]         = Locale("zh", "CN");
   1.871 +        tLocaleCache[eTAIWAN]        = Locale("zh", "TW");
   1.872 +        tLocaleCache[eUK]            = Locale("en", "GB");
   1.873 +        tLocaleCache[eUS]            = Locale("en", "US");
   1.874 +        tLocaleCache[eCANADA]        = Locale("en", "CA");
   1.875 +        tLocaleCache[eCANADA_FRENCH] = Locale("fr", "CA");
   1.876 +
   1.877 +        umtx_lock(NULL);
   1.878 +        if (gLocaleCache == NULL) {
   1.879 +            gLocaleCache = tLocaleCache;
   1.880 +            tLocaleCache = NULL;
   1.881 +            ucln_common_registerCleanup(UCLN_COMMON_LOCALE, locale_cleanup);
   1.882 +        }
   1.883 +        umtx_unlock(NULL);
   1.884 +        if (tLocaleCache) {
   1.885 +            delete [] tLocaleCache;  // Fancy array delete will destruct each member.
   1.886 +        }
   1.887 +    }
   1.888 +    return gLocaleCache;
   1.889 +}
   1.890 +
   1.891 +class KeywordEnumeration : public StringEnumeration {
   1.892 +private:
   1.893 +    char *keywords;
   1.894 +    char *current;
   1.895 +    int32_t length;
   1.896 +    UnicodeString currUSKey;
   1.897 +    static const char fgClassID;/* Warning this is used beyond the typical RTTI usage. */
   1.898 +
   1.899 +public:
   1.900 +    static UClassID U_EXPORT2 getStaticClassID(void) { return (UClassID)&fgClassID; }
   1.901 +    virtual UClassID getDynamicClassID(void) const { return getStaticClassID(); }
   1.902 +public:
   1.903 +    KeywordEnumeration(const char *keys, int32_t keywordLen, int32_t currentIndex, UErrorCode &status)
   1.904 +        : keywords((char *)&fgClassID), current((char *)&fgClassID), length(0) {
   1.905 +        if(U_SUCCESS(status) && keywordLen != 0) {
   1.906 +            if(keys == NULL || keywordLen < 0) {
   1.907 +                status = U_ILLEGAL_ARGUMENT_ERROR;
   1.908 +            } else {
   1.909 +                keywords = (char *)uprv_malloc(keywordLen+1);
   1.910 +                if (keywords == NULL) {
   1.911 +                    status = U_MEMORY_ALLOCATION_ERROR;
   1.912 +                }
   1.913 +                else {
   1.914 +                    uprv_memcpy(keywords, keys, keywordLen);
   1.915 +                    keywords[keywordLen] = 0;
   1.916 +                    current = keywords + currentIndex;
   1.917 +                    length = keywordLen;
   1.918 +                }
   1.919 +            }
   1.920 +        }
   1.921 +    }
   1.922 +
   1.923 +    virtual ~KeywordEnumeration();
   1.924 +
   1.925 +    virtual StringEnumeration * clone() const
   1.926 +    {
   1.927 +        UErrorCode status = U_ZERO_ERROR;
   1.928 +        return new KeywordEnumeration(keywords, length, (int32_t)(current - keywords), status);
   1.929 +    }
   1.930 +
   1.931 +    virtual int32_t count(UErrorCode &/*status*/) const {
   1.932 +        char *kw = keywords;
   1.933 +        int32_t result = 0;
   1.934 +        while(*kw) {
   1.935 +            result++;
   1.936 +            kw += uprv_strlen(kw)+1;
   1.937 +        }
   1.938 +        return result;
   1.939 +    }
   1.940 +
   1.941 +    virtual const char* next(int32_t* resultLength, UErrorCode& status) {
   1.942 +        const char* result;
   1.943 +        int32_t len;
   1.944 +        if(U_SUCCESS(status) && *current != 0) {
   1.945 +            result = current;
   1.946 +            len = (int32_t)uprv_strlen(current);
   1.947 +            current += len+1;
   1.948 +            if(resultLength != NULL) {
   1.949 +                *resultLength = len;
   1.950 +            }
   1.951 +        } else {
   1.952 +            if(resultLength != NULL) {
   1.953 +                *resultLength = 0;
   1.954 +            }
   1.955 +            result = NULL;
   1.956 +        }
   1.957 +        return result;
   1.958 +    }
   1.959 +
   1.960 +    virtual const UnicodeString* snext(UErrorCode& status) {
   1.961 +        int32_t resultLength = 0;
   1.962 +        const char *s = next(&resultLength, status);
   1.963 +        return setChars(s, resultLength, status);
   1.964 +    }
   1.965 +
   1.966 +    virtual void reset(UErrorCode& /*status*/) {
   1.967 +        current = keywords;
   1.968 +    }
   1.969 +};
   1.970 +
   1.971 +const char KeywordEnumeration::fgClassID = '\0';
   1.972 +
   1.973 +KeywordEnumeration::~KeywordEnumeration() {
   1.974 +    uprv_free(keywords);
   1.975 +}
   1.976 +
   1.977 +StringEnumeration *
   1.978 +Locale::createKeywords(UErrorCode &status) const
   1.979 +{
   1.980 +    char keywords[256];
   1.981 +    int32_t keywordCapacity = 256;
   1.982 +    StringEnumeration *result = NULL;
   1.983 +
   1.984 +    const char* variantStart = uprv_strchr(fullName, '@');
   1.985 +    const char* assignment = uprv_strchr(fullName, '=');
   1.986 +    if(variantStart) {
   1.987 +        if(assignment > variantStart) {
   1.988 +            int32_t keyLen = locale_getKeywords(variantStart+1, '@', keywords, keywordCapacity, NULL, 0, NULL, FALSE, &status);
   1.989 +            if(keyLen) {
   1.990 +                result = new KeywordEnumeration(keywords, keyLen, 0, status);
   1.991 +            }
   1.992 +        } else {
   1.993 +            status = U_INVALID_FORMAT_ERROR;
   1.994 +        }
   1.995 +    }
   1.996 +    return result;
   1.997 +}
   1.998 +
   1.999 +int32_t
  1.1000 +Locale::getKeywordValue(const char* keywordName, char *buffer, int32_t bufLen, UErrorCode &status) const
  1.1001 +{
  1.1002 +    return uloc_getKeywordValue(fullName, keywordName, buffer, bufLen, &status);
  1.1003 +}
  1.1004 +
  1.1005 +void
  1.1006 +Locale::setKeywordValue(const char* keywordName, const char* keywordValue, UErrorCode &status)
  1.1007 +{
  1.1008 +    uloc_setKeywordValue(keywordName, keywordValue, fullName, ULOC_FULLNAME_CAPACITY, &status);
  1.1009 +}
  1.1010 +
  1.1011 +const char *
  1.1012 +Locale::getBaseName() const
  1.1013 +{
  1.1014 +    // lazy init
  1.1015 +    UErrorCode status = U_ZERO_ERROR;
  1.1016 +    // semantically const
  1.1017 +    if(baseName == 0) {
  1.1018 +        ((Locale *)this)->baseName = ((Locale *)this)->baseNameBuffer;
  1.1019 +        int32_t baseNameSize = uloc_getBaseName(fullName, baseName, ULOC_FULLNAME_CAPACITY, &status);
  1.1020 +        if(baseNameSize >= ULOC_FULLNAME_CAPACITY) {
  1.1021 +            ((Locale *)this)->baseName = (char *)uprv_malloc(sizeof(char) * baseNameSize + 1);
  1.1022 +            if (baseName == NULL) {
  1.1023 +                return baseName;
  1.1024 +            }
  1.1025 +            uloc_getBaseName(fullName, baseName, baseNameSize+1, &status);
  1.1026 +        }
  1.1027 +        baseName[baseNameSize] = 0;
  1.1028 +
  1.1029 +        // the computation of variantBegin leaves it equal to the length
  1.1030 +        // of fullName if there is no variant.  It should instead be
  1.1031 +        // the length of the baseName.  Patch around this for now.
  1.1032 +        if (variantBegin == (int32_t)uprv_strlen(fullName)) {
  1.1033 +          ((Locale*)this)->variantBegin = baseNameSize;
  1.1034 +        }
  1.1035 +    }
  1.1036 +    return baseName;
  1.1037 +}
  1.1038 +
  1.1039 +//eof
  1.1040 +U_NAMESPACE_END

mercurial