/*
**********************************************************************
- * Copyright (C) 1997-2004, International Business Machines
+ * Copyright (C) 1997-2010, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
*
#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
-static Locale* availableLocaleList = NULL;
-static int32_t availableLocaleListCount;
typedef enum ELocalePos {
eENGLISH,
eFRENCH,
eUS,
eCANADA,
eCANADA_FRENCH,
+ eROOT,
//eDEFAULT,
UBool valuesToo,
UErrorCode *status);
-static Locale *gLocaleCache = NULL;
-static const Locale *gDefaultLocale = NULL;
-static UHashtable *gDefaultLocalesHashT = NULL;
+static U_NAMESPACE_QUALIFIER Locale *gLocaleCache = NULL;
+static U_NAMESPACE_QUALIFIER Locale *gDefaultLocale = NULL;
+static UHashtable *gDefaultLocalesHashT = NULL;
U_CDECL_BEGIN
//
//
static void U_CALLCONV
deleteLocale(void *obj) {
- delete (Locale *) obj;
+ delete (U_NAMESPACE_QUALIFIER Locale *) obj;
}
static UBool U_CALLCONV locale_cleanup(void)
{
U_NAMESPACE_USE
- if (availableLocaleList) {
- delete []availableLocaleList;
- availableLocaleList = NULL;
- }
- availableLocaleListCount = 0;
-
if (gLocaleCache) {
delete [] gLocaleCache;
gLocaleCache = NULL;
uhash_close(gDefaultLocalesHashT); // Automatically deletes all elements, using deleter func.
gDefaultLocalesHashT = NULL;
}
+ else if (gDefaultLocale) {
+ // The cache wasn't created, and only one default locale was created.
+ delete gDefaultLocale;
+ }
gDefaultLocale = NULL;
return TRUE;
U_CDECL_END
U_NAMESPACE_BEGIN
-UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Locale)
-
//
// locale_set_default_internal.
//
void locale_set_default_internal(const char *id)
{
- U_NAMESPACE_USE
UErrorCode status = U_ZERO_ERROR;
UBool canonicalize = FALSE;
// (long names are truncated.)
// Lazy creation of the hash table itself, if needed.
- //
+ UBool isOnlyLocale;
+ UMTX_CHECK(NULL, (gDefaultLocale == NULL), isOnlyLocale);
+ if (isOnlyLocale) {
+ // We haven't seen this locale id before.
+ // Create a new Locale object for it.
+ Locale *newFirstDefault = new Locale(Locale::eBOGUS);
+ if (newFirstDefault == NULL) {
+ // No way to report errors from here.
+ return;
+ }
+ newFirstDefault->init(localeNameBuf, FALSE);
+ umtx_lock(NULL);
+ if (gDefaultLocale == NULL) {
+ gDefaultLocale = newFirstDefault; // Assignment to gDefaultLocale must happen inside mutex
+ newFirstDefault = NULL;
+ ucln_common_registerCleanup(UCLN_COMMON_LOCALE, locale_cleanup);
+
+ // We were successful in setting the locale, and we were the first one to set it.
+ umtx_unlock(NULL);
+ return;
+ }
+ // Else some other thread raced us through here, and set the new Locale.
+ else {
+ delete newFirstDefault;
+ }
+ umtx_unlock(NULL);
+ }
+
+ // Create the hash table next, unless the racing thread is setting the same locale as default.
+ // The new locale might have been set while a racing thread was stuck at the lock
+ // earlier in this function. (For example, see uprv_getDefaultLocaleID above).
umtx_lock(NULL);
- UBool hashTableNeedsInit = (gDefaultLocalesHashT == NULL);
+ // Only create the hash table if we need to.
+ if (uprv_strcmp(gDefaultLocale->getName(), localeNameBuf) == 0) {
+ umtx_unlock(NULL);
+ return;
+ }
umtx_unlock(NULL);
+ // start using the hash table.
+
+
+ // Lazy creation of the hash table itself, if needed.
+ UBool hashTableNeedsInit;
+ UMTX_CHECK(NULL, (gDefaultLocalesHashT == NULL), hashTableNeedsInit);
if (hashTableNeedsInit) {
status = U_ZERO_ERROR;
- UHashtable *tHashTable = uhash_open(uhash_hashChars, uhash_compareChars, &status);
+ UHashtable *tHashTable = uhash_open(uhash_hashChars, uhash_compareChars, NULL, &status);
if (U_FAILURE(status)) {
return;
}
if (gDefaultLocalesHashT == NULL) {
gDefaultLocalesHashT = tHashTable;
ucln_common_registerCleanup(UCLN_COMMON_LOCALE, locale_cleanup);
- umtx_unlock(NULL);
+
+ uhash_put(gDefaultLocalesHashT, (void *)gDefaultLocale->getName(), gDefaultLocale, &status);
} else {
- umtx_unlock(NULL);
uhash_close(tHashTable);
}
+ umtx_unlock(NULL);
}
// Hash table lookup, key is the locale full name
const char *key = newDefault->getName();
U_ASSERT(uprv_strcmp(key, localeNameBuf) == 0);
umtx_lock(NULL);
- const Locale *hashTableVal = (const Locale *)uhash_get(gDefaultLocalesHashT, key);
+ Locale *hashTableVal = (Locale *)uhash_get(gDefaultLocalesHashT, key);
if (hashTableVal == NULL) {
uhash_put(gDefaultLocalesHashT, (void *)key, newDefault, &status);
gDefaultLocale = newDefault;
- umtx_unlock(NULL);
// ignore errors from hash table insert. (Couldn't do anything anyway)
// We can still set the default Locale,
// it just wont be cached, and will eventually leak.
// Some other thread raced us through here, and got the new Locale
// into the hash table before us. Use that one.
gDefaultLocale = hashTableVal; // Assignment to gDefaultLocale must happen inside mutex
- umtx_unlock(NULL);
delete newDefault;
}
+ umtx_unlock(NULL);
}
}
U_NAMESPACE_END
-
/* sfb 07/21/99 */
U_CFUNC void
locale_set_default(const char *id)
U_NAMESPACE_BEGIN
+UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Locale)
+
/*Character separating the posix id fields*/
// '_'
// In the platform codepage.
}
else
{
- char togo_stack[ULOC_FULLNAME_CAPACITY];
- char *togo;
- char *togo_heap = NULL;
+ MaybeStackArray<char, ULOC_FULLNAME_CAPACITY> togo;
int32_t size = 0;
int32_t lsize = 0;
int32_t csize = 0;
/*if the whole string is longer than our internal limit, we need
to go to the heap for temporary buffers*/
- if (size > ULOC_FULLNAME_CAPACITY)
- {
- togo_heap = (char *)uprv_malloc(sizeof(char)*(size+1));
- togo = togo_heap;
- }
- else
+ if (size >= togo.getCapacity())
{
- togo = togo_stack;
+ // If togo_heap could not be created, initialize with default settings.
+ if (togo.resize(size+1) == NULL) {
+ init(NULL, FALSE);
+ }
}
togo[0] = 0;
// Now, copy it back.
- p = togo;
+ p = togo.getAlias();
if ( lsize != 0 )
{
uprv_strcpy(p, newLanguage);
// Parse it, because for example 'language' might really be a complete
// string.
- init(togo, FALSE);
-
- if (togo_heap) {
- uprv_free(togo_heap);
- }
+ init(togo.getAlias(), FALSE);
}
}
/* Allocate the full name if necessary */
if(other.fullName != other.fullNameBuffer) {
fullName = (char *)uprv_malloc(sizeof(char)*(uprv_strlen(other.fullName)+1));
+ if (fullName == NULL) {
+ return *this;
+ }
}
/* Copy the full name */
uprv_strcpy(fullName, other.fullName);
uprv_strcpy(script, other.script);
uprv_strcpy(country, other.country);
- /* The variantBegin is an offset into fullName, just copy it */
+ /* The variantBegin is an offset, just copy it */
variantBegin = other.variantBegin;
fIsBogus = other.fIsBogus;
return *this;
fieldIdx = 1;
while ((separator = uprv_strchr(field[fieldIdx-1], SEP_CHAR)) && fieldIdx < (int32_t)(sizeof(field)/sizeof(field[0]))-1) {
field[fieldIdx] = separator + 1;
- fieldLen[fieldIdx-1] = separator - field[fieldIdx-1];
+ fieldLen[fieldIdx-1] = (int32_t)(separator - field[fieldIdx-1]);
fieldIdx++;
}
// variant may contain @foo or .foo POSIX cruft; remove it
if (separator==NULL || (sep2!=NULL && separator > sep2)) {
separator = sep2;
}
- fieldLen[fieldIdx-1] = separator - field[fieldIdx-1];
+ fieldLen[fieldIdx-1] = (int32_t)(separator - field[fieldIdx-1]);
} else {
fieldLen[fieldIdx-1] = length - (int32_t)(field[fieldIdx-1] - fullName);
}
// successful end of init()
return *this;
- } while(0);
+ } while(0); /*loop doesn't iterate*/
// when an error occurs, then set this object to "bogus" (there is no UErrorCode here)
setToBogus();
uprv_free(fullName);
fullName = fullNameBuffer;
}
+ if(baseName && baseName != baseNameBuffer) {
+ uprv_free(baseName);
+ baseName = NULL;
+ }
*fullNameBuffer = 0;
*language = 0;
*script = 0;
Locale::getDefault()
{
const Locale *retLocale;
- umtx_lock(NULL);
- retLocale = gDefaultLocale;
- umtx_unlock(NULL);
+ UMTX_CHECK(NULL, gDefaultLocale, retLocale);
if (retLocale == NULL) {
locale_set_default_internal(NULL);
umtx_lock(NULL);
return uloc_getLCID(fullName);
}
-UnicodeString&
-Locale::getDisplayLanguage(UnicodeString& dispLang) const
-{
- return this->getDisplayLanguage(getDefault(), dispLang);
-}
-
-/*We cannot make any assumptions on the size of the output display strings
-* Yet, since we are calling through to a C API, we need to set limits on
-* buffer size. For all the following getDisplay functions we first attempt
-* to fill up a stack allocated buffer. If it is to small we heap allocated
-* the exact buffer we need copy it to the UnicodeString and delete it*/
-
-UnicodeString&
-Locale::getDisplayLanguage(const Locale &displayLocale,
- UnicodeString &result) const {
- UChar *buffer;
- UErrorCode errorCode=U_ZERO_ERROR;
- int32_t length;
-
- buffer=result.getBuffer(ULOC_FULLNAME_CAPACITY);
- if(buffer==0) {
- result.truncate(0);
- return result;
- }
-
- length=uloc_getDisplayLanguage(fullName, displayLocale.fullName,
- buffer, result.getCapacity(),
- &errorCode);
- result.releaseBuffer(length);
-
- if(errorCode==U_BUFFER_OVERFLOW_ERROR) {
- buffer=result.getBuffer(length);
- if(buffer==0) {
- result.truncate(0);
- return result;
- }
- errorCode=U_ZERO_ERROR;
- length=uloc_getDisplayLanguage(fullName, displayLocale.fullName,
- buffer, result.getCapacity(),
- &errorCode);
- result.releaseBuffer(length);
- }
-
- if(U_FAILURE(errorCode)) {
- result.truncate(0);
- }
-
- return result;
-}
-
-UnicodeString&
-Locale::getDisplayScript(UnicodeString& dispScript) const
-{
- return this->getDisplayScript(getDefault(), dispScript);
-}
-
-UnicodeString&
-Locale::getDisplayScript(const Locale &displayLocale,
- UnicodeString &result) const {
- UChar *buffer;
- UErrorCode errorCode=U_ZERO_ERROR;
- int32_t length;
-
- buffer=result.getBuffer(ULOC_FULLNAME_CAPACITY);
- if(buffer==0) {
- result.truncate(0);
- return result;
- }
-
- length=uloc_getDisplayScript(fullName, displayLocale.fullName,
- buffer, result.getCapacity(),
- &errorCode);
- result.releaseBuffer(length);
-
- if(errorCode==U_BUFFER_OVERFLOW_ERROR) {
- buffer=result.getBuffer(length);
- if(buffer==0) {
- result.truncate(0);
- return result;
- }
- errorCode=U_ZERO_ERROR;
- length=uloc_getDisplayScript(fullName, displayLocale.fullName,
- buffer, result.getCapacity(),
- &errorCode);
- result.releaseBuffer(length);
- }
-
- if(U_FAILURE(errorCode)) {
- result.truncate(0);
- }
-
- return result;
-}
-
-UnicodeString&
-Locale::getDisplayCountry(UnicodeString& dispCntry) const
-{
- return this->getDisplayCountry(getDefault(), dispCntry);
-}
-
-UnicodeString&
-Locale::getDisplayCountry(const Locale &displayLocale,
- UnicodeString &result) const {
- UChar *buffer;
- UErrorCode errorCode=U_ZERO_ERROR;
- int32_t length;
-
- buffer=result.getBuffer(ULOC_FULLNAME_CAPACITY);
- if(buffer==0) {
- result.truncate(0);
- return result;
- }
-
- length=uloc_getDisplayCountry(fullName, displayLocale.fullName,
- buffer, result.getCapacity(),
- &errorCode);
- result.releaseBuffer(length);
-
- if(errorCode==U_BUFFER_OVERFLOW_ERROR) {
- buffer=result.getBuffer(length);
- if(buffer==0) {
- result.truncate(0);
- return result;
- }
- errorCode=U_ZERO_ERROR;
- length=uloc_getDisplayCountry(fullName, displayLocale.fullName,
- buffer, result.getCapacity(),
- &errorCode);
- result.releaseBuffer(length);
- }
-
- if(U_FAILURE(errorCode)) {
- result.truncate(0);
- }
-
- return result;
-}
-
-UnicodeString&
-Locale::getDisplayVariant(UnicodeString& dispVar) const
-{
- return this->getDisplayVariant(getDefault(), dispVar);
-}
-
-UnicodeString&
-Locale::getDisplayVariant(const Locale &displayLocale,
- UnicodeString &result) const {
- UChar *buffer;
- UErrorCode errorCode=U_ZERO_ERROR;
- int32_t length;
-
- buffer=result.getBuffer(ULOC_FULLNAME_CAPACITY);
- if(buffer==0) {
- result.truncate(0);
- return result;
- }
-
- length=uloc_getDisplayVariant(fullName, displayLocale.fullName,
- buffer, result.getCapacity(),
- &errorCode);
- result.releaseBuffer(length);
-
- if(errorCode==U_BUFFER_OVERFLOW_ERROR) {
- buffer=result.getBuffer(length);
- if(buffer==0) {
- result.truncate(0);
- return result;
- }
- errorCode=U_ZERO_ERROR;
- length=uloc_getDisplayVariant(fullName, displayLocale.fullName,
- buffer, result.getCapacity(),
- &errorCode);
- result.releaseBuffer(length);
- }
-
- if(U_FAILURE(errorCode)) {
- result.truncate(0);
- }
-
- return result;
-}
-
-UnicodeString&
-Locale::getDisplayName( UnicodeString& name ) const
-{
- return this->getDisplayName(getDefault(), name);
-}
-
-UnicodeString&
-Locale::getDisplayName(const Locale &displayLocale,
- UnicodeString &result) const {
- UChar *buffer;
- UErrorCode errorCode=U_ZERO_ERROR;
- int32_t length;
-
- buffer=result.getBuffer(ULOC_FULLNAME_CAPACITY);
- if(buffer==0) {
- result.truncate(0);
- return result;
- }
-
- length=uloc_getDisplayName(fullName, displayLocale.fullName,
- buffer, result.getCapacity(),
- &errorCode);
- result.releaseBuffer(length);
-
- if(errorCode==U_BUFFER_OVERFLOW_ERROR) {
- buffer=result.getBuffer(length);
- if(buffer==0) {
- result.truncate(0);
- return result;
- }
- errorCode=U_ZERO_ERROR;
- length=uloc_getDisplayName(fullName, displayLocale.fullName,
- buffer, result.getCapacity(),
- &errorCode);
- result.releaseBuffer(length);
- }
-
- if(U_FAILURE(errorCode)) {
- result.truncate(0);
- }
-
- return result;
-}
-const Locale* U_EXPORT2
-Locale::getAvailableLocales(int32_t& count)
-{
- // for now, there is a hardcoded list, so just walk through that list and set it up.
- umtx_lock(NULL);
- UBool needInit = availableLocaleList == 0;
- umtx_unlock(NULL);
-
- if (needInit) {
- int32_t locCount = uloc_countAvailable();
- Locale *newLocaleList = 0;
- if(locCount) {
- newLocaleList = new Locale[locCount];
- }
- if (newLocaleList == NULL) {
- return NULL;
- }
-
- count = locCount;
-
- while(--locCount >= 0) {
- newLocaleList[locCount].setFromPOSIXID(uloc_getAvailable(locCount));
- }
-
- umtx_lock(NULL);
- if(availableLocaleList == 0) {
- availableLocaleListCount = count;
- availableLocaleList = newLocaleList;
- newLocaleList = NULL;
- ucln_common_registerCleanup(UCLN_COMMON_LOCALE, locale_cleanup);
- }
- umtx_unlock(NULL);
- delete []newLocaleList;
- }
- count = availableLocaleListCount;
- return availableLocaleList;
-}
-
const char* const* U_EXPORT2 Locale::getISOCountries()
{
return uloc_getISOCountries();
init(posixID, TRUE);
}
+const Locale & U_EXPORT2
+Locale::getRoot(void)
+{
+ return getLocale(eROOT);
+}
+
const Locale & U_EXPORT2
Locale::getEnglish(void)
{
Locale::getLocale(int locid)
{
Locale *localeCache = getLocaleCache();
- U_ASSERT(locid < eMAX_LOCALES);
+ U_ASSERT((locid < eMAX_LOCALES)&&(locid>=0));
if (localeCache == NULL) {
// Failure allocating the locale cache.
// The best we can do is return a NULL reference.
locid = 0;
}
- return localeCache[locid];
+ return localeCache[locid]; /*operating on NULL*/
}
/*
if (tLocaleCache == NULL) {
return NULL;
}
+ tLocaleCache[eROOT] = Locale("");
tLocaleCache[eENGLISH] = Locale("en");
tLocaleCache[eFRENCH] = Locale("fr");
tLocaleCache[eGERMAN] = Locale("de");
int32_t len;
if(U_SUCCESS(status) && *current != 0) {
result = current;
- len = uprv_strlen(current);
+ len = (int32_t)uprv_strlen(current);
current += len+1;
if(resultLength != NULL) {
*resultLength = len;
return uloc_getKeywordValue(fullName, keywordName, buffer, bufLen, &status);
}
+void
+Locale::setKeywordValue(const char* keywordName, const char* keywordValue, UErrorCode &status)
+{
+ uloc_setKeywordValue(keywordName, keywordValue, fullName, ULOC_FULLNAME_CAPACITY, &status);
+}
+
const char *
Locale::getBaseName() const
{
int32_t baseNameSize = uloc_getBaseName(fullName, baseName, ULOC_FULLNAME_CAPACITY, &status);
if(baseNameSize >= ULOC_FULLNAME_CAPACITY) {
((Locale *)this)->baseName = (char *)uprv_malloc(sizeof(char) * baseNameSize + 1);
+ if (baseName == NULL) {
+ return baseName;
+ }
uloc_getBaseName(fullName, baseName, baseNameSize+1, &status);
}
baseName[baseNameSize] = 0;
+
+ // the computation of variantBegin leaves it equal to the length
+ // of fullName if there is no variant. It should instead be
+ // the length of the baseName. Patch around this for now.
+ if (variantBegin == (int32_t)uprv_strlen(fullName)) {
+ ((Locale*)this)->variantBegin = baseNameSize;
+ }
}
return baseName;
}
-
//eof
U_NAMESPACE_END