michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2002-2011, International Business Machines Corporation and michael@0: * others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: */ michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_SERVICE || !UCONFIG_NO_TRANSLITERATION michael@0: michael@0: #include "unicode/resbund.h" michael@0: #include "cmemory.h" michael@0: #include "ustrfmt.h" michael@0: #include "locutil.h" michael@0: #include "charstr.h" michael@0: #include "ucln_cmn.h" michael@0: #include "uassert.h" michael@0: #include "umutex.h" michael@0: michael@0: // see LocaleUtility::getAvailableLocaleNames michael@0: static icu::Hashtable * LocaleUtility_cache = NULL; michael@0: michael@0: #define UNDERSCORE_CHAR ((UChar)0x005f) michael@0: #define AT_SIGN_CHAR ((UChar)64) michael@0: #define PERIOD_CHAR ((UChar)46) michael@0: michael@0: /* michael@0: ****************************************************************** michael@0: */ michael@0: michael@0: /** michael@0: * Release all static memory held by Locale Utility. michael@0: */ michael@0: U_CDECL_BEGIN michael@0: static UBool U_CALLCONV service_cleanup(void) { michael@0: if (LocaleUtility_cache) { michael@0: delete LocaleUtility_cache; michael@0: LocaleUtility_cache = NULL; michael@0: } michael@0: return TRUE; michael@0: } michael@0: U_CDECL_END michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: UnicodeString& michael@0: LocaleUtility::canonicalLocaleString(const UnicodeString* id, UnicodeString& result) michael@0: { michael@0: if (id == NULL) { michael@0: result.setToBogus(); michael@0: } else { michael@0: // Fix case only (no other changes) up to the first '@' or '.' or michael@0: // end of string, whichever comes first. In 3.0 I changed this to michael@0: // stop at first '@' or '.'. It used to run out to the end of michael@0: // string. My fix makes the tests pass but is probably michael@0: // structurally incorrect. See below. [alan 3.0] michael@0: michael@0: // TODO: Doug, you might want to revise this... michael@0: result = *id; michael@0: int32_t i = 0; michael@0: int32_t end = result.indexOf(AT_SIGN_CHAR); michael@0: int32_t n = result.indexOf(PERIOD_CHAR); michael@0: if (n >= 0 && n < end) { michael@0: end = n; michael@0: } michael@0: if (end < 0) { michael@0: end = result.length(); michael@0: } michael@0: n = result.indexOf(UNDERSCORE_CHAR); michael@0: if (n < 0) { michael@0: n = end; michael@0: } michael@0: for (; i < n; ++i) { michael@0: UChar c = result.charAt(i); michael@0: if (c >= 0x0041 && c <= 0x005a) { michael@0: c += 0x20; michael@0: result.setCharAt(i, c); michael@0: } michael@0: } michael@0: for (n = end; i < n; ++i) { michael@0: UChar c = result.charAt(i); michael@0: if (c >= 0x0061 && c <= 0x007a) { michael@0: c -= 0x20; michael@0: result.setCharAt(i, c); michael@0: } michael@0: } michael@0: } michael@0: return result; michael@0: michael@0: #if 0 michael@0: // This code does a proper full level 2 canonicalization of id. michael@0: // It's nasty to go from UChar to char to char to UChar -- but michael@0: // that's what you have to do to use the uloc_canonicalize michael@0: // function on UnicodeStrings. michael@0: michael@0: // I ended up doing the alternate fix (see above) not for michael@0: // performance reasons, although performance will certainly be michael@0: // better, but because doing a full level 2 canonicalization michael@0: // causes some tests to fail. [alan 3.0] michael@0: michael@0: // TODO: Doug, you might want to revisit this... michael@0: result.setToBogus(); michael@0: if (id != 0) { michael@0: int32_t buflen = id->length() + 8; // space for NUL michael@0: char* buf = (char*) uprv_malloc(buflen); michael@0: char* canon = (buf == 0) ? 0 : (char*) uprv_malloc(buflen); michael@0: if (buf != 0 && canon != 0) { michael@0: U_ASSERT(id->extract(0, INT32_MAX, buf, buflen) < buflen); michael@0: UErrorCode ec = U_ZERO_ERROR; michael@0: uloc_canonicalize(buf, canon, buflen, &ec); michael@0: if (U_SUCCESS(ec)) { michael@0: result = UnicodeString(canon); michael@0: } michael@0: } michael@0: uprv_free(buf); michael@0: uprv_free(canon); michael@0: } michael@0: return result; michael@0: #endif michael@0: } michael@0: michael@0: Locale& michael@0: LocaleUtility::initLocaleFromName(const UnicodeString& id, Locale& result) michael@0: { michael@0: enum { BUFLEN = 128 }; // larger than ever needed michael@0: michael@0: if (id.isBogus() || id.length() >= BUFLEN) { michael@0: result.setToBogus(); michael@0: } else { michael@0: /* michael@0: * We need to convert from a UnicodeString to char * in order to michael@0: * create a Locale. michael@0: * michael@0: * Problem: Locale ID strings may contain '@' which is a variant michael@0: * character and cannot be handled by invariant-character conversion. michael@0: * michael@0: * Hack: Since ICU code can handle locale IDs with multiple encodings michael@0: * of '@' (at least for EBCDIC; it's not known to be a problem for michael@0: * ASCII-based systems), michael@0: * we use regular invariant-character conversion for everything else michael@0: * and manually convert U+0040 into a compiler-char-constant '@'. michael@0: * While this compilation-time constant may not match the runtime michael@0: * encoding of '@', it should be one of the encodings which ICU michael@0: * recognizes. michael@0: * michael@0: * There should be only at most one '@' in a locale ID. michael@0: */ michael@0: char buffer[BUFLEN]; michael@0: int32_t prev, i; michael@0: prev = 0; michael@0: for(;;) { michael@0: i = id.indexOf((UChar)0x40, prev); michael@0: if(i < 0) { michael@0: // no @ between prev and the rest of the string michael@0: id.extract(prev, INT32_MAX, buffer + prev, BUFLEN - prev, US_INV); michael@0: break; // done michael@0: } else { michael@0: // normal invariant-character conversion for text between @s michael@0: id.extract(prev, i - prev, buffer + prev, BUFLEN - prev, US_INV); michael@0: // manually "convert" U+0040 at id[i] into '@' at buffer[i] michael@0: buffer[i] = '@'; michael@0: prev = i + 1; michael@0: } michael@0: } michael@0: result = Locale::createFromName(buffer); michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: UnicodeString& michael@0: LocaleUtility::initNameFromLocale(const Locale& locale, UnicodeString& result) michael@0: { michael@0: if (locale.isBogus()) { michael@0: result.setToBogus(); michael@0: } else { michael@0: result.append(UnicodeString(locale.getName(), -1, US_INV)); michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: const Hashtable* michael@0: LocaleUtility::getAvailableLocaleNames(const UnicodeString& bundleID) michael@0: { michael@0: // LocaleUtility_cache is a hash-of-hashes. The top-level keys michael@0: // are path strings ('bundleID') passed to michael@0: // ures_openAvailableLocales. The top-level values are michael@0: // second-level hashes. The second-level keys are result strings michael@0: // from ures_openAvailableLocales. The second-level values are michael@0: // garbage ((void*)1 or other random pointer). michael@0: michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: Hashtable* cache; michael@0: umtx_lock(NULL); michael@0: cache = LocaleUtility_cache; michael@0: umtx_unlock(NULL); michael@0: michael@0: if (cache == NULL) { michael@0: cache = new Hashtable(status); michael@0: if (cache == NULL || U_FAILURE(status)) { michael@0: return NULL; // catastrophic failure; e.g. out of memory michael@0: } michael@0: cache->setValueDeleter(uhash_deleteHashtable); michael@0: Hashtable* h; // set this to final LocaleUtility_cache value michael@0: umtx_lock(NULL); michael@0: h = LocaleUtility_cache; michael@0: if (h == NULL) { michael@0: LocaleUtility_cache = h = cache; michael@0: cache = NULL; michael@0: ucln_common_registerCleanup(UCLN_COMMON_SERVICE, service_cleanup); michael@0: } michael@0: umtx_unlock(NULL); michael@0: if(cache != NULL) { michael@0: delete cache; michael@0: } michael@0: cache = h; michael@0: } michael@0: michael@0: U_ASSERT(cache != NULL); michael@0: michael@0: Hashtable* htp; michael@0: umtx_lock(NULL); michael@0: htp = (Hashtable*) cache->get(bundleID); michael@0: umtx_unlock(NULL); michael@0: michael@0: if (htp == NULL) { michael@0: htp = new Hashtable(status); michael@0: if (htp && U_SUCCESS(status)) { michael@0: CharString cbundleID; michael@0: cbundleID.appendInvariantChars(bundleID, status); michael@0: const char* path = cbundleID.isEmpty() ? NULL : cbundleID.data(); michael@0: UEnumeration *uenum = ures_openAvailableLocales(path, &status); michael@0: for (;;) { michael@0: const UChar* id = uenum_unext(uenum, NULL, &status); michael@0: if (id == NULL) { michael@0: break; michael@0: } michael@0: htp->put(UnicodeString(id), (void*)htp, status); michael@0: } michael@0: uenum_close(uenum); michael@0: if (U_FAILURE(status)) { michael@0: delete htp; michael@0: return NULL; michael@0: } michael@0: umtx_lock(NULL); michael@0: cache->put(bundleID, (void*)htp, status); michael@0: umtx_unlock(NULL); michael@0: } michael@0: } michael@0: return htp; michael@0: } michael@0: michael@0: UBool michael@0: LocaleUtility::isFallbackOf(const UnicodeString& root, const UnicodeString& child) michael@0: { michael@0: return child.indexOf(root) == 0 && michael@0: (child.length() == root.length() || michael@0: child.charAt(root.length()) == UNDERSCORE_CHAR); michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: /* !UCONFIG_NO_SERVICE */ michael@0: #endif michael@0: michael@0: