static UResourceBundle * measurementTypeBundleForLocale(const char *localeID, const char *measurementType, UErrorCode *status){ char region[ULOC_COUNTRY_CAPACITY]; UResourceBundle *rb; UResourceBundle *measTypeBundle = NULL; ulocimp_getRegionForSupplementalData(localeID, TRUE, region, ULOC_COUNTRY_CAPACITY, status); rb = ures_openDirect(NULL, "supplementalData", status); ures_getByKey(rb, "measurementData", rb, status); if (rb != NULL) { UResourceBundle *measDataBundle = ures_getByKey(rb, region, NULL, status); if (U_SUCCESS(*status)) { measTypeBundle = ures_getByKey(measDataBundle, measurementType, NULL, status); } if (*status == U_MISSING_RESOURCE_ERROR) { *status = U_ZERO_ERROR; if (measDataBundle != NULL) { ures_close(measDataBundle); } measDataBundle = ures_getByKey(rb, "001", NULL, status); measTypeBundle = ures_getByKey(measDataBundle, measurementType, NULL, status); } ures_close(measDataBundle); } ures_close(rb); return measTypeBundle; }
U_CAPI void U_EXPORT2 ulocdata_getCLDRVersion(UVersionInfo versionArray, UErrorCode *status) { UResourceBundle *rb = NULL; rb = ures_openDirect(NULL, "supplementalData", status); ures_getVersionByKey(rb, "cldrVersion", versionArray, status); ures_close(rb); }
const UChar* ZoneMeta::getShortIDFromCanonical(const UChar* canonicalID) { const UChar* shortID = NULL; int32_t len = u_strlen(canonicalID); char tzidKey[ZID_KEY_MAX + 1]; u_UCharsToChars(canonicalID, tzidKey, len); tzidKey[len] = (char) 0; // Make sure it is null terminated. // replace '/' with ':' char *p = tzidKey; while (*p++) { if (*p == '/') { *p = ':'; } } UErrorCode status = U_ZERO_ERROR; UResourceBundle *rb = ures_openDirect(NULL, gKeyTypeData, &status); ures_getByKey(rb, gTypeMapTag, rb, &status); ures_getByKey(rb, gTimezoneTag, rb, &status); shortID = ures_getStringByKey(rb, tzidKey, NULL, &status); ures_close(rb); return shortID; }
UnicodeString& U_EXPORT2 ZoneMeta::getSingleCountry(const UnicodeString &tzid, UnicodeString &country) { UErrorCode status = U_ZERO_ERROR; // Get canonical country for the zone getCanonicalCountry(tzid, country); if (!country.isEmpty()) { UResourceBundle *supplementalDataBundle = ures_openDirect(NULL, gSupplementalData, &status); UResourceBundle *zoneFormatting = ures_getByKey(supplementalDataBundle, gZoneFormattingTag, NULL, &status); UResourceBundle *multizone = ures_getByKey(zoneFormatting, gMultizoneTag, NULL, &status); if (U_SUCCESS(status)) { while (ures_hasNext(multizone)) { int32_t len; const UChar* multizoneCountry = ures_getNextString(multizone, &len, NULL, &status); if (country.compare(multizoneCountry, len) == 0) { // Included in the multizone country list country.remove(); break; } } } ures_close(multizone); ures_close(zoneFormatting); ures_close(supplementalDataBundle); } return country; }
static void U_CALLCONV initAvailableMetaZoneIDs () { U_ASSERT(gMetaZoneIDs == NULL); U_ASSERT(gMetaZoneIDTable == NULL); ucln_i18n_registerCleanup(UCLN_I18N_ZONEMETA, zoneMeta_cleanup); UErrorCode status = U_ZERO_ERROR; gMetaZoneIDTable = uhash_open(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, &status); if (U_FAILURE(status) || gMetaZoneIDTable == NULL) { gMetaZoneIDTable = NULL; return; } uhash_setKeyDeleter(gMetaZoneIDTable, uprv_deleteUObject); // No valueDeleter, because the vector maintain the value objects gMetaZoneIDs = new UVector(NULL, uhash_compareUChars, status); if (U_FAILURE(status) || gMetaZoneIDs == NULL) { gMetaZoneIDs = NULL; uhash_close(gMetaZoneIDTable); gMetaZoneIDTable = NULL; return; } gMetaZoneIDs->setDeleter(uprv_free); UResourceBundle *rb = ures_openDirect(NULL, gMetaZones, &status); UResourceBundle *bundle = ures_getByKey(rb, gMapTimezonesTag, NULL, &status); UResourceBundle res; ures_initStackObject(&res); while (U_SUCCESS(status) && ures_hasNext(bundle)) { ures_getNextResource(bundle, &res, &status); if (U_FAILURE(status)) { break; } const char *mzID = ures_getKey(&res); int32_t len = uprv_strlen(mzID); UChar *uMzID = (UChar*)uprv_malloc(sizeof(UChar) * (len + 1)); if (uMzID == NULL) { status = U_MEMORY_ALLOCATION_ERROR; break; } u_charsToUChars(mzID, uMzID, len); uMzID[len] = 0; UnicodeString *usMzID = new UnicodeString(uMzID); if (uhash_get(gMetaZoneIDTable, usMzID) == NULL) { gMetaZoneIDs->addElement((void *)uMzID, status); uhash_put(gMetaZoneIDTable, (void *)usMzID, (void *)uMzID, &status); } else { uprv_free(uMzID); delete usMzID; } } ures_close(&res); ures_close(bundle); ures_close(rb); if (U_FAILURE(status)) { uhash_close(gMetaZoneIDTable); delete gMetaZoneIDs; gMetaZoneIDTable = NULL; gMetaZoneIDs = NULL; } }
const char* TimeZone::getTZDataVersion(UErrorCode& status) { /* This is here to prevent race conditions. */ UBool needsInit; UMTX_CHECK(&LOCK, !TZDataVersionInitialized, needsInit); if (needsInit) { int32_t len = 0; UResourceBundle *bundle = ures_openDirect(NULL, "zoneinfo", &status); const UChar *tzver = ures_getStringByKey(bundle, "TZVersion", &len, &status); if (U_SUCCESS(status)) { if (len >= (int32_t)sizeof(TZDATA_VERSION)) { // Ensure that there is always space for a trailing nul in TZDATA_VERSION len = sizeof(TZDATA_VERSION) - 1; } umtx_lock(&LOCK); if (!TZDataVersionInitialized) { u_UCharsToChars(tzver, TZDATA_VERSION, len); TZDataVersionInitialized = TRUE; } umtx_unlock(&LOCK); ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup); } ures_close(bundle); } if (U_FAILURE(status)) { return NULL; } return (const char*)TZDATA_VERSION; }
static UResourceBundle* openOlsonResource(const UnicodeString& id, UResourceBundle& res, UErrorCode& ec) { #if U_DEBUG_TZ char buf[128]; id.extract(0, sizeof(buf)-1, buf, sizeof(buf), ""); #endif UResourceBundle *top = ures_openDirect(0, kZONEINFO, &ec); U_DEBUG_TZ_MSG(("pre: res sz=%d\n", ures_getSize(&res))); /* &res = */ getZoneByName(top, id, &res, ec); // Dereference if this is an alias. Docs say result should be 1 // but it is 0 in 2.8 (?). U_DEBUG_TZ_MSG(("Loading zone '%s' (%s, size %d) - %s\n", buf, ures_getKey((UResourceBundle*)&res), ures_getSize(&res), u_errorName(ec))); if (ures_getSize(&res) <= 1 && getOlsonMeta(top)) { int32_t deref = ures_getInt(&res, &ec) + 0; U_DEBUG_TZ_MSG(("getInt: %s - type is %d\n", u_errorName(ec), ures_getType(&res))); UResourceBundle *ares = ures_getByKey(top, kZONES, NULL, &ec); // dereference Zones section ures_getByIndex(ares, deref, &res, &ec); ures_close(ares); U_DEBUG_TZ_MSG(("alias to #%d (%s) - %s\n", deref, "??", u_errorName(ec))); } else { U_DEBUG_TZ_MSG(("not an alias - size %d\n", ures_getSize(&res))); } U_DEBUG_TZ_MSG(("%s - final status is %s\n", buf, u_errorName(ec))); return top; }
const UChar* TimeZone::dereferOlsonLink(const UnicodeString& id) { const UChar *result = NULL; UErrorCode ec = U_ZERO_ERROR; UResourceBundle *rb = ures_openDirect(NULL, kZONEINFO, &ec); // resolve zone index by name UResourceBundle *names = ures_getByKey(rb, kNAMES, NULL, &ec); int32_t idx = findInStringArray(names, id, ec); result = ures_getStringByIndex(names, idx, NULL, &ec); // open the zone bundle by index ures_getByKey(rb, kZONES, rb, &ec); ures_getByIndex(rb, idx, rb, &ec); if (U_SUCCESS(ec)) { if (ures_getType(rb) == URES_INT) { // this is a link - dereference the link int32_t deref = ures_getInt(rb, &ec); const UChar* tmp = ures_getStringByIndex(names, deref, NULL, &ec); if (U_SUCCESS(ec)) { result = tmp; } } } ures_close(names); ures_close(rb); return result; }
NumberingSystem* U_EXPORT2 NumberingSystem::createInstanceByName(const char *name, UErrorCode& status) { UResourceBundle *numberingSystemsInfo = NULL; UResourceBundle *nsTop, *nsCurrent; int32_t radix = 10; int32_t algorithmic = 0; numberingSystemsInfo = ures_openDirect(NULL,gNumberingSystems, &status); nsCurrent = ures_getByKey(numberingSystemsInfo,gNumberingSystems,NULL,&status); nsTop = ures_getByKey(nsCurrent,name,NULL,&status); UnicodeString nsd = ures_getUnicodeStringByKey(nsTop,gDesc,&status); ures_getByKey(nsTop,gRadix,nsCurrent,&status); radix = ures_getInt(nsCurrent,&status); ures_getByKey(nsTop,gAlgorithmic,nsCurrent,&status); algorithmic = ures_getInt(nsCurrent,&status); UBool isAlgorithmic = ( algorithmic == 1 ); ures_close(nsCurrent); ures_close(nsTop); ures_close(numberingSystemsInfo); if (U_FAILURE(status)) { status = U_UNSUPPORTED_ERROR; return NULL; } NumberingSystem* ns = NumberingSystem::createInstance(radix,isAlgorithmic,nsd,status); ns->setName(name); return ns; }
static void U_CALLCONV initAvailableLocaleList(UErrorCode &status) { U_ASSERT(availableLocaleListCount == 0); U_ASSERT(availableLocaleList == NULL); // for now, there is a hardcoded list, so just walk through that list and set it up. UResourceBundle *index = NULL; UResourceBundle installed; int32_t i = 0; ures_initStackObject(&installed); index = ures_openDirect(U_ICUDATA_COLL, "res_index", &status); ures_getByKey(index, "InstalledLocales", &installed, &status); if(U_SUCCESS(status)) { availableLocaleListCount = ures_getSize(&installed); availableLocaleList = new Locale[availableLocaleListCount]; if (availableLocaleList != NULL) { ures_resetIterator(&installed); while(ures_hasNext(&installed)) { const char *tempKey = NULL; ures_getNextString(&installed, NULL, &tempKey, &status); availableLocaleList[i++] = Locale(tempKey); } } U_ASSERT(availableLocaleListCount == i); ures_close(&installed); } ures_close(index); ucln_i18n_registerCleanup(UCLN_I18N_COLLATOR, collator_cleanup); }
static UResourceBundle * measurementTypeBundleForLocale(const char *localeID, const char *measurementType, UErrorCode *status){ char fullLoc[ULOC_FULLNAME_CAPACITY]; char region[ULOC_COUNTRY_CAPACITY]; UResourceBundle *rb; UResourceBundle *measTypeBundle = NULL; /* The following code is basically copied from Calendar::setWeekData and * Calendar::getCalendarTypeForLocale with adjustments for resource name */ uloc_addLikelySubtags(localeID, fullLoc, ULOC_FULLNAME_CAPACITY, status); uloc_getCountry(fullLoc, region, ULOC_COUNTRY_CAPACITY, status); rb = ures_openDirect(NULL, "supplementalData", status); ures_getByKey(rb, "measurementData", rb, status); if (rb != NULL) { UResourceBundle *measDataBundle = ures_getByKey(rb, region, NULL, status); if (U_SUCCESS(*status)) { measTypeBundle = ures_getByKey(measDataBundle, measurementType, NULL, status); } if (*status == U_MISSING_RESOURCE_ERROR) { *status = U_ZERO_ERROR; if (measDataBundle != NULL) { ures_close(measDataBundle); } measDataBundle = ures_getByKey(rb, "001", NULL, status); measTypeBundle = ures_getByKey(measDataBundle, measurementType, NULL, status); } ures_close(measDataBundle); } ures_close(rb); return measTypeBundle; }
/* {{{ ResourceBundle_ctor */ static int resourcebundle_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_constructor) { const char *bundlename; size_t bundlename_len = 0; const char *locale; size_t locale_len = 0; zend_bool fallback = 1; int zpp_flags = is_constructor ? ZEND_PARSE_PARAMS_THROW : 0; zval *object = return_value; ResourceBundle_object *rb = Z_INTL_RESOURCEBUNDLE_P( object ); intl_error_reset( NULL ); if( zend_parse_parameters_ex( zpp_flags, ZEND_NUM_ARGS(), "s!s!|b", &locale, &locale_len, &bundlename, &bundlename_len, &fallback ) == FAILURE ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "resourcebundle_ctor: unable to parse input parameters", 0 ); return FAILURE; } INTL_CHECK_LOCALE_LEN_OR_FAILURE(locale_len); if (locale == NULL) { locale = intl_locale_get_default(); } if (bundlename_len >= MAXPATHLEN) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "Bundle name too long", 0 ); zval_ptr_dtor(return_value); ZVAL_NULL(return_value); return FAILURE; } if (fallback) { rb->me = ures_open(bundlename, locale, &INTL_DATA_ERROR_CODE(rb)); } else { rb->me = ures_openDirect(bundlename, locale, &INTL_DATA_ERROR_CODE(rb)); } INTL_CTOR_CHECK_STATUS(rb, "resourcebundle_ctor: Cannot load libICU resource bundle"); if (!fallback && (INTL_DATA_ERROR_CODE(rb) == U_USING_FALLBACK_WARNING || INTL_DATA_ERROR_CODE(rb) == U_USING_DEFAULT_WARNING)) { char *pbuf; intl_errors_set_code(NULL, INTL_DATA_ERROR_CODE(rb)); spprintf(&pbuf, 0, "resourcebundle_ctor: Cannot load libICU resource " "'%s' without fallback from %s to %s", bundlename ? bundlename : "(default data)", locale, ures_getLocaleByType( rb->me, ULOC_ACTUAL_LOCALE, &INTL_DATA_ERROR_CODE(rb))); intl_errors_set_custom_msg(INTL_DATA_ERROR_P(rb), pbuf, 1); efree(pbuf); return FAILURE; } return SUCCESS; }
TZEnumeration(const char* country) : map(NULL), len(0), pos(0) { if (!getOlsonMeta()) { return; } UErrorCode ec = U_ZERO_ERROR; UResourceBundle *res = ures_openDirect(0, kZONEINFO, &ec); ures_getByKey(res, kREGIONS, res, &ec); if (U_SUCCESS(ec) && ures_getType(res) == URES_ARRAY) { UChar uCountry[] = {0, 0, 0, 0}; if (country) { u_charsToUChars(country, uCountry, 2); } else { u_strcpy(uCountry, WORLD); } // count matches int32_t count = 0; int32_t i; const UChar *region; for (i = 0; i < ures_getSize(res); i++) { region = ures_getStringByIndex(res, i, NULL, &ec); if (U_FAILURE(ec)) { break; } if (u_strcmp(uCountry, region) == 0) { count++; } } if (count > 0) { map = (int32_t*)uprv_malloc(sizeof(int32_t) * count); if (map != NULL) { int32_t idx = 0; for (i = 0; i < ures_getSize(res); i++) { region = ures_getStringByIndex(res, i, NULL, &ec); if (U_FAILURE(ec)) { break; } if (u_strcmp(uCountry, region) == 0) { map[idx++] = i; } } if (U_SUCCESS(ec)) { len = count; } else { uprv_free(map); map = NULL; } } else { U_DEBUG_TZ_MSG(("Failed to load tz for region %s: %s\n", country, u_errorName(ec))); } } } ures_close(res); }
static UBool getOlsonMeta() { if (OLSON_ZONE_COUNT == 0) { UErrorCode ec = U_ZERO_ERROR; UResourceBundle *top = ures_openDirect(0, kZONEINFO, &ec); if (U_SUCCESS(ec)) { getOlsonMeta(top); } ures_close(top); } return (OLSON_ZONE_COUNT > 0); }
/** * Internal function to look up currency data. Result is an array of * two integers. The first is the fraction digits. The second is the * rounding increment, or 0 if none. The rounding increment is in * units of 10^(-fraction_digits). */ static const int32_t* _findMetaData(const UChar* currency, UErrorCode& ec) { if (currency == 0 || *currency == 0) { if (U_SUCCESS(ec)) { ec = U_ILLEGAL_ARGUMENT_ERROR; } return LAST_RESORT_DATA; } // Get CurrencyMeta resource out of root locale file. [This may // move out of the root locale file later; if it does, update this // code.] UResourceBundle* currencyData = ures_openDirect(NULL, CURRENCY_DATA, &ec); UResourceBundle* currencyMeta = ures_getByKey(currencyData, CURRENCY_META, currencyData, &ec); if (U_FAILURE(ec)) { ures_close(currencyMeta); // Config/build error; return hard-coded defaults return LAST_RESORT_DATA; } // Look up our currency, or if that's not available, then DEFAULT char buf[ISO_COUNTRY_CODE_LENGTH+1]; UErrorCode ec2 = U_ZERO_ERROR; // local error code: soft failure UResourceBundle* rb = ures_getByKey(currencyMeta, myUCharsToChars(buf, currency), NULL, &ec2); if (U_FAILURE(ec2)) { ures_close(rb); rb = ures_getByKey(currencyMeta,DEFAULT_META, NULL, &ec); if (U_FAILURE(ec)) { ures_close(currencyMeta); ures_close(rb); // Config/build error; return hard-coded defaults return LAST_RESORT_DATA; } } int32_t len; const int32_t *data = ures_getIntVector(rb, &len, &ec); if (U_FAILURE(ec) || len != 2) { // Config/build error; return hard-coded defaults if (U_SUCCESS(ec)) { ec = U_INVALID_FORMAT_ERROR; } ures_close(currencyMeta); ures_close(rb); return LAST_RESORT_DATA; } ures_close(currencyMeta); ures_close(rb); return data; }
static UBool loadOlsonIDs() { if (OLSON_IDS != 0) { return TRUE; } UErrorCode ec = U_ZERO_ERROR; UnicodeString* ids = 0; int32_t count = 0; UResourceBundle *top = ures_openDirect(0, kZONEINFO, &ec); UResourceBundle *nres = ures_getByKey(top, kNAMES, NULL, &ec); // dereference Names section if (U_SUCCESS(ec)) { getOlsonMeta(top); int32_t start = 0; count = ures_getSize(nres); ids = new UnicodeString[(count > 0) ? count : 1]; // Null pointer check if (ids != NULL) { for (int32_t i=0; i<count; ++i) { int32_t idLen = 0; const UChar* id = ures_getStringByIndex(nres, i, &idLen, &ec); ids[i].fastCopyFrom(UnicodeString(TRUE, id, idLen)); if (U_FAILURE(ec)) { break; } } } else { ec = U_MEMORY_ALLOCATION_ERROR; } } ures_close(nres); ures_close(top); if (U_FAILURE(ec)) { delete[] ids; return FALSE; } // Keep mutexed operations as short as possible by doing all // computations first, then doing pointer copies within the mutex. umtx_lock(&LOCK); if (OLSON_IDS == 0) { OLSON_IDS = ids; ids = 0; ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup); } umtx_unlock(&LOCK); // If another thread initialized the statics first, then delete // our unused data. delete[] ids; return TRUE; }
static UBool isAvailableLocaleListInitialized(UErrorCode &status) { // for now, there is a hardcoded list, so just walk through that list and set it up. UBool needInit; UMTX_CHECK(NULL, availableLocaleList == NULL, needInit); if (needInit) { UResourceBundle *index = NULL; UResourceBundle installed; Locale * temp; int32_t i = 0; int32_t localeCount; ures_initStackObject(&installed); index = ures_openDirect(U_ICUDATA_COLL, "res_index", &status); ures_getByKey(index, "InstalledLocales", &installed, &status); if(U_SUCCESS(status)) { localeCount = ures_getSize(&installed); temp = new Locale[localeCount]; if (temp != NULL) { ures_resetIterator(&installed); while(ures_hasNext(&installed)) { const char *tempKey = NULL; ures_getNextString(&installed, NULL, &tempKey, &status); temp[i++] = Locale(tempKey); } umtx_lock(NULL); if (availableLocaleList == NULL) { availableLocaleListCount = localeCount; availableLocaleList = temp; temp = NULL; ucln_i18n_registerCleanup(UCLN_I18N_COLLATOR, collator_cleanup); } umtx_unlock(NULL); needInit = FALSE; if (temp) { delete []temp; } } ures_close(&installed); } ures_close(index); } return !needInit; }
U_CAPI void U_EXPORT2 u_getDataVersion(UVersionInfo dataVersionFillin, UErrorCode *status) { UResourceBundle *icudatares = NULL; if (U_FAILURE(*status)) { return; } if (dataVersionFillin != NULL) { icudatares = ures_openDirect(NULL, U_ICU_VERSION_BUNDLE , status); if (U_SUCCESS(*status)) { ures_getVersionByKey(icudatares, U_ICU_DATA_KEY, dataVersionFillin, status); } ures_close(icudatares); } }
static jstring getCurrencyCodeNative(JNIEnv* env, jclass clazz, jstring key) { UErrorCode status = U_ZERO_ERROR; ScopedResourceBundle supplData(ures_openDirect(NULL, "supplementalData", &status)); if (U_FAILURE(status)) { return NULL; } ScopedResourceBundle currencyMap(ures_getByKey(supplData.get(), "CurrencyMap", NULL, &status)); if (U_FAILURE(status)) { return NULL; } const char* keyChars = env->GetStringUTFChars(key, NULL); ScopedResourceBundle currency(ures_getByKey(currencyMap.get(), keyChars, NULL, &status)); env->ReleaseStringUTFChars(key, keyChars); if (U_FAILURE(status)) { return NULL; } ScopedResourceBundle currencyElem(ures_getByIndex(currency.get(), 0, NULL, &status)); if (U_FAILURE(status)) { return env->NewStringUTF("None"); } // check if there is a 'to' date. If there is, the currency isn't used anymore. ScopedResourceBundle currencyTo(ures_getByKey(currencyElem.get(), "to", NULL, &status)); if (!U_FAILURE(status)) { // return and let the caller throw an exception return NULL; } // We need to reset 'status'. It works like errno in that ICU doesn't set it // to U_ZERO_ERROR on success: it only touches it on error, and the test // above means it now holds a failure code. status = U_ZERO_ERROR; ScopedResourceBundle currencyId(ures_getByKey(currencyElem.get(), "id", NULL, &status)); if (U_FAILURE(status)) { // No id defined for this country return env->NewStringUTF("None"); } int length; const jchar* id = ures_getString(currencyId.get(), &length, &status); if (U_FAILURE(status) || length == 0) { return env->NewStringUTF("None"); } return env->NewString(id, length); }
UBool getID(int32_t i) { UErrorCode ec = U_ZERO_ERROR; int32_t idLen = 0; const UChar* id = NULL; UResourceBundle *top = ures_openDirect(0, kZONEINFO, &ec); top = ures_getByKey(top, kNAMES, top, &ec); // dereference Zones section id = ures_getStringByIndex(top, i, &idLen, &ec); if(U_FAILURE(ec)) { unistr.truncate(0); } else { unistr.fastCopyFrom(UnicodeString(TRUE, id, idLen)); } ures_close(top); return U_SUCCESS(ec); }
StringEnumeration* NumberingSystem::getAvailableNames(UErrorCode &status) { // TODO(ticket #11908): Init-once static cache, with u_cleanup() callback. static StringEnumeration* availableNames = NULL; if (U_FAILURE(status)) { return NULL; } if ( availableNames == NULL ) { // TODO: Simple array of UnicodeString objects, based on length of table resource? LocalPointer<UVector> numsysNames(new UVector(uprv_deleteUObject, NULL, status), status); if (U_FAILURE(status)) { return NULL; } UErrorCode rbstatus = U_ZERO_ERROR; UResourceBundle *numberingSystemsInfo = ures_openDirect(NULL, "numberingSystems", &rbstatus); numberingSystemsInfo = ures_getByKey(numberingSystemsInfo,"numberingSystems",numberingSystemsInfo,&rbstatus); if(U_FAILURE(rbstatus)) { status = U_MISSING_RESOURCE_ERROR; ures_close(numberingSystemsInfo); return NULL; } while ( ures_hasNext(numberingSystemsInfo) ) { UResourceBundle *nsCurrent = ures_getNextResource(numberingSystemsInfo,NULL,&rbstatus); const char *nsName = ures_getKey(nsCurrent); numsysNames->addElement(new UnicodeString(nsName, -1, US_INV),status); ures_close(nsCurrent); } ures_close(numberingSystemsInfo); if (U_FAILURE(status)) { return NULL; } availableNames = new NumsysNameEnumeration(numsysNames.getAlias(), status); if (availableNames == NULL) { status = U_MEMORY_ALLOCATION_ERROR; return NULL; } numsysNames.orphan(); // The names got adopted. } return availableNames; }
UnicodeString& U_EXPORT2 ZoneMeta::getZoneIdByMetazone(const UnicodeString &mzid, const UnicodeString ®ion, UnicodeString &result) { UErrorCode status = U_ZERO_ERROR; const UChar *tzid = NULL; int32_t tzidLen = 0; char keyBuf[ZID_KEY_MAX + 1]; int32_t keyLen = 0; if (mzid.isBogus() || mzid.length() > ZID_KEY_MAX) { result.setToBogus(); return result; } keyLen = mzid.extract(0, mzid.length(), keyBuf, ZID_KEY_MAX + 1, US_INV); keyBuf[keyLen] = 0; UResourceBundle *rb = ures_openDirect(NULL, gMetaZones, &status); ures_getByKey(rb, gMapTimezonesTag, rb, &status); ures_getByKey(rb, keyBuf, rb, &status); if (U_SUCCESS(status)) { // check region mapping if (region.length() == 2 || region.length() == 3) { keyLen = region.extract(0, region.length(), keyBuf, ZID_KEY_MAX + 1, US_INV); keyBuf[keyLen] = 0; tzid = ures_getStringByKey(rb, keyBuf, &tzidLen, &status); if (status == U_MISSING_RESOURCE_ERROR) { status = U_ZERO_ERROR; } } if (U_SUCCESS(status) && tzid == NULL) { // try "001" tzid = ures_getStringByKey(rb, gWorldTag, &tzidLen, &status); } } ures_close(rb); if (tzid == NULL) { result.setToBogus(); } else { result.setTo(tzid, tzidLen); } return result; }
//Get test data from ResourceBundles UResourceBundle* RBTestDataModule::getTestBundle(const char* bundleName, UErrorCode &status) { if(U_SUCCESS(status)) { UResourceBundle *testBundle = NULL; const char* icu_data = fLog.getTestDataPath(status); if (testBundle == NULL) { testBundle = ures_openDirect(icu_data, bundleName, &status); if (status != U_ZERO_ERROR) { fLog.dataerrln(UNICODE_STRING_SIMPLE("Could not load test data from resourcebundle: ") + UnicodeString(bundleName, -1, US_INV) + "\n"); fDataTestValid = FALSE; } } return testBundle; } else { return NULL; } }
void U_CALLCONV DayPeriodRules::load(UErrorCode &errorCode) { if (U_FAILURE(errorCode)) { return; } data = new DayPeriodRulesData(); data->localeToRuleSetNumMap = uhash_open(uhash_hashChars, uhash_compareChars, NULL, &errorCode); LocalUResourceBundlePointer rb_dayPeriods(ures_openDirect(NULL, "dayPeriods", &errorCode)); // Get the largest rule set number (so we allocate enough objects). DayPeriodRulesCountSink countSink; ures_getAllItemsWithFallback(rb_dayPeriods.getAlias(), "rules", countSink, errorCode); // Populate rules. DayPeriodRulesDataSink sink; ures_getAllItemsWithFallback(rb_dayPeriods.getAlias(), "", sink, errorCode); ucln_i18n_registerCleanup(UCLN_I18N_DAYPERIODRULES, dayPeriodRulesCleanup); }
// TODO: rewrite this with int32_t ucurr_forLocale(const char* locale, UChar* buff, int32_t buffCapacity, UErrorCode* ec)... static jstring ICU_getCurrencyCode(JNIEnv* env, jclass, jstring javaCountryCode) { UErrorCode status = U_ZERO_ERROR; ScopedResourceBundle supplData(ures_openDirect(U_ICUDATA_CURR, "supplementalData", &status)); if (U_FAILURE(status)) { return NULL; } ScopedResourceBundle currencyMap(ures_getByKey(supplData.get(), "CurrencyMap", NULL, &status)); if (U_FAILURE(status)) { return NULL; } ScopedUtfChars countryCode(env, javaCountryCode); ScopedResourceBundle currency(ures_getByKey(currencyMap.get(), countryCode.c_str(), NULL, &status)); if (U_FAILURE(status)) { return NULL; } ScopedResourceBundle currencyElem(ures_getByIndex(currency.get(), 0, NULL, &status)); if (U_FAILURE(status)) { return env->NewStringUTF("XXX"); } // Check if there's a 'to' date. If there is, the currency isn't used anymore. ScopedResourceBundle currencyTo(ures_getByKey(currencyElem.get(), "to", NULL, &status)); if (!U_FAILURE(status)) { return NULL; } // Ignore the failure to find a 'to' date. status = U_ZERO_ERROR; ScopedResourceBundle currencyId(ures_getByKey(currencyElem.get(), "id", NULL, &status)); if (U_FAILURE(status)) { // No id defined for this country return env->NewStringUTF("XXX"); } int32_t charCount; const jchar* chars = ures_getString(currencyId.get(), &charCount, &status); return (charCount == 0) ? env->NewStringUTF("XXX") : env->NewString(chars, charCount); }
const GenderInfo* GenderInfo::loadInstance(const Locale& locale, UErrorCode& status) { LocalUResourceBundlePointer rb( ures_openDirect(NULL, "genderList", &status)); if (U_FAILURE(status)) { return NULL; } LocalUResourceBundlePointer locRes(ures_getByKey(rb.getAlias(), "genderList", NULL, &status)); if (U_FAILURE(status)) { return NULL; } int32_t resLen = 0; const char* curLocaleName = locale.getName(); UErrorCode key_status = U_ZERO_ERROR; const UChar* s = ures_getStringByKey(locRes.getAlias(), curLocaleName, &resLen, &key_status); if (s == NULL) { key_status = U_ZERO_ERROR; char parentLocaleName[ULOC_FULLNAME_CAPACITY]; uprv_strcpy(parentLocaleName, curLocaleName); while (s == NULL && uloc_getParent(parentLocaleName, parentLocaleName, ULOC_FULLNAME_CAPACITY, &key_status) > 0) { key_status = U_ZERO_ERROR; resLen = 0; s = ures_getStringByKey(locRes.getAlias(), parentLocaleName, &resLen, &key_status); key_status = U_ZERO_ERROR; } } if (s == NULL) { return &gObjs[NEUTRAL]; } char type_str[256]; u_UCharsToChars(s, type_str, resLen + 1); if (uprv_strcmp(type_str, gNeutralStr) == 0) { return &gObjs[NEUTRAL]; } if (uprv_strcmp(type_str, gMixedNeutralStr) == 0) { return &gObjs[MIXED_NEUTRAL]; } if (uprv_strcmp(type_str, gMailTaintsStr) == 0) { return &gObjs[MALE_TAINTS]; } return &gObjs[NEUTRAL]; }
/** * This function looks for the localeID in the likelySubtags resource. * * @param localeID The tag to find. * @param buffer A buffer to hold the matching entry * @param bufferLength The length of the output buffer * @return A pointer to "buffer" if found, or a null pointer if not. */ static const char* U_CALLCONV findLikelySubtags(const char* localeID, char* buffer, int32_t bufferLength, UErrorCode* err) { const char* result = NULL; if (!U_FAILURE(*err)) { int32_t resLen = 0; const UChar* s = NULL; UErrorCode tmpErr = U_ZERO_ERROR; UResourceBundle* subtags = ures_openDirect(NULL, "likelySubtags", &tmpErr); if (U_SUCCESS(tmpErr)) { s = ures_getStringByKey(subtags, localeID, &resLen, &tmpErr); if (U_FAILURE(tmpErr)) { /* * If a resource is missing, it's not really an error, it's * just that we don't have any data for that particular locale ID. */ if (tmpErr != U_MISSING_RESOURCE_ERROR) { *err = tmpErr; } } else if (resLen >= bufferLength) { /* The buffer should never overflow. */ *err = U_INTERNAL_PROGRAM_ERROR; } else { u_UCharsToChars(s, buffer, resLen + 1); result = buffer; } ures_close(subtags); } else { *err = tmpErr; } } return result; }
const UChar* TimeZone::getRegion(const UnicodeString& id) { const UChar *result = WORLD; UErrorCode ec = U_ZERO_ERROR; UResourceBundle *rb = ures_openDirect(NULL, kZONEINFO, &ec); // resolve zone index by name UResourceBundle *res = ures_getByKey(rb, kNAMES, NULL, &ec); int32_t idx = findInStringArray(res, id, ec); // get region mapping ures_getByKey(rb, kREGIONS, res, &ec); const UChar *tmp = ures_getStringByIndex(res, idx, NULL, &ec); if (U_SUCCESS(ec)) { result = tmp; } ures_close(res); ures_close(rb); return result; }
TZEnumeration(const char* country) : map(NULL), len(0), pos(0) { if (!getOlsonMeta()) { return; } char key[] = {0, 0, 0, 0,0, 0, 0,0, 0, 0,0}; // e.g., "US", or "Default" for no country if (country) { uprv_strncat(key, country, 2); } else { uprv_strcpy(key, kDEFAULT); } UErrorCode ec = U_ZERO_ERROR; UResourceBundle *top = ures_openDirect(0, kZONEINFO, &ec); top = ures_getByKey(top, kREGIONS, top, &ec); // dereference 'Regions' section if (U_SUCCESS(ec)) { UResourceBundle res; ures_initStackObject(&res); ures_getByKey(top, key, &res, &ec); // The list of zones is a list of integers, from 0..n-1, // where n is the total number of system zones. const int32_t* v = ures_getIntVector(&res, &len, &ec); if (U_SUCCESS(ec)) { U_ASSERT(len > 0); map = (int32_t*)uprv_malloc(sizeof(int32_t) * len); if (map != 0) { for (uint16_t i=0; i<len; ++i) { U_ASSERT(v[i] >= 0 && v[i] < OLSON_ZONE_COUNT); map[i] = v[i]; } } } else { U_DEBUG_TZ_MSG(("Failed to load tz for region %s: %s\n", country, u_errorName(ec))); } ures_close(&res); } ures_close(top); }
UnicodeString& TimeZone::dereferOlsonLink(const UnicodeString& linkTo, UnicodeString& linkFrom) { UErrorCode ec = U_ZERO_ERROR; linkFrom.remove(); UResourceBundle *top = ures_openDirect(0, kZONEINFO, &ec); UResourceBundle *res = getZoneByName(top, linkTo, NULL, ec); if (U_SUCCESS(ec)) { if (ures_getSize(res) == 1) { int32_t deref = ures_getInt(res, &ec); UResourceBundle *nres = ures_getByKey(top, kNAMES, NULL, &ec); // dereference Names section int32_t len; const UChar* tmp = ures_getStringByIndex(nres, deref, &len, &ec); if (U_SUCCESS(ec)) { linkFrom.setTo(tmp, len); } ures_close(nres); } else { linkFrom.setTo(linkTo); } } ures_close(res); ures_close(top); return linkFrom; }