michael@0: /* michael@0: ********************************************************************** michael@0: * Copyright (c) 2002-2013, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ********************************************************************** michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/ucurr.h" michael@0: #include "unicode/locid.h" michael@0: #include "unicode/ures.h" michael@0: #include "unicode/ustring.h" michael@0: #include "unicode/choicfmt.h" michael@0: #include "unicode/parsepos.h" michael@0: #include "ustr_imp.h" michael@0: #include "cmemory.h" michael@0: #include "cstring.h" michael@0: #include "uassert.h" michael@0: #include "umutex.h" michael@0: #include "ucln_in.h" michael@0: #include "uenumimp.h" michael@0: #include "uhash.h" michael@0: #include "hash.h" michael@0: #include "uresimp.h" michael@0: #include "ulist.h" michael@0: #include "ureslocs.h" michael@0: michael@0: //#define UCURR_DEBUG_EQUIV 1 michael@0: #ifdef UCURR_DEBUG_EQUIV michael@0: #include "stdio.h" michael@0: #endif michael@0: //#define UCURR_DEBUG 1 michael@0: #ifdef UCURR_DEBUG michael@0: #include "stdio.h" michael@0: #endif michael@0: michael@0: typedef struct IsoCodeEntry { michael@0: const UChar *isoCode; /* const because it's a reference to a resource bundle string. */ michael@0: UDate from; michael@0: UDate to; michael@0: } IsoCodeEntry; michael@0: michael@0: //------------------------------------------------------------ michael@0: // Constants michael@0: michael@0: // Default currency meta data of last resort. We try to use the michael@0: // defaults encoded in the meta data resource bundle. If there is a michael@0: // configuration/build error and these are not available, we use these michael@0: // hard-coded defaults (which should be identical). michael@0: static const int32_t LAST_RESORT_DATA[] = { 2, 0, 2, 0 }; michael@0: michael@0: // POW10[i] = 10^i, i=0..MAX_POW10 michael@0: static const int32_t POW10[] = { 1, 10, 100, 1000, 10000, 100000, michael@0: 1000000, 10000000, 100000000, 1000000000 }; michael@0: michael@0: static const int32_t MAX_POW10 = (sizeof(POW10)/sizeof(POW10[0])) - 1; michael@0: michael@0: // Defines equivalent currency symbols. michael@0: static const char *EQUIV_CURRENCY_SYMBOLS[][2] = { michael@0: {"\\u00a5", "\\uffe5"}, michael@0: {"$", "\\ufe69"}, michael@0: {"$", "\\uff04"}, michael@0: {"\\u20a8", "\\u20b9"}, michael@0: {"\\u00a3", "\\u20a4"}}; michael@0: michael@0: #define ISO_CURRENCY_CODE_LENGTH 3 michael@0: michael@0: //------------------------------------------------------------ michael@0: // Resource tags michael@0: // michael@0: michael@0: static const char CURRENCY_DATA[] = "supplementalData"; michael@0: // Tag for meta-data, in root. michael@0: static const char CURRENCY_META[] = "CurrencyMeta"; michael@0: michael@0: // Tag for map from countries to currencies, in root. michael@0: static const char CURRENCY_MAP[] = "CurrencyMap"; michael@0: michael@0: // Tag for default meta-data, in CURRENCY_META michael@0: static const char DEFAULT_META[] = "DEFAULT"; michael@0: michael@0: // Variant for legacy pre-euro mapping in CurrencyMap michael@0: static const char VAR_PRE_EURO[] = "PREEURO"; michael@0: michael@0: // Variant for legacy euro mapping in CurrencyMap michael@0: static const char VAR_EURO[] = "EURO"; michael@0: michael@0: // Variant delimiter michael@0: static const char VAR_DELIM = '_'; michael@0: static const char VAR_DELIM_STR[] = "_"; michael@0: michael@0: // Variant for legacy euro mapping in CurrencyMap michael@0: //static const char VAR_DELIM_EURO[] = "_EURO"; michael@0: michael@0: #define VARIANT_IS_EMPTY 0 michael@0: #define VARIANT_IS_EURO 0x1 michael@0: #define VARIANT_IS_PREEURO 0x2 michael@0: michael@0: // Tag for localized display names (symbols) of currencies michael@0: static const char CURRENCIES[] = "Currencies"; michael@0: static const char CURRENCYPLURALS[] = "CurrencyPlurals"; michael@0: michael@0: // Marker character indicating that a display name is a ChoiceFormat michael@0: // pattern. Strings that start with one mark are ChoiceFormat michael@0: // patterns. Strings that start with 2 marks are static strings, and michael@0: // the first mark is deleted. michael@0: static const UChar CHOICE_FORMAT_MARK = 0x003D; // Equals sign michael@0: michael@0: static const UChar EUR_STR[] = {0x0045,0x0055,0x0052,0}; michael@0: michael@0: // ISO codes mapping table michael@0: static const UHashtable* gIsoCodes = NULL; michael@0: static icu::UInitOnce gIsoCodesInitOnce = U_INITONCE_INITIALIZER; michael@0: michael@0: // Currency symbol equivalances michael@0: static const icu::Hashtable* gCurrSymbolsEquiv = NULL; michael@0: static icu::UInitOnce gCurrSymbolsEquivInitOnce = U_INITONCE_INITIALIZER; michael@0: michael@0: // EquivIterator iterates over all strings that are equivalent to a given michael@0: // string, s. Note that EquivIterator will never yield s itself. michael@0: class EquivIterator : icu::UMemory { michael@0: public: michael@0: // Constructor. hash stores the equivalence relationships; s is the string michael@0: // for which we find equivalent strings. michael@0: inline EquivIterator(const icu::Hashtable& hash, const icu::UnicodeString& s) michael@0: : _hash(hash) { michael@0: _start = _current = &s; michael@0: } michael@0: inline ~EquivIterator() { } michael@0: michael@0: // next returns the next equivalent string or NULL if there are no more. michael@0: // If s has no equivalent strings, next returns NULL on the first call. michael@0: const icu::UnicodeString *next(); michael@0: private: michael@0: const icu::Hashtable& _hash; michael@0: const icu::UnicodeString* _start; michael@0: const icu::UnicodeString* _current; michael@0: }; michael@0: michael@0: const icu::UnicodeString * michael@0: EquivIterator::next() { michael@0: const icu::UnicodeString* _next = (const icu::UnicodeString*) _hash.get(*_current); michael@0: if (_next == NULL) { michael@0: U_ASSERT(_current == _start); michael@0: return NULL; michael@0: } michael@0: if (*_next == *_start) { michael@0: return NULL; michael@0: } michael@0: _current = _next; michael@0: return _next; michael@0: } michael@0: michael@0: // makeEquivalent makes lhs and rhs equivalent by updating the equivalence michael@0: // relations in hash accordingly. michael@0: static void makeEquivalent( michael@0: const icu::UnicodeString &lhs, michael@0: const icu::UnicodeString &rhs, michael@0: icu::Hashtable* hash, UErrorCode &status) { michael@0: if (U_FAILURE(status)) { michael@0: return; michael@0: } michael@0: if (lhs == rhs) { michael@0: // already equivalent michael@0: return; michael@0: } michael@0: EquivIterator leftIter(*hash, lhs); michael@0: EquivIterator rightIter(*hash, rhs); michael@0: const icu::UnicodeString *firstLeft = leftIter.next(); michael@0: const icu::UnicodeString *firstRight = rightIter.next(); michael@0: const icu::UnicodeString *nextLeft = firstLeft; michael@0: const icu::UnicodeString *nextRight = firstRight; michael@0: while (nextLeft != NULL && nextRight != NULL) { michael@0: if (*nextLeft == rhs || *nextRight == lhs) { michael@0: // Already equivalent michael@0: return; michael@0: } michael@0: nextLeft = leftIter.next(); michael@0: nextRight = rightIter.next(); michael@0: } michael@0: // Not equivalent. Must join. michael@0: icu::UnicodeString *newFirstLeft; michael@0: icu::UnicodeString *newFirstRight; michael@0: if (firstRight == NULL && firstLeft == NULL) { michael@0: // Neither lhs or rhs belong to an equivalence circle, so we form michael@0: // a new equivalnce circle of just lhs and rhs. michael@0: newFirstLeft = new icu::UnicodeString(rhs); michael@0: newFirstRight = new icu::UnicodeString(lhs); michael@0: } else if (firstRight == NULL) { michael@0: // lhs belongs to an equivalence circle, but rhs does not, so we link michael@0: // rhs into lhs' circle. michael@0: newFirstLeft = new icu::UnicodeString(rhs); michael@0: newFirstRight = new icu::UnicodeString(*firstLeft); michael@0: } else if (firstLeft == NULL) { michael@0: // rhs belongs to an equivlance circle, but lhs does not, so we link michael@0: // lhs into rhs' circle. michael@0: newFirstLeft = new icu::UnicodeString(*firstRight); michael@0: newFirstRight = new icu::UnicodeString(lhs); michael@0: } else { michael@0: // Both lhs and rhs belong to different equivalnce circles. We link michael@0: // them together to form one single, larger equivalnce circle. michael@0: newFirstLeft = new icu::UnicodeString(*firstRight); michael@0: newFirstRight = new icu::UnicodeString(*firstLeft); michael@0: } michael@0: if (newFirstLeft == NULL || newFirstRight == NULL) { michael@0: delete newFirstLeft; michael@0: delete newFirstRight; michael@0: status = U_MEMORY_ALLOCATION_ERROR; michael@0: return; michael@0: } michael@0: hash->put(lhs, (void *) newFirstLeft, status); michael@0: hash->put(rhs, (void *) newFirstRight, status); michael@0: } michael@0: michael@0: // countEquivalent counts how many strings are equivalent to s. michael@0: // hash stores all the equivalnce relations. michael@0: // countEquivalent does not include s itself in the count. michael@0: static int32_t countEquivalent(const icu::Hashtable &hash, const icu::UnicodeString &s) { michael@0: int32_t result = 0; michael@0: EquivIterator iter(hash, s); michael@0: while (iter.next() != NULL) { michael@0: ++result; michael@0: } michael@0: #ifdef UCURR_DEBUG_EQUIV michael@0: { michael@0: char tmp[200]; michael@0: s.extract(0,s.length(),tmp, "UTF-8"); michael@0: printf("CountEquivalent('%s') = %d\n", tmp, result); michael@0: } michael@0: #endif michael@0: return result; michael@0: } michael@0: michael@0: static const icu::Hashtable* getCurrSymbolsEquiv(); michael@0: michael@0: //------------------------------------------------------------ michael@0: // Code michael@0: michael@0: /** michael@0: * Cleanup callback func michael@0: */ michael@0: static UBool U_CALLCONV michael@0: isoCodes_cleanup(void) michael@0: { michael@0: if (gIsoCodes != NULL) { michael@0: uhash_close(const_cast(gIsoCodes)); michael@0: gIsoCodes = NULL; michael@0: } michael@0: gIsoCodesInitOnce.reset(); michael@0: return TRUE; michael@0: } michael@0: michael@0: /** michael@0: * Cleanup callback func michael@0: */ michael@0: static UBool U_CALLCONV michael@0: currSymbolsEquiv_cleanup(void) michael@0: { michael@0: delete const_cast(gCurrSymbolsEquiv); michael@0: gCurrSymbolsEquiv = NULL; michael@0: gCurrSymbolsEquivInitOnce.reset(); michael@0: return TRUE; michael@0: } michael@0: michael@0: /** michael@0: * Deleter for OlsonToMetaMappingEntry michael@0: */ michael@0: static void U_CALLCONV michael@0: deleteIsoCodeEntry(void *obj) { michael@0: IsoCodeEntry *entry = (IsoCodeEntry*)obj; michael@0: uprv_free(entry); michael@0: } michael@0: michael@0: /** michael@0: * Deleter for gCurrSymbolsEquiv. michael@0: */ michael@0: static void U_CALLCONV michael@0: deleteUnicode(void *obj) { michael@0: icu::UnicodeString *entry = (icu::UnicodeString*)obj; michael@0: delete entry; michael@0: } michael@0: michael@0: /** michael@0: * Unfortunately, we have to convert the UChar* currency code to char* michael@0: * to use it as a resource key. michael@0: */ michael@0: static inline char* michael@0: myUCharsToChars(char* resultOfLen4, const UChar* currency) { michael@0: u_UCharsToChars(currency, resultOfLen4, ISO_CURRENCY_CODE_LENGTH); michael@0: resultOfLen4[ISO_CURRENCY_CODE_LENGTH] = 0; michael@0: return resultOfLen4; michael@0: } michael@0: michael@0: /** michael@0: * Internal function to look up currency data. Result is an array of michael@0: * four integers. The first is the fraction digits. The second is the michael@0: * rounding increment, or 0 if none. The rounding increment is in michael@0: * units of 10^(-fraction_digits). The third and fourth are the same michael@0: * except that they are those used in cash transations ( cashDigits michael@0: * and cashRounding ). michael@0: */ michael@0: static const int32_t* michael@0: _findMetaData(const UChar* currency, UErrorCode& ec) { michael@0: michael@0: if (currency == 0 || *currency == 0) { michael@0: if (U_SUCCESS(ec)) { michael@0: ec = U_ILLEGAL_ARGUMENT_ERROR; michael@0: } michael@0: return LAST_RESORT_DATA; michael@0: } michael@0: michael@0: // Get CurrencyMeta resource out of root locale file. [This may michael@0: // move out of the root locale file later; if it does, update this michael@0: // code.] michael@0: UResourceBundle* currencyData = ures_openDirect(U_ICUDATA_CURR, CURRENCY_DATA, &ec); michael@0: UResourceBundle* currencyMeta = ures_getByKey(currencyData, CURRENCY_META, currencyData, &ec); michael@0: michael@0: if (U_FAILURE(ec)) { michael@0: ures_close(currencyMeta); michael@0: // Config/build error; return hard-coded defaults michael@0: return LAST_RESORT_DATA; michael@0: } michael@0: michael@0: // Look up our currency, or if that's not available, then DEFAULT michael@0: char buf[ISO_CURRENCY_CODE_LENGTH+1]; michael@0: UErrorCode ec2 = U_ZERO_ERROR; // local error code: soft failure michael@0: UResourceBundle* rb = ures_getByKey(currencyMeta, myUCharsToChars(buf, currency), NULL, &ec2); michael@0: if (U_FAILURE(ec2)) { michael@0: ures_close(rb); michael@0: rb = ures_getByKey(currencyMeta,DEFAULT_META, NULL, &ec); michael@0: if (U_FAILURE(ec)) { michael@0: ures_close(currencyMeta); michael@0: ures_close(rb); michael@0: // Config/build error; return hard-coded defaults michael@0: return LAST_RESORT_DATA; michael@0: } michael@0: } michael@0: michael@0: int32_t len; michael@0: const int32_t *data = ures_getIntVector(rb, &len, &ec); michael@0: if (U_FAILURE(ec) || len != 4) { michael@0: // Config/build error; return hard-coded defaults michael@0: if (U_SUCCESS(ec)) { michael@0: ec = U_INVALID_FORMAT_ERROR; michael@0: } michael@0: ures_close(currencyMeta); michael@0: ures_close(rb); michael@0: return LAST_RESORT_DATA; michael@0: } michael@0: michael@0: ures_close(currencyMeta); michael@0: ures_close(rb); michael@0: return data; michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: /** michael@0: * @see VARIANT_IS_EURO michael@0: * @see VARIANT_IS_PREEURO michael@0: */ michael@0: static uint32_t michael@0: idForLocale(const char* locale, char* countryAndVariant, int capacity, UErrorCode* ec) michael@0: { michael@0: uint32_t variantType = 0; michael@0: // !!! this is internal only, assumes buffer is not null and capacity is sufficient michael@0: // Extract the country name and variant name. We only michael@0: // recognize two variant names, EURO and PREEURO. michael@0: char variant[ULOC_FULLNAME_CAPACITY]; michael@0: uloc_getCountry(locale, countryAndVariant, capacity, ec); michael@0: uloc_getVariant(locale, variant, sizeof(variant), ec); michael@0: if (variant[0] != 0) { michael@0: variantType = (uint32_t)(0 == uprv_strcmp(variant, VAR_EURO)) michael@0: | ((uint32_t)(0 == uprv_strcmp(variant, VAR_PRE_EURO)) << 1); michael@0: if (variantType) michael@0: { michael@0: uprv_strcat(countryAndVariant, VAR_DELIM_STR); michael@0: uprv_strcat(countryAndVariant, variant); michael@0: } michael@0: } michael@0: return variantType; michael@0: } michael@0: michael@0: // ------------------------------------------ michael@0: // michael@0: // Registration michael@0: // michael@0: //------------------------------------------- michael@0: michael@0: // don't use ICUService since we don't need fallback michael@0: michael@0: U_CDECL_BEGIN michael@0: static UBool U_CALLCONV currency_cleanup(void); michael@0: U_CDECL_END michael@0: michael@0: #if !UCONFIG_NO_SERVICE michael@0: struct CReg; michael@0: michael@0: static UMutex gCRegLock = U_MUTEX_INITIALIZER; michael@0: static CReg* gCRegHead = 0; michael@0: michael@0: struct CReg : public icu::UMemory { michael@0: CReg *next; michael@0: UChar iso[ISO_CURRENCY_CODE_LENGTH+1]; michael@0: char id[ULOC_FULLNAME_CAPACITY]; michael@0: michael@0: CReg(const UChar* _iso, const char* _id) michael@0: : next(0) michael@0: { michael@0: int32_t len = (int32_t)uprv_strlen(_id); michael@0: if (len > (int32_t)(sizeof(id)-1)) { michael@0: len = (sizeof(id)-1); michael@0: } michael@0: uprv_strncpy(id, _id, len); michael@0: id[len] = 0; michael@0: uprv_memcpy(iso, _iso, ISO_CURRENCY_CODE_LENGTH * sizeof(const UChar)); michael@0: iso[ISO_CURRENCY_CODE_LENGTH] = 0; michael@0: } michael@0: michael@0: static UCurrRegistryKey reg(const UChar* _iso, const char* _id, UErrorCode* status) michael@0: { michael@0: if (status && U_SUCCESS(*status) && _iso && _id) { michael@0: CReg* n = new CReg(_iso, _id); michael@0: if (n) { michael@0: umtx_lock(&gCRegLock); michael@0: if (!gCRegHead) { michael@0: /* register for the first time */ michael@0: ucln_i18n_registerCleanup(UCLN_I18N_CURRENCY, currency_cleanup); michael@0: } michael@0: n->next = gCRegHead; michael@0: gCRegHead = n; michael@0: umtx_unlock(&gCRegLock); michael@0: return n; michael@0: } michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: static UBool unreg(UCurrRegistryKey key) { michael@0: UBool found = FALSE; michael@0: umtx_lock(&gCRegLock); michael@0: michael@0: CReg** p = &gCRegHead; michael@0: while (*p) { michael@0: if (*p == key) { michael@0: *p = ((CReg*)key)->next; michael@0: delete (CReg*)key; michael@0: found = TRUE; michael@0: break; michael@0: } michael@0: p = &((*p)->next); michael@0: } michael@0: michael@0: umtx_unlock(&gCRegLock); michael@0: return found; michael@0: } michael@0: michael@0: static const UChar* get(const char* id) { michael@0: const UChar* result = NULL; michael@0: umtx_lock(&gCRegLock); michael@0: CReg* p = gCRegHead; michael@0: michael@0: /* register cleanup of the mutex */ michael@0: ucln_i18n_registerCleanup(UCLN_I18N_CURRENCY, currency_cleanup); michael@0: while (p) { michael@0: if (uprv_strcmp(id, p->id) == 0) { michael@0: result = p->iso; michael@0: break; michael@0: } michael@0: p = p->next; michael@0: } michael@0: umtx_unlock(&gCRegLock); michael@0: return result; michael@0: } michael@0: michael@0: /* This doesn't need to be thread safe. It's for u_cleanup only. */ michael@0: static void cleanup(void) { michael@0: while (gCRegHead) { michael@0: CReg* n = gCRegHead; michael@0: gCRegHead = gCRegHead->next; michael@0: delete n; michael@0: } michael@0: } michael@0: }; michael@0: michael@0: // ------------------------------------- michael@0: michael@0: U_CAPI UCurrRegistryKey U_EXPORT2 michael@0: ucurr_register(const UChar* isoCode, const char* locale, UErrorCode *status) michael@0: { michael@0: if (status && U_SUCCESS(*status)) { michael@0: char id[ULOC_FULLNAME_CAPACITY]; michael@0: idForLocale(locale, id, sizeof(id), status); michael@0: return CReg::reg(isoCode, id, status); michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: ucurr_unregister(UCurrRegistryKey key, UErrorCode* status) michael@0: { michael@0: if (status && U_SUCCESS(*status)) { michael@0: return CReg::unreg(key); michael@0: } michael@0: return FALSE; michael@0: } michael@0: #endif /* UCONFIG_NO_SERVICE */ michael@0: michael@0: // ------------------------------------- michael@0: michael@0: /** michael@0: * Release all static memory held by currency. michael@0: */ michael@0: /*The declaration here is needed so currency_cleanup(void) michael@0: * can call this function. michael@0: */ michael@0: static UBool U_CALLCONV michael@0: currency_cache_cleanup(void); michael@0: michael@0: U_CDECL_BEGIN michael@0: static UBool U_CALLCONV currency_cleanup(void) { michael@0: #if !UCONFIG_NO_SERVICE michael@0: CReg::cleanup(); michael@0: #endif michael@0: /* michael@0: * There might be some cached currency data or isoCodes data. michael@0: */ michael@0: currency_cache_cleanup(); michael@0: isoCodes_cleanup(); michael@0: currSymbolsEquiv_cleanup(); michael@0: michael@0: return TRUE; michael@0: } michael@0: U_CDECL_END michael@0: michael@0: // ------------------------------------- michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucurr_forLocale(const char* locale, michael@0: UChar* buff, michael@0: int32_t buffCapacity, michael@0: UErrorCode* ec) michael@0: { michael@0: int32_t resLen = 0; michael@0: const UChar* s = NULL; michael@0: if (ec != NULL && U_SUCCESS(*ec)) { michael@0: if ((buff && buffCapacity) || !buffCapacity) { michael@0: UErrorCode localStatus = U_ZERO_ERROR; michael@0: char id[ULOC_FULLNAME_CAPACITY]; michael@0: if ((resLen = uloc_getKeywordValue(locale, "currency", id, ULOC_FULLNAME_CAPACITY, &localStatus))) { michael@0: // there is a currency keyword. Try to see if it's valid michael@0: if(buffCapacity > resLen) { michael@0: /* Normalize the currency keyword value to upper case. */ michael@0: T_CString_toUpperCase(id); michael@0: u_charsToUChars(id, buff, resLen); michael@0: } michael@0: } else { michael@0: // get country or country_variant in `id' michael@0: uint32_t variantType = idForLocale(locale, id, sizeof(id), ec); michael@0: michael@0: if (U_FAILURE(*ec)) { michael@0: return 0; michael@0: } michael@0: michael@0: #if !UCONFIG_NO_SERVICE michael@0: const UChar* result = CReg::get(id); michael@0: if (result) { michael@0: if(buffCapacity > u_strlen(result)) { michael@0: u_strcpy(buff, result); michael@0: } michael@0: return u_strlen(result); michael@0: } michael@0: #endif michael@0: // Remove variants, which is only needed for registration. michael@0: char *idDelim = strchr(id, VAR_DELIM); michael@0: if (idDelim) { michael@0: idDelim[0] = 0; michael@0: } michael@0: michael@0: // Look up the CurrencyMap element in the root bundle. michael@0: UResourceBundle *rb = ures_openDirect(U_ICUDATA_CURR, CURRENCY_DATA, &localStatus); michael@0: UResourceBundle *cm = ures_getByKey(rb, CURRENCY_MAP, rb, &localStatus); michael@0: UResourceBundle *countryArray = ures_getByKey(rb, id, cm, &localStatus); michael@0: UResourceBundle *currencyReq = ures_getByIndex(countryArray, 0, NULL, &localStatus); michael@0: s = ures_getStringByKey(currencyReq, "id", &resLen, &localStatus); michael@0: michael@0: /* michael@0: Get the second item when PREEURO is requested, and this is a known Euro country. michael@0: If the requested variant is PREEURO, and this isn't a Euro country, assume michael@0: that the country changed over to the Euro in the future. This is probably michael@0: an old version of ICU that hasn't been updated yet. The latest currency is michael@0: probably correct. michael@0: */ michael@0: if (U_SUCCESS(localStatus)) { michael@0: if ((variantType & VARIANT_IS_PREEURO) && u_strcmp(s, EUR_STR) == 0) { michael@0: currencyReq = ures_getByIndex(countryArray, 1, currencyReq, &localStatus); michael@0: s = ures_getStringByKey(currencyReq, "id", &resLen, &localStatus); michael@0: } michael@0: else if ((variantType & VARIANT_IS_EURO)) { michael@0: s = EUR_STR; michael@0: } michael@0: } michael@0: ures_close(countryArray); michael@0: ures_close(currencyReq); michael@0: michael@0: if ((U_FAILURE(localStatus)) && strchr(id, '_') != 0) michael@0: { michael@0: // We don't know about it. Check to see if we support the variant. michael@0: uloc_getParent(locale, id, sizeof(id), ec); michael@0: *ec = U_USING_FALLBACK_WARNING; michael@0: return ucurr_forLocale(id, buff, buffCapacity, ec); michael@0: } michael@0: else if (*ec == U_ZERO_ERROR || localStatus != U_ZERO_ERROR) { michael@0: // There is nothing to fallback to. Report the failure/warning if possible. michael@0: *ec = localStatus; michael@0: } michael@0: if (U_SUCCESS(*ec)) { michael@0: if(buffCapacity > resLen) { michael@0: u_strcpy(buff, s); michael@0: } michael@0: } michael@0: } michael@0: return u_terminateUChars(buff, buffCapacity, resLen, ec); michael@0: } else { michael@0: *ec = U_ILLEGAL_ARGUMENT_ERROR; michael@0: } michael@0: } michael@0: return resLen; michael@0: } michael@0: michael@0: // end registration michael@0: michael@0: /** michael@0: * Modify the given locale name by removing the rightmost _-delimited michael@0: * element. If there is none, empty the string ("" == root). michael@0: * NOTE: The string "root" is not recognized; do not use it. michael@0: * @return TRUE if the fallback happened; FALSE if locale is already michael@0: * root (""). michael@0: */ michael@0: static UBool fallback(char *loc) { michael@0: if (!*loc) { michael@0: return FALSE; michael@0: } michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: uloc_getParent(loc, loc, (int32_t)uprv_strlen(loc), &status); michael@0: /* michael@0: char *i = uprv_strrchr(loc, '_'); michael@0: if (i == NULL) { michael@0: i = loc; michael@0: } michael@0: *i = 0; michael@0: */ michael@0: return TRUE; michael@0: } michael@0: michael@0: michael@0: U_CAPI const UChar* U_EXPORT2 michael@0: ucurr_getName(const UChar* currency, michael@0: const char* locale, michael@0: UCurrNameStyle nameStyle, michael@0: UBool* isChoiceFormat, // fillin michael@0: int32_t* len, // fillin michael@0: UErrorCode* ec) { michael@0: michael@0: // Look up the Currencies resource for the given locale. The michael@0: // Currencies locale data looks like this: michael@0: //|en { michael@0: //| Currencies { michael@0: //| USD { "US$", "US Dollar" } michael@0: //| CHF { "Sw F", "Swiss Franc" } michael@0: //| INR { "=0#Rs|1#Re|1 1) { michael@0: *ec = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: // In the future, resource bundles may implement multi-level michael@0: // fallback. That is, if a currency is not found in the en_US michael@0: // Currencies data, then the en Currencies data will be searched. michael@0: // Currently, if a Currencies datum exists in en_US and en, the michael@0: // en_US entry hides that in en. michael@0: michael@0: // We want multi-level fallback for this resource, so we implement michael@0: // it manually. michael@0: michael@0: // Use a separate UErrorCode here that does not propagate out of michael@0: // this function. michael@0: UErrorCode ec2 = U_ZERO_ERROR; michael@0: michael@0: char loc[ULOC_FULLNAME_CAPACITY]; michael@0: uloc_getName(locale, loc, sizeof(loc), &ec2); michael@0: if (U_FAILURE(ec2) || ec2 == U_STRING_NOT_TERMINATED_WARNING) { michael@0: *ec = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: char buf[ISO_CURRENCY_CODE_LENGTH+1]; michael@0: myUCharsToChars(buf, currency); michael@0: michael@0: /* Normalize the keyword value to uppercase */ michael@0: T_CString_toUpperCase(buf); michael@0: michael@0: const UChar* s = NULL; michael@0: ec2 = U_ZERO_ERROR; michael@0: UResourceBundle* rb = ures_open(U_ICUDATA_CURR, loc, &ec2); michael@0: michael@0: rb = ures_getByKey(rb, CURRENCIES, rb, &ec2); michael@0: michael@0: // Fetch resource with multi-level resource inheritance fallback michael@0: rb = ures_getByKeyWithFallback(rb, buf, rb, &ec2); michael@0: michael@0: s = ures_getStringByIndex(rb, choice, len, &ec2); michael@0: ures_close(rb); michael@0: michael@0: // If we've succeeded we're done. Otherwise, try to fallback. michael@0: // If that fails (because we are already at root) then exit. michael@0: if (U_SUCCESS(ec2)) { michael@0: if (ec2 == U_USING_DEFAULT_WARNING michael@0: || (ec2 == U_USING_FALLBACK_WARNING && *ec != U_USING_DEFAULT_WARNING)) { michael@0: *ec = ec2; michael@0: } michael@0: } michael@0: michael@0: // Determine if this is a ChoiceFormat pattern. One leading mark michael@0: // indicates a ChoiceFormat. Two indicates a static string that michael@0: // starts with a mark. In either case, the first mark is ignored, michael@0: // if present. Marks in the rest of the string have no special michael@0: // meaning. michael@0: *isChoiceFormat = FALSE; michael@0: if (U_SUCCESS(ec2)) { michael@0: U_ASSERT(s != NULL); michael@0: int32_t i=0; michael@0: while (i < *len && s[i] == CHOICE_FORMAT_MARK && i < 2) { michael@0: ++i; michael@0: } michael@0: *isChoiceFormat = (i == 1); michael@0: if (i != 0) ++s; // Skip over first mark michael@0: return s; michael@0: } michael@0: michael@0: // If we fail to find a match, use the ISO 4217 code michael@0: *len = u_strlen(currency); // Should == ISO_CURRENCY_CODE_LENGTH, but maybe not...? michael@0: *ec = U_USING_DEFAULT_WARNING; michael@0: return currency; michael@0: } michael@0: michael@0: U_CAPI const UChar* U_EXPORT2 michael@0: ucurr_getPluralName(const UChar* currency, michael@0: const char* locale, michael@0: UBool* isChoiceFormat, michael@0: const char* pluralCount, michael@0: int32_t* len, // fillin michael@0: UErrorCode* ec) { michael@0: // Look up the Currencies resource for the given locale. The michael@0: // Currencies locale data looks like this: michael@0: //|en { michael@0: //| CurrencyPlurals { michael@0: //| USD{ michael@0: //| one{"US dollar"} michael@0: //| other{"US dollars"} michael@0: //| } michael@0: //| } michael@0: //|} michael@0: michael@0: if (U_FAILURE(*ec)) { michael@0: return 0; michael@0: } michael@0: michael@0: // Use a separate UErrorCode here that does not propagate out of michael@0: // this function. michael@0: UErrorCode ec2 = U_ZERO_ERROR; michael@0: michael@0: char loc[ULOC_FULLNAME_CAPACITY]; michael@0: uloc_getName(locale, loc, sizeof(loc), &ec2); michael@0: if (U_FAILURE(ec2) || ec2 == U_STRING_NOT_TERMINATED_WARNING) { michael@0: *ec = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: char buf[ISO_CURRENCY_CODE_LENGTH+1]; michael@0: myUCharsToChars(buf, currency); michael@0: michael@0: const UChar* s = NULL; michael@0: ec2 = U_ZERO_ERROR; michael@0: UResourceBundle* rb = ures_open(U_ICUDATA_CURR, loc, &ec2); michael@0: michael@0: rb = ures_getByKey(rb, CURRENCYPLURALS, rb, &ec2); michael@0: michael@0: // Fetch resource with multi-level resource inheritance fallback michael@0: rb = ures_getByKeyWithFallback(rb, buf, rb, &ec2); michael@0: michael@0: s = ures_getStringByKeyWithFallback(rb, pluralCount, len, &ec2); michael@0: if (U_FAILURE(ec2)) { michael@0: // fall back to "other" michael@0: ec2 = U_ZERO_ERROR; michael@0: s = ures_getStringByKeyWithFallback(rb, "other", len, &ec2); michael@0: if (U_FAILURE(ec2)) { michael@0: ures_close(rb); michael@0: // fall back to long name in Currencies michael@0: return ucurr_getName(currency, locale, UCURR_LONG_NAME, michael@0: isChoiceFormat, len, ec); michael@0: } michael@0: } michael@0: ures_close(rb); michael@0: michael@0: // If we've succeeded we're done. Otherwise, try to fallback. michael@0: // If that fails (because we are already at root) then exit. michael@0: if (U_SUCCESS(ec2)) { michael@0: if (ec2 == U_USING_DEFAULT_WARNING michael@0: || (ec2 == U_USING_FALLBACK_WARNING && *ec != U_USING_DEFAULT_WARNING)) { michael@0: *ec = ec2; michael@0: } michael@0: U_ASSERT(s != NULL); michael@0: return s; michael@0: } michael@0: michael@0: // If we fail to find a match, use the ISO 4217 code michael@0: *len = u_strlen(currency); // Should == ISO_CURRENCY_CODE_LENGTH, but maybe not...? michael@0: *ec = U_USING_DEFAULT_WARNING; michael@0: return currency; michael@0: } michael@0: michael@0: michael@0: //======================================================================== michael@0: // Following are structure and function for parsing currency names michael@0: michael@0: #define NEED_TO_BE_DELETED 0x1 michael@0: michael@0: // TODO: a better way to define this? michael@0: #define MAX_CURRENCY_NAME_LEN 100 michael@0: michael@0: typedef struct { michael@0: const char* IsoCode; // key michael@0: UChar* currencyName; // value michael@0: int32_t currencyNameLen; // value length michael@0: int32_t flag; // flags michael@0: } CurrencyNameStruct; michael@0: michael@0: michael@0: #ifndef MIN michael@0: #define MIN(a,b) (((a)<(b)) ? (a) : (b)) michael@0: #endif michael@0: michael@0: #ifndef MAX michael@0: #define MAX(a,b) (((a)<(b)) ? (b) : (a)) michael@0: #endif michael@0: michael@0: michael@0: // Comparason function used in quick sort. michael@0: static int U_CALLCONV currencyNameComparator(const void* a, const void* b) { michael@0: const CurrencyNameStruct* currName_1 = (const CurrencyNameStruct*)a; michael@0: const CurrencyNameStruct* currName_2 = (const CurrencyNameStruct*)b; michael@0: for (int32_t i = 0; michael@0: i < MIN(currName_1->currencyNameLen, currName_2->currencyNameLen); michael@0: ++i) { michael@0: if (currName_1->currencyName[i] < currName_2->currencyName[i]) { michael@0: return -1; michael@0: } michael@0: if (currName_1->currencyName[i] > currName_2->currencyName[i]) { michael@0: return 1; michael@0: } michael@0: } michael@0: if (currName_1->currencyNameLen < currName_2->currencyNameLen) { michael@0: return -1; michael@0: } else if (currName_1->currencyNameLen > currName_2->currencyNameLen) { michael@0: return 1; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: michael@0: // Give a locale, return the maximum number of currency names associated with michael@0: // this locale. michael@0: // It gets currency names from resource bundles using fallback. michael@0: // It is the maximum number because in the fallback chain, some of the michael@0: // currency names are duplicated. michael@0: // For example, given locale as "en_US", the currency names get from resource michael@0: // bundle in "en_US" and "en" are duplicated. The fallback mechanism will count michael@0: // all currency names in "en_US" and "en". michael@0: static void michael@0: getCurrencyNameCount(const char* loc, int32_t* total_currency_name_count, int32_t* total_currency_symbol_count) { michael@0: U_NAMESPACE_USE michael@0: *total_currency_name_count = 0; michael@0: *total_currency_symbol_count = 0; michael@0: const UChar* s = NULL; michael@0: char locale[ULOC_FULLNAME_CAPACITY]; michael@0: uprv_strcpy(locale, loc); michael@0: const icu::Hashtable *currencySymbolsEquiv = getCurrSymbolsEquiv(); michael@0: for (;;) { michael@0: UErrorCode ec2 = U_ZERO_ERROR; michael@0: // TODO: ures_openDirect? michael@0: UResourceBundle* rb = ures_open(U_ICUDATA_CURR, locale, &ec2); michael@0: UResourceBundle* curr = ures_getByKey(rb, CURRENCIES, NULL, &ec2); michael@0: int32_t n = ures_getSize(curr); michael@0: for (int32_t i=0; i 0 && s[0] == CHOICE_FORMAT_MARK) { michael@0: ++s; michael@0: --len; michael@0: if (len > 0 && s[0] != CHOICE_FORMAT_MARK) { michael@0: isChoice = TRUE; michael@0: } michael@0: } michael@0: if (isChoice) { michael@0: ChoiceFormat fmt(UnicodeString(TRUE, s, len), ec2); michael@0: int32_t fmt_count; michael@0: fmt.getFormats(fmt_count); michael@0: *total_currency_symbol_count += fmt_count; michael@0: } else { michael@0: ++(*total_currency_symbol_count); // currency symbol michael@0: if (currencySymbolsEquiv != NULL) { michael@0: *total_currency_symbol_count += countEquivalent(*currencySymbolsEquiv, UnicodeString(TRUE, s, len)); michael@0: } michael@0: } michael@0: michael@0: ++(*total_currency_symbol_count); // iso code michael@0: ++(*total_currency_name_count); // long name michael@0: ures_close(names); michael@0: } michael@0: michael@0: // currency plurals michael@0: UErrorCode ec3 = U_ZERO_ERROR; michael@0: UResourceBundle* curr_p = ures_getByKey(rb, CURRENCYPLURALS, NULL, &ec3); michael@0: n = ures_getSize(curr_p); michael@0: for (int32_t i=0; i 0 && s[0] == CHOICE_FORMAT_MARK) { michael@0: ++s; michael@0: --len; michael@0: if (len > 0 && s[0] != CHOICE_FORMAT_MARK) { michael@0: isChoice = TRUE; michael@0: } michael@0: } michael@0: if (isChoice) { michael@0: ChoiceFormat fmt(UnicodeString(TRUE, s, len), ec2); michael@0: int32_t fmt_count; michael@0: const UnicodeString* formats = fmt.getFormats(fmt_count); michael@0: for (int i = 0; i < fmt_count; ++i) { michael@0: // put iso, formats[i]; into array michael@0: int32_t length = formats[i].length(); michael@0: UChar* name = (UChar*)uprv_malloc(sizeof(UChar)*length); michael@0: formats[i].extract(0, length, name); michael@0: (*currencySymbols)[*total_currency_symbol_count].IsoCode = iso; michael@0: (*currencySymbols)[*total_currency_symbol_count].currencyName = name; michael@0: (*currencySymbols)[*total_currency_symbol_count].flag = NEED_TO_BE_DELETED; michael@0: (*currencySymbols)[(*total_currency_symbol_count)++].currencyNameLen = length; michael@0: } michael@0: } else { michael@0: // Add currency symbol. michael@0: (*currencySymbols)[*total_currency_symbol_count].IsoCode = iso; michael@0: (*currencySymbols)[*total_currency_symbol_count].currencyName = (UChar*)s; michael@0: (*currencySymbols)[*total_currency_symbol_count].flag = 0; michael@0: (*currencySymbols)[(*total_currency_symbol_count)++].currencyNameLen = len; michael@0: // Add equivalent symbols michael@0: if (currencySymbolsEquiv != NULL) { michael@0: EquivIterator iter(*currencySymbolsEquiv, UnicodeString(TRUE, s, len)); michael@0: const UnicodeString *symbol; michael@0: while ((symbol = iter.next()) != NULL) { michael@0: (*currencySymbols)[*total_currency_symbol_count].IsoCode = iso; michael@0: (*currencySymbols)[*total_currency_symbol_count].currencyName = (UChar*) symbol->getBuffer(); michael@0: (*currencySymbols)[*total_currency_symbol_count].flag = 0; michael@0: (*currencySymbols)[(*total_currency_symbol_count)++].currencyNameLen = symbol->length(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Add currency long name. michael@0: s = ures_getStringByIndex(names, UCURR_LONG_NAME, &len, &ec2); michael@0: (*currencyNames)[*total_currency_name_count].IsoCode = iso; michael@0: UChar* upperName = toUpperCase(s, len, locale); michael@0: (*currencyNames)[*total_currency_name_count].currencyName = upperName; michael@0: (*currencyNames)[*total_currency_name_count].flag = NEED_TO_BE_DELETED; michael@0: (*currencyNames)[(*total_currency_name_count)++].currencyNameLen = len; michael@0: michael@0: // put (iso, 3, and iso) in to array michael@0: // Add currency ISO code. michael@0: (*currencySymbols)[*total_currency_symbol_count].IsoCode = iso; michael@0: (*currencySymbols)[*total_currency_symbol_count].currencyName = (UChar*)uprv_malloc(sizeof(UChar)*3); michael@0: // Must convert iso[] into Unicode michael@0: u_charsToUChars(iso, (*currencySymbols)[*total_currency_symbol_count].currencyName, 3); michael@0: (*currencySymbols)[*total_currency_symbol_count].flag = NEED_TO_BE_DELETED; michael@0: (*currencySymbols)[(*total_currency_symbol_count)++].currencyNameLen = 3; michael@0: michael@0: ures_close(names); michael@0: } michael@0: michael@0: // currency plurals michael@0: UErrorCode ec3 = U_ZERO_ERROR; michael@0: UResourceBundle* curr_p = ures_getByKey(rb, CURRENCYPLURALS, NULL, &ec3); michael@0: n = ures_getSize(curr_p); michael@0: for (int32_t i=0; i= currencyNames[mid].currencyNameLen) { michael@0: first = mid + 1; michael@0: } else { michael@0: if (key > currencyNames[mid].currencyName[indexInCurrencyNames]) { michael@0: first = mid + 1; michael@0: } michael@0: else if (key < currencyNames[mid].currencyName[indexInCurrencyNames]) { michael@0: last = mid - 1; michael@0: } michael@0: else { michael@0: // Find a match, and looking for ranges michael@0: // Now do two more binary searches. First, on the left side for michael@0: // the greatest L such that CurrencyNameStruct[L] < key. michael@0: int32_t L = *begin; michael@0: int32_t R = mid; michael@0: michael@0: #ifdef UCURR_DEBUG michael@0: printf("mid = %d\n", mid); michael@0: #endif michael@0: while (L < R) { michael@0: int32_t M = (L + R) / 2; michael@0: #ifdef UCURR_DEBUG michael@0: printf("L = %d, R = %d, M = %d\n", L, R, M); michael@0: #endif michael@0: if (indexInCurrencyNames >= currencyNames[M].currencyNameLen) { michael@0: L = M + 1; michael@0: } else { michael@0: if (currencyNames[M].currencyName[indexInCurrencyNames] < key) { michael@0: L = M + 1; michael@0: } else { michael@0: #ifdef UCURR_DEBUG michael@0: U_ASSERT(currencyNames[M].currencyName[indexInCurrencyNames] == key); michael@0: #endif michael@0: R = M; michael@0: } michael@0: } michael@0: } michael@0: #ifdef UCURR_DEBUG michael@0: U_ASSERT(L == R); michael@0: #endif michael@0: *begin = L; michael@0: #ifdef UCURR_DEBUG michael@0: printf("begin = %d\n", *begin); michael@0: U_ASSERT(currencyNames[*begin].currencyName[indexInCurrencyNames] == key); michael@0: #endif michael@0: michael@0: // Now for the second search, finding the least R such that michael@0: // key < CurrencyNameStruct[R]. michael@0: L = mid; michael@0: R = *end; michael@0: while (L < R) { michael@0: int32_t M = (L + R) / 2; michael@0: #ifdef UCURR_DEBUG michael@0: printf("L = %d, R = %d, M = %d\n", L, R, M); michael@0: #endif michael@0: if (currencyNames[M].currencyNameLen < indexInCurrencyNames) { michael@0: L = M + 1; michael@0: } else { michael@0: if (currencyNames[M].currencyName[indexInCurrencyNames] > key) { michael@0: R = M; michael@0: } else { michael@0: #ifdef UCURR_DEBUG michael@0: U_ASSERT(currencyNames[M].currencyName[indexInCurrencyNames] == key); michael@0: #endif michael@0: L = M + 1; michael@0: } michael@0: } michael@0: } michael@0: #ifdef UCURR_DEBUG michael@0: U_ASSERT(L == R); michael@0: #endif michael@0: if (currencyNames[R].currencyName[indexInCurrencyNames] > key) { michael@0: *end = R - 1; michael@0: } else { michael@0: *end = R; michael@0: } michael@0: #ifdef UCURR_DEBUG michael@0: printf("end = %d\n", *end); michael@0: #endif michael@0: michael@0: // now, found the range. check whether there is exact match michael@0: if (currencyNames[*begin].currencyNameLen == indexInCurrencyNames + 1) { michael@0: return *begin; // find range and exact match. michael@0: } michael@0: return -1; // find range, but no exact match. michael@0: } michael@0: } michael@0: } michael@0: *begin = -1; michael@0: *end = -1; michael@0: return -1; // failed to find range. michael@0: } michael@0: michael@0: michael@0: // Linear search "text" in "currencyNames". michael@0: // @param begin, end: the begin and end index in currencyNames, within which michael@0: // range should the search be performed. michael@0: // @param textLen: the length of the text to be compared michael@0: // @param maxMatchLen(IN/OUT): passing in the computed max matching length michael@0: // pass out the new max matching length michael@0: // @param maxMatchIndex: the index in currencyName which has the longest michael@0: // match with input text. michael@0: static void michael@0: linearSearch(const CurrencyNameStruct* currencyNames, michael@0: int32_t begin, int32_t end, michael@0: const UChar* text, int32_t textLen, michael@0: int32_t *maxMatchLen, int32_t* maxMatchIndex) { michael@0: for (int32_t index = begin; index <= end; ++index) { michael@0: int32_t len = currencyNames[index].currencyNameLen; michael@0: if (len > *maxMatchLen && len <= textLen && michael@0: uprv_memcmp(currencyNames[index].currencyName, text, len * sizeof(UChar)) == 0) { michael@0: *maxMatchIndex = index; michael@0: *maxMatchLen = len; michael@0: #ifdef UCURR_DEBUG michael@0: printf("maxMatchIndex = %d, maxMatchLen = %d\n", michael@0: *maxMatchIndex, *maxMatchLen); michael@0: #endif michael@0: } michael@0: } michael@0: } michael@0: michael@0: #define LINEAR_SEARCH_THRESHOLD 10 michael@0: michael@0: // Find longest match between "text" and currency names in "currencyNames". michael@0: // @param total_currency_count: total number of currency names in CurrencyNames. michael@0: // @param textLen: the length of the text to be compared michael@0: // @param maxMatchLen: passing in the computed max matching length michael@0: // pass out the new max matching length michael@0: // @param maxMatchIndex: the index in currencyName which has the longest michael@0: // match with input text. michael@0: static void michael@0: searchCurrencyName(const CurrencyNameStruct* currencyNames, michael@0: int32_t total_currency_count, michael@0: const UChar* text, int32_t textLen, michael@0: int32_t* maxMatchLen, int32_t* maxMatchIndex) { michael@0: *maxMatchIndex = -1; michael@0: *maxMatchLen = 0; michael@0: int32_t matchIndex = -1; michael@0: int32_t binarySearchBegin = 0; michael@0: int32_t binarySearchEnd = total_currency_count - 1; michael@0: // It is a variant of binary search. michael@0: // For example, given the currency names in currencyNames array are: michael@0: // A AB ABC AD AZ B BB BBEX BBEXYZ BS C D E.... michael@0: // and the input text is BBEXST michael@0: // The first round binary search search "B" in the text against michael@0: // the first char in currency names, and find the first char matching range michael@0: // to be "B BB BBEX BBEXYZ BS" (and the maximum matching "B"). michael@0: // The 2nd round binary search search the second "B" in the text against michael@0: // the 2nd char in currency names, and narrow the matching range to michael@0: // "BB BBEX BBEXYZ" (and the maximum matching "BB"). michael@0: // The 3rd round returnes the range as "BBEX BBEXYZ" (without changing michael@0: // maximum matching). michael@0: // The 4th round returns the same range (the maximum matching is "BBEX"). michael@0: // The 5th round returns no matching range. michael@0: for (int32_t index = 0; index < textLen; ++index) { michael@0: // matchIndex saves the one with exact match till the current point. michael@0: // [binarySearchBegin, binarySearchEnd] saves the matching range. michael@0: matchIndex = binarySearch(currencyNames, index, michael@0: text[index], michael@0: &binarySearchBegin, &binarySearchEnd); michael@0: if (binarySearchBegin == -1) { // did not find the range michael@0: break; michael@0: } michael@0: if (matchIndex != -1) { michael@0: // find an exact match for text from text[0] to text[index] michael@0: // in currencyNames array. michael@0: *maxMatchLen = index + 1; michael@0: *maxMatchIndex = matchIndex; michael@0: } michael@0: if (binarySearchEnd - binarySearchBegin < LINEAR_SEARCH_THRESHOLD) { michael@0: // linear search if within threshold. michael@0: linearSearch(currencyNames, binarySearchBegin, binarySearchEnd, michael@0: text, textLen, michael@0: maxMatchLen, maxMatchIndex); michael@0: break; michael@0: } michael@0: } michael@0: return; michael@0: } michael@0: michael@0: //========================= currency name cache ===================== michael@0: typedef struct { michael@0: char locale[ULOC_FULLNAME_CAPACITY]; //key michael@0: // currency names, case insensitive michael@0: CurrencyNameStruct* currencyNames; // value michael@0: int32_t totalCurrencyNameCount; // currency name count michael@0: // currency symbols and ISO code, case sensitive michael@0: CurrencyNameStruct* currencySymbols; // value michael@0: int32_t totalCurrencySymbolCount; // count michael@0: // reference count. michael@0: // reference count is set to 1 when an entry is put to cache. michael@0: // it increases by 1 before accessing, and decreased by 1 after accessing. michael@0: // The entry is deleted when ref count is zero, which means michael@0: // the entry is replaced out of cache and no process is accessing it. michael@0: int32_t refCount; michael@0: } CurrencyNameCacheEntry; michael@0: michael@0: michael@0: #define CURRENCY_NAME_CACHE_NUM 10 michael@0: michael@0: // Reserve 10 cache entries. michael@0: static CurrencyNameCacheEntry* currCache[CURRENCY_NAME_CACHE_NUM] = {NULL}; michael@0: // Using an index to indicate which entry to be replaced when cache is full. michael@0: // It is a simple round-robin replacement strategy. michael@0: static int8_t currentCacheEntryIndex = 0; michael@0: michael@0: static UMutex gCurrencyCacheMutex = U_MUTEX_INITIALIZER; michael@0: michael@0: // Cache deletion michael@0: static void michael@0: deleteCurrencyNames(CurrencyNameStruct* currencyNames, int32_t count) { michael@0: for (int32_t index = 0; index < count; ++index) { michael@0: if ( (currencyNames[index].flag & NEED_TO_BE_DELETED) ) { michael@0: uprv_free(currencyNames[index].currencyName); michael@0: } michael@0: } michael@0: uprv_free(currencyNames); michael@0: } michael@0: michael@0: michael@0: static void michael@0: deleteCacheEntry(CurrencyNameCacheEntry* entry) { michael@0: deleteCurrencyNames(entry->currencyNames, entry->totalCurrencyNameCount); michael@0: deleteCurrencyNames(entry->currencySymbols, entry->totalCurrencySymbolCount); michael@0: uprv_free(entry); michael@0: } michael@0: michael@0: michael@0: // Cache clean up michael@0: static UBool U_CALLCONV michael@0: currency_cache_cleanup(void) { michael@0: for (int32_t i = 0; i < CURRENCY_NAME_CACHE_NUM; ++i) { michael@0: if (currCache[i]) { michael@0: deleteCacheEntry(currCache[i]); michael@0: currCache[i] = 0; michael@0: } michael@0: } michael@0: return TRUE; michael@0: } michael@0: michael@0: michael@0: U_CFUNC void michael@0: uprv_parseCurrency(const char* locale, michael@0: const icu::UnicodeString& text, michael@0: icu::ParsePosition& pos, michael@0: int8_t type, michael@0: UChar* result, michael@0: UErrorCode& ec) michael@0: { michael@0: U_NAMESPACE_USE michael@0: michael@0: if (U_FAILURE(ec)) { michael@0: return; michael@0: } michael@0: michael@0: int32_t total_currency_name_count = 0; michael@0: CurrencyNameStruct* currencyNames = NULL; michael@0: int32_t total_currency_symbol_count = 0; michael@0: CurrencyNameStruct* currencySymbols = NULL; michael@0: CurrencyNameCacheEntry* cacheEntry = NULL; michael@0: michael@0: umtx_lock(&gCurrencyCacheMutex); michael@0: // in order to handle racing correctly, michael@0: // not putting 'search' in a separate function. michael@0: int8_t found = -1; michael@0: for (int8_t i = 0; i < CURRENCY_NAME_CACHE_NUM; ++i) { michael@0: if (currCache[i]!= NULL && michael@0: uprv_strcmp(locale, currCache[i]->locale) == 0) { michael@0: found = i; michael@0: break; michael@0: } michael@0: } michael@0: if (found != -1) { michael@0: cacheEntry = currCache[found]; michael@0: currencyNames = cacheEntry->currencyNames; michael@0: total_currency_name_count = cacheEntry->totalCurrencyNameCount; michael@0: currencySymbols = cacheEntry->currencySymbols; michael@0: total_currency_symbol_count = cacheEntry->totalCurrencySymbolCount; michael@0: ++(cacheEntry->refCount); michael@0: } michael@0: umtx_unlock(&gCurrencyCacheMutex); michael@0: if (found == -1) { michael@0: collectCurrencyNames(locale, ¤cyNames, &total_currency_name_count, ¤cySymbols, &total_currency_symbol_count, ec); michael@0: if (U_FAILURE(ec)) { michael@0: return; michael@0: } michael@0: umtx_lock(&gCurrencyCacheMutex); michael@0: // check again. michael@0: int8_t found = -1; michael@0: for (int8_t i = 0; i < CURRENCY_NAME_CACHE_NUM; ++i) { michael@0: if (currCache[i]!= NULL && michael@0: uprv_strcmp(locale, currCache[i]->locale) == 0) { michael@0: found = i; michael@0: break; michael@0: } michael@0: } michael@0: if (found == -1) { michael@0: // insert new entry to michael@0: // currentCacheEntryIndex % CURRENCY_NAME_CACHE_NUM michael@0: // and remove the existing entry michael@0: // currentCacheEntryIndex % CURRENCY_NAME_CACHE_NUM michael@0: // from cache. michael@0: cacheEntry = currCache[currentCacheEntryIndex]; michael@0: if (cacheEntry) { michael@0: --(cacheEntry->refCount); michael@0: // delete if the ref count is zero michael@0: if (cacheEntry->refCount == 0) { michael@0: deleteCacheEntry(cacheEntry); michael@0: } michael@0: } michael@0: cacheEntry = (CurrencyNameCacheEntry*)uprv_malloc(sizeof(CurrencyNameCacheEntry)); michael@0: currCache[currentCacheEntryIndex] = cacheEntry; michael@0: uprv_strcpy(cacheEntry->locale, locale); michael@0: cacheEntry->currencyNames = currencyNames; michael@0: cacheEntry->totalCurrencyNameCount = total_currency_name_count; michael@0: cacheEntry->currencySymbols = currencySymbols; michael@0: cacheEntry->totalCurrencySymbolCount = total_currency_symbol_count; michael@0: cacheEntry->refCount = 2; // one for cache, one for reference michael@0: currentCacheEntryIndex = (currentCacheEntryIndex + 1) % CURRENCY_NAME_CACHE_NUM; michael@0: ucln_i18n_registerCleanup(UCLN_I18N_CURRENCY, currency_cache_cleanup); michael@0: michael@0: } else { michael@0: deleteCurrencyNames(currencyNames, total_currency_name_count); michael@0: deleteCurrencyNames(currencySymbols, total_currency_symbol_count); michael@0: cacheEntry = currCache[found]; michael@0: currencyNames = cacheEntry->currencyNames; michael@0: total_currency_name_count = cacheEntry->totalCurrencyNameCount; michael@0: currencySymbols = cacheEntry->currencySymbols; michael@0: total_currency_symbol_count = cacheEntry->totalCurrencySymbolCount; michael@0: ++(cacheEntry->refCount); michael@0: } michael@0: umtx_unlock(&gCurrencyCacheMutex); michael@0: } michael@0: michael@0: int32_t start = pos.getIndex(); michael@0: michael@0: UChar inputText[MAX_CURRENCY_NAME_LEN]; michael@0: UChar upperText[MAX_CURRENCY_NAME_LEN]; michael@0: int32_t textLen = MIN(MAX_CURRENCY_NAME_LEN, text.length() - start); michael@0: text.extract(start, textLen, inputText); michael@0: UErrorCode ec1 = U_ZERO_ERROR; michael@0: textLen = u_strToUpper(upperText, MAX_CURRENCY_NAME_LEN, inputText, textLen, locale, &ec1); michael@0: michael@0: int32_t max = 0; michael@0: int32_t matchIndex = -1; michael@0: // case in-sensitive comparision against currency names michael@0: searchCurrencyName(currencyNames, total_currency_name_count, michael@0: upperText, textLen, &max, &matchIndex); michael@0: michael@0: #ifdef UCURR_DEBUG michael@0: printf("search in names, max = %d, matchIndex = %d\n", max, matchIndex); michael@0: #endif michael@0: michael@0: int32_t maxInSymbol = 0; michael@0: int32_t matchIndexInSymbol = -1; michael@0: if (type != UCURR_LONG_NAME) { // not name only michael@0: // case sensitive comparison against currency symbols and ISO code. michael@0: searchCurrencyName(currencySymbols, total_currency_symbol_count, michael@0: inputText, textLen, michael@0: &maxInSymbol, &matchIndexInSymbol); michael@0: } michael@0: michael@0: #ifdef UCURR_DEBUG michael@0: printf("search in symbols, maxInSymbol = %d, matchIndexInSymbol = %d\n", maxInSymbol, matchIndexInSymbol); michael@0: if(matchIndexInSymbol != -1) { michael@0: printf("== ISO=%s\n", currencySymbols[matchIndexInSymbol].IsoCode); michael@0: } michael@0: #endif michael@0: michael@0: if (max >= maxInSymbol && matchIndex != -1) { michael@0: u_charsToUChars(currencyNames[matchIndex].IsoCode, result, 4); michael@0: pos.setIndex(start + max); michael@0: } else if (maxInSymbol >= max && matchIndexInSymbol != -1) { michael@0: u_charsToUChars(currencySymbols[matchIndexInSymbol].IsoCode, result, 4); michael@0: pos.setIndex(start + maxInSymbol); michael@0: } michael@0: michael@0: // decrease reference count michael@0: umtx_lock(&gCurrencyCacheMutex); michael@0: --(cacheEntry->refCount); michael@0: if (cacheEntry->refCount == 0) { // remove michael@0: deleteCacheEntry(cacheEntry); michael@0: } michael@0: umtx_unlock(&gCurrencyCacheMutex); michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Internal method. Given a currency ISO code and a locale, return michael@0: * the "static" currency name. This is usually the same as the michael@0: * UCURR_SYMBOL_NAME, but if the latter is a choice format, then the michael@0: * format is applied to the number 2.0 (to yield the more common michael@0: * plural) to return a static name. michael@0: * michael@0: * This is used for backward compatibility with old currency logic in michael@0: * DecimalFormat and DecimalFormatSymbols. michael@0: */ michael@0: U_CFUNC void michael@0: uprv_getStaticCurrencyName(const UChar* iso, const char* loc, michael@0: icu::UnicodeString& result, UErrorCode& ec) michael@0: { michael@0: U_NAMESPACE_USE michael@0: michael@0: UBool isChoiceFormat; michael@0: int32_t len; michael@0: const UChar* currname = ucurr_getName(iso, loc, UCURR_SYMBOL_NAME, michael@0: &isChoiceFormat, &len, &ec); michael@0: if (U_SUCCESS(ec)) { michael@0: // If this is a ChoiceFormat currency, then format an michael@0: // arbitrary value; pick something != 1; more common. michael@0: result.truncate(0); michael@0: if (isChoiceFormat) { michael@0: ChoiceFormat f(UnicodeString(TRUE, currname, len), ec); michael@0: if (U_SUCCESS(ec)) { michael@0: f.format(2.0, result); michael@0: } else { michael@0: result.setTo(iso, -1); michael@0: } michael@0: } else { michael@0: result.setTo(currname, -1); michael@0: } michael@0: } michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucurr_getDefaultFractionDigits(const UChar* currency, UErrorCode* ec) { michael@0: return (_findMetaData(currency, *ec))[0]; michael@0: } michael@0: michael@0: U_CAPI double U_EXPORT2 michael@0: ucurr_getRoundingIncrement(const UChar* currency, UErrorCode* ec) { michael@0: const int32_t *data = _findMetaData(currency, *ec); michael@0: michael@0: // If the meta data is invalid, return 0.0. michael@0: if (data[0] < 0 || data[0] > MAX_POW10) { michael@0: if (U_SUCCESS(*ec)) { michael@0: *ec = U_INVALID_FORMAT_ERROR; michael@0: } michael@0: return 0.0; michael@0: } michael@0: michael@0: // If there is no rounding, return 0.0 to indicate no rounding. A michael@0: // rounding value (data[1]) of 0 or 1 indicates no rounding. michael@0: if (data[1] < 2) { michael@0: return 0.0; michael@0: } michael@0: michael@0: // Return data[1] / 10^(data[0]). The only actual rounding data, michael@0: // as of this writing, is CHF { 2, 5 }. michael@0: return double(data[1]) / POW10[data[0]]; michael@0: } michael@0: michael@0: U_CDECL_BEGIN michael@0: michael@0: typedef struct UCurrencyContext { michael@0: uint32_t currType; /* UCurrCurrencyType */ michael@0: uint32_t listIdx; michael@0: } UCurrencyContext; michael@0: michael@0: /* michael@0: Please keep this list in alphabetical order. michael@0: You can look at the CLDR supplemental data or ISO-4217 for the meaning of some michael@0: of these items. michael@0: ISO-4217: http://www.iso.org/iso/en/prods-services/popstds/currencycodeslist.html michael@0: */ michael@0: static const struct CurrencyList { michael@0: const char *currency; michael@0: uint32_t currType; michael@0: } gCurrencyList[] = { michael@0: {"ADP", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"AED", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"AFA", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"AFN", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"ALK", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"ALL", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"AMD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"ANG", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"AOA", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"AOK", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"AON", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"AOR", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"ARA", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"ARL", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"ARM", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"ARP", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"ARS", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"ATS", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"AUD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"AWG", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"AZM", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"AZN", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"BAD", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"BAM", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"BAN", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"BBD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"BDT", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"BEC", UCURR_UNCOMMON|UCURR_DEPRECATED}, michael@0: {"BEF", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"BEL", UCURR_UNCOMMON|UCURR_DEPRECATED}, michael@0: {"BGL", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"BGM", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"BGN", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"BGO", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"BHD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"BIF", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"BMD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"BND", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"BOB", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"BOL", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"BOP", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"BOV", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"BRB", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"BRC", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"BRE", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"BRL", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"BRN", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"BRR", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"BRZ", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"BSD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"BTN", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"BUK", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"BWP", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"BYB", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"BYR", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"BZD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"CAD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"CDF", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"CHE", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"CHF", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"CHW", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"CLE", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"CLF", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"CLP", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"CNX", UCURR_UNCOMMON|UCURR_DEPRECATED}, michael@0: {"CNY", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"COP", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"COU", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"CRC", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"CSD", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"CSK", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"CUC", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"CUP", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"CVE", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"CYP", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"CZK", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"DDM", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"DEM", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"DJF", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"DKK", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"DOP", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"DZD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"ECS", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"ECV", UCURR_UNCOMMON|UCURR_DEPRECATED}, michael@0: {"EEK", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"EGP", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"EQE", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"ERN", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"ESA", UCURR_UNCOMMON|UCURR_DEPRECATED}, michael@0: {"ESB", UCURR_UNCOMMON|UCURR_DEPRECATED}, michael@0: {"ESP", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"ETB", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"EUR", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"FIM", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"FJD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"FKP", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"FRF", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"GBP", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"GEK", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"GEL", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"GHC", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"GHS", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"GIP", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"GMD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"GNF", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"GNS", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"GQE", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"GRD", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"GTQ", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"GWE", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"GWP", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"GYD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"HKD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"HNL", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"HRD", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"HRK", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"HTG", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"HUF", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"IDR", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"IEP", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"ILP", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"ILR", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"ILS", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"INR", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"IQD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"IRR", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"ISJ", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"ISK", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"ITL", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"JMD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"JOD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"JPY", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"KES", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"KGS", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"KHR", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"KMF", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"KPW", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"KRH", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"KRO", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"KRW", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"KWD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"KYD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"KZT", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"LAK", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"LBP", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"LKR", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"LRD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"LSL", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"LSM", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"LTL", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"LTT", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"LUC", UCURR_UNCOMMON|UCURR_DEPRECATED}, michael@0: {"LUF", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"LUL", UCURR_UNCOMMON|UCURR_DEPRECATED}, michael@0: {"LVL", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"LVR", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"LYD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"MAD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"MAF", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"MCF", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"MDC", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"MDL", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"MGA", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"MGF", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"MKD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"MKN", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"MLF", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"MMK", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"MNT", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"MOP", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"MRO", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"MTL", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"MTP", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"MUR", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"MVP", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"MVR", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"MWK", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"MXN", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"MXP", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"MXV", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"MYR", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"MZE", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"MZM", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"MZN", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"NAD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"NGN", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"NIC", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"NIO", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"NLG", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"NOK", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"NPR", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"NZD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"OMR", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"PAB", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"PEI", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"PEN", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"PES", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"PGK", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"PHP", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"PKR", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"PLN", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"PLZ", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"PTE", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"PYG", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"QAR", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"RHD", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"ROL", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"RON", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"RSD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"RUB", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"RUR", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"RWF", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"SAR", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"SBD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"SCR", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"SDD", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"SDG", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"SDP", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"SEK", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"SGD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"SHP", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"SIT", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"SKK", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"SLL", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"SOS", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"SRD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"SRG", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"SSP", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"STD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"SUR", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"SVC", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"SYP", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"SZL", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"THB", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"TJR", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"TJS", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"TMM", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"TMT", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"TND", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"TOP", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"TPE", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"TRL", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"TRY", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"TTD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"TWD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"TZS", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"UAH", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"UAK", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"UGS", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"UGX", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"USD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"USN", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"USS", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"UYI", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"UYP", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"UYU", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"UZS", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"VEB", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"VEF", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"VND", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"VNN", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"VUV", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"WST", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"XAF", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"XAG", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"XAU", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"XBA", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"XBB", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"XBC", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"XBD", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"XCD", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"XDR", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"XEU", UCURR_UNCOMMON|UCURR_DEPRECATED}, michael@0: {"XFO", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"XFU", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"XOF", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"XPD", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"XPF", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"XPT", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"XRE", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"XSU", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"XTS", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"XUA", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"XXX", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"YDD", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"YER", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"YUD", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"YUM", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"YUN", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"YUR", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"ZAL", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, michael@0: {"ZAR", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"ZMK", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"ZMW", UCURR_COMMON|UCURR_NON_DEPRECATED}, michael@0: {"ZRN", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"ZRZ", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"ZWL", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"ZWR", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: {"ZWD", UCURR_COMMON|UCURR_DEPRECATED}, michael@0: { NULL, 0 } // Leave here to denote the end of the list. michael@0: }; michael@0: michael@0: #define UCURR_MATCHES_BITMASK(variable, typeToMatch) \ michael@0: ((typeToMatch) == UCURR_ALL || ((variable) & (typeToMatch)) == (typeToMatch)) michael@0: michael@0: static int32_t U_CALLCONV michael@0: ucurr_countCurrencyList(UEnumeration *enumerator, UErrorCode * /*pErrorCode*/) { michael@0: UCurrencyContext *myContext = (UCurrencyContext *)(enumerator->context); michael@0: uint32_t currType = myContext->currType; michael@0: int32_t count = 0; michael@0: michael@0: /* Count the number of items matching the type we are looking for. */ michael@0: for (int32_t idx = 0; gCurrencyList[idx].currency != NULL; idx++) { michael@0: if (UCURR_MATCHES_BITMASK(gCurrencyList[idx].currType, currType)) { michael@0: count++; michael@0: } michael@0: } michael@0: return count; michael@0: } michael@0: michael@0: static const char* U_CALLCONV michael@0: ucurr_nextCurrencyList(UEnumeration *enumerator, michael@0: int32_t* resultLength, michael@0: UErrorCode * /*pErrorCode*/) michael@0: { michael@0: UCurrencyContext *myContext = (UCurrencyContext *)(enumerator->context); michael@0: michael@0: /* Find the next in the list that matches the type we are looking for. */ michael@0: while (myContext->listIdx < (sizeof(gCurrencyList)/sizeof(gCurrencyList[0]))-1) { michael@0: const struct CurrencyList *currItem = &gCurrencyList[myContext->listIdx++]; michael@0: if (UCURR_MATCHES_BITMASK(currItem->currType, myContext->currType)) michael@0: { michael@0: if (resultLength) { michael@0: *resultLength = 3; /* Currency codes are only 3 chars long */ michael@0: } michael@0: return currItem->currency; michael@0: } michael@0: } michael@0: /* We enumerated too far. */ michael@0: if (resultLength) { michael@0: *resultLength = 0; michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: static void U_CALLCONV michael@0: ucurr_resetCurrencyList(UEnumeration *enumerator, UErrorCode * /*pErrorCode*/) { michael@0: ((UCurrencyContext *)(enumerator->context))->listIdx = 0; michael@0: } michael@0: michael@0: static void U_CALLCONV michael@0: ucurr_closeCurrencyList(UEnumeration *enumerator) { michael@0: uprv_free(enumerator->context); michael@0: uprv_free(enumerator); michael@0: } michael@0: michael@0: static void U_CALLCONV michael@0: ucurr_createCurrencyList(UHashtable *isoCodes, UErrorCode* status){ michael@0: UErrorCode localStatus = U_ZERO_ERROR; michael@0: michael@0: // Look up the CurrencyMap element in the root bundle. michael@0: UResourceBundle *rb = ures_openDirect(U_ICUDATA_CURR, CURRENCY_DATA, &localStatus); michael@0: UResourceBundle *currencyMapArray = ures_getByKey(rb, CURRENCY_MAP, rb, &localStatus); michael@0: michael@0: if (U_SUCCESS(localStatus)) { michael@0: // process each entry in currency map michael@0: for (int32_t i=0; iisoCode = isoCode; michael@0: entry->from = fromDate; michael@0: entry->to = toDate; michael@0: michael@0: localStatus = U_ZERO_ERROR; michael@0: uhash_put(isoCodes, (UChar *)isoCode, entry, &localStatus); michael@0: } michael@0: } else { michael@0: *status = localStatus; michael@0: } michael@0: ures_close(currencyArray); michael@0: } michael@0: } else { michael@0: *status = localStatus; michael@0: } michael@0: michael@0: ures_close(currencyMapArray); michael@0: } michael@0: michael@0: static const UEnumeration gEnumCurrencyList = { michael@0: NULL, michael@0: NULL, michael@0: ucurr_closeCurrencyList, michael@0: ucurr_countCurrencyList, michael@0: uenum_unextDefault, michael@0: ucurr_nextCurrencyList, michael@0: ucurr_resetCurrencyList michael@0: }; michael@0: U_CDECL_END michael@0: michael@0: michael@0: static void U_CALLCONV initIsoCodes(UErrorCode &status) { michael@0: U_ASSERT(gIsoCodes == NULL); michael@0: ucln_i18n_registerCleanup(UCLN_I18N_CURRENCY, currency_cleanup); michael@0: michael@0: UHashtable *isoCodes = uhash_open(uhash_hashUChars, uhash_compareUChars, NULL, &status); michael@0: if (U_FAILURE(status)) { michael@0: return; michael@0: } michael@0: uhash_setValueDeleter(isoCodes, deleteIsoCodeEntry); michael@0: michael@0: ucurr_createCurrencyList(isoCodes, &status); michael@0: if (U_FAILURE(status)) { michael@0: uhash_close(isoCodes); michael@0: return; michael@0: } michael@0: gIsoCodes = isoCodes; // Note: gIsoCodes is const. Once set up here it is never altered, michael@0: // and read only access is safe without synchronization. michael@0: } michael@0: michael@0: static void populateCurrSymbolsEquiv(icu::Hashtable *hash, UErrorCode &status) { michael@0: if (U_FAILURE(status)) { michael@0: return; michael@0: } michael@0: int32_t length = sizeof(EQUIV_CURRENCY_SYMBOLS) / sizeof(EQUIV_CURRENCY_SYMBOLS[0]); michael@0: for (int32_t i = 0; i < length; ++i) { michael@0: icu::UnicodeString lhs(EQUIV_CURRENCY_SYMBOLS[i][0], -1, US_INV); michael@0: icu::UnicodeString rhs(EQUIV_CURRENCY_SYMBOLS[i][1], -1, US_INV); michael@0: makeEquivalent(lhs.unescape(), rhs.unescape(), hash, status); michael@0: if (U_FAILURE(status)) { michael@0: return; michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void U_CALLCONV initCurrSymbolsEquiv() { michael@0: U_ASSERT(gCurrSymbolsEquiv == NULL); michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: ucln_i18n_registerCleanup(UCLN_I18N_CURRENCY, currency_cleanup); michael@0: icu::Hashtable *temp = new icu::Hashtable(status); michael@0: if (temp == NULL) { michael@0: return; michael@0: } michael@0: if (U_FAILURE(status)) { michael@0: delete temp; michael@0: return; michael@0: } michael@0: temp->setValueDeleter(deleteUnicode); michael@0: populateCurrSymbolsEquiv(temp, status); michael@0: if (U_FAILURE(status)) { michael@0: delete temp; michael@0: return; michael@0: } michael@0: gCurrSymbolsEquiv = temp; michael@0: } michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: ucurr_isAvailable(const UChar* isoCode, UDate from, UDate to, UErrorCode* eErrorCode) { michael@0: umtx_initOnce(gIsoCodesInitOnce, &initIsoCodes, *eErrorCode); michael@0: if (U_FAILURE(*eErrorCode)) { michael@0: return FALSE; michael@0: } michael@0: michael@0: IsoCodeEntry* result = (IsoCodeEntry *) uhash_get(gIsoCodes, isoCode); michael@0: if (result == NULL) { michael@0: return FALSE; michael@0: } else if (from > to) { michael@0: *eErrorCode = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return FALSE; michael@0: } else if ((from > result->to) || (to < result->from)) { michael@0: return FALSE; michael@0: } michael@0: return TRUE; michael@0: } michael@0: michael@0: static const icu::Hashtable* getCurrSymbolsEquiv() { michael@0: umtx_initOnce(gCurrSymbolsEquivInitOnce, &initCurrSymbolsEquiv); michael@0: return gCurrSymbolsEquiv; michael@0: } michael@0: michael@0: U_CAPI UEnumeration * U_EXPORT2 michael@0: ucurr_openISOCurrencies(uint32_t currType, UErrorCode *pErrorCode) { michael@0: UEnumeration *myEnum = NULL; michael@0: UCurrencyContext *myContext; michael@0: michael@0: myEnum = (UEnumeration*)uprv_malloc(sizeof(UEnumeration)); michael@0: if (myEnum == NULL) { michael@0: *pErrorCode = U_MEMORY_ALLOCATION_ERROR; michael@0: return NULL; michael@0: } michael@0: uprv_memcpy(myEnum, &gEnumCurrencyList, sizeof(UEnumeration)); michael@0: myContext = (UCurrencyContext*)uprv_malloc(sizeof(UCurrencyContext)); michael@0: if (myContext == NULL) { michael@0: *pErrorCode = U_MEMORY_ALLOCATION_ERROR; michael@0: uprv_free(myEnum); michael@0: return NULL; michael@0: } michael@0: myContext->currType = currType; michael@0: myContext->listIdx = 0; michael@0: myEnum->context = myContext; michael@0: return myEnum; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucurr_countCurrencies(const char* locale, michael@0: UDate date, michael@0: UErrorCode* ec) michael@0: { michael@0: int32_t currCount = 0; michael@0: michael@0: if (ec != NULL && U_SUCCESS(*ec)) michael@0: { michael@0: // local variables michael@0: UErrorCode localStatus = U_ZERO_ERROR; michael@0: char id[ULOC_FULLNAME_CAPACITY]; michael@0: uloc_getKeywordValue(locale, "currency", id, ULOC_FULLNAME_CAPACITY, &localStatus); michael@0: // get country or country_variant in `id' michael@0: /*uint32_t variantType =*/ idForLocale(locale, id, sizeof(id), ec); michael@0: michael@0: if (U_FAILURE(*ec)) michael@0: { michael@0: return 0; michael@0: } michael@0: michael@0: // Remove variants, which is only needed for registration. michael@0: char *idDelim = strchr(id, VAR_DELIM); michael@0: if (idDelim) michael@0: { michael@0: idDelim[0] = 0; michael@0: } michael@0: michael@0: // Look up the CurrencyMap element in the root bundle. michael@0: UResourceBundle *rb = ures_openDirect(U_ICUDATA_CURR, CURRENCY_DATA, &localStatus); michael@0: UResourceBundle *cm = ures_getByKey(rb, CURRENCY_MAP, rb, &localStatus); michael@0: michael@0: // Using the id derived from the local, get the currency data michael@0: UResourceBundle *countryArray = ures_getByKey(rb, id, cm, &localStatus); michael@0: michael@0: // process each currency to see which one is valid for the given date michael@0: if (U_SUCCESS(localStatus)) michael@0: { michael@0: for (int32_t i=0; i 2) michael@0: { michael@0: int32_t toLength = 0; michael@0: UResourceBundle *toRes = ures_getByKey(currencyRes, "to", NULL, &localStatus); michael@0: const int32_t *toArray = ures_getIntVector(toRes, &toLength, &localStatus); michael@0: michael@0: currDate64 = (int64_t)toArray[0] << 32; michael@0: currDate64 |= ((int64_t)toArray[1] & (int64_t)INT64_C(0x00000000FFFFFFFF)); michael@0: UDate toDate = (UDate)currDate64; michael@0: michael@0: if ((fromDate <= date) && (date < toDate)) michael@0: { michael@0: currCount++; michael@0: } michael@0: michael@0: ures_close(toRes); michael@0: } michael@0: else michael@0: { michael@0: if (fromDate <= date) michael@0: { michael@0: currCount++; michael@0: } michael@0: } michael@0: michael@0: // close open resources michael@0: ures_close(currencyRes); michael@0: ures_close(fromRes); michael@0: michael@0: } // end For loop michael@0: } // end if (U_SUCCESS(localStatus)) michael@0: michael@0: ures_close(countryArray); michael@0: michael@0: // Check for errors michael@0: if (*ec == U_ZERO_ERROR || localStatus != U_ZERO_ERROR) michael@0: { michael@0: // There is nothing to fallback to. michael@0: // Report the failure/warning if possible. michael@0: *ec = localStatus; michael@0: } michael@0: michael@0: if (U_SUCCESS(*ec)) michael@0: { michael@0: // no errors michael@0: return currCount; michael@0: } michael@0: michael@0: } michael@0: michael@0: // If we got here, either error code is invalid or michael@0: // some argument passed is no good. michael@0: return 0; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucurr_forLocaleAndDate(const char* locale, michael@0: UDate date, michael@0: int32_t index, michael@0: UChar* buff, michael@0: int32_t buffCapacity, michael@0: UErrorCode* ec) michael@0: { michael@0: int32_t resLen = 0; michael@0: int32_t currIndex = 0; michael@0: const UChar* s = NULL; michael@0: michael@0: if (ec != NULL && U_SUCCESS(*ec)) michael@0: { michael@0: // check the arguments passed michael@0: if ((buff && buffCapacity) || !buffCapacity ) michael@0: { michael@0: // local variables michael@0: UErrorCode localStatus = U_ZERO_ERROR; michael@0: char id[ULOC_FULLNAME_CAPACITY]; michael@0: resLen = uloc_getKeywordValue(locale, "currency", id, ULOC_FULLNAME_CAPACITY, &localStatus); michael@0: michael@0: // get country or country_variant in `id' michael@0: /*uint32_t variantType =*/ idForLocale(locale, id, sizeof(id), ec); michael@0: if (U_FAILURE(*ec)) michael@0: { michael@0: return 0; michael@0: } michael@0: michael@0: // Remove variants, which is only needed for registration. michael@0: char *idDelim = strchr(id, VAR_DELIM); michael@0: if (idDelim) michael@0: { michael@0: idDelim[0] = 0; michael@0: } michael@0: michael@0: // Look up the CurrencyMap element in the root bundle. michael@0: UResourceBundle *rb = ures_openDirect(U_ICUDATA_CURR, CURRENCY_DATA, &localStatus); michael@0: UResourceBundle *cm = ures_getByKey(rb, CURRENCY_MAP, rb, &localStatus); michael@0: michael@0: // Using the id derived from the local, get the currency data michael@0: UResourceBundle *countryArray = ures_getByKey(rb, id, cm, &localStatus); michael@0: michael@0: // process each currency to see which one is valid for the given date michael@0: bool matchFound = false; michael@0: if (U_SUCCESS(localStatus)) michael@0: { michael@0: if ((index <= 0) || (index> ures_getSize(countryArray))) michael@0: { michael@0: // requested index is out of bounds michael@0: ures_close(countryArray); michael@0: return 0; michael@0: } michael@0: michael@0: for (int32_t i=0; i 2) michael@0: { michael@0: int32_t toLength = 0; michael@0: UResourceBundle *toRes = ures_getByKey(currencyRes, "to", NULL, &localStatus); michael@0: const int32_t *toArray = ures_getIntVector(toRes, &toLength, &localStatus); michael@0: michael@0: currDate64 = (int64_t)toArray[0] << 32; michael@0: currDate64 |= ((int64_t)toArray[1] & (int64_t)INT64_C(0x00000000FFFFFFFF)); michael@0: UDate toDate = (UDate)currDate64; michael@0: michael@0: if ((fromDate <= date) && (date < toDate)) michael@0: { michael@0: currIndex++; michael@0: if (currIndex == index) michael@0: { michael@0: matchFound = true; michael@0: } michael@0: } michael@0: michael@0: ures_close(toRes); michael@0: } michael@0: else michael@0: { michael@0: if (fromDate <= date) michael@0: { michael@0: currIndex++; michael@0: if (currIndex == index) michael@0: { michael@0: matchFound = true; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // close open resources michael@0: ures_close(currencyRes); michael@0: ures_close(fromRes); michael@0: michael@0: // check for loop exit michael@0: if (matchFound) michael@0: { michael@0: break; michael@0: } michael@0: michael@0: } // end For loop michael@0: } michael@0: michael@0: ures_close(countryArray); michael@0: michael@0: // Check for errors michael@0: if (*ec == U_ZERO_ERROR || localStatus != U_ZERO_ERROR) michael@0: { michael@0: // There is nothing to fallback to. michael@0: // Report the failure/warning if possible. michael@0: *ec = localStatus; michael@0: } michael@0: michael@0: if (U_SUCCESS(*ec)) michael@0: { michael@0: // no errors michael@0: if((buffCapacity> resLen) && matchFound) michael@0: { michael@0: // write out the currency value michael@0: u_strcpy(buff, s); michael@0: } michael@0: else michael@0: { michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: // return null terminated currency string michael@0: return u_terminateUChars(buff, buffCapacity, resLen, ec); michael@0: } michael@0: else michael@0: { michael@0: // illegal argument encountered michael@0: *ec = U_ILLEGAL_ARGUMENT_ERROR; michael@0: } michael@0: michael@0: } michael@0: michael@0: // If we got here, either error code is invalid or michael@0: // some argument passed is no good. michael@0: return resLen; michael@0: } michael@0: michael@0: static const UEnumeration defaultKeywordValues = { michael@0: NULL, michael@0: NULL, michael@0: ulist_close_keyword_values_iterator, michael@0: ulist_count_keyword_values, michael@0: uenum_unextDefault, michael@0: ulist_next_keyword_value, michael@0: ulist_reset_keyword_values_iterator michael@0: }; michael@0: michael@0: U_CAPI UEnumeration *U_EXPORT2 ucurr_getKeywordValuesForLocale(const char *key, const char *locale, UBool commonlyUsed, UErrorCode* status) { michael@0: // Resolve region michael@0: char prefRegion[ULOC_FULLNAME_CAPACITY] = ""; michael@0: int32_t prefRegionLength = 0; michael@0: prefRegionLength = uloc_getCountry(locale, prefRegion, sizeof(prefRegion), status); michael@0: if (prefRegionLength == 0) { michael@0: char loc[ULOC_FULLNAME_CAPACITY] = ""; michael@0: uloc_addLikelySubtags(locale, loc, sizeof(loc), status); michael@0: michael@0: prefRegionLength = uloc_getCountry(loc, prefRegion, sizeof(prefRegion), status); michael@0: } michael@0: michael@0: // Read value from supplementalData michael@0: UList *values = ulist_createEmptyList(status); michael@0: UList *otherValues = ulist_createEmptyList(status); michael@0: UEnumeration *en = (UEnumeration *)uprv_malloc(sizeof(UEnumeration)); michael@0: if (U_FAILURE(*status) || en == NULL) { michael@0: if (en == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: } else { michael@0: uprv_free(en); michael@0: } michael@0: ulist_deleteList(values); michael@0: ulist_deleteList(otherValues); michael@0: return NULL; michael@0: } michael@0: memcpy(en, &defaultKeywordValues, sizeof(UEnumeration)); michael@0: en->context = values; michael@0: michael@0: UResourceBundle *bundle = ures_openDirect(U_ICUDATA_CURR, "supplementalData", status); michael@0: ures_getByKey(bundle, "CurrencyMap", bundle, status); michael@0: UResourceBundle bundlekey, regbndl, curbndl, to; michael@0: ures_initStackObject(&bundlekey); michael@0: ures_initStackObject(®bndl); michael@0: ures_initStackObject(&curbndl); michael@0: ures_initStackObject(&to); michael@0: michael@0: while (U_SUCCESS(*status) && ures_hasNext(bundle)) { michael@0: ures_getNextResource(bundle, &bundlekey, status); michael@0: if (U_FAILURE(*status)) { michael@0: break; michael@0: } michael@0: const char *region = ures_getKey(&bundlekey); michael@0: UBool isPrefRegion = uprv_strcmp(region, prefRegion) == 0 ? TRUE : FALSE; michael@0: if (!isPrefRegion && commonlyUsed) { michael@0: // With commonlyUsed=true, we do not put michael@0: // currencies for other regions in the michael@0: // result list. michael@0: continue; michael@0: } michael@0: ures_getByKey(bundle, region, ®bndl, status); michael@0: if (U_FAILURE(*status)) { michael@0: break; michael@0: } michael@0: while (U_SUCCESS(*status) && ures_hasNext(®bndl)) { michael@0: ures_getNextResource(®bndl, &curbndl, status); michael@0: if (ures_getType(&curbndl) != URES_TABLE) { michael@0: // Currently, an empty ARRAY is mixed in. michael@0: continue; michael@0: } michael@0: char *curID = (char *)uprv_malloc(sizeof(char) * ULOC_KEYWORDS_CAPACITY); michael@0: int32_t curIDLength = ULOC_KEYWORDS_CAPACITY; michael@0: if (curID == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: break; michael@0: } michael@0: michael@0: #if U_CHARSET_FAMILY==U_ASCII_FAMILY michael@0: ures_getUTF8StringByKey(&curbndl, "id", curID, &curIDLength, TRUE, status); michael@0: /* optimize - use the utf-8 string */ michael@0: #else michael@0: { michael@0: const UChar* defString = ures_getStringByKey(&curbndl, "id", &curIDLength, status); michael@0: if(U_SUCCESS(*status)) { michael@0: if(curIDLength+1 > ULOC_KEYWORDS_CAPACITY) { michael@0: *status = U_BUFFER_OVERFLOW_ERROR; michael@0: } else { michael@0: u_UCharsToChars(defString, curID, curIDLength+1); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: if (U_FAILURE(*status)) { michael@0: break; michael@0: } michael@0: UBool hasTo = FALSE; michael@0: ures_getByKey(&curbndl, "to", &to, status); michael@0: if (U_FAILURE(*status)) { michael@0: // Do nothing here... michael@0: *status = U_ZERO_ERROR; michael@0: } else { michael@0: hasTo = TRUE; michael@0: } michael@0: if (isPrefRegion && !hasTo && !ulist_containsString(values, curID, (int32_t)uprv_strlen(curID))) { michael@0: // Currently active currency for the target country michael@0: ulist_addItemEndList(values, curID, TRUE, status); michael@0: } else if (!ulist_containsString(otherValues, curID, (int32_t)uprv_strlen(curID)) && !commonlyUsed) { michael@0: ulist_addItemEndList(otherValues, curID, TRUE, status); michael@0: } else { michael@0: uprv_free(curID); michael@0: } michael@0: } michael@0: michael@0: } michael@0: if (U_SUCCESS(*status)) { michael@0: if (commonlyUsed) { michael@0: if (ulist_getListSize(values) == 0) { michael@0: // This could happen if no valid region is supplied in the input michael@0: // locale. In this case, we use the CLDR's default. michael@0: uenum_close(en); michael@0: en = ucurr_getKeywordValuesForLocale(key, "und", TRUE, status); michael@0: } michael@0: } else { michael@0: // Consolidate the list michael@0: char *value = NULL; michael@0: ulist_resetList(otherValues); michael@0: while ((value = (char *)ulist_getNext(otherValues)) != NULL) { michael@0: if (!ulist_containsString(values, value, (int32_t)uprv_strlen(value))) { michael@0: char *tmpValue = (char *)uprv_malloc(sizeof(char) * ULOC_KEYWORDS_CAPACITY); michael@0: uprv_memcpy(tmpValue, value, uprv_strlen(value) + 1); michael@0: ulist_addItemEndList(values, tmpValue, TRUE, status); michael@0: if (U_FAILURE(*status)) { michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: ulist_resetList((UList *)(en->context)); michael@0: } else { michael@0: ulist_deleteList(values); michael@0: uprv_free(en); michael@0: values = NULL; michael@0: en = NULL; michael@0: } michael@0: ures_close(&to); michael@0: ures_close(&curbndl); michael@0: ures_close(®bndl); michael@0: ures_close(&bundlekey); michael@0: ures_close(bundle); michael@0: michael@0: ulist_deleteList(otherValues); michael@0: michael@0: return en; michael@0: } michael@0: michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucurr_getNumericCode(const UChar* currency) { michael@0: int32_t code = 0; michael@0: if (currency && u_strlen(currency) == ISO_CURRENCY_CODE_LENGTH) { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: michael@0: UResourceBundle *bundle = ures_openDirect(0, "currencyNumericCodes", &status); michael@0: ures_getByKey(bundle, "codeMap", bundle, &status); michael@0: if (U_SUCCESS(status)) { michael@0: char alphaCode[ISO_CURRENCY_CODE_LENGTH+1]; michael@0: myUCharsToChars(alphaCode, currency); michael@0: T_CString_toUpperCase(alphaCode); michael@0: ures_getByKey(bundle, alphaCode, bundle, &status); michael@0: int tmpCode = ures_getInt(bundle, &status); michael@0: if (U_SUCCESS(status)) { michael@0: code = tmpCode; michael@0: } michael@0: } michael@0: ures_close(bundle); michael@0: } michael@0: return code; michael@0: } michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */ michael@0: michael@0: //eof