static void TestFallback() { UErrorCode status = U_ZERO_ERROR; UResourceBundle *fr_FR = NULL; UResourceBundle *subResource = NULL; const UChar *junk; /* ignored */ int32_t resultLen; log_verbose("Opening fr_FR.."); fr_FR = ures_open(NULL, "fr_FR", &status); if(U_FAILURE(status)) { log_err_status(status, "Couldn't open fr_FR - %s\n", u_errorName(status)); return; } status = U_ZERO_ERROR; /* clear it out.. just do some calls to get the gears turning */ junk = ures_getStringByKey(fr_FR, "LocaleID", &resultLen, &status); (void)junk; /* Suppress set but not used warning. */ status = U_ZERO_ERROR; junk = ures_getStringByKey(fr_FR, "LocaleString", &resultLen, &status); status = U_ZERO_ERROR; junk = ures_getStringByKey(fr_FR, "LocaleID", &resultLen, &status); status = U_ZERO_ERROR; /* OK first one. This should be a Default value. */ subResource = ures_getByKey(fr_FR, "layout", NULL, &status); if(status != U_USING_DEFAULT_WARNING) { log_data_err("Expected U_USING_DEFAULT_ERROR when trying to get layout from fr_FR, got %s\n", u_errorName(status)); } ures_close(subResource); status = U_ZERO_ERROR; /* and this is a Fallback, to fr */ junk = ures_getStringByKey(fr_FR, "ExemplarCharacters", &resultLen, &status); if(status != U_USING_FALLBACK_WARNING) { log_data_err("Expected U_USING_FALLBACK_ERROR when trying to get ExemplarCharacters from fr_FR, got %s\n", u_errorName(status)); } status = U_ZERO_ERROR; ures_close(fr_FR); }
U_CAPI USet* U_EXPORT2 ulocdata_getExemplarSet(ULocaleData *uld, USet *fillIn, uint32_t options, ULocaleDataExemplarSetType extype, UErrorCode *status){ static const char* const exemplarSetTypes[] = { "ExemplarCharacters", "AuxExemplarCharacters" }; const UChar *exemplarChars = NULL; int32_t len = 0; UErrorCode localStatus = U_ZERO_ERROR; if (U_FAILURE(*status)) return NULL; exemplarChars = ures_getStringByKey(uld->bundle, exemplarSetTypes[extype], &len, &localStatus); if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) { localStatus = U_MISSING_RESOURCE_ERROR; } if (localStatus != U_ZERO_ERROR) { *status = localStatus; } if (U_FAILURE(*status)) return NULL; if(fillIn != NULL) uset_applyPattern(fillIn, exemplarChars, len, USET_IGNORE_SPACE | options, status); else fillIn = uset_openPatternOptions(exemplarChars, len, USET_IGNORE_SPACE | options, status); return fillIn; }
U_CAPI const UChar* U_EXPORT2 ucurr_forLocale(const char* locale, UErrorCode* ec) { if (ec != NULL && U_SUCCESS(*ec)) { char id[ULOC_FULLNAME_CAPACITY]; idForLocale(locale, id, sizeof(id), ec); if (U_FAILURE(*ec)) { return NULL; } const UChar* result = CReg::get(id); if (result) { return result; } // Look up the CurrencyMap element in the root bundle. UResourceBundle* rb = ures_open(NULL, "", ec); UResourceBundle* cm = ures_getByKey(rb, CURRENCY_MAP, NULL, ec); int32_t len; const UChar* s = ures_getStringByKey(cm, id, &len, ec); ures_close(cm); ures_close(rb); if (U_SUCCESS(*ec)) { return s; } } return NULL; }
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; }
U_CAPI int32_t U_EXPORT2 ucol_getRulesEx(const UCollator *coll, UColRuleOption delta, UChar *buffer, int32_t bufferLen) { UErrorCode status = U_ZERO_ERROR; int32_t len = 0; int32_t UCAlen = 0; const UChar* ucaRules = 0; const UChar *rules = ucol_getRules(coll, &len); if(delta == UCOL_FULL_RULES) { /* take the UCA rules and append real rules at the end */ /* UCA rules will be probably coming from the root RB */ ucaRules = ures_getStringByKey(coll->rb,"UCARules",&UCAlen,&status); /* UResourceBundle* cresb = ures_getByKeyWithFallback(coll->rb, "collations", NULL, &status); UResourceBundle* uca = ures_getByKeyWithFallback(cresb, "UCA", NULL, &status); ucaRules = ures_getStringByKey(uca,"Sequence",&UCAlen,&status); ures_close(uca); ures_close(cresb); */ } if(U_FAILURE(status)) { return 0; } if(buffer!=0 && bufferLen>0){ *buffer=0; if(UCAlen > 0) { u_memcpy(buffer, ucaRules, uprv_min(UCAlen, bufferLen)); } if(len > 0 && bufferLen > UCAlen) { u_memcpy(buffer+UCAlen, rules, uprv_min(len, bufferLen-UCAlen)); } } return u_terminateUChars(buffer, bufferLen, len+UCAlen, &status); }
virtual UObject* create(const ICUServiceKey& key, const ICUService* /*service*/, UErrorCode& status) const { LocaleKey &lkey = (LocaleKey&)key; Locale loc; lkey.currentLocale(loc); #ifdef U_DEBUG_CALSVC fprintf(stderr, "DefaultCalendar factory %p: looking up %s\n", this, (const char*)loc.getName()); #endif UErrorCode resStatus = U_ZERO_ERROR; UResourceBundle *rb = ures_open(NULL, (const char*)loc.getName(), &resStatus); #ifdef U_DEBUG_CALSVC fprintf(stderr, "... ures_open -> %s\n", u_errorName(resStatus)); #endif if(U_FAILURE(resStatus) || (resStatus == U_USING_DEFAULT_WARNING) || (resStatus==U_USING_FALLBACK_WARNING)) { //Don't want to handle fallback data. ures_close(rb); status = resStatus; // propagate err back to caller #ifdef U_DEBUG_CALSVC fprintf(stderr, "... exitting (NULL)\n"); #endif return NULL; } int32_t len = 0; UnicodeString myString = ures_getUnicodeStringByKey(rb, Calendar::kDefaultCalendar, &status); #ifdef U_DEBUG_CALSVC UErrorCode debugStatus = U_ZERO_ERROR; const UChar *defCal = ures_getStringByKey(rb, Calendar::kDefaultCalendar, &len, &debugStatus); fprintf(stderr, "... get string(%d) -> %s\n", len, u_errorName(debugStatus)); #endif ures_close(rb); if(U_FAILURE(status)) { return NULL; } #ifdef U_DEBUG_CALSVC { char defCalStr[200]; if(len > 199) { len = 199; } u_UCharsToChars(defCal, defCalStr, len); defCalStr[len]=0; fprintf(stderr, "DefaultCalendarFactory: looked up %s, got DefaultCalendar= %s\n", (const char*)loc.getName(), defCalStr); } #endif return myString.clone(); }
static void usage(const char *pname, int ecode) { const UChar *msg; int32_t msgLen; UErrorCode err = U_ZERO_ERROR; FILE *fp = ecode ? stderr : stdout; int res; initMsg(pname); msg = ures_getStringByKey(gBundle, ecode ? "lcUsageWord" : "ucUsageWord", &msgLen, &err); UnicodeString upname(pname, (int32_t)(uprv_strlen(pname) + 1)); UnicodeString mname(msg, msgLen + 1); res = u_wmsg(fp, "usage", mname.getBuffer(), upname.getBuffer()); if (!ecode) { if (!res) { fputc('\n', fp); } if (!u_wmsg(fp, "help")) { /* Now dump callbacks and finish. */ int i, count = sizeof(transcode_callbacks) / sizeof(*transcode_callbacks); for (i = 0; i < count; ++i) { fprintf(fp, " %s", transcode_callbacks[i].name); } fputc('\n', fp); } } exit(ecode); }
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; }
void ResourceBundleTest::TestExemplar(){ int32_t locCount = uloc_countAvailable(); int32_t locIndex=0; int num=0; UErrorCode status = U_ZERO_ERROR; for(;locIndex<locCount;locIndex++){ const char* locale = uloc_getAvailable(locIndex); UResourceBundle *resb =ures_open(NULL,locale,&status); if(U_SUCCESS(status) && status!=U_USING_FALLBACK_WARNING && status!=U_USING_DEFAULT_WARNING){ int32_t len=0; const UChar* strSet = ures_getStringByKey(resb,"ExemplarCharacters",&len,&status); UnicodeSet set(strSet,status); if(U_FAILURE(status)){ errln("Could not construct UnicodeSet from pattern for ExemplarCharacters in locale : %s. Error: %s",locale,u_errorName(status)); status=U_ZERO_ERROR; } num++; } ures_close(resb); } logln("Number of installed locales with exemplar characters that could be tested: %d",num); }
U_CAPI const UChar* U_EXPORT2 u_catgets(u_nl_catd catd, int32_t set_num, int32_t msg_num, const UChar* s, int32_t* len, UErrorCode* ec) { char key[MAX_KEY_LEN]; const UChar* result; if (ec == NULL || U_FAILURE(*ec)) { goto ERROR; } result = ures_getStringByKey((const UResourceBundle*) catd, _catkey(key, set_num, msg_num), len, ec); if (U_FAILURE(*ec)) { goto ERROR; } return result; ERROR: /* In case of any failure, return s */ if (len != NULL) { *len = u_strlen(s); } return s; }
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; }
static void setStringField(JNIEnv* env, jobject obj, const char* fieldName, UResourceBundle* bundle, const char* key) { UErrorCode status = U_ZERO_ERROR; int charCount; const UChar* chars = ures_getStringByKey(bundle, key, &charCount, &status); if (U_SUCCESS(status)) { setStringField(env, obj, fieldName, env->NewString(chars, charCount)); } else { ALOGE("Error setting String field %s from ICU resource (key %s): %s", fieldName, key, u_errorName(status)); } }
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]; }
UBool LocaleAliasTest::isLocaleAvailable(const char* loc){ if(resIndex==NULL){ return FALSE; } UErrorCode status = U_ZERO_ERROR; int32_t len = 0; ures_getStringByKey(resIndex, loc,&len, &status); if(U_FAILURE(status)){ return FALSE; } return TRUE; }
U_CAPI int32_t U_EXPORT2 ulocdata_getDelimiter(ULocaleData *uld, ULocaleDataDelimiterType type, UChar *result, int32_t resultLength, UErrorCode *status){ static const char* const delimiterKeys[] = { "quotationStart", "quotationEnd", "alternateQuotationStart", "alternateQuotationEnd" }; UResourceBundle *delimiterBundle; int32_t len = 0; const UChar *delimiter = NULL; UErrorCode localStatus = U_ZERO_ERROR; if (U_FAILURE(*status)) return 0; delimiterBundle = ures_getByKey(uld->bundle, "delimiters", NULL, &localStatus); if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) { localStatus = U_MISSING_RESOURCE_ERROR; } if (localStatus != U_ZERO_ERROR) { *status = localStatus; } if (U_FAILURE(*status)){ ures_close(delimiterBundle); return 0; } delimiter = ures_getStringByKey(delimiterBundle, delimiterKeys[type], &len, &localStatus); ures_close(delimiterBundle); if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) { localStatus = U_MISSING_RESOURCE_ERROR; } if (localStatus != U_ZERO_ERROR) { *status = localStatus; } if (U_FAILURE(*status)){ return 0; } u_strncpy(result,delimiter, resultLength); return len; }
U_CDECL_END /****************************************************************************/ /* Following are the open/close functions */ /* */ /****************************************************************************/ static UCollator* tryOpeningFromRules(UResourceBundle *collElem, UErrorCode *status) { int32_t rulesLen = 0; const UChar *rules = ures_getStringByKey(collElem, "Sequence", &rulesLen, status); return ucol_openRules(rules, rulesLen, UCOL_DEFAULT, UCOL_DEFAULT, NULL, status); }
static int32_t _getStringOrCopyKey(const char *path, const char *locale, const char *tableKey, const char* subTableKey, const char *itemKey, const char *substitute, UChar *dest, int32_t destCapacity, UErrorCode *pErrorCode) { const UChar *s = NULL; int32_t length = 0; if(itemKey==NULL) { /* top-level item: normal resource bundle access */ UResourceBundle *rb; rb=ures_open(path, locale, pErrorCode); if(U_SUCCESS(*pErrorCode)) { s=ures_getStringByKey(rb, tableKey, &length, pErrorCode); /* see comment about closing rb near "return item;" in _res_getTableStringWithFallback() */ ures_close(rb); } } else { /* Language code should not be a number. If it is, set the error code. */ if (!uprv_strncmp(tableKey, "Languages", 9) && uprv_strtol(itemKey, NULL, 10)) { *pErrorCode = U_MISSING_RESOURCE_ERROR; } else { /* second-level item, use special fallback */ s=uloc_getTableStringWithFallback(path, locale, tableKey, subTableKey, itemKey, &length, pErrorCode); } } if(U_SUCCESS(*pErrorCode)) { int32_t copyLength=uprv_min(length, destCapacity); if(copyLength>0 && s != NULL) { u_memcpy(dest, s, copyLength); } } else { /* no string from a resource bundle: convert the substitute */ length=(int32_t)uprv_strlen(substitute); u_charsToUChars(substitute, dest, uprv_min(length, destCapacity)); *pErrorCode=U_USING_DEFAULT_WARNING; } return u_terminateUChars(dest, destCapacity, length, pErrorCode); }
U_CFUNC const UChar *u_wmsg_errorName(UErrorCode err) { UChar *msg; int32_t msgLen; UErrorCode subErr = U_ZERO_ERROR; const char *textMsg = NULL; /* try the cache */ msg = (UChar*)fetchErrorName(err); if(msg) { return msg; } if(gBundle == NULL) { msg = NULL; } else { const char *errname = u_errorName(err); if (errname) { msg = (UChar*)ures_getStringByKey(gBundle, errname, &msgLen, &subErr); if(U_FAILURE(subErr)) { msg = NULL; } } } if(msg == NULL) /* Couldn't find it anywhere.. */ { char error[128]; textMsg = u_errorName(err); if (!textMsg) { sprintf(error, "UNDOCUMENTED ICU ERROR %d", err); textMsg = error; } msg = (UChar*)malloc((strlen(textMsg)+1)*sizeof(msg[0])); u_charsToUChars(textMsg, msg, (int32_t)(strlen(textMsg)+1)); } if(err>=0) gErrMessages[err] = msg; else gInfoMessages[err-U_ERROR_WARNING_START] = msg; return msg; }
U_DRAFT int32_t U_EXPORT2 ulocdata_getLocaleSeparator(ULocaleData *uld, UChar *result, int32_t resultCapacity, UErrorCode *status) { UResourceBundle *separatorBundle; int32_t len = 0; const UChar *separator = NULL; UErrorCode localStatus = U_ZERO_ERROR; if (U_FAILURE(*status)) return 0; separatorBundle = ures_getByKey(uld->bundle, "localeDisplayPattern", NULL, &localStatus); if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) { localStatus = U_MISSING_RESOURCE_ERROR; } if (localStatus != U_ZERO_ERROR) { *status = localStatus; } if (U_FAILURE(*status)){ ures_close(separatorBundle); return 0; } separator = ures_getStringByKey(separatorBundle, "separator", &len, &localStatus); ures_close(separatorBundle); if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) { localStatus = U_MISSING_RESOURCE_ERROR; } if (localStatus != U_ZERO_ERROR) { *status = localStatus; } if (U_FAILURE(*status)){ return 0; } u_strncpy(result, separator, resultCapacity); return len; }
U_CAPI int32_t U_EXPORT2 ucnv_getDisplayName(const UConverter *cnv, const char *displayLocale, UChar *displayName, int32_t displayNameCapacity, UErrorCode *pErrorCode) { UResourceBundle *rb; const UChar *name; int32_t length; UErrorCode localStatus = U_ZERO_ERROR; /* check arguments */ if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { return 0; } if(cnv==NULL || displayNameCapacity<0 || (displayNameCapacity>0 && displayName==NULL)) { *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; return 0; } /* open the resource bundle and get the display name string */ rb=ures_open(NULL, displayLocale, pErrorCode); if(U_FAILURE(*pErrorCode)) { return 0; } /* use the internal name as the key */ name=ures_getStringByKey(rb, cnv->sharedData->staticData->name, &length, &localStatus); ures_close(rb); if(U_SUCCESS(localStatus)) { /* copy the string */ if (*pErrorCode == U_ZERO_ERROR) { *pErrorCode = localStatus; } u_memcpy(displayName, name, uprv_min(length, displayNameCapacity)*U_SIZEOF_UCHAR); } else { /* convert the internal name into a Unicode string */ length=(int32_t)uprv_strlen(cnv->sharedData->staticData->name); u_charsToUChars(cnv->sharedData->staticData->name, displayName, uprv_min(length, displayNameCapacity)); } return u_terminateUChars(displayName, displayNameCapacity, length, pErrorCode); }
void TestAliasConflict(void) { UErrorCode status = U_ZERO_ERROR; UResourceBundle *he = NULL; UResourceBundle *iw = NULL; const UChar *result = NULL; int32_t resultLen; he = ures_open(NULL, "he", &status); iw = ures_open(NULL, "iw", &status); if(U_FAILURE(status)) { log_err_status(status, "Failed to get resource with %s\n", myErrorName(status)); } ures_close(iw); result = ures_getStringByKey(he, "ExemplarCharacters", &resultLen, &status); if(U_FAILURE(status) || result == NULL) { log_err_status(status, "Failed to get resource with %s\n", myErrorName(status)); } ures_close(he); }
/** * 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; }
NumberingSystem* U_EXPORT2 NumberingSystem::createInstanceByName(const char *name, UErrorCode& status) { UResourceBundle *numberingSystemsInfo = NULL; UResourceBundle *nsTop, *nsCurrent; const UChar* description = NULL; int32_t radix = 10; int32_t algorithmic = 0; int32_t len; numberingSystemsInfo = ures_openDirect(NULL,gNumberingSystems, &status); nsCurrent = ures_getByKey(numberingSystemsInfo,gNumberingSystems,NULL,&status); nsTop = ures_getByKey(nsCurrent,name,NULL,&status); description = ures_getStringByKey(nsTop,gDesc,&len,&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 ); UnicodeString nsd; nsd.setTo(description); 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; }
//constructor NamePrepTransform::NamePrepTransform(UParseError& parseError, UErrorCode& status) : unassigned(), prohibited(), labelSeparatorSet(){ mapping = NULL; bundle = NULL; const char* testDataName = IntlTest::loadTestData(status); if(U_FAILURE(status)){ return; } bundle = ures_openDirect(testDataName,"idna_rules",&status); if(bundle != NULL && U_SUCCESS(status)){ // create the mapping transliterator int32_t ruleLen = 0; const UChar* ruleUChar = ures_getStringByKey(bundle, "MapNFKC",&ruleLen, &status); int32_t mapRuleLen = 0; const UChar *mapRuleUChar = ures_getStringByKey(bundle, "MapNoNormalization", &mapRuleLen, &status); UnicodeString rule(mapRuleUChar, mapRuleLen); rule.append(ruleUChar, ruleLen); mapping = Transliterator::createFromRules(UnicodeString("NamePrepTransform", ""), rule, UTRANS_FORWARD, parseError,status); if(U_FAILURE(status)) { return; } //create the unassigned set int32_t patternLen =0; const UChar* pattern = ures_getStringByKey(bundle,"UnassignedSet",&patternLen, &status); unassigned.applyPattern(UnicodeString(pattern, patternLen), status); //create prohibited set patternLen=0; pattern = ures_getStringByKey(bundle,"ProhibitedSet",&patternLen, &status); UnicodeString test(pattern,patternLen); prohibited.applyPattern(test,status); #ifdef DEBUG if(U_FAILURE(status)){ printf("Construction of Unicode set failed\n"); } if(U_SUCCESS(status)){ if(prohibited.contains((UChar) 0x644)){ printf("The string contains 0x644 ... damn !!\n"); } UnicodeString temp; prohibited.toPattern(temp,TRUE); for(int32_t i=0;i<temp.length();i++){ printf("%c", (char)temp.charAt(i)); } printf("\n"); } #endif //create label separator set patternLen=0; pattern = ures_getStringByKey(bundle,"LabelSeparatorSet",&patternLen, &status); labelSeparatorSet.applyPattern(UnicodeString(pattern,patternLen),status); } if(U_SUCCESS(status) && (mapping == NULL) ){ status = U_MEMORY_ALLOCATION_ERROR; delete mapping; ures_close(bundle); mapping = NULL; bundle = NULL; } }
/** * Main Windows time zone detection function. Returns the Windows * time zone, translated to an ICU time zone, or NULL upon failure. */ U_CFUNC const char* U_EXPORT2 uprv_detectWindowsTimeZone() { UErrorCode status = U_ZERO_ERROR; UResourceBundle* bundle = NULL; char* icuid = NULL; char apiStdName[MAX_LENGTH_ID]; char regStdName[MAX_LENGTH_ID]; char tmpid[MAX_LENGTH_ID]; int32_t len; int id; int errorCode; UChar ISOcodeW[3]; /* 2 letter iso code in UTF-16*/ char ISOcodeA[3]; /* 2 letter iso code in ansi */ LONG result; TZI tziKey; TZI tziReg; TIME_ZONE_INFORMATION apiTZI; BOOL isVistaOrHigher; BOOL tryPreVistaFallback; OSVERSIONINFO osVerInfo; /* Obtain TIME_ZONE_INFORMATION from the API, and then convert it to TZI. We could also interrogate the registry directly; we do this below if needed. */ uprv_memset(&apiTZI, 0, sizeof(apiTZI)); uprv_memset(&tziKey, 0, sizeof(tziKey)); uprv_memset(&tziReg, 0, sizeof(tziReg)); GetTimeZoneInformation(&apiTZI); tziKey.bias = apiTZI.Bias; uprv_memcpy((char *)&tziKey.standardDate, (char*)&apiTZI.StandardDate, sizeof(apiTZI.StandardDate)); uprv_memcpy((char *)&tziKey.daylightDate, (char*)&apiTZI.DaylightDate, sizeof(apiTZI.DaylightDate)); /* Convert the wchar_t* standard name to char* */ uprv_memset(apiStdName, 0, sizeof(apiStdName)); wcstombs(apiStdName, apiTZI.StandardName, MAX_LENGTH_ID); tmpid[0] = 0; id = GetUserGeoID(GEOCLASS_NATION); errorCode = GetGeoInfoW(id,GEO_ISO2,ISOcodeW,3,0); u_strToUTF8(ISOcodeA, 3, NULL, ISOcodeW, 3, &status); bundle = ures_openDirect(NULL, "windowsZones", &status); ures_getByKey(bundle, "mapTimezones", bundle, &status); /* Windows Vista+ provides us with a "TimeZoneKeyName" that is not localized and can be used to directly map a name in our bundle. Try to use that first if we're on Vista or higher */ uprv_memset(&osVerInfo, 0, sizeof(osVerInfo)); osVerInfo.dwOSVersionInfoSize = sizeof(osVerInfo); GetVersionEx(&osVerInfo); isVistaOrHigher = osVerInfo.dwMajorVersion >= 6; /* actually includes Windows Server 2008 as well, but don't worry about it */ tryPreVistaFallback = TRUE; if(isVistaOrHigher) { result = getTZKeyName(regStdName, sizeof(regStdName)); if(ERROR_SUCCESS == result) { UResourceBundle* winTZ = ures_getByKey(bundle, regStdName, NULL, &status); if(U_SUCCESS(status)) { const UChar* icuTZ = NULL; if (errorCode != 0) { icuTZ = ures_getStringByKey(winTZ, ISOcodeA, &len, &status); } if (errorCode==0 || icuTZ==NULL) { /* fallback to default "001" and reset status */ status = U_ZERO_ERROR; icuTZ = ures_getStringByKey(winTZ, "001", &len, &status); } if(U_SUCCESS(status)) { int index=0; while (! (*icuTZ == '\0' || *icuTZ ==' ')) { tmpid[index++]=(char)(*icuTZ++); /* safe to assume 'char' is ASCII compatible on windows */ } tmpid[index]='\0'; tryPreVistaFallback = FALSE; } } ures_close(winTZ); } } if(tryPreVistaFallback) { /* Note: We get the winid not from static tables but from resource bundle. */ while (U_SUCCESS(status) && ures_hasNext(bundle)) { UBool idFound = FALSE; const char* winid; UResourceBundle* winTZ = ures_getNextResource(bundle, NULL, &status); if (U_FAILURE(status)) { break; } winid = ures_getKey(winTZ); result = getTZI(winid, &tziReg); if (result == ERROR_SUCCESS) { /* Windows alters the DaylightBias in some situations. Using the bias and the rules suffices, so overwrite these unreliable fields. */ tziKey.standardBias = tziReg.standardBias; tziKey.daylightBias = tziReg.daylightBias; if (uprv_memcmp((char *)&tziKey, (char*)&tziReg, sizeof(tziKey)) == 0) { const UChar* icuTZ = NULL; if (errorCode != 0) { icuTZ = ures_getStringByKey(winTZ, ISOcodeA, &len, &status); } if (errorCode==0 || icuTZ==NULL) { /* fallback to default "001" and reset status */ status = U_ZERO_ERROR; icuTZ = ures_getStringByKey(winTZ, "001", &len, &status); } if (U_SUCCESS(status)) { /* Get the standard name from the registry key to compare with the one from Windows API call. */ uprv_memset(regStdName, 0, sizeof(regStdName)); result = getSTDName(winid, regStdName, sizeof(regStdName)); if (result == ERROR_SUCCESS) { if (uprv_strcmp(apiStdName, regStdName) == 0) { idFound = TRUE; } } /* tmpid buffer holds the ICU timezone ID corresponding to the timezone ID from Windows. * If none is found, tmpid buffer will contain a fallback ID (i.e. the time zone ID matching * the current time zone information) */ if (idFound || tmpid[0] == 0) { /* if icuTZ has more than one city, take only the first (i.e. terminate icuTZ at first space) */ int index=0; while (! (*icuTZ == '\0' || *icuTZ ==' ')) { tmpid[index++]=(char)(*icuTZ++); /* safe to assume 'char' is ASCII compatible on windows */ } tmpid[index]='\0'; } } } } ures_close(winTZ); if (idFound) { break; } } } /* * Copy the timezone ID to icuid to be returned. */ if (tmpid[0] != 0) { len = uprv_strlen(tmpid); icuid = (char*)uprv_calloc(len + 1, sizeof(char)); if (icuid != NULL) { uprv_strcpy(icuid, tmpid); } } ures_close(bundle); return icuid; }
U_CFUNC const char* U_EXPORT2 uprv_detectWindowsTimeZone() { UErrorCode status = U_ZERO_ERROR; UResourceBundle* bundle = NULL; char* icuid = NULL; LONG result; TZI tziKey; TZI tziReg; TIME_ZONE_INFORMATION apiTZI; /* Obtain TIME_ZONE_INFORMATION from the API, and then convert it to TZI. We could also interrogate the registry directly; we do this below if needed. */ uprv_memset(&apiTZI, 0, sizeof(apiTZI)); uprv_memset(&tziKey, 0, sizeof(tziKey)); uprv_memset(&tziReg, 0, sizeof(tziReg)); GetTimeZoneInformation(&apiTZI); tziKey.bias = apiTZI.Bias; uprv_memcpy((char *)&tziKey.standardDate, (char*)&apiTZI.StandardDate, sizeof(apiTZI.StandardDate)); uprv_memcpy((char *)&tziKey.daylightDate, (char*)&apiTZI.DaylightDate, sizeof(apiTZI.DaylightDate)); bundle = ures_openDirect(NULL, "windowsZones", &status); ures_getByKey(bundle, "mapTimezones", bundle, &status); /* Note: We get the winid not from static tables but from resource bundle. */ while (U_SUCCESS(status) && ures_hasNext(bundle)) { const char* winid; int32_t len; UResourceBundle* winTZ = ures_getNextResource(bundle, NULL, &status); if (U_FAILURE(status)) { break; } winid = ures_getKey(winTZ); result = getTZI(winid, &tziReg); if (result == ERROR_SUCCESS) { /* Windows alters the DaylightBias in some situations. Using the bias and the rules suffices, so overwrite these unreliable fields. */ tziKey.standardBias = tziReg.standardBias; tziKey.daylightBias = tziReg.daylightBias; if (uprv_memcmp((char *)&tziKey, (char*)&tziReg, sizeof(tziKey)) == 0) { const UChar* icuTZ = ures_getStringByKey(winTZ, "001", &len, &status); if (U_SUCCESS(status)) { icuid = (char*)uprv_malloc(sizeof(char) * (len + 1)); uprv_memset(icuid, 0, len + 1); u_austrncpy(icuid, icuTZ, len); } } } ures_close(winTZ); if (icuid != NULL) { break; } } ures_close(bundle); return icuid; }
UBool testTag(const char* frag, UBool in_Root, UBool in_te, UBool in_te_IN) { int32_t passNum=pass; /* Make array from input params */ UBool is_in[3]; const char *NAME[] = { "ROOT", "TE", "TE_IN" }; /* Now try to load the desired items */ UResourceBundle* theBundle = NULL; char tag[99]; char action[256]; UErrorCode status = U_ZERO_ERROR,expected_resource_status = U_ZERO_ERROR; UChar* base = NULL; UChar* expected_string = NULL; const UChar* string = NULL; char item_tag[10]; int32_t i,j; int32_t actual_bundle; int32_t resultLen; const char *testdatapath = loadTestData(&status); is_in[0] = in_Root; is_in[1] = in_te; is_in[2] = in_te_IN; strcpy(item_tag, "tag"); status = U_ZERO_ERROR; theBundle = ures_open(testdatapath, "root", &status); if(U_FAILURE(status)) { ures_close(theBundle); log_err("Couldn't open root bundle in %s", testdatapath); return FALSE; } ures_close(theBundle); theBundle = NULL; for (i=0; i<bundles_count; ++i) { strcpy(action,"construction for"); strcat(action, param[i].name); status = U_ZERO_ERROR; theBundle = ures_open(testdatapath, param[i].name, &status); /*theBundle = ures_open("c:\\icu\\icu\\source\\test\\testdata\\testdata", param[i].name, &status);*/ CONFIRM_ErrorCode(status,param[i].expected_constructor_status); if(i == 5) actual_bundle = 0; /* ne -> default */ else if(i == 3) actual_bundle = 1; /* te_NE -> te */ else if(i == 4) actual_bundle = 2; /* te_IN_NE -> te_IN */ else actual_bundle = i; expected_resource_status = U_MISSING_RESOURCE_ERROR; for (j=e_te_IN; j>=e_Root; --j) { if (is_in[j] && param[i].inherits[j]) { if(j == actual_bundle) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */ expected_resource_status = U_ZERO_ERROR; else if(j == 0) expected_resource_status = U_USING_DEFAULT_WARNING; else expected_resource_status = U_USING_FALLBACK_WARNING; log_verbose("%s[%d]::%s: in<%d:%s> inherits<%d:%s>. actual_bundle=%s\n", param[i].name, i, frag, j, is_in[j]?"Yes":"No", j, param[i].inherits[j]?"Yes":"No", param[actual_bundle].name); break; } } for (j=param[i].where; j>=0; --j) { if (is_in[j]) { if(base != NULL) { free(base); base = NULL; } base=(UChar*)malloc(sizeof(UChar)*(strlen(NAME[j]) + 1)); u_uastrcpy(base,NAME[j]); break; } else { if(base != NULL) { free(base); base = NULL; } base = (UChar*) malloc(sizeof(UChar) * 1); *base = 0x0000; } } /*-------------------------------------------------------------------- */ /* string */ strcpy(tag,"string_"); strcat(tag,frag); strcpy(action,param[i].name); strcat(action, ".ures_get(" ); strcat(action,tag); strcat(action, ")"); string= kERROR; status = U_ZERO_ERROR; ures_getStringByKey(theBundle, tag, &resultLen, &status); if(U_SUCCESS(status)) { status = U_ZERO_ERROR; string=ures_getStringByKey(theBundle, tag, &resultLen, &status); } log_verbose("%s got %d, expected %d\n", action, status, expected_resource_status); CONFIRM_ErrorCode(status, expected_resource_status); if(U_SUCCESS(status)){ expected_string=(UChar*)malloc(sizeof(UChar)*(u_strlen(base) + 3)); u_strcpy(expected_string,base); } else { expected_string = (UChar*)malloc(sizeof(UChar)*(u_strlen(kERROR) + 1)); u_strcpy(expected_string,kERROR); } CONFIRM_EQ(string, expected_string); free(expected_string); ures_close(theBundle); } free(base); return (UBool)(passNum == pass); }
void TestConstruction1() { UResourceBundle *test1 = 0, *test2 = 0; const UChar *result1, *result2; int32_t resultLen; UChar temp[7]; UErrorCode err = U_ZERO_ERROR; const char* testdatapath ; const char* locale="te_IN"; log_verbose("Testing ures_open()......\n"); testdatapath=loadTestData(&err); if(U_FAILURE(err)) { log_data_err("Could not load testdata.dat %s \n",myErrorName(err)); return; } test1=ures_open(testdatapath, NULL, &err); if(U_FAILURE(err)) { log_err("construction of %s did not succeed : %s \n",NULL, myErrorName(err)); return; } test2=ures_open(testdatapath, locale, &err); if(U_FAILURE(err)) { log_err("construction of %s did not succeed : %s \n",locale, myErrorName(err)); return; } result1= ures_getStringByKey(test1, "string_in_Root_te_te_IN", &resultLen, &err); result2= ures_getStringByKey(test2, "string_in_Root_te_te_IN", &resultLen, &err); if (U_FAILURE(err)) { log_err("Something threw an error in TestConstruction(): %s\n", myErrorName(err)); return; } u_uastrcpy(temp, "TE_IN"); if(u_strcmp(result2, temp)!=0) { int n; log_err("Construction test failed for ures_open();\n"); if(!getTestOption(VERBOSITY_OPTION)) log_info("(run verbose for more information)\n"); log_verbose("\nGot->"); for(n=0;result2[n];n++) { log_verbose("%04X ",result2[n]); } log_verbose("<\n"); log_verbose("\nWant>"); for(n=0;temp[n];n++) { log_verbose("%04X ",temp[n]); } log_verbose("<\n"); } log_verbose("for string_in_Root_te_te_IN, default.txt had %s\n", austrdup(result1)); log_verbose("for string_in_Root_te_te_IN, te_IN.txt had %s\n", austrdup(result2)); /* Test getVersionNumber*/ log_verbose("Testing version number\n"); log_verbose("for getVersionNumber : %s\n", ures_getVersionNumber(test1)); ures_close(test1); ures_close(test2); }
UnicodeString& U_EXPORT2 ZoneMeta::getCanonicalSystemID(const UnicodeString &tzid, UnicodeString &systemID, UErrorCode& status) { int32_t len = tzid.length(); if ( len >= ZID_KEY_MAX ) { status = U_ILLEGAL_ARGUMENT_ERROR; systemID.remove(); return systemID; } char id[ZID_KEY_MAX]; const UChar* idChars = tzid.getBuffer(); u_UCharsToChars(idChars,id,len); id[len] = (char) 0; // Make sure it is null terminated. // replace '/' with ':' char *p = id; while (*p++) { if (*p == '/') { *p = ':'; } } UErrorCode tmpStatus = U_ZERO_ERROR; UResourceBundle *top = ures_openDirect(NULL, gTimeZoneTypes, &tmpStatus); UResourceBundle *rb = ures_getByKey(top, gTypeMapTag, NULL, &tmpStatus); ures_getByKey(rb, gTimezoneTag, rb, &tmpStatus); ures_getByKey(rb, id, rb, &tmpStatus); if (U_SUCCESS(tmpStatus)) { // direct map found systemID.setTo(tzid); ures_close(rb); ures_close(top); return systemID; } // If a map element not found, then look for an alias tmpStatus = U_ZERO_ERROR; ures_getByKey(top, gTypeAliasTag, rb, &tmpStatus); ures_getByKey(rb, gTimezoneTag, rb, &tmpStatus); const UChar *alias = ures_getStringByKey(rb,id,NULL,&tmpStatus); if (U_SUCCESS(tmpStatus)) { // alias found ures_close(rb); ures_close(top); systemID.setTo(alias); return systemID; } // Dereference the input ID using the tz data const UChar *derefer = TimeZone::dereferOlsonLink(tzid); if (derefer == NULL) { systemID.remove(); status = U_ILLEGAL_ARGUMENT_ERROR; } else { len = u_strlen(derefer); u_UCharsToChars(derefer,id,len); id[len] = (char) 0; // Make sure it is null terminated. // replace '/' with ':' char *p = id; while (*p++) { if (*p == '/') { *p = ':'; } } // If a dereference turned something up then look for an alias. // rb still points to the alias table, so we don't have to go looking // for it. tmpStatus = U_ZERO_ERROR; const UChar *alias = ures_getStringByKey(rb,id,NULL,&tmpStatus); if (U_SUCCESS(tmpStatus)) { // alias found systemID.setTo(alias); } else { systemID.setTo(derefer); } } ures_close(rb); ures_close(top); return systemID; }
bool hasKey(const char* key) { UErrorCode status = U_ZERO_ERROR; ures_getStringByKey(bundle_, key, NULL, &status); return U_SUCCESS(status); }