michael@0: /* michael@0: ****************************************************************************** michael@0: * Copyright (C) 1997-2013, International Business Machines Corporation and michael@0: * others. All Rights Reserved. michael@0: ****************************************************************************** michael@0: * michael@0: * File URESBUND.C michael@0: * michael@0: * Modification History: michael@0: * michael@0: * Date Name Description michael@0: * 04/01/97 aliu Creation. michael@0: * 06/14/99 stephen Removed functions taking a filename suffix. michael@0: * 07/20/99 stephen Changed for UResourceBundle typedef'd to void* michael@0: * 11/09/99 weiv Added ures_getLocale() michael@0: * March 2000 weiv Total overhaul - using data in DLLs michael@0: * 06/20/2000 helena OS/400 port changes; mostly typecast. michael@0: * 06/24/02 weiv Added support for resource sharing michael@0: ****************************************************************************** michael@0: */ michael@0: michael@0: #include "unicode/ustring.h" michael@0: #include "unicode/ucnv.h" michael@0: #include "charstr.h" michael@0: #include "uresimp.h" michael@0: #include "ustr_imp.h" michael@0: #include "cwchar.h" michael@0: #include "ucln_cmn.h" michael@0: #include "cmemory.h" michael@0: #include "cstring.h" michael@0: #include "uhash.h" michael@0: #include "unicode/uenum.h" michael@0: #include "uenumimp.h" michael@0: #include "ulocimp.h" michael@0: #include "umutex.h" michael@0: #include "putilimp.h" michael@0: #include "uassert.h" michael@0: michael@0: michael@0: /* michael@0: Static cache for already opened resource bundles - mostly for keeping fallback info michael@0: TODO: This cache should probably be removed when the deprecated code is michael@0: completely removed. michael@0: */ michael@0: static UHashtable *cache = NULL; michael@0: static icu::UInitOnce gCacheInitOnce; michael@0: michael@0: static UMutex resbMutex = U_MUTEX_INITIALIZER; michael@0: michael@0: /* INTERNAL: hashes an entry */ michael@0: static int32_t U_CALLCONV hashEntry(const UHashTok parm) { michael@0: UResourceDataEntry *b = (UResourceDataEntry *)parm.pointer; michael@0: UHashTok namekey, pathkey; michael@0: namekey.pointer = b->fName; michael@0: pathkey.pointer = b->fPath; michael@0: return uhash_hashChars(namekey)+37*uhash_hashChars(pathkey); michael@0: } michael@0: michael@0: /* INTERNAL: compares two entries */ michael@0: static UBool U_CALLCONV compareEntries(const UHashTok p1, const UHashTok p2) { michael@0: UResourceDataEntry *b1 = (UResourceDataEntry *)p1.pointer; michael@0: UResourceDataEntry *b2 = (UResourceDataEntry *)p2.pointer; michael@0: UHashTok name1, name2, path1, path2; michael@0: name1.pointer = b1->fName; michael@0: name2.pointer = b2->fName; michael@0: path1.pointer = b1->fPath; michael@0: path2.pointer = b2->fPath; michael@0: return (UBool)(uhash_compareChars(name1, name2) && michael@0: uhash_compareChars(path1, path2)); michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Internal function, gets parts of locale name according michael@0: * to the position of '_' character michael@0: */ michael@0: static UBool chopLocale(char *name) { michael@0: char *i = uprv_strrchr(name, '_'); michael@0: michael@0: if(i != NULL) { michael@0: *i = '\0'; michael@0: return TRUE; michael@0: } michael@0: michael@0: return FALSE; michael@0: } michael@0: michael@0: /** michael@0: * Internal function michael@0: */ michael@0: static void entryIncrease(UResourceDataEntry *entry) { michael@0: umtx_lock(&resbMutex); michael@0: entry->fCountExisting++; michael@0: while(entry->fParent != NULL) { michael@0: entry = entry->fParent; michael@0: entry->fCountExisting++; michael@0: } michael@0: umtx_unlock(&resbMutex); michael@0: } michael@0: michael@0: /** michael@0: * Internal function. Tries to find a resource in given Resource michael@0: * Bundle, as well as in its parents michael@0: */ michael@0: static const ResourceData *getFallbackData(const UResourceBundle* resBundle, const char* * resTag, UResourceDataEntry* *realData, Resource *res, UErrorCode *status) { michael@0: UResourceDataEntry *resB = resBundle->fData; michael@0: int32_t indexR = -1; michael@0: int32_t i = 0; michael@0: *res = RES_BOGUS; michael@0: if(resB != NULL) { michael@0: if(resB->fBogus == U_ZERO_ERROR) { /* if this resource is real, */ michael@0: *res = res_getTableItemByKey(&(resB->fData), resB->fData.rootRes, &indexR, resTag); /* try to get data from there */ michael@0: i++; michael@0: } michael@0: if(resBundle->fHasFallback == TRUE) { michael@0: while(*res == RES_BOGUS && resB->fParent != NULL) { /* Otherwise, we'll look in parents */ michael@0: resB = resB->fParent; michael@0: if(resB->fBogus == U_ZERO_ERROR) { michael@0: i++; michael@0: *res = res_getTableItemByKey(&(resB->fData), resB->fData.rootRes, &indexR, resTag); michael@0: } michael@0: } michael@0: } michael@0: michael@0: if(*res != RES_BOGUS) { /* If the resource is found in parents, we need to adjust the error */ michael@0: if(i>1) { michael@0: if(uprv_strcmp(resB->fName, uloc_getDefault())==0 || uprv_strcmp(resB->fName, kRootLocaleName)==0) { michael@0: *status = U_USING_DEFAULT_WARNING; michael@0: } else { michael@0: *status = U_USING_FALLBACK_WARNING; michael@0: } michael@0: } michael@0: *realData = resB; michael@0: return (&(resB->fData)); michael@0: } else { /* If resource is not found, we need to give an error */ michael@0: *status = U_MISSING_RESOURCE_ERROR; michael@0: return NULL; michael@0: } michael@0: } else { michael@0: *status = U_MISSING_RESOURCE_ERROR; michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: static void michael@0: free_entry(UResourceDataEntry *entry) { michael@0: UResourceDataEntry *alias; michael@0: res_unload(&(entry->fData)); michael@0: if(entry->fName != NULL && entry->fName != entry->fNameBuffer) { michael@0: uprv_free(entry->fName); michael@0: } michael@0: if(entry->fPath != NULL) { michael@0: uprv_free(entry->fPath); michael@0: } michael@0: if(entry->fPool != NULL) { michael@0: --entry->fPool->fCountExisting; michael@0: } michael@0: alias = entry->fAlias; michael@0: if(alias != NULL) { michael@0: while(alias->fAlias != NULL) { michael@0: alias = alias->fAlias; michael@0: } michael@0: --alias->fCountExisting; michael@0: } michael@0: uprv_free(entry); michael@0: } michael@0: michael@0: /* Works just like ucnv_flushCache() */ michael@0: static int32_t ures_flushCache() michael@0: { michael@0: UResourceDataEntry *resB; michael@0: int32_t pos; michael@0: int32_t rbDeletedNum = 0; michael@0: const UHashElement *e; michael@0: UBool deletedMore; michael@0: michael@0: /*if shared data hasn't even been lazy evaluated yet michael@0: * return 0 michael@0: */ michael@0: umtx_lock(&resbMutex); michael@0: if (cache == NULL) { michael@0: umtx_unlock(&resbMutex); michael@0: return 0; michael@0: } michael@0: michael@0: do { michael@0: deletedMore = FALSE; michael@0: /*creates an enumeration to iterate through every element in the table */ michael@0: pos = -1; michael@0: while ((e = uhash_nextElement(cache, &pos)) != NULL) michael@0: { michael@0: resB = (UResourceDataEntry *) e->value.pointer; michael@0: /* Deletes only if reference counter == 0 michael@0: * Don't worry about the children of this node. michael@0: * Those will eventually get deleted too, if not already. michael@0: * Don't worry about the parents of this node. michael@0: * Those will eventually get deleted too, if not already. michael@0: */ michael@0: /* 04/05/2002 [weiv] fCountExisting should now be accurate. If it's not zero, that means that */ michael@0: /* some resource bundles are still open somewhere. */ michael@0: michael@0: if (resB->fCountExisting == 0) { michael@0: rbDeletedNum++; michael@0: deletedMore = TRUE; michael@0: uhash_removeElement(cache, e); michael@0: free_entry(resB); michael@0: } michael@0: } michael@0: /* michael@0: * Do it again to catch bundles (aliases, pool bundle) whose fCountExisting michael@0: * got decremented by free_entry(). michael@0: */ michael@0: } while(deletedMore); michael@0: umtx_unlock(&resbMutex); michael@0: michael@0: return rbDeletedNum; michael@0: } michael@0: michael@0: #ifdef URES_DEBUG michael@0: #include michael@0: michael@0: U_CAPI UBool U_EXPORT2 ures_dumpCacheContents(void) { michael@0: UBool cacheNotEmpty = FALSE; michael@0: int32_t pos = -1; michael@0: const UHashElement *e; michael@0: UResourceDataEntry *resB; michael@0: michael@0: umtx_lock(&resbMutex); michael@0: if (cache == NULL) { michael@0: umtx_unlock(&resbMutex); michael@0: fprintf(stderr,"%s:%d: RB Cache is NULL.\n", __FILE__, __LINE__); michael@0: return FALSE; michael@0: } michael@0: michael@0: while ((e = uhash_nextElement(cache, &pos)) != NULL) { michael@0: cacheNotEmpty=TRUE; michael@0: resB = (UResourceDataEntry *) e->value.pointer; michael@0: fprintf(stderr,"%s:%d: RB Cache: Entry @0x%p, refcount %d, name %s:%s. Pool 0x%p, alias 0x%p, parent 0x%p\n", michael@0: __FILE__, __LINE__, michael@0: (void*)resB, resB->fCountExisting, michael@0: resB->fName?resB->fName:"NULL", michael@0: resB->fPath?resB->fPath:"NULL", michael@0: (void*)resB->fPool, michael@0: (void*)resB->fAlias, michael@0: (void*)resB->fParent); michael@0: } michael@0: michael@0: fprintf(stderr,"%s:%d: RB Cache still contains %d items.\n", __FILE__, __LINE__, uhash_count(cache)); michael@0: michael@0: umtx_unlock(&resbMutex); michael@0: michael@0: return cacheNotEmpty; michael@0: } michael@0: michael@0: #endif michael@0: michael@0: static UBool U_CALLCONV ures_cleanup(void) michael@0: { michael@0: if (cache != NULL) { michael@0: ures_flushCache(); michael@0: uhash_close(cache); michael@0: cache = NULL; michael@0: } michael@0: gCacheInitOnce.reset(); michael@0: return TRUE; michael@0: } michael@0: michael@0: /** INTERNAL: Initializes the cache for resources */ michael@0: static void createCache(UErrorCode &status) { michael@0: U_ASSERT(cache == NULL); michael@0: cache = uhash_open(hashEntry, compareEntries, NULL, &status); michael@0: ucln_common_registerCleanup(UCLN_COMMON_URES, ures_cleanup); michael@0: } michael@0: michael@0: static void initCache(UErrorCode *status) { michael@0: umtx_initOnce(gCacheInitOnce, &createCache, *status); michael@0: } michael@0: michael@0: /** INTERNAL: sets the name (locale) of the resource bundle to given name */ michael@0: michael@0: static void setEntryName(UResourceDataEntry *res, const char *name, UErrorCode *status) { michael@0: int32_t len = (int32_t)uprv_strlen(name); michael@0: if(res->fName != NULL && res->fName != res->fNameBuffer) { michael@0: uprv_free(res->fName); michael@0: } michael@0: if (len < (int32_t)sizeof(res->fNameBuffer)) { michael@0: res->fName = res->fNameBuffer; michael@0: } michael@0: else { michael@0: res->fName = (char *)uprv_malloc(len+1); michael@0: } michael@0: if(res->fName == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: } else { michael@0: uprv_strcpy(res->fName, name); michael@0: } michael@0: } michael@0: michael@0: static UResourceDataEntry * michael@0: getPoolEntry(const char *path, UErrorCode *status); michael@0: michael@0: /** michael@0: * INTERNAL: Inits and opens an entry from a data DLL. michael@0: * CAUTION: resbMutex must be locked when calling this function. michael@0: */ michael@0: static UResourceDataEntry *init_entry(const char *localeID, const char *path, UErrorCode *status) { michael@0: UResourceDataEntry *r = NULL; michael@0: UResourceDataEntry find; michael@0: /*int32_t hashValue;*/ michael@0: const char *name; michael@0: char aliasName[100] = { 0 }; michael@0: int32_t aliasLen = 0; michael@0: /*UBool isAlias = FALSE;*/ michael@0: /*UHashTok hashkey; */ michael@0: michael@0: if(U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: michael@0: /* here we try to deduce the right locale name */ michael@0: if(localeID == NULL) { /* if localeID is NULL, we're trying to open default locale */ michael@0: name = uloc_getDefault(); michael@0: } else if(*localeID == 0) { /* if localeID is "" then we try to open root locale */ michael@0: name = kRootLocaleName; michael@0: } else { /* otherwise, we'll open what we're given */ michael@0: name = localeID; michael@0: } michael@0: michael@0: find.fName = (char *)name; michael@0: find.fPath = (char *)path; michael@0: michael@0: /* calculate the hash value of the entry */ michael@0: /*hashkey.pointer = (void *)&find;*/ michael@0: /*hashValue = hashEntry(hashkey);*/ michael@0: michael@0: /* check to see if we already have this entry */ michael@0: r = (UResourceDataEntry *)uhash_get(cache, &find); michael@0: if(r == NULL) { michael@0: /* if the entry is not yet in the hash table, we'll try to construct a new one */ michael@0: r = (UResourceDataEntry *) uprv_malloc(sizeof(UResourceDataEntry)); michael@0: if(r == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: uprv_memset(r, 0, sizeof(UResourceDataEntry)); michael@0: /*r->fHashKey = hashValue;*/ michael@0: michael@0: setEntryName(r, name, status); michael@0: if (U_FAILURE(*status)) { michael@0: uprv_free(r); michael@0: return NULL; michael@0: } michael@0: michael@0: if(path != NULL) { michael@0: r->fPath = (char *)uprv_strdup(path); michael@0: if(r->fPath == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: uprv_free(r); michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: /* this is the actual loading */ michael@0: res_load(&(r->fData), r->fPath, r->fName, status); michael@0: michael@0: if (U_FAILURE(*status)) { michael@0: /* we have no such entry in dll, so it will always use fallback */ michael@0: *status = U_USING_FALLBACK_WARNING; michael@0: r->fBogus = U_USING_FALLBACK_WARNING; michael@0: } else { /* if we have a regular entry */ michael@0: Resource aliasres; michael@0: if (r->fData.usesPoolBundle) { michael@0: r->fPool = getPoolEntry(r->fPath, status); michael@0: if (U_SUCCESS(*status)) { michael@0: const int32_t *poolIndexes = r->fPool->fData.pRoot + 1; michael@0: if(r->fData.pRoot[1 + URES_INDEX_POOL_CHECKSUM] == poolIndexes[URES_INDEX_POOL_CHECKSUM]) { michael@0: r->fData.poolBundleKeys = (const char *)(poolIndexes + (poolIndexes[URES_INDEX_LENGTH] & 0xff)); michael@0: } else { michael@0: r->fBogus = *status = U_INVALID_FORMAT_ERROR; michael@0: } michael@0: } else { michael@0: r->fBogus = *status; michael@0: } michael@0: } michael@0: if (U_SUCCESS(*status)) { michael@0: /* handle the alias by trying to get out the %%Alias tag.*/ michael@0: /* We'll try to get alias string from the bundle */ michael@0: aliasres = res_getResource(&(r->fData), "%%ALIAS"); michael@0: if (aliasres != RES_BOGUS) { michael@0: const UChar *alias = res_getString(&(r->fData), aliasres, &aliasLen); michael@0: if(alias != NULL && aliasLen > 0) { /* if there is actual alias - unload and load new data */ michael@0: u_UCharsToChars(alias, aliasName, aliasLen+1); michael@0: r->fAlias = init_entry(aliasName, path, status); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: { michael@0: UResourceDataEntry *oldR = NULL; michael@0: if((oldR = (UResourceDataEntry *)uhash_get(cache, r)) == NULL) { /* if the data is not cached */ michael@0: /* just insert it in the cache */ michael@0: UErrorCode cacheStatus = U_ZERO_ERROR; michael@0: uhash_put(cache, (void *)r, r, &cacheStatus); michael@0: if (U_FAILURE(cacheStatus)) { michael@0: *status = cacheStatus; michael@0: free_entry(r); michael@0: r = NULL; michael@0: } michael@0: } else { michael@0: /* somebody have already inserted it while we were working, discard newly opened data */ michael@0: /* Also, we could get here IF we opened an alias */ michael@0: free_entry(r); michael@0: r = oldR; michael@0: } michael@0: } michael@0: michael@0: } michael@0: if(r != NULL) { michael@0: /* return the real bundle */ michael@0: while(r->fAlias != NULL) { michael@0: r = r->fAlias; michael@0: } michael@0: r->fCountExisting++; /* we increase its reference count */ michael@0: /* if the resource has a warning */ michael@0: /* we don't want to overwrite a status with no error */ michael@0: if(r->fBogus != U_ZERO_ERROR && U_SUCCESS(*status)) { michael@0: *status = r->fBogus; /* set the returning status */ michael@0: } michael@0: } michael@0: return r; michael@0: } michael@0: michael@0: static UResourceDataEntry * michael@0: getPoolEntry(const char *path, UErrorCode *status) { michael@0: UResourceDataEntry *poolBundle = init_entry(kPoolBundleName, path, status); michael@0: if( U_SUCCESS(*status) && michael@0: (poolBundle == NULL || poolBundle->fBogus != U_ZERO_ERROR || !poolBundle->fData.isPoolBundle) michael@0: ) { michael@0: *status = U_INVALID_FORMAT_ERROR; michael@0: } michael@0: return poolBundle; michael@0: } michael@0: michael@0: /* INTERNAL: */ michael@0: /* CAUTION: resbMutex must be locked when calling this function! */ michael@0: static UResourceDataEntry *findFirstExisting(const char* path, char* name, UBool *isRoot, UBool *hasChopped, UBool *isDefault, UErrorCode* status) { michael@0: UResourceDataEntry *r = NULL; michael@0: UBool hasRealData = FALSE; michael@0: const char *defaultLoc = uloc_getDefault(); michael@0: *hasChopped = TRUE; /* we're starting with a fresh name */ michael@0: michael@0: while(*hasChopped && !hasRealData) { michael@0: r = init_entry(name, path, status); michael@0: /* Null pointer test */ michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: *isDefault = (UBool)(uprv_strncmp(name, defaultLoc, uprv_strlen(name)) == 0); michael@0: hasRealData = (UBool)(r->fBogus == U_ZERO_ERROR); michael@0: if(!hasRealData) { michael@0: /* this entry is not real. We will discard it. */ michael@0: /* However, the parent line for this entry is */ michael@0: /* not to be used - as there might be parent */ michael@0: /* lines in cache from previous openings that */ michael@0: /* are not updated yet. */ michael@0: r->fCountExisting--; michael@0: /*entryCloseInt(r);*/ michael@0: r = NULL; michael@0: *status = U_USING_FALLBACK_WARNING; michael@0: } else { michael@0: uprv_strcpy(name, r->fName); /* this is needed for supporting aliases */ michael@0: } michael@0: michael@0: *isRoot = (UBool)(uprv_strcmp(name, kRootLocaleName) == 0); michael@0: michael@0: /*Fallback data stuff*/ michael@0: *hasChopped = chopLocale(name); michael@0: } michael@0: return r; michael@0: } michael@0: michael@0: static void ures_setIsStackObject( UResourceBundle* resB, UBool state) { michael@0: if(state) { michael@0: resB->fMagic1 = 0; michael@0: resB->fMagic2 = 0; michael@0: } else { michael@0: resB->fMagic1 = MAGIC1; michael@0: resB->fMagic2 = MAGIC2; michael@0: } michael@0: } michael@0: michael@0: static UBool ures_isStackObject(const UResourceBundle* resB) { michael@0: return((resB->fMagic1 == MAGIC1 && resB->fMagic2 == MAGIC2)?FALSE:TRUE); michael@0: } michael@0: michael@0: michael@0: U_CFUNC void ures_initStackObject(UResourceBundle* resB) { michael@0: uprv_memset(resB, 0, sizeof(UResourceBundle)); michael@0: ures_setIsStackObject(resB, TRUE); michael@0: } michael@0: michael@0: static UResourceDataEntry *entryOpen(const char* path, const char* localeID, UErrorCode* status) { michael@0: UErrorCode intStatus = U_ZERO_ERROR; michael@0: UErrorCode parentStatus = U_ZERO_ERROR; michael@0: UErrorCode usrStatus = U_ZERO_ERROR; michael@0: UResourceDataEntry *r = NULL; michael@0: UResourceDataEntry *t1 = NULL; michael@0: UResourceDataEntry *t2 = NULL; michael@0: UResourceDataEntry *u1 = NULL; michael@0: UResourceDataEntry *u2 = NULL; michael@0: UBool isDefault = FALSE; michael@0: UBool isRoot = FALSE; michael@0: UBool hasRealData = FALSE; michael@0: UBool hasChopped = TRUE; michael@0: UBool usingUSRData = U_USE_USRDATA && ( path == NULL || uprv_strncmp(path,U_ICUDATA_NAME,8) == 0); michael@0: michael@0: char name[ULOC_FULLNAME_CAPACITY]; michael@0: char usrDataPath[96]; michael@0: michael@0: initCache(status); michael@0: michael@0: if(U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: michael@0: uprv_strncpy(name, localeID, sizeof(name) - 1); michael@0: name[sizeof(name) - 1] = 0; michael@0: michael@0: if ( usingUSRData ) { michael@0: if ( path == NULL ) { michael@0: uprv_strcpy(usrDataPath, U_USRDATA_NAME); michael@0: } else { michael@0: uprv_strncpy(usrDataPath, path, sizeof(usrDataPath) - 1); michael@0: usrDataPath[0] = 'u'; michael@0: usrDataPath[1] = 's'; michael@0: usrDataPath[2] = 'r'; michael@0: usrDataPath[sizeof(usrDataPath) - 1] = 0; michael@0: } michael@0: } michael@0: michael@0: umtx_lock(&resbMutex); michael@0: { /* umtx_lock */ michael@0: /* We're going to skip all the locales that do not have any data */ michael@0: r = findFirstExisting(path, name, &isRoot, &hasChopped, &isDefault, &intStatus); michael@0: michael@0: if(r != NULL) { /* if there is one real locale, we can look for parents. */ michael@0: t1 = r; michael@0: hasRealData = TRUE; michael@0: if ( usingUSRData ) { /* This code inserts user override data into the inheritance chain */ michael@0: u1 = init_entry(t1->fName, usrDataPath, &usrStatus); michael@0: if ( u1 != NULL ) { michael@0: if(u1->fBogus == U_ZERO_ERROR) { michael@0: u1->fParent = t1; michael@0: r = u1; michael@0: } else { michael@0: /* the USR override data wasn't found, set it to be deleted */ michael@0: u1->fCountExisting = 0; michael@0: } michael@0: } michael@0: } michael@0: while (hasChopped && !isRoot && t1->fParent == NULL && !t1->fData.noFallback) { michael@0: if ( res_getResource(&t1->fData,"%%Parent") != RES_BOGUS) { /* An explicit parent was found */ michael@0: int32_t parentLocaleLen = 0; michael@0: const UChar *parentLocaleName = res_getString(&(t1->fData), res_getResource(&t1->fData,"%%Parent") , &parentLocaleLen); michael@0: if(parentLocaleName != NULL && parentLocaleLen > 0) { michael@0: u_UCharsToChars(parentLocaleName, name, parentLocaleLen+1); michael@0: if ( !uprv_strcmp(name,"root") ) { /* If parent is root, we just terminate the loop */ michael@0: hasChopped = FALSE; michael@0: continue; michael@0: } michael@0: } michael@0: } michael@0: /* insert regular parents */ michael@0: t2 = init_entry(name, t1->fPath, &parentStatus); michael@0: if ( usingUSRData ) { /* This code inserts user override data into the inheritance chain */ michael@0: usrStatus = U_ZERO_ERROR; michael@0: u2 = init_entry(name, usrDataPath, &usrStatus); michael@0: } michael@0: /* Check for null pointer. */ michael@0: if (t2 == NULL || ( usingUSRData && u2 == NULL)) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: goto finishUnlock; michael@0: } michael@0: michael@0: if ( usingUSRData && u2->fBogus == U_ZERO_ERROR ) { michael@0: t1->fParent = u2; michael@0: u2->fParent = t2; michael@0: } else { michael@0: t1->fParent = t2; michael@0: if(usingUSRData) { michael@0: /* the USR override data wasn't found, set it to be deleted */ michael@0: u2->fCountExisting = 0; michael@0: } michael@0: } michael@0: t1 = t2; michael@0: hasChopped = chopLocale(name); michael@0: } michael@0: } michael@0: michael@0: /* we could have reached this point without having any real data */ michael@0: /* if that is the case, we need to chain in the default locale */ michael@0: if(r==NULL && !isDefault && !isRoot /*&& t1->fParent == NULL*/) { michael@0: /* insert default locale */ michael@0: uprv_strcpy(name, uloc_getDefault()); michael@0: r = findFirstExisting(path, name, &isRoot, &hasChopped, &isDefault, &intStatus); michael@0: intStatus = U_USING_DEFAULT_WARNING; michael@0: if(r != NULL) { /* the default locale exists */ michael@0: t1 = r; michael@0: hasRealData = TRUE; michael@0: isDefault = TRUE; michael@0: while (hasChopped && t1->fParent == NULL) { michael@0: if ( res_getResource(&t1->fData,"%%Parent") != RES_BOGUS) { /* An explicit parent was found */ michael@0: int32_t parentLocaleLen = 0; michael@0: const UChar *parentLocaleName = res_getString(&(t1->fData), res_getResource(&t1->fData,"%%Parent") , &parentLocaleLen); michael@0: if(parentLocaleName != NULL && parentLocaleLen > 0) { michael@0: u_UCharsToChars(parentLocaleName, name, parentLocaleLen+1); michael@0: if ( !uprv_strcmp(name,"root") ) { /* If parent is root, we just terminate the loop */ michael@0: hasChopped = FALSE; michael@0: continue; michael@0: } michael@0: } michael@0: } michael@0: /* insert chopped defaults */ michael@0: t2 = init_entry(name, t1->fPath, &parentStatus); michael@0: /* Check for null pointer. */ michael@0: if (t2 == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: goto finishUnlock; michael@0: } michael@0: michael@0: if ( res_getResource(&t1->fData,"%%ParentIsRoot") == RES_BOGUS) { michael@0: t1->fParent = t2; michael@0: t1 = t2; michael@0: } michael@0: hasChopped = chopLocale(name); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* we could still have r == NULL at this point - maybe even default locale is not */ michael@0: /* present */ michael@0: if(r == NULL) { michael@0: uprv_strcpy(name, kRootLocaleName); michael@0: r = findFirstExisting(path, name, &isRoot, &hasChopped, &isDefault, &intStatus); michael@0: if(r != NULL) { michael@0: t1 = r; michael@0: intStatus = U_USING_DEFAULT_WARNING; michael@0: hasRealData = TRUE; michael@0: } else { /* we don't even have the root locale */ michael@0: *status = U_MISSING_RESOURCE_ERROR; michael@0: goto finishUnlock; michael@0: } michael@0: } else if(!isRoot && uprv_strcmp(t1->fName, kRootLocaleName) != 0 && t1->fParent == NULL && !r->fData.noFallback) { michael@0: /* insert root locale */ michael@0: t2 = init_entry(kRootLocaleName, t1->fPath, &parentStatus); michael@0: /* Check for null pointer. */ michael@0: if (t2 == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: goto finishUnlock; michael@0: } michael@0: if(!hasRealData) { michael@0: r->fBogus = U_USING_DEFAULT_WARNING; michael@0: } michael@0: hasRealData = (UBool)((t2->fBogus == U_ZERO_ERROR) || hasRealData); michael@0: t1->fParent = t2; michael@0: t1 = t2; michael@0: } michael@0: michael@0: while(r != NULL && !isRoot && t1->fParent != NULL) { michael@0: t1->fParent->fCountExisting++; michael@0: t1 = t1->fParent; michael@0: hasRealData = (UBool)((t1->fBogus == U_ZERO_ERROR) || hasRealData); michael@0: } michael@0: } /* umtx_lock */ michael@0: finishUnlock: michael@0: umtx_unlock(&resbMutex); michael@0: michael@0: if(U_SUCCESS(*status)) { michael@0: if(U_SUCCESS(parentStatus)) { michael@0: if(intStatus != U_ZERO_ERROR) { michael@0: *status = intStatus; michael@0: } michael@0: return r; michael@0: } else { michael@0: *status = parentStatus; michael@0: return NULL; michael@0: } michael@0: } else { michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Functions to create and destroy resource bundles. michael@0: * CAUTION: resbMutex must be locked when calling this function. michael@0: */ michael@0: /* INTERNAL: */ michael@0: static void entryCloseInt(UResourceDataEntry *resB) { michael@0: UResourceDataEntry *p = resB; michael@0: michael@0: while(resB != NULL) { michael@0: p = resB->fParent; michael@0: resB->fCountExisting--; michael@0: michael@0: /* Entries are left in the cache. TODO: add ures_flushCache() to force a flush michael@0: of the cache. */ michael@0: /* michael@0: if(resB->fCountExisting <= 0) { michael@0: uhash_remove(cache, resB); michael@0: if(resB->fBogus == U_ZERO_ERROR) { michael@0: res_unload(&(resB->fData)); michael@0: } michael@0: if(resB->fName != NULL) { michael@0: uprv_free(resB->fName); michael@0: } michael@0: if(resB->fPath != NULL) { michael@0: uprv_free(resB->fPath); michael@0: } michael@0: uprv_free(resB); michael@0: } michael@0: */ michael@0: michael@0: resB = p; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * API: closes a resource bundle and cleans up. michael@0: */ michael@0: michael@0: static void entryClose(UResourceDataEntry *resB) { michael@0: umtx_lock(&resbMutex); michael@0: entryCloseInt(resB); michael@0: umtx_unlock(&resbMutex); michael@0: } michael@0: michael@0: /* michael@0: U_CFUNC void ures_setResPath(UResourceBundle *resB, const char* toAdd) { michael@0: if(resB->fResPath == NULL) { michael@0: resB->fResPath = resB->fResBuf; michael@0: *(resB->fResPath) = 0; michael@0: } michael@0: resB->fResPathLen = uprv_strlen(toAdd); michael@0: if(RES_BUFSIZE <= resB->fResPathLen+1) { michael@0: if(resB->fResPath == resB->fResBuf) { michael@0: resB->fResPath = (char *)uprv_malloc((resB->fResPathLen+1)*sizeof(char)); michael@0: } else { michael@0: resB->fResPath = (char *)uprv_realloc(resB->fResPath, (resB->fResPathLen+1)*sizeof(char)); michael@0: } michael@0: } michael@0: uprv_strcpy(resB->fResPath, toAdd); michael@0: } michael@0: */ michael@0: static void ures_appendResPath(UResourceBundle *resB, const char* toAdd, int32_t lenToAdd, UErrorCode *status) { michael@0: int32_t resPathLenOrig = resB->fResPathLen; michael@0: if(resB->fResPath == NULL) { michael@0: resB->fResPath = resB->fResBuf; michael@0: *(resB->fResPath) = 0; michael@0: resB->fResPathLen = 0; michael@0: } michael@0: resB->fResPathLen += lenToAdd; michael@0: if(RES_BUFSIZE <= resB->fResPathLen+1) { michael@0: if(resB->fResPath == resB->fResBuf) { michael@0: resB->fResPath = (char *)uprv_malloc((resB->fResPathLen+1)*sizeof(char)); michael@0: /* Check that memory was allocated correctly. */ michael@0: if (resB->fResPath == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return; michael@0: } michael@0: uprv_strcpy(resB->fResPath, resB->fResBuf); michael@0: } else { michael@0: char *temp = (char *)uprv_realloc(resB->fResPath, (resB->fResPathLen+1)*sizeof(char)); michael@0: /* Check that memory was reallocated correctly. */ michael@0: if (temp == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return; michael@0: } michael@0: resB->fResPath = temp; michael@0: } michael@0: } michael@0: uprv_strcpy(resB->fResPath + resPathLenOrig, toAdd); michael@0: } michael@0: michael@0: static void ures_freeResPath(UResourceBundle *resB) { michael@0: if (resB->fResPath && resB->fResPath != resB->fResBuf) { michael@0: uprv_free(resB->fResPath); michael@0: } michael@0: resB->fResPath = NULL; michael@0: resB->fResPathLen = 0; michael@0: } michael@0: michael@0: static void michael@0: ures_closeBundle(UResourceBundle* resB, UBool freeBundleObj) michael@0: { michael@0: if(resB != NULL) { michael@0: if(resB->fData != NULL) { michael@0: entryClose(resB->fData); michael@0: } michael@0: if(resB->fVersion != NULL) { michael@0: uprv_free(resB->fVersion); michael@0: } michael@0: ures_freeResPath(resB); michael@0: michael@0: if(ures_isStackObject(resB) == FALSE && freeBundleObj) { michael@0: uprv_free(resB); michael@0: } michael@0: #if 0 /*U_DEBUG*/ michael@0: else { michael@0: /* poison the data */ michael@0: uprv_memset(resB, -1, sizeof(UResourceBundle)); michael@0: } michael@0: #endif michael@0: } michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: ures_close(UResourceBundle* resB) michael@0: { michael@0: ures_closeBundle(resB, TRUE); michael@0: } michael@0: michael@0: static UResourceBundle *init_resb_result(const ResourceData *rdata, Resource r, michael@0: const char *key, int32_t idx, UResourceDataEntry *realData, michael@0: const UResourceBundle *parent, int32_t noAlias, michael@0: UResourceBundle *resB, UErrorCode *status) michael@0: { michael@0: if(status == NULL || U_FAILURE(*status)) { michael@0: return resB; michael@0: } michael@0: if (parent == NULL) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: if(RES_GET_TYPE(r) == URES_ALIAS) { /* This is an alias, need to exchange with real data */ michael@0: if(noAlias < URES_MAX_ALIAS_LEVEL) { michael@0: int32_t len = 0; michael@0: const UChar *alias = res_getAlias(rdata, r, &len); michael@0: if(len > 0) { michael@0: /* we have an alias, now let's cut it up */ michael@0: char stackAlias[200]; michael@0: char *chAlias = NULL, *path = NULL, *locale = NULL, *keyPath = NULL; michael@0: int32_t capacity; michael@0: michael@0: /* michael@0: * Allocate enough space for both the char * version michael@0: * of the alias and parent->fResPath. michael@0: * michael@0: * We do this so that res_findResource() can modify the path, michael@0: * which allows us to remove redundant _res_findResource() variants michael@0: * in uresdata.c. michael@0: * res_findResource() now NUL-terminates each segment so that table keys michael@0: * can always be compared with strcmp() instead of strncmp(). michael@0: * Saves code there and simplifies testing and code coverage. michael@0: * michael@0: * markus 2003oct17 michael@0: */ michael@0: ++len; /* count the terminating NUL */ michael@0: if(parent->fResPath != NULL) { michael@0: capacity = (int32_t)uprv_strlen(parent->fResPath) + 1; michael@0: } else { michael@0: capacity = 0; michael@0: } michael@0: if(capacity < len) { michael@0: capacity = len; michael@0: } michael@0: if(capacity <= (int32_t)sizeof(stackAlias)) { michael@0: capacity = (int32_t)sizeof(stackAlias); michael@0: chAlias = stackAlias; michael@0: } else { michael@0: chAlias = (char *)uprv_malloc(capacity); michael@0: /* test for NULL */ michael@0: if(chAlias == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return NULL; michael@0: } michael@0: } michael@0: u_UCharsToChars(alias, chAlias, len); michael@0: michael@0: if(*chAlias == RES_PATH_SEPARATOR) { michael@0: /* there is a path included */ michael@0: locale = uprv_strchr(chAlias+1, RES_PATH_SEPARATOR); michael@0: if(locale == NULL) { michael@0: locale = uprv_strchr(chAlias, 0); /* avoid locale == NULL to make code below work */ michael@0: } else { michael@0: *locale = 0; michael@0: locale++; michael@0: } michael@0: path = chAlias+1; michael@0: if(uprv_strcmp(path, "LOCALE") == 0) { michael@0: /* this is an XPath alias, starting with "/LOCALE/" */ michael@0: /* it contains the path to a resource which should be looked up */ michael@0: /* starting in the requested locale */ michael@0: keyPath = locale; michael@0: locale = parent->fTopLevelData->fName; /* this is the requested locale's name */ michael@0: path = realData->fPath; /* we will be looking in the same package */ michael@0: } else { michael@0: if(uprv_strcmp(path, "ICUDATA") == 0) { /* want ICU data */ michael@0: path = NULL; michael@0: } michael@0: keyPath = uprv_strchr(locale, RES_PATH_SEPARATOR); michael@0: if(keyPath) { michael@0: *keyPath = 0; michael@0: keyPath++; michael@0: } michael@0: } michael@0: } else { michael@0: /* no path, start with a locale */ michael@0: locale = chAlias; michael@0: keyPath = uprv_strchr(locale, RES_PATH_SEPARATOR); michael@0: if(keyPath) { michael@0: *keyPath = 0; michael@0: keyPath++; michael@0: } michael@0: path = realData->fPath; michael@0: } michael@0: michael@0: michael@0: { michael@0: /* got almost everything, let's try to open */ michael@0: /* first, open the bundle with real data */ michael@0: UResourceBundle *result = resB; michael@0: const char* temp = NULL; michael@0: UErrorCode intStatus = U_ZERO_ERROR; michael@0: UResourceBundle *mainRes = ures_openDirect(path, locale, &intStatus); michael@0: if(U_SUCCESS(intStatus)) { michael@0: if(keyPath == NULL) { michael@0: /* no key path. This means that we are going to michael@0: * to use the corresponding resource from michael@0: * another bundle michael@0: */ michael@0: /* first, we are going to get a corresponding parent michael@0: * resource to the one we are searching. michael@0: */ michael@0: char *aKey = parent->fResPath; michael@0: if(aKey) { michael@0: uprv_strcpy(chAlias, aKey); /* allocated large enough above */ michael@0: aKey = chAlias; michael@0: r = res_findResource(&(mainRes->fResData), mainRes->fRes, &aKey, &temp); michael@0: } else { michael@0: r = mainRes->fRes; michael@0: } michael@0: if(key) { michael@0: /* we need to make keyPath from parent's fResPath and michael@0: * current key, if there is a key associated michael@0: */ michael@0: len = (int32_t)(uprv_strlen(key) + 1); michael@0: if(len > capacity) { michael@0: capacity = len; michael@0: if(chAlias == stackAlias) { michael@0: chAlias = (char *)uprv_malloc(capacity); michael@0: } else { michael@0: chAlias = (char *)uprv_realloc(chAlias, capacity); michael@0: } michael@0: if(chAlias == NULL) { michael@0: ures_close(mainRes); michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return NULL; michael@0: } michael@0: } michael@0: uprv_memcpy(chAlias, key, len); michael@0: aKey = chAlias; michael@0: r = res_findResource(&(mainRes->fResData), r, &aKey, &temp); michael@0: } else if(idx != -1) { michael@0: /* if there is no key, but there is an index, try to get by the index */ michael@0: /* here we have either a table or an array, so get the element */ michael@0: int32_t type = RES_GET_TYPE(r); michael@0: if(URES_IS_TABLE(type)) { michael@0: r = res_getTableItemByIndex(&(mainRes->fResData), r, idx, (const char **)&aKey); michael@0: } else { /* array */ michael@0: r = res_getArrayItem(&(mainRes->fResData), r, idx); michael@0: } michael@0: } michael@0: if(r != RES_BOGUS) { michael@0: result = init_resb_result(&(mainRes->fResData), r, temp, -1, mainRes->fData, mainRes, noAlias+1, resB, status); michael@0: } else { michael@0: *status = U_MISSING_RESOURCE_ERROR; michael@0: result = resB; michael@0: } michael@0: } else { michael@0: /* this one is a bit trickier. michael@0: * we start finding keys, but after we resolve one alias, the path might continue. michael@0: * Consider: michael@0: * aliastest:alias { "testtypes/anotheralias/Sequence" } michael@0: * anotheralias:alias { "/ICUDATA/sh/CollationElements" } michael@0: * aliastest resource should finally have the sequence, not collation elements. michael@0: */ michael@0: UResourceDataEntry *dataEntry = mainRes->fData; michael@0: char stackPath[URES_MAX_BUFFER_SIZE]; michael@0: char *pathBuf = stackPath, *myPath = pathBuf; michael@0: if(uprv_strlen(keyPath) > URES_MAX_BUFFER_SIZE) { michael@0: pathBuf = (char *)uprv_malloc((uprv_strlen(keyPath)+1)*sizeof(char)); michael@0: if(pathBuf == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return NULL; michael@0: } michael@0: } michael@0: uprv_strcpy(pathBuf, keyPath); michael@0: result = mainRes; michael@0: /* now we have fallback following here */ michael@0: do { michael@0: r = dataEntry->fData.rootRes; michael@0: /* this loop handles 'found' resources over several levels */ michael@0: while(*myPath && U_SUCCESS(*status)) { michael@0: r = res_findResource(&(dataEntry->fData), r, &myPath, &temp); michael@0: if(r != RES_BOGUS) { /* found a resource, but it might be an indirection */ michael@0: resB = init_resb_result(&(dataEntry->fData), r, temp, -1, dataEntry, result, noAlias+1, resB, status); michael@0: result = resB; michael@0: if(result) { michael@0: r = result->fRes; /* switch to a new resource, possibly a new tree */ michael@0: dataEntry = result->fData; michael@0: } michael@0: } else { /* no resource found, we don't really want to look anymore on this level */ michael@0: break; michael@0: } michael@0: } michael@0: dataEntry = dataEntry->fParent; michael@0: uprv_strcpy(pathBuf, keyPath); michael@0: myPath = pathBuf; michael@0: } while(r == RES_BOGUS && dataEntry != NULL); michael@0: if(r == RES_BOGUS) { michael@0: *status = U_MISSING_RESOURCE_ERROR; michael@0: result = resB; michael@0: } michael@0: if(pathBuf != stackPath) { michael@0: uprv_free(pathBuf); michael@0: } michael@0: } michael@0: } else { /* we failed to open the resource we're aliasing to */ michael@0: *status = intStatus; michael@0: } michael@0: if(chAlias != stackAlias) { michael@0: uprv_free(chAlias); michael@0: } michael@0: if(mainRes != result) { michael@0: ures_close(mainRes); michael@0: } michael@0: return result; michael@0: } michael@0: } else { michael@0: /* bad alias, should be an error */ michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return resB; michael@0: } michael@0: } else { michael@0: *status = U_TOO_MANY_ALIASES_ERROR; michael@0: return resB; michael@0: } michael@0: } michael@0: if(resB == NULL) { michael@0: resB = (UResourceBundle *)uprv_malloc(sizeof(UResourceBundle)); michael@0: /* test for NULL */ michael@0: if (resB == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return NULL; michael@0: } michael@0: ures_setIsStackObject(resB, FALSE); michael@0: resB->fResPath = NULL; michael@0: resB->fResPathLen = 0; michael@0: } else { michael@0: if(resB->fData != NULL) { michael@0: entryClose(resB->fData); michael@0: } michael@0: if(resB->fVersion != NULL) { michael@0: uprv_free(resB->fVersion); michael@0: } michael@0: /* michael@0: weiv: if stack object was passed in, it doesn't really need to be reinited, michael@0: since the purpose of initing is to remove stack junk. However, at this point michael@0: we would not do anything to an allocated object, so stack object should be michael@0: treated the same michael@0: */ michael@0: /* michael@0: if(ures_isStackObject(resB) != FALSE) { michael@0: ures_initStackObject(resB); michael@0: } michael@0: */ michael@0: if(parent != resB) { michael@0: ures_freeResPath(resB); michael@0: } michael@0: } michael@0: resB->fData = realData; michael@0: entryIncrease(resB->fData); michael@0: resB->fHasFallback = FALSE; michael@0: resB->fIsTopLevel = FALSE; michael@0: resB->fIndex = -1; michael@0: resB->fKey = key; michael@0: /*resB->fParentRes = parent;*/ michael@0: resB->fTopLevelData = parent->fTopLevelData; michael@0: if(parent->fResPath && parent != resB) { michael@0: ures_appendResPath(resB, parent->fResPath, parent->fResPathLen, status); michael@0: } michael@0: if(key != NULL) { michael@0: ures_appendResPath(resB, key, (int32_t)uprv_strlen(key), status); michael@0: if(resB->fResPath[resB->fResPathLen-1] != RES_PATH_SEPARATOR) { michael@0: ures_appendResPath(resB, RES_PATH_SEPARATOR_S, 1, status); michael@0: } michael@0: } else if(idx >= 0) { michael@0: char buf[256]; michael@0: int32_t len = T_CString_integerToString(buf, idx, 10); michael@0: ures_appendResPath(resB, buf, len, status); michael@0: if(resB->fResPath[resB->fResPathLen-1] != RES_PATH_SEPARATOR) { michael@0: ures_appendResPath(resB, RES_PATH_SEPARATOR_S, 1, status); michael@0: } michael@0: } michael@0: /* Make sure that Purify doesn't complain about uninitialized memory copies. */ michael@0: { michael@0: int32_t usedLen = ((resB->fResBuf == resB->fResPath) ? resB->fResPathLen : 0); michael@0: uprv_memset(resB->fResBuf + usedLen, 0, sizeof(resB->fResBuf) - usedLen); michael@0: } michael@0: michael@0: resB->fVersion = NULL; michael@0: resB->fRes = r; michael@0: /*resB->fParent = parent->fRes;*/ michael@0: uprv_memmove(&resB->fResData, rdata, sizeof(ResourceData)); michael@0: resB->fSize = res_countArrayItems(&(resB->fResData), resB->fRes); michael@0: return resB; michael@0: } michael@0: michael@0: UResourceBundle *ures_copyResb(UResourceBundle *r, const UResourceBundle *original, UErrorCode *status) { michael@0: UBool isStackObject; michael@0: if(U_FAILURE(*status) || r == original) { michael@0: return r; michael@0: } michael@0: if(original != NULL) { michael@0: if(r == NULL) { michael@0: isStackObject = FALSE; michael@0: r = (UResourceBundle *)uprv_malloc(sizeof(UResourceBundle)); michael@0: /* test for NULL */ michael@0: if (r == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return NULL; michael@0: } michael@0: } else { michael@0: isStackObject = ures_isStackObject(r); michael@0: ures_closeBundle(r, FALSE); michael@0: } michael@0: uprv_memcpy(r, original, sizeof(UResourceBundle)); michael@0: r->fResPath = NULL; michael@0: r->fResPathLen = 0; michael@0: if(original->fResPath) { michael@0: ures_appendResPath(r, original->fResPath, original->fResPathLen, status); michael@0: } michael@0: ures_setIsStackObject(r, isStackObject); michael@0: if(r->fData != NULL) { michael@0: entryIncrease(r->fData); michael@0: } michael@0: } michael@0: return r; michael@0: } michael@0: michael@0: /** michael@0: * Functions to retrieve data from resource bundles. michael@0: */ michael@0: michael@0: U_CAPI const UChar* U_EXPORT2 ures_getString(const UResourceBundle* resB, int32_t* len, UErrorCode* status) { michael@0: const UChar *s; michael@0: if (status==NULL || U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: if(resB == NULL) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: s = res_getString(&(resB->fResData), resB->fRes, len); michael@0: if (s == NULL) { michael@0: *status = U_RESOURCE_TYPE_MISMATCH; michael@0: } michael@0: return s; michael@0: } michael@0: michael@0: static const char * michael@0: ures_toUTF8String(const UChar *s16, int32_t length16, michael@0: char *dest, int32_t *pLength, michael@0: UBool forceCopy, michael@0: UErrorCode *status) { michael@0: int32_t capacity; michael@0: michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: if (pLength != NULL) { michael@0: capacity = *pLength; michael@0: } else { michael@0: capacity = 0; michael@0: } michael@0: if (capacity < 0 || (capacity > 0 && dest == NULL)) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: if (length16 == 0) { michael@0: /* empty string, return as read-only pointer */ michael@0: if (pLength != NULL) { michael@0: *pLength = 0; michael@0: } michael@0: if (forceCopy) { michael@0: u_terminateChars(dest, capacity, 0, status); michael@0: return dest; michael@0: } else { michael@0: return ""; michael@0: } michael@0: } else { michael@0: /* We need to transform the string to the destination buffer. */ michael@0: if (capacity < length16) { michael@0: /* No chance for the string to fit. Pure preflighting. */ michael@0: return u_strToUTF8(NULL, 0, pLength, s16, length16, status); michael@0: } michael@0: if (!forceCopy && (length16 <= 0x2aaaaaaa)) { michael@0: /* michael@0: * We know the string will fit into dest because each UChar turns michael@0: * into at most three UTF-8 bytes. Fill the latter part of dest michael@0: * so that callers do not expect to use dest as a string pointer, michael@0: * hopefully leading to more robust code for when resource bundles michael@0: * may store UTF-8 natively. michael@0: * (In which case dest would not be used at all.) michael@0: * michael@0: * We do not do this if forceCopy=TRUE because then the caller michael@0: * expects the string to start exactly at dest. michael@0: * michael@0: * The test above for <= 0x2aaaaaaa prevents overflows. michael@0: * The +1 is for the NUL terminator. michael@0: */ michael@0: int32_t maxLength = 3 * length16 + 1; michael@0: if (capacity > maxLength) { michael@0: dest += capacity - maxLength; michael@0: capacity = maxLength; michael@0: } michael@0: } michael@0: return u_strToUTF8(dest, capacity, pLength, s16, length16, status); michael@0: } michael@0: } michael@0: michael@0: U_CAPI const char * U_EXPORT2 michael@0: ures_getUTF8String(const UResourceBundle *resB, michael@0: char *dest, int32_t *pLength, michael@0: UBool forceCopy, michael@0: UErrorCode *status) { michael@0: int32_t length16; michael@0: const UChar *s16 = ures_getString(resB, &length16, status); michael@0: return ures_toUTF8String(s16, length16, dest, pLength, forceCopy, status); michael@0: } michael@0: michael@0: U_CAPI const uint8_t* U_EXPORT2 ures_getBinary(const UResourceBundle* resB, int32_t* len, michael@0: UErrorCode* status) { michael@0: const uint8_t *p; michael@0: if (status==NULL || U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: if(resB == NULL) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: p = res_getBinary(&(resB->fResData), resB->fRes, len); michael@0: if (p == NULL) { michael@0: *status = U_RESOURCE_TYPE_MISMATCH; michael@0: } michael@0: return p; michael@0: } michael@0: michael@0: U_CAPI const int32_t* U_EXPORT2 ures_getIntVector(const UResourceBundle* resB, int32_t* len, michael@0: UErrorCode* status) { michael@0: const int32_t *p; michael@0: if (status==NULL || U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: if(resB == NULL) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: p = res_getIntVector(&(resB->fResData), resB->fRes, len); michael@0: if (p == NULL) { michael@0: *status = U_RESOURCE_TYPE_MISMATCH; michael@0: } michael@0: return p; michael@0: } michael@0: michael@0: /* this function returns a signed integer */ michael@0: /* it performs sign extension */ michael@0: U_CAPI int32_t U_EXPORT2 ures_getInt(const UResourceBundle* resB, UErrorCode *status) { michael@0: if (status==NULL || U_FAILURE(*status)) { michael@0: return 0xffffffff; michael@0: } michael@0: if(resB == NULL) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0xffffffff; michael@0: } michael@0: if(RES_GET_TYPE(resB->fRes) != URES_INT) { michael@0: *status = U_RESOURCE_TYPE_MISMATCH; michael@0: return 0xffffffff; michael@0: } michael@0: return RES_GET_INT(resB->fRes); michael@0: } michael@0: michael@0: U_CAPI uint32_t U_EXPORT2 ures_getUInt(const UResourceBundle* resB, UErrorCode *status) { michael@0: if (status==NULL || U_FAILURE(*status)) { michael@0: return 0xffffffff; michael@0: } michael@0: if(resB == NULL) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0xffffffff; michael@0: } michael@0: if(RES_GET_TYPE(resB->fRes) != URES_INT) { michael@0: *status = U_RESOURCE_TYPE_MISMATCH; michael@0: return 0xffffffff; michael@0: } michael@0: return RES_GET_UINT(resB->fRes); michael@0: } michael@0: michael@0: U_CAPI UResType U_EXPORT2 ures_getType(const UResourceBundle *resB) { michael@0: if(resB == NULL) { michael@0: return URES_NONE; michael@0: } michael@0: return res_getPublicType(resB->fRes); michael@0: } michael@0: michael@0: U_CAPI const char * U_EXPORT2 ures_getKey(const UResourceBundle *resB) { michael@0: if(resB == NULL) { michael@0: return NULL; michael@0: } michael@0: michael@0: return(resB->fKey); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 ures_getSize(const UResourceBundle *resB) { michael@0: if(resB == NULL) { michael@0: return 0; michael@0: } michael@0: michael@0: return resB->fSize; michael@0: } michael@0: michael@0: static const UChar* ures_getStringWithAlias(const UResourceBundle *resB, Resource r, int32_t sIndex, int32_t *len, UErrorCode *status) { michael@0: if(RES_GET_TYPE(r) == URES_ALIAS) { michael@0: const UChar* result = 0; michael@0: UResourceBundle *tempRes = ures_getByIndex(resB, sIndex, NULL, status); michael@0: result = ures_getString(tempRes, len, status); michael@0: ures_close(tempRes); michael@0: return result; michael@0: } else { michael@0: return res_getString(&(resB->fResData), r, len); michael@0: } michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 ures_resetIterator(UResourceBundle *resB){ michael@0: if(resB == NULL) { michael@0: return; michael@0: } michael@0: resB->fIndex = -1; michael@0: } michael@0: michael@0: U_CAPI UBool U_EXPORT2 ures_hasNext(const UResourceBundle *resB) { michael@0: if(resB == NULL) { michael@0: return FALSE; michael@0: } michael@0: return (UBool)(resB->fIndex < resB->fSize-1); michael@0: } michael@0: michael@0: U_CAPI const UChar* U_EXPORT2 ures_getNextString(UResourceBundle *resB, int32_t* len, const char ** key, UErrorCode *status) { michael@0: Resource r = RES_BOGUS; michael@0: michael@0: if (status==NULL || U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: if(resB == NULL) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: if(resB->fIndex == resB->fSize-1) { michael@0: *status = U_INDEX_OUTOFBOUNDS_ERROR; michael@0: } else { michael@0: resB->fIndex++; michael@0: switch(RES_GET_TYPE(resB->fRes)) { michael@0: case URES_STRING: michael@0: case URES_STRING_V2: michael@0: return res_getString(&(resB->fResData), resB->fRes, len); michael@0: case URES_TABLE: michael@0: case URES_TABLE16: michael@0: case URES_TABLE32: michael@0: r = res_getTableItemByIndex(&(resB->fResData), resB->fRes, resB->fIndex, key); michael@0: if(r == RES_BOGUS && resB->fHasFallback) { michael@0: /* TODO: do the fallback */ michael@0: } michael@0: return ures_getStringWithAlias(resB, r, resB->fIndex, len, status); michael@0: case URES_ARRAY: michael@0: case URES_ARRAY16: michael@0: r = res_getArrayItem(&(resB->fResData), resB->fRes, resB->fIndex); michael@0: if(r == RES_BOGUS && resB->fHasFallback) { michael@0: /* TODO: do the fallback */ michael@0: } michael@0: return ures_getStringWithAlias(resB, r, resB->fIndex, len, status); michael@0: case URES_ALIAS: michael@0: return ures_getStringWithAlias(resB, resB->fRes, resB->fIndex, len, status); michael@0: case URES_INT: michael@0: case URES_BINARY: michael@0: case URES_INT_VECTOR: michael@0: *status = U_RESOURCE_TYPE_MISMATCH; michael@0: default: /*fall through*/ michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: return NULL; michael@0: } michael@0: michael@0: U_CAPI UResourceBundle* U_EXPORT2 ures_getNextResource(UResourceBundle *resB, UResourceBundle *fillIn, UErrorCode *status) { michael@0: const char *key = NULL; michael@0: Resource r = RES_BOGUS; michael@0: michael@0: if (status==NULL || U_FAILURE(*status)) { michael@0: /*return NULL;*/ michael@0: return fillIn; michael@0: } michael@0: if(resB == NULL) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: /*return NULL;*/ michael@0: return fillIn; michael@0: } michael@0: michael@0: if(resB->fIndex == resB->fSize-1) { michael@0: *status = U_INDEX_OUTOFBOUNDS_ERROR; michael@0: /*return NULL;*/ michael@0: } else { michael@0: resB->fIndex++; michael@0: switch(RES_GET_TYPE(resB->fRes)) { michael@0: case URES_INT: michael@0: case URES_BINARY: michael@0: case URES_STRING: michael@0: case URES_STRING_V2: michael@0: case URES_INT_VECTOR: michael@0: return ures_copyResb(fillIn, resB, status); michael@0: case URES_TABLE: michael@0: case URES_TABLE16: michael@0: case URES_TABLE32: michael@0: r = res_getTableItemByIndex(&(resB->fResData), resB->fRes, resB->fIndex, &key); michael@0: if(r == RES_BOGUS && resB->fHasFallback) { michael@0: /* TODO: do the fallback */ michael@0: } michael@0: return init_resb_result(&(resB->fResData), r, key, resB->fIndex, resB->fData, resB, 0, fillIn, status); michael@0: case URES_ARRAY: michael@0: case URES_ARRAY16: michael@0: r = res_getArrayItem(&(resB->fResData), resB->fRes, resB->fIndex); michael@0: if(r == RES_BOGUS && resB->fHasFallback) { michael@0: /* TODO: do the fallback */ michael@0: } michael@0: return init_resb_result(&(resB->fResData), r, key, resB->fIndex, resB->fData, resB, 0, fillIn, status); michael@0: default: michael@0: /*return NULL;*/ michael@0: return fillIn; michael@0: } michael@0: } michael@0: /*return NULL;*/ michael@0: return fillIn; michael@0: } michael@0: michael@0: U_CAPI UResourceBundle* U_EXPORT2 ures_getByIndex(const UResourceBundle *resB, int32_t indexR, UResourceBundle *fillIn, UErrorCode *status) { michael@0: const char* key = NULL; michael@0: Resource r = RES_BOGUS; michael@0: michael@0: if (status==NULL || U_FAILURE(*status)) { michael@0: /*return NULL;*/ michael@0: return fillIn; michael@0: } michael@0: if(resB == NULL) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: /*return NULL;*/ michael@0: return fillIn; michael@0: } michael@0: michael@0: if(indexR >= 0 && resB->fSize > indexR) { michael@0: switch(RES_GET_TYPE(resB->fRes)) { michael@0: case URES_INT: michael@0: case URES_BINARY: michael@0: case URES_STRING: michael@0: case URES_STRING_V2: michael@0: case URES_INT_VECTOR: michael@0: return ures_copyResb(fillIn, resB, status); michael@0: case URES_TABLE: michael@0: case URES_TABLE16: michael@0: case URES_TABLE32: michael@0: r = res_getTableItemByIndex(&(resB->fResData), resB->fRes, indexR, &key); michael@0: if(r == RES_BOGUS && resB->fHasFallback) { michael@0: /* TODO: do the fallback */ michael@0: } michael@0: return init_resb_result(&(resB->fResData), r, key, indexR, resB->fData, resB, 0, fillIn, status); michael@0: case URES_ARRAY: michael@0: case URES_ARRAY16: michael@0: r = res_getArrayItem(&(resB->fResData), resB->fRes, indexR); michael@0: if(r == RES_BOGUS && resB->fHasFallback) { michael@0: /* TODO: do the fallback */ michael@0: } michael@0: return init_resb_result(&(resB->fResData), r, key, indexR, resB->fData, resB, 0, fillIn, status); michael@0: default: michael@0: /*return NULL;*/ michael@0: return fillIn; michael@0: } michael@0: } else { michael@0: *status = U_MISSING_RESOURCE_ERROR; michael@0: } michael@0: /*return NULL;*/ michael@0: return fillIn; michael@0: } michael@0: michael@0: U_CAPI const UChar* U_EXPORT2 ures_getStringByIndex(const UResourceBundle *resB, int32_t indexS, int32_t* len, UErrorCode *status) { michael@0: const char* key = NULL; michael@0: Resource r = RES_BOGUS; michael@0: michael@0: if (status==NULL || U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: if(resB == NULL) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: if(indexS >= 0 && resB->fSize > indexS) { michael@0: switch(RES_GET_TYPE(resB->fRes)) { michael@0: case URES_STRING: michael@0: case URES_STRING_V2: michael@0: return res_getString(&(resB->fResData), resB->fRes, len); michael@0: case URES_TABLE: michael@0: case URES_TABLE16: michael@0: case URES_TABLE32: michael@0: r = res_getTableItemByIndex(&(resB->fResData), resB->fRes, indexS, &key); michael@0: if(r == RES_BOGUS && resB->fHasFallback) { michael@0: /* TODO: do the fallback */ michael@0: } michael@0: return ures_getStringWithAlias(resB, r, indexS, len, status); michael@0: case URES_ARRAY: michael@0: case URES_ARRAY16: michael@0: r = res_getArrayItem(&(resB->fResData), resB->fRes, indexS); michael@0: if(r == RES_BOGUS && resB->fHasFallback) { michael@0: /* TODO: do the fallback */ michael@0: } michael@0: return ures_getStringWithAlias(resB, r, indexS, len, status); michael@0: case URES_ALIAS: michael@0: return ures_getStringWithAlias(resB, resB->fRes, indexS, len, status); michael@0: case URES_INT: michael@0: case URES_BINARY: michael@0: case URES_INT_VECTOR: michael@0: *status = U_RESOURCE_TYPE_MISMATCH; michael@0: break; michael@0: default: michael@0: /* must not occur */ michael@0: *status = U_INTERNAL_PROGRAM_ERROR; michael@0: break; michael@0: } michael@0: } else { michael@0: *status = U_MISSING_RESOURCE_ERROR; michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: U_CAPI const char * U_EXPORT2 michael@0: ures_getUTF8StringByIndex(const UResourceBundle *resB, michael@0: int32_t idx, michael@0: char *dest, int32_t *pLength, michael@0: UBool forceCopy, michael@0: UErrorCode *status) { michael@0: int32_t length16; michael@0: const UChar *s16 = ures_getStringByIndex(resB, idx, &length16, status); michael@0: return ures_toUTF8String(s16, length16, dest, pLength, forceCopy, status); michael@0: } michael@0: michael@0: /*U_CAPI const char *ures_getResPath(UResourceBundle *resB) { michael@0: return resB->fResPath; michael@0: }*/ michael@0: michael@0: U_CAPI UResourceBundle* U_EXPORT2 michael@0: ures_findResource(const char* path, UResourceBundle *fillIn, UErrorCode *status) michael@0: { michael@0: UResourceBundle *first = NULL; michael@0: UResourceBundle *result = fillIn; michael@0: char *packageName = NULL; michael@0: char *pathToResource = NULL, *save = NULL; michael@0: char *locale = NULL, *localeEnd = NULL; michael@0: int32_t length; michael@0: michael@0: if(status == NULL || U_FAILURE(*status)) { michael@0: return result; michael@0: } michael@0: michael@0: length = (int32_t)(uprv_strlen(path)+1); michael@0: save = pathToResource = (char *)uprv_malloc(length*sizeof(char)); michael@0: /* test for NULL */ michael@0: if(pathToResource == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return result; michael@0: } michael@0: uprv_memcpy(pathToResource, path, length); michael@0: michael@0: locale = pathToResource; michael@0: if(*pathToResource == RES_PATH_SEPARATOR) { /* there is a path specification */ michael@0: pathToResource++; michael@0: packageName = pathToResource; michael@0: pathToResource = uprv_strchr(pathToResource, RES_PATH_SEPARATOR); michael@0: if(pathToResource == NULL) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: } else { michael@0: *pathToResource = 0; michael@0: locale = pathToResource+1; michael@0: } michael@0: } michael@0: michael@0: localeEnd = uprv_strchr(locale, RES_PATH_SEPARATOR); michael@0: if(localeEnd != NULL) { michael@0: *localeEnd = 0; michael@0: } michael@0: michael@0: first = ures_open(packageName, locale, status); michael@0: michael@0: if(U_SUCCESS(*status)) { michael@0: if(localeEnd) { michael@0: result = ures_findSubResource(first, localeEnd+1, fillIn, status); michael@0: } else { michael@0: result = ures_copyResb(fillIn, first, status); michael@0: } michael@0: ures_close(first); michael@0: } michael@0: uprv_free(save); michael@0: return result; michael@0: } michael@0: michael@0: U_CAPI UResourceBundle* U_EXPORT2 michael@0: ures_findSubResource(const UResourceBundle *resB, char* path, UResourceBundle *fillIn, UErrorCode *status) michael@0: { michael@0: Resource res = RES_BOGUS; michael@0: UResourceBundle *result = fillIn; michael@0: const char *key; michael@0: michael@0: if(status == NULL || U_FAILURE(*status)) { michael@0: return result; michael@0: } michael@0: michael@0: /* here we do looping and circular alias checking */ michael@0: /* this loop is here because aliasing is resolved on this level, not on res level */ michael@0: /* so, when we encounter an alias, it is not an aggregate resource, so we return */ michael@0: do { michael@0: res = res_findResource(&(resB->fResData), resB->fRes, &path, &key); michael@0: if(res != RES_BOGUS) { michael@0: result = init_resb_result(&(resB->fResData), res, key, -1, resB->fData, resB, 0, fillIn, status); michael@0: resB = result; michael@0: } else { michael@0: *status = U_MISSING_RESOURCE_ERROR; michael@0: break; michael@0: } michael@0: } while(*path); /* there is more stuff in the path */ michael@0: michael@0: return result; michael@0: } michael@0: U_INTERNAL const UChar* U_EXPORT2 michael@0: ures_getStringByKeyWithFallback(const UResourceBundle *resB, michael@0: const char* inKey, michael@0: int32_t* len, michael@0: UErrorCode *status) { michael@0: michael@0: UResourceBundle stack; michael@0: const UChar* retVal = NULL; michael@0: ures_initStackObject(&stack); michael@0: ures_getByKeyWithFallback(resB, inKey, &stack, status); michael@0: int32_t length; michael@0: retVal = ures_getString(&stack, &length, status); michael@0: ures_close(&stack); michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: if (length == 3 && retVal[0] == EMPTY_SET && retVal[1] == EMPTY_SET && retVal[2] == EMPTY_SET ) { michael@0: retVal = NULL; michael@0: length = 0; michael@0: *status = U_MISSING_RESOURCE_ERROR; michael@0: } michael@0: if (len != NULL) { michael@0: *len = length; michael@0: } michael@0: return retVal; michael@0: } michael@0: michael@0: /* michael@0: Like res_getTableItemByKey but accepts full paths like "NumberElements/latn/patternsShort". michael@0: */ michael@0: static Resource getTableItemByKeyPath(const ResourceData *pResData, Resource table, const char *key) { michael@0: Resource resource = table; /* The current resource */ michael@0: icu::CharString path; michael@0: UErrorCode errorCode = U_ZERO_ERROR; michael@0: path.append(key, errorCode); michael@0: if (U_FAILURE(errorCode)) { return RES_BOGUS; } michael@0: char *pathPart = path.data(); /* Path from current resource to desired resource */ michael@0: UResType type = (UResType)RES_GET_TYPE(resource); /* the current resource type */ michael@0: while (*pathPart && resource != RES_BOGUS && URES_IS_CONTAINER(type)) { michael@0: char *nextPathPart = uprv_strchr(pathPart, RES_PATH_SEPARATOR); michael@0: if (nextPathPart != NULL) { michael@0: *nextPathPart = 0; /* Terminating null for this part of path. */ michael@0: nextPathPart++; michael@0: } else { michael@0: nextPathPart = uprv_strchr(pathPart, 0); michael@0: } michael@0: int32_t t; michael@0: const char *pathP = pathPart; michael@0: resource = res_getTableItemByKey(pResData, resource, &t, &pathP); michael@0: type = (UResType)RES_GET_TYPE(resource); michael@0: pathPart = nextPathPart; michael@0: } michael@0: if (*pathPart) { michael@0: return RES_BOGUS; michael@0: } michael@0: return resource; michael@0: } michael@0: michael@0: U_CAPI UResourceBundle* U_EXPORT2 michael@0: ures_getByKeyWithFallback(const UResourceBundle *resB, michael@0: const char* inKey, michael@0: UResourceBundle *fillIn, michael@0: UErrorCode *status) { michael@0: Resource res = RES_BOGUS, rootRes = RES_BOGUS; michael@0: /*UResourceDataEntry *realData = NULL;*/ michael@0: UResourceBundle *helper = NULL; michael@0: michael@0: if (status==NULL || U_FAILURE(*status)) { michael@0: return fillIn; michael@0: } michael@0: if(resB == NULL) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return fillIn; michael@0: } michael@0: michael@0: int32_t type = RES_GET_TYPE(resB->fRes); michael@0: if(URES_IS_TABLE(type)) { michael@0: res = getTableItemByKeyPath(&(resB->fResData), resB->fRes, inKey); michael@0: const char* key = inKey; michael@0: if(res == RES_BOGUS) { michael@0: UResourceDataEntry *dataEntry = resB->fData; michael@0: char path[256]; michael@0: char* myPath = path; michael@0: const char* resPath = resB->fResPath; michael@0: int32_t len = resB->fResPathLen; michael@0: while(res == RES_BOGUS && dataEntry->fParent != NULL) { /* Otherwise, we'll look in parents */ michael@0: dataEntry = dataEntry->fParent; michael@0: rootRes = dataEntry->fData.rootRes; michael@0: michael@0: if(dataEntry->fBogus == U_ZERO_ERROR) { michael@0: if (len > 0) { michael@0: uprv_memcpy(path, resPath, len); michael@0: } michael@0: uprv_strcpy(path+len, inKey); michael@0: myPath = path; michael@0: key = inKey; michael@0: do { michael@0: res = res_findResource(&(dataEntry->fData), rootRes, &myPath, &key); michael@0: if (RES_GET_TYPE(res) == URES_ALIAS && *myPath) { michael@0: /* We hit an alias, but we didn't finish following the path. */ michael@0: helper = init_resb_result(&(dataEntry->fData), res, NULL, -1, dataEntry, resB, 0, helper, status); michael@0: /*helper = init_resb_result(&(dataEntry->fData), res, inKey, -1, dataEntry, resB, 0, helper, status);*/ michael@0: if(helper) { michael@0: dataEntry = helper->fData; michael@0: rootRes = helper->fRes; michael@0: resPath = helper->fResPath; michael@0: len = helper->fResPathLen; michael@0: michael@0: } else { michael@0: break; michael@0: } michael@0: } michael@0: } while(*myPath); /* Continue until the whole path is consumed */ michael@0: } michael@0: } michael@0: /*const ResourceData *rd = getFallbackData(resB, &key, &realData, &res, status);*/ michael@0: if(res != RES_BOGUS) { michael@0: /* check if resB->fResPath gives the right name here */ michael@0: if(uprv_strcmp(dataEntry->fName, uloc_getDefault())==0 || uprv_strcmp(dataEntry->fName, kRootLocaleName)==0) { michael@0: *status = U_USING_DEFAULT_WARNING; michael@0: } else { michael@0: *status = U_USING_FALLBACK_WARNING; michael@0: } michael@0: michael@0: fillIn = init_resb_result(&(dataEntry->fData), res, inKey, -1, dataEntry, resB, 0, fillIn, status); michael@0: } else { michael@0: *status = U_MISSING_RESOURCE_ERROR; michael@0: } michael@0: } else { michael@0: fillIn = init_resb_result(&(resB->fResData), res, key, -1, resB->fData, resB, 0, fillIn, status); michael@0: } michael@0: } michael@0: else { michael@0: *status = U_RESOURCE_TYPE_MISMATCH; michael@0: } michael@0: ures_close(helper); michael@0: return fillIn; michael@0: } michael@0: michael@0: michael@0: U_CAPI UResourceBundle* U_EXPORT2 ures_getByKey(const UResourceBundle *resB, const char* inKey, UResourceBundle *fillIn, UErrorCode *status) { michael@0: Resource res = RES_BOGUS; michael@0: UResourceDataEntry *realData = NULL; michael@0: const char *key = inKey; michael@0: michael@0: if (status==NULL || U_FAILURE(*status)) { michael@0: return fillIn; michael@0: } michael@0: if(resB == NULL) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return fillIn; michael@0: } michael@0: michael@0: int32_t type = RES_GET_TYPE(resB->fRes); michael@0: if(URES_IS_TABLE(type)) { michael@0: int32_t t; michael@0: res = res_getTableItemByKey(&(resB->fResData), resB->fRes, &t, &key); michael@0: if(res == RES_BOGUS) { michael@0: key = inKey; michael@0: if(resB->fHasFallback == TRUE) { michael@0: const ResourceData *rd = getFallbackData(resB, &key, &realData, &res, status); michael@0: if(U_SUCCESS(*status)) { michael@0: /* check if resB->fResPath gives the right name here */ michael@0: return init_resb_result(rd, res, key, -1, realData, resB, 0, fillIn, status); michael@0: } else { michael@0: *status = U_MISSING_RESOURCE_ERROR; michael@0: } michael@0: } else { michael@0: *status = U_MISSING_RESOURCE_ERROR; michael@0: } michael@0: } else { michael@0: return init_resb_result(&(resB->fResData), res, key, -1, resB->fData, resB, 0, fillIn, status); michael@0: } michael@0: } michael@0: #if 0 michael@0: /* this is a kind of TODO item. If we have an array with an index table, we could do this. */ michael@0: /* not currently */ michael@0: else if(RES_GET_TYPE(resB->fRes) == URES_ARRAY && resB->fHasFallback == TRUE) { michael@0: /* here should go a first attempt to locate the key using index table */ michael@0: const ResourceData *rd = getFallbackData(resB, &key, &realData, &res, status); michael@0: if(U_SUCCESS(*status)) { michael@0: return init_resb_result(rd, res, key, realData, resB, fillIn, status); michael@0: } else { michael@0: *status = U_MISSING_RESOURCE_ERROR; michael@0: } michael@0: } michael@0: #endif michael@0: else { michael@0: *status = U_RESOURCE_TYPE_MISMATCH; michael@0: } michael@0: return fillIn; michael@0: } michael@0: michael@0: U_CAPI const UChar* U_EXPORT2 ures_getStringByKey(const UResourceBundle *resB, const char* inKey, int32_t* len, UErrorCode *status) { michael@0: Resource res = RES_BOGUS; michael@0: UResourceDataEntry *realData = NULL; michael@0: const char* key = inKey; michael@0: michael@0: if (status==NULL || U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: if(resB == NULL) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: int32_t type = RES_GET_TYPE(resB->fRes); michael@0: if(URES_IS_TABLE(type)) { michael@0: int32_t t=0; michael@0: michael@0: res = res_getTableItemByKey(&(resB->fResData), resB->fRes, &t, &key); michael@0: michael@0: if(res == RES_BOGUS) { michael@0: key = inKey; michael@0: if(resB->fHasFallback == TRUE) { michael@0: const ResourceData *rd = getFallbackData(resB, &key, &realData, &res, status); michael@0: if(U_SUCCESS(*status)) { michael@0: switch (RES_GET_TYPE(res)) { michael@0: case URES_STRING: michael@0: case URES_STRING_V2: michael@0: return res_getString(rd, res, len); michael@0: case URES_ALIAS: michael@0: { michael@0: const UChar* result = 0; michael@0: UResourceBundle *tempRes = ures_getByKey(resB, inKey, NULL, status); michael@0: result = ures_getString(tempRes, len, status); michael@0: ures_close(tempRes); michael@0: return result; michael@0: } michael@0: default: michael@0: *status = U_RESOURCE_TYPE_MISMATCH; michael@0: } michael@0: } else { michael@0: *status = U_MISSING_RESOURCE_ERROR; michael@0: } michael@0: } else { michael@0: *status = U_MISSING_RESOURCE_ERROR; michael@0: } michael@0: } else { michael@0: switch (RES_GET_TYPE(res)) { michael@0: case URES_STRING: michael@0: case URES_STRING_V2: michael@0: return res_getString(&(resB->fResData), res, len); michael@0: case URES_ALIAS: michael@0: { michael@0: const UChar* result = 0; michael@0: UResourceBundle *tempRes = ures_getByKey(resB, inKey, NULL, status); michael@0: result = ures_getString(tempRes, len, status); michael@0: ures_close(tempRes); michael@0: return result; michael@0: } michael@0: default: michael@0: *status = U_RESOURCE_TYPE_MISMATCH; michael@0: } michael@0: } michael@0: } michael@0: #if 0 michael@0: /* this is a kind of TODO item. If we have an array with an index table, we could do this. */ michael@0: /* not currently */ michael@0: else if(RES_GET_TYPE(resB->fRes) == URES_ARRAY && resB->fHasFallback == TRUE) { michael@0: /* here should go a first attempt to locate the key using index table */ michael@0: const ResourceData *rd = getFallbackData(resB, &key, &realData, &res, status); michael@0: if(U_SUCCESS(*status)) { michael@0: return res_getString(rd, res, len); michael@0: } else { michael@0: *status = U_MISSING_RESOURCE_ERROR; michael@0: } michael@0: } michael@0: #endif michael@0: else { michael@0: *status = U_RESOURCE_TYPE_MISMATCH; michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: U_CAPI const char * U_EXPORT2 michael@0: ures_getUTF8StringByKey(const UResourceBundle *resB, michael@0: const char *key, michael@0: char *dest, int32_t *pLength, michael@0: UBool forceCopy, michael@0: UErrorCode *status) { michael@0: int32_t length16; michael@0: const UChar *s16 = ures_getStringByKey(resB, key, &length16, status); michael@0: return ures_toUTF8String(s16, length16, dest, pLength, forceCopy, status); michael@0: } michael@0: michael@0: /* TODO: clean from here down */ michael@0: michael@0: /** michael@0: * INTERNAL: Get the name of the first real locale (not placeholder) michael@0: * that has resource bundle data. michael@0: */ michael@0: U_INTERNAL const char* U_EXPORT2 michael@0: ures_getLocaleInternal(const UResourceBundle* resourceBundle, UErrorCode* status) michael@0: { michael@0: if (status==NULL || U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: if (!resourceBundle) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } else { michael@0: return resourceBundle->fData->fName; michael@0: } michael@0: } michael@0: michael@0: U_CAPI const char* U_EXPORT2 michael@0: ures_getLocale(const UResourceBundle* resourceBundle, michael@0: UErrorCode* status) michael@0: { michael@0: return ures_getLocaleInternal(resourceBundle, status); michael@0: } michael@0: michael@0: michael@0: U_CAPI const char* U_EXPORT2 michael@0: ures_getLocaleByType(const UResourceBundle* resourceBundle, michael@0: ULocDataLocaleType type, michael@0: UErrorCode* status) { michael@0: if (status==NULL || U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: if (!resourceBundle) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } else { michael@0: switch(type) { michael@0: case ULOC_ACTUAL_LOCALE: michael@0: return resourceBundle->fData->fName; michael@0: case ULOC_VALID_LOCALE: michael@0: return resourceBundle->fTopLevelData->fName; michael@0: case ULOC_REQUESTED_LOCALE: michael@0: return NULL; michael@0: default: michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: } michael@0: } michael@0: michael@0: U_CFUNC const char* ures_getName(const UResourceBundle* resB) { michael@0: if(resB == NULL) { michael@0: return NULL; michael@0: } michael@0: michael@0: return resB->fData->fName; michael@0: } michael@0: michael@0: #ifdef URES_DEBUG michael@0: U_CFUNC const char* ures_getPath(const UResourceBundle* resB) { michael@0: if(resB == NULL) { michael@0: return NULL; michael@0: } michael@0: michael@0: return resB->fData->fPath; michael@0: } michael@0: #endif michael@0: michael@0: /* OLD API implementation */ michael@0: michael@0: /** michael@0: * API: This function is used to open a resource bundle michael@0: * proper fallback chaining is executed while initialization. michael@0: * The result is stored in cache for later fallback search. michael@0: */ michael@0: U_CAPI void U_EXPORT2 michael@0: ures_openFillIn(UResourceBundle *r, const char* path, michael@0: const char* localeID, UErrorCode* status) { michael@0: if(r == NULL) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: } else { michael@0: UResourceDataEntry *firstData; michael@0: UBool isStackObject = ures_isStackObject(r); michael@0: char canonLocaleID[ULOC_FULLNAME_CAPACITY]; michael@0: michael@0: uloc_getBaseName(localeID, canonLocaleID, sizeof(canonLocaleID), status); michael@0: if(U_FAILURE(*status) || *status == U_STRING_NOT_TERMINATED_WARNING) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return; michael@0: } michael@0: michael@0: ures_closeBundle(r, FALSE); michael@0: uprv_memset(r, 0, sizeof(UResourceBundle)); michael@0: ures_setIsStackObject(r, isStackObject); michael@0: r->fHasFallback = TRUE; michael@0: r->fIsTopLevel = TRUE; michael@0: r->fIndex = -1; michael@0: r->fData = entryOpen(path, canonLocaleID, status); michael@0: if(U_FAILURE(*status)) { michael@0: return; michael@0: } michael@0: /* this is a quick fix to get regular data in bundle - until construction is cleaned up */ michael@0: firstData = r->fData; michael@0: while(firstData->fBogus != U_ZERO_ERROR && firstData->fParent != NULL) { michael@0: firstData = firstData->fParent; michael@0: } michael@0: uprv_memcpy(&r->fResData, &firstData->fData, sizeof(ResourceData)); michael@0: r->fHasFallback=(UBool)!r->fResData.noFallback; michael@0: r->fRes = r->fResData.rootRes; michael@0: r->fSize = res_countArrayItems(&(r->fResData), r->fRes); michael@0: r->fTopLevelData = r->fData; michael@0: } michael@0: } michael@0: michael@0: U_CAPI UResourceBundle* U_EXPORT2 michael@0: ures_open(const char* path, michael@0: const char* localeID, michael@0: UErrorCode* status) michael@0: { michael@0: char canonLocaleID[ULOC_FULLNAME_CAPACITY]; michael@0: UResourceDataEntry *hasData = NULL; michael@0: UResourceBundle *r; michael@0: michael@0: if(status == NULL || U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: michael@0: /* first "canonicalize" the locale ID */ michael@0: uloc_getBaseName(localeID, canonLocaleID, sizeof(canonLocaleID), status); michael@0: if(U_FAILURE(*status) || *status == U_STRING_NOT_TERMINATED_WARNING) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: r = (UResourceBundle *)uprv_malloc(sizeof(UResourceBundle)); michael@0: if(r == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: uprv_memset(r, 0, sizeof(UResourceBundle)); michael@0: r->fHasFallback = TRUE; michael@0: r->fIsTopLevel = TRUE; michael@0: ures_setIsStackObject(r, FALSE); michael@0: r->fIndex = -1; michael@0: r->fData = entryOpen(path, canonLocaleID, status); michael@0: if(U_FAILURE(*status)) { michael@0: uprv_free(r); michael@0: return NULL; michael@0: } michael@0: r->fTopLevelData = r->fData; michael@0: michael@0: hasData = r->fData; michael@0: while(hasData->fBogus != U_ZERO_ERROR) { michael@0: hasData = hasData->fParent; michael@0: if(hasData == NULL) { michael@0: /* This can happen only if fallback chain gets broken by an act of God */ michael@0: /* TODO: this unlikely to happen, consider removing it */ michael@0: entryClose(r->fData); michael@0: uprv_free(r); michael@0: *status = U_MISSING_RESOURCE_ERROR; michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: uprv_memcpy(&r->fResData, &hasData->fData, sizeof(ResourceData)); michael@0: r->fHasFallback=(UBool)!r->fResData.noFallback; michael@0: r->fRes = r->fResData.rootRes; michael@0: r->fSize = res_countArrayItems(&(r->fResData), r->fRes); michael@0: /* michael@0: if(r->fData->fPath != NULL) { michael@0: ures_setResPath(r, r->fData->fPath); michael@0: ures_appendResPath(r, RES_PATH_PACKAGE_S); michael@0: ures_appendResPath(r, r->fData->fName); michael@0: } else { michael@0: ures_setResPath(r, r->fData->fName); michael@0: } michael@0: */ michael@0: michael@0: michael@0: return r; michael@0: } michael@0: michael@0: /** michael@0: * Opens a resource bundle without "canonicalizing" the locale name. No fallback will be performed michael@0: * or sought. However, alias substitution will happen! michael@0: */ michael@0: U_CAPI UResourceBundle* U_EXPORT2 michael@0: ures_openDirect(const char* path, const char* localeID, UErrorCode* status) { michael@0: UResourceBundle *r; michael@0: UErrorCode subStatus = U_ZERO_ERROR; michael@0: michael@0: if(status == NULL || U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: michael@0: r = (UResourceBundle *)uprv_malloc(sizeof(UResourceBundle)); michael@0: if(r == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: r->fHasFallback = FALSE; michael@0: r->fIsTopLevel = TRUE; michael@0: ures_setIsStackObject(r, FALSE); michael@0: r->fIndex = -1; michael@0: r->fData = entryOpen(path, localeID, &subStatus); michael@0: if(U_FAILURE(subStatus)) { michael@0: *status = subStatus; michael@0: uprv_free(r); michael@0: return NULL; michael@0: } michael@0: if(subStatus != U_ZERO_ERROR /*r->fData->fBogus != U_ZERO_ERROR*/) { michael@0: /* we didn't find one we were looking for - so openDirect */ michael@0: /* should fail */ michael@0: entryClose(r->fData); michael@0: uprv_free(r); michael@0: *status = U_MISSING_RESOURCE_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: r->fKey = NULL; michael@0: r->fVersion = NULL; michael@0: uprv_memcpy(&r->fResData, &r->fData->fData, sizeof(ResourceData)); michael@0: /* r->fHasFallback remains FALSE here in ures_openDirect() */ michael@0: r->fRes = r->fResData.rootRes; michael@0: /*r->fParent = RES_BOGUS;*/ michael@0: r->fSize = res_countArrayItems(&(r->fResData), r->fRes); michael@0: r->fResPath = NULL; michael@0: r->fResPathLen = 0; michael@0: /*r->fParentRes = NULL;*/ michael@0: r->fTopLevelData = r->fData; michael@0: michael@0: return r; michael@0: } michael@0: michael@0: /** michael@0: * API: Counts members. For arrays and tables, returns number of resources. michael@0: * For strings, returns 1. michael@0: */ michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ures_countArrayItems(const UResourceBundle* resourceBundle, michael@0: const char* resourceKey, michael@0: UErrorCode* status) michael@0: { michael@0: UResourceBundle resData; michael@0: ures_initStackObject(&resData); michael@0: if (status==NULL || U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: if(resourceBundle == NULL) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: ures_getByKey(resourceBundle, resourceKey, &resData, status); michael@0: michael@0: if(resData.fResData.data != NULL) { michael@0: int32_t result = res_countArrayItems(&resData.fResData, resData.fRes); michael@0: ures_close(&resData); michael@0: return result; michael@0: } else { michael@0: *status = U_MISSING_RESOURCE_ERROR; michael@0: ures_close(&resData); michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Internal function. michael@0: * Return the version number associated with this ResourceBundle as a string. michael@0: * michael@0: * @param resourceBundle The resource bundle for which the version is checked. michael@0: * @return A version number string as specified in the resource bundle or its parent. michael@0: * The caller does not own this string. michael@0: * @see ures_getVersion michael@0: * @internal michael@0: */ michael@0: U_INTERNAL const char* U_EXPORT2 michael@0: ures_getVersionNumberInternal(const UResourceBundle *resourceBundle) michael@0: { michael@0: if (!resourceBundle) return NULL; michael@0: michael@0: if(resourceBundle->fVersion == NULL) { michael@0: michael@0: /* If the version ID has not been built yet, then do so. Retrieve */ michael@0: /* the minor version from the file. */ michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: int32_t minor_len = 0; michael@0: int32_t len; michael@0: michael@0: const UChar* minor_version = ures_getStringByKey(resourceBundle, kVersionTag, &minor_len, &status); michael@0: michael@0: /* Determine the length of of the final version string. This is */ michael@0: /* the length of the major part + the length of the separator */ michael@0: /* (==1) + the length of the minor part (+ 1 for the zero byte at */ michael@0: /* the end). */ michael@0: michael@0: len = (minor_len > 0) ? minor_len : 1; michael@0: michael@0: /* Allocate the string, and build it up. */ michael@0: /* + 1 for zero byte */ michael@0: michael@0: michael@0: ((UResourceBundle *)resourceBundle)->fVersion = (char *)uprv_malloc(1 + len); michael@0: /* Check for null pointer. */ michael@0: if (((UResourceBundle *)resourceBundle)->fVersion == NULL) { michael@0: return NULL; michael@0: } michael@0: michael@0: if(minor_len > 0) { michael@0: u_UCharsToChars(minor_version, resourceBundle->fVersion , minor_len); michael@0: resourceBundle->fVersion[len] = '\0'; michael@0: } michael@0: else { michael@0: uprv_strcpy(resourceBundle->fVersion, kDefaultMinorVersion); michael@0: } michael@0: } michael@0: michael@0: return resourceBundle->fVersion; michael@0: } michael@0: michael@0: U_CAPI const char* U_EXPORT2 michael@0: ures_getVersionNumber(const UResourceBundle* resourceBundle) michael@0: { michael@0: return ures_getVersionNumberInternal(resourceBundle); michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 ures_getVersion(const UResourceBundle* resB, UVersionInfo versionInfo) { michael@0: if (!resB) return; michael@0: michael@0: u_versionFromString(versionInfo, ures_getVersionNumberInternal(resB)); michael@0: } michael@0: michael@0: /** Tree support functions *******************************/ michael@0: #define INDEX_LOCALE_NAME "res_index" michael@0: #define INDEX_TAG "InstalledLocales" michael@0: #define DEFAULT_TAG "default" michael@0: michael@0: #if defined(URES_TREE_DEBUG) michael@0: #include michael@0: #endif michael@0: michael@0: typedef struct ULocalesContext { michael@0: UResourceBundle installed; michael@0: UResourceBundle curr; michael@0: } ULocalesContext; michael@0: michael@0: static void U_CALLCONV michael@0: ures_loc_closeLocales(UEnumeration *enumerator) { michael@0: ULocalesContext *ctx = (ULocalesContext *)enumerator->context; michael@0: ures_close(&ctx->curr); michael@0: ures_close(&ctx->installed); michael@0: uprv_free(ctx); michael@0: uprv_free(enumerator); michael@0: } michael@0: michael@0: static int32_t U_CALLCONV michael@0: ures_loc_countLocales(UEnumeration *en, UErrorCode * /*status*/) { michael@0: ULocalesContext *ctx = (ULocalesContext *)en->context; michael@0: return ures_getSize(&ctx->installed); michael@0: } michael@0: michael@0: static const char* U_CALLCONV michael@0: ures_loc_nextLocale(UEnumeration* en, michael@0: int32_t* resultLength, michael@0: UErrorCode* status) { michael@0: ULocalesContext *ctx = (ULocalesContext *)en->context; michael@0: UResourceBundle *res = &(ctx->installed); michael@0: UResourceBundle *k = NULL; michael@0: const char *result = NULL; michael@0: int32_t len = 0; michael@0: if(ures_hasNext(res) && (k = ures_getNextResource(res, &ctx->curr, status))) { michael@0: result = ures_getKey(k); michael@0: len = (int32_t)uprv_strlen(result); michael@0: } michael@0: if (resultLength) { michael@0: *resultLength = len; michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: static void U_CALLCONV michael@0: ures_loc_resetLocales(UEnumeration* en, michael@0: UErrorCode* /*status*/) { michael@0: UResourceBundle *res = &((ULocalesContext *)en->context)->installed; michael@0: ures_resetIterator(res); michael@0: } michael@0: michael@0: michael@0: static const UEnumeration gLocalesEnum = { michael@0: NULL, michael@0: NULL, michael@0: ures_loc_closeLocales, michael@0: ures_loc_countLocales, michael@0: uenum_unextDefault, michael@0: ures_loc_nextLocale, michael@0: ures_loc_resetLocales michael@0: }; michael@0: michael@0: michael@0: U_CAPI UEnumeration* U_EXPORT2 michael@0: ures_openAvailableLocales(const char *path, UErrorCode *status) michael@0: { michael@0: UResourceBundle *idx = NULL; michael@0: UEnumeration *en = NULL; michael@0: ULocalesContext *myContext = NULL; michael@0: michael@0: if(U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: myContext = static_cast(uprv_malloc(sizeof(ULocalesContext))); michael@0: en = (UEnumeration *)uprv_malloc(sizeof(UEnumeration)); michael@0: if(!en || !myContext) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: uprv_free(en); michael@0: uprv_free(myContext); michael@0: return NULL; michael@0: } michael@0: uprv_memcpy(en, &gLocalesEnum, sizeof(UEnumeration)); michael@0: michael@0: ures_initStackObject(&myContext->installed); michael@0: ures_initStackObject(&myContext->curr); michael@0: idx = ures_openDirect(path, INDEX_LOCALE_NAME, status); michael@0: ures_getByKey(idx, INDEX_TAG, &myContext->installed, status); michael@0: if(U_SUCCESS(*status)) { michael@0: #if defined(URES_TREE_DEBUG) michael@0: fprintf(stderr, "Got %s::%s::[%s] : %s\n", michael@0: path, INDEX_LOCALE_NAME, INDEX_TAG, ures_getKey(&myContext->installed)); michael@0: #endif michael@0: en->context = myContext; michael@0: } else { michael@0: #if defined(URES_TREE_DEBUG) michael@0: fprintf(stderr, "%s open failed - %s\n", path, u_errorName(*status)); michael@0: #endif michael@0: ures_close(&myContext->installed); michael@0: uprv_free(myContext); michael@0: uprv_free(en); michael@0: en = NULL; michael@0: } michael@0: michael@0: ures_close(idx); michael@0: michael@0: return en; michael@0: } michael@0: michael@0: static UBool isLocaleInList(UEnumeration *locEnum, const char *locToSearch, UErrorCode *status) { michael@0: const char *loc; michael@0: while ((loc = uenum_next(locEnum, NULL, status)) != NULL) { michael@0: if (uprv_strcmp(loc, locToSearch) == 0) { michael@0: return TRUE; michael@0: } michael@0: } michael@0: return FALSE; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ures_getFunctionalEquivalent(char *result, int32_t resultCapacity, michael@0: const char *path, const char *resName, const char *keyword, const char *locid, michael@0: UBool *isAvailable, UBool omitDefault, UErrorCode *status) michael@0: { michael@0: char kwVal[1024] = ""; /* value of keyword 'keyword' */ michael@0: char defVal[1024] = ""; /* default value for given locale */ michael@0: char defLoc[1024] = ""; /* default value for given locale */ michael@0: char base[1024] = ""; /* base locale */ michael@0: char found[1024]; michael@0: char parent[1024]; michael@0: char full[1024] = ""; michael@0: UResourceBundle bund1, bund2; michael@0: UResourceBundle *res = NULL; michael@0: UErrorCode subStatus = U_ZERO_ERROR; michael@0: int32_t length = 0; michael@0: if(U_FAILURE(*status)) return 0; michael@0: uloc_getKeywordValue(locid, keyword, kwVal, 1024-1,&subStatus); michael@0: if(!uprv_strcmp(kwVal, DEFAULT_TAG)) { michael@0: kwVal[0]=0; michael@0: } michael@0: uloc_getBaseName(locid, base, 1024-1,&subStatus); michael@0: #if defined(URES_TREE_DEBUG) michael@0: fprintf(stderr, "getFunctionalEquivalent: \"%s\" [%s=%s] in %s - %s\n", michael@0: locid, keyword, kwVal, base, u_errorName(subStatus)); michael@0: #endif michael@0: ures_initStackObject(&bund1); michael@0: ures_initStackObject(&bund2); michael@0: michael@0: michael@0: uprv_strcpy(parent, base); michael@0: uprv_strcpy(found, base); michael@0: michael@0: if(isAvailable) { michael@0: UEnumeration *locEnum = ures_openAvailableLocales(path, &subStatus); michael@0: *isAvailable = TRUE; michael@0: if (U_SUCCESS(subStatus)) { michael@0: *isAvailable = isLocaleInList(locEnum, parent, &subStatus); michael@0: } michael@0: uenum_close(locEnum); michael@0: } michael@0: michael@0: if(U_FAILURE(subStatus)) { michael@0: *status = subStatus; michael@0: return 0; michael@0: } michael@0: michael@0: do { michael@0: subStatus = U_ZERO_ERROR; michael@0: res = ures_open(path, parent, &subStatus); michael@0: if(((subStatus == U_USING_FALLBACK_WARNING) || michael@0: (subStatus == U_USING_DEFAULT_WARNING)) && isAvailable) michael@0: { michael@0: *isAvailable = FALSE; michael@0: } michael@0: isAvailable = NULL; /* only want to set this the first time around */ michael@0: michael@0: #if defined(URES_TREE_DEBUG) michael@0: fprintf(stderr, "%s;%s -> %s [%s]\n", path?path:"ICUDATA", parent, u_errorName(subStatus), ures_getLocale(res, &subStatus)); michael@0: #endif michael@0: if(U_FAILURE(subStatus)) { michael@0: *status = subStatus; michael@0: } else if(subStatus == U_ZERO_ERROR) { michael@0: ures_getByKey(res,resName,&bund1, &subStatus); michael@0: if(subStatus == U_ZERO_ERROR) { michael@0: const UChar *defUstr; michael@0: int32_t defLen; michael@0: /* look for default item */ michael@0: #if defined(URES_TREE_DEBUG) michael@0: fprintf(stderr, "%s;%s : loaded default -> %s\n", michael@0: path?path:"ICUDATA", parent, u_errorName(subStatus)); michael@0: #endif michael@0: defUstr = ures_getStringByKey(&bund1, DEFAULT_TAG, &defLen, &subStatus); michael@0: if(U_SUCCESS(subStatus) && defLen) { michael@0: u_UCharsToChars(defUstr, defVal, u_strlen(defUstr)); michael@0: #if defined(URES_TREE_DEBUG) michael@0: fprintf(stderr, "%s;%s -> default %s=%s, %s\n", michael@0: path?path:"ICUDATA", parent, keyword, defVal, u_errorName(subStatus)); michael@0: #endif michael@0: uprv_strcpy(defLoc, parent); michael@0: if(kwVal[0]==0) { michael@0: uprv_strcpy(kwVal, defVal); michael@0: #if defined(URES_TREE_DEBUG) michael@0: fprintf(stderr, "%s;%s -> kwVal = %s\n", michael@0: path?path:"ICUDATA", parent, keyword, kwVal); michael@0: #endif michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: subStatus = U_ZERO_ERROR; michael@0: michael@0: if (res != NULL) { michael@0: uprv_strcpy(found, ures_getLocaleByType(res, ULOC_VALID_LOCALE, &subStatus)); michael@0: } michael@0: michael@0: uloc_getParent(found,parent,sizeof(parent),&subStatus); michael@0: ures_close(res); michael@0: } while(!defVal[0] && *found && uprv_strcmp(found, "root") != 0 && U_SUCCESS(*status)); michael@0: michael@0: /* Now, see if we can find the kwVal collator.. start the search over.. */ michael@0: uprv_strcpy(parent, base); michael@0: uprv_strcpy(found, base); michael@0: michael@0: do { michael@0: subStatus = U_ZERO_ERROR; michael@0: res = ures_open(path, parent, &subStatus); michael@0: if((subStatus == U_USING_FALLBACK_WARNING) && isAvailable) { michael@0: *isAvailable = FALSE; michael@0: } michael@0: isAvailable = NULL; /* only want to set this the first time around */ michael@0: michael@0: #if defined(URES_TREE_DEBUG) michael@0: fprintf(stderr, "%s;%s -> %s (looking for %s)\n", michael@0: path?path:"ICUDATA", parent, u_errorName(subStatus), kwVal); michael@0: #endif michael@0: if(U_FAILURE(subStatus)) { michael@0: *status = subStatus; michael@0: } else if(subStatus == U_ZERO_ERROR) { michael@0: ures_getByKey(res,resName,&bund1, &subStatus); michael@0: #if defined(URES_TREE_DEBUG) michael@0: /**/ fprintf(stderr,"@%d [%s] %s\n", __LINE__, resName, u_errorName(subStatus)); michael@0: #endif michael@0: if(subStatus == U_ZERO_ERROR) { michael@0: ures_getByKey(&bund1, kwVal, &bund2, &subStatus); michael@0: #if defined(URES_TREE_DEBUG) michael@0: /**/ fprintf(stderr,"@%d [%s] %s\n", __LINE__, kwVal, u_errorName(subStatus)); michael@0: #endif michael@0: if(subStatus == U_ZERO_ERROR) { michael@0: #if defined(URES_TREE_DEBUG) michael@0: fprintf(stderr, "%s;%s -> full0 %s=%s, %s\n", michael@0: path?path:"ICUDATA", parent, keyword, kwVal, u_errorName(subStatus)); michael@0: #endif michael@0: uprv_strcpy(full, parent); michael@0: if(*full == 0) { michael@0: uprv_strcpy(full, "root"); michael@0: } michael@0: /* now, recalculate default kw if need be */ michael@0: if(uprv_strlen(defLoc) > uprv_strlen(full)) { michael@0: const UChar *defUstr; michael@0: int32_t defLen; michael@0: /* look for default item */ michael@0: #if defined(URES_TREE_DEBUG) michael@0: fprintf(stderr, "%s;%s -> recalculating Default0\n", michael@0: path?path:"ICUDATA", full); michael@0: #endif michael@0: defUstr = ures_getStringByKey(&bund1, DEFAULT_TAG, &defLen, &subStatus); michael@0: if(U_SUCCESS(subStatus) && defLen) { michael@0: u_UCharsToChars(defUstr, defVal, u_strlen(defUstr)); michael@0: #if defined(URES_TREE_DEBUG) michael@0: fprintf(stderr, "%s;%s -> default0 %s=%s, %s\n", michael@0: path?path:"ICUDATA", full, keyword, defVal, u_errorName(subStatus)); michael@0: #endif michael@0: uprv_strcpy(defLoc, full); michael@0: } michael@0: } /* end of recalculate default KW */ michael@0: #if defined(URES_TREE_DEBUG) michael@0: else { michael@0: fprintf(stderr, "No trim0, %s <= %s\n", defLoc, full); michael@0: } michael@0: #endif michael@0: } else { michael@0: #if defined(URES_TREE_DEBUG) michael@0: fprintf(stderr, "err=%s in %s looking for %s\n", michael@0: u_errorName(subStatus), parent, kwVal); michael@0: #endif michael@0: } michael@0: } michael@0: } michael@0: michael@0: subStatus = U_ZERO_ERROR; michael@0: michael@0: uprv_strcpy(found, parent); michael@0: uloc_getParent(found,parent,1023,&subStatus); michael@0: ures_close(res); michael@0: } while(!full[0] && *found && U_SUCCESS(*status)); michael@0: michael@0: if((full[0]==0) && uprv_strcmp(kwVal, defVal)) { michael@0: #if defined(URES_TREE_DEBUG) michael@0: fprintf(stderr, "Failed to locate kw %s - try default %s\n", kwVal, defVal); michael@0: #endif michael@0: uprv_strcpy(kwVal, defVal); michael@0: uprv_strcpy(parent, base); michael@0: uprv_strcpy(found, base); michael@0: michael@0: do { /* search for 'default' named item */ michael@0: subStatus = U_ZERO_ERROR; michael@0: res = ures_open(path, parent, &subStatus); michael@0: if((subStatus == U_USING_FALLBACK_WARNING) && isAvailable) { michael@0: *isAvailable = FALSE; michael@0: } michael@0: isAvailable = NULL; /* only want to set this the first time around */ michael@0: michael@0: #if defined(URES_TREE_DEBUG) michael@0: fprintf(stderr, "%s;%s -> %s (looking for default %s)\n", michael@0: path?path:"ICUDATA", parent, u_errorName(subStatus), kwVal); michael@0: #endif michael@0: if(U_FAILURE(subStatus)) { michael@0: *status = subStatus; michael@0: } else if(subStatus == U_ZERO_ERROR) { michael@0: ures_getByKey(res,resName,&bund1, &subStatus); michael@0: if(subStatus == U_ZERO_ERROR) { michael@0: ures_getByKey(&bund1, kwVal, &bund2, &subStatus); michael@0: if(subStatus == U_ZERO_ERROR) { michael@0: #if defined(URES_TREE_DEBUG) michael@0: fprintf(stderr, "%s;%s -> full1 %s=%s, %s\n", path?path:"ICUDATA", michael@0: parent, keyword, kwVal, u_errorName(subStatus)); michael@0: #endif michael@0: uprv_strcpy(full, parent); michael@0: if(*full == 0) { michael@0: uprv_strcpy(full, "root"); michael@0: } michael@0: michael@0: /* now, recalculate default kw if need be */ michael@0: if(uprv_strlen(defLoc) > uprv_strlen(full)) { michael@0: const UChar *defUstr; michael@0: int32_t defLen; michael@0: /* look for default item */ michael@0: #if defined(URES_TREE_DEBUG) michael@0: fprintf(stderr, "%s;%s -> recalculating Default1\n", michael@0: path?path:"ICUDATA", full); michael@0: #endif michael@0: defUstr = ures_getStringByKey(&bund1, DEFAULT_TAG, &defLen, &subStatus); michael@0: if(U_SUCCESS(subStatus) && defLen) { michael@0: u_UCharsToChars(defUstr, defVal, u_strlen(defUstr)); michael@0: #if defined(URES_TREE_DEBUG) michael@0: fprintf(stderr, "%s;%s -> default %s=%s, %s\n", michael@0: path?path:"ICUDATA", full, keyword, defVal, u_errorName(subStatus)); michael@0: #endif michael@0: uprv_strcpy(defLoc, full); michael@0: } michael@0: } /* end of recalculate default KW */ michael@0: #if defined(URES_TREE_DEBUG) michael@0: else { michael@0: fprintf(stderr, "No trim1, %s <= %s\n", defLoc, full); michael@0: } michael@0: #endif michael@0: } michael@0: } michael@0: } michael@0: subStatus = U_ZERO_ERROR; michael@0: michael@0: uprv_strcpy(found, parent); michael@0: uloc_getParent(found,parent,1023,&subStatus); michael@0: ures_close(res); michael@0: } while(!full[0] && *found && U_SUCCESS(*status)); michael@0: } michael@0: michael@0: if(U_SUCCESS(*status)) { michael@0: if(!full[0]) { michael@0: #if defined(URES_TREE_DEBUG) michael@0: fprintf(stderr, "Still could not load keyword %s=%s\n", keyword, kwVal); michael@0: #endif michael@0: *status = U_MISSING_RESOURCE_ERROR; michael@0: } else if(omitDefault) { michael@0: #if defined(URES_TREE_DEBUG) michael@0: fprintf(stderr,"Trim? full=%s, defLoc=%s, found=%s\n", full, defLoc, found); michael@0: #endif michael@0: if(uprv_strlen(defLoc) <= uprv_strlen(full)) { michael@0: /* found the keyword in a *child* of where the default tag was present. */ michael@0: if(!uprv_strcmp(kwVal, defVal)) { /* if the requested kw is default, */ michael@0: /* and the default is in or in an ancestor of the current locale */ michael@0: #if defined(URES_TREE_DEBUG) michael@0: fprintf(stderr, "Removing unneeded var %s=%s\n", keyword, kwVal); michael@0: #endif michael@0: kwVal[0]=0; michael@0: } michael@0: } michael@0: } michael@0: uprv_strcpy(found, full); michael@0: if(kwVal[0]) { michael@0: uprv_strcat(found, "@"); michael@0: uprv_strcat(found, keyword); michael@0: uprv_strcat(found, "="); michael@0: uprv_strcat(found, kwVal); michael@0: } else if(!omitDefault) { michael@0: uprv_strcat(found, "@"); michael@0: uprv_strcat(found, keyword); michael@0: uprv_strcat(found, "="); michael@0: uprv_strcat(found, defVal); michael@0: } michael@0: } michael@0: /* we found the default locale - no need to repeat it.*/ michael@0: michael@0: ures_close(&bund1); michael@0: ures_close(&bund2); michael@0: michael@0: length = (int32_t)uprv_strlen(found); michael@0: michael@0: if(U_SUCCESS(*status)) { michael@0: int32_t copyLength = uprv_min(length, resultCapacity); michael@0: if(copyLength>0) { michael@0: uprv_strncpy(result, found, copyLength); michael@0: } michael@0: if(length == 0) { michael@0: *status = U_MISSING_RESOURCE_ERROR; michael@0: } michael@0: } else { michael@0: length = 0; michael@0: result[0]=0; michael@0: } michael@0: return u_terminateChars(result, resultCapacity, length, status); michael@0: } michael@0: michael@0: U_CAPI UEnumeration* U_EXPORT2 michael@0: ures_getKeywordValues(const char *path, const char *keyword, UErrorCode *status) michael@0: { michael@0: #define VALUES_BUF_SIZE 2048 michael@0: #define VALUES_LIST_SIZE 512 michael@0: michael@0: char valuesBuf[VALUES_BUF_SIZE]; michael@0: int32_t valuesIndex = 0; michael@0: const char *valuesList[VALUES_LIST_SIZE]; michael@0: int32_t valuesCount = 0; michael@0: michael@0: const char *locale; michael@0: int32_t locLen; michael@0: michael@0: UEnumeration *locs = NULL; michael@0: michael@0: UResourceBundle item; michael@0: UResourceBundle subItem; michael@0: michael@0: ures_initStackObject(&item); michael@0: ures_initStackObject(&subItem); michael@0: locs = ures_openAvailableLocales(path, status); michael@0: michael@0: if(U_FAILURE(*status)) { michael@0: ures_close(&item); michael@0: ures_close(&subItem); michael@0: return NULL; michael@0: } michael@0: michael@0: valuesBuf[0]=0; michael@0: valuesBuf[1]=0; michael@0: michael@0: while((locale = uenum_next(locs, &locLen, status))) { michael@0: UResourceBundle *bund = NULL; michael@0: UResourceBundle *subPtr = NULL; michael@0: UErrorCode subStatus = U_ZERO_ERROR; /* don't fail if a bundle is unopenable */ michael@0: bund = ures_openDirect(path, locale, &subStatus); michael@0: michael@0: #if defined(URES_TREE_DEBUG) michael@0: if(!bund || U_FAILURE(subStatus)) { michael@0: fprintf(stderr, "%s-%s values: Can't open %s locale - skipping. (%s)\n", michael@0: path?path:"", keyword, locale, u_errorName(subStatus)); michael@0: } michael@0: #endif michael@0: michael@0: ures_getByKey(bund, keyword, &item, &subStatus); michael@0: michael@0: if(!bund || U_FAILURE(subStatus)) { michael@0: #if defined(URES_TREE_DEBUG) michael@0: fprintf(stderr, "%s-%s values: Can't find in %s - skipping. (%s)\n", michael@0: path?path:"", keyword, locale, u_errorName(subStatus)); michael@0: #endif michael@0: ures_close(bund); michael@0: bund = NULL; michael@0: continue; michael@0: } michael@0: michael@0: while((subPtr = ures_getNextResource(&item,&subItem,&subStatus)) michael@0: && U_SUCCESS(subStatus)) { michael@0: const char *k; michael@0: int32_t i; michael@0: k = ures_getKey(subPtr); michael@0: michael@0: #if defined(URES_TREE_DEBUG) michael@0: /* fprintf(stderr, "%s | %s | %s | %s\n", path?path:"", keyword, locale, k); */ michael@0: #endif michael@0: for(i=0;k&&i= (VALUES_LIST_SIZE-1)) || /* no more space in list .. */ michael@0: ((valuesIndex+kLen+1+1) >= VALUES_BUF_SIZE)) { /* no more space in buffer (string + 2 nulls) */ michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; /* out of space.. */ michael@0: } else { michael@0: uprv_strcpy(valuesBuf+valuesIndex, k); michael@0: valuesList[valuesCount++] = valuesBuf+valuesIndex; michael@0: valuesIndex += kLen; michael@0: #if defined(URES_TREE_DEBUG) michael@0: fprintf(stderr, "%s | %s | %s | [%s] (UNIQUE)\n", michael@0: path?path:"", keyword, locale, k); michael@0: #endif michael@0: valuesBuf[valuesIndex++] = 0; /* terminate */ michael@0: } michael@0: } michael@0: } michael@0: ures_close(bund); michael@0: } michael@0: valuesBuf[valuesIndex++] = 0; /* terminate */ michael@0: michael@0: ures_close(&item); michael@0: ures_close(&subItem); michael@0: uenum_close(locs); michael@0: #if defined(URES_TREE_DEBUG) michael@0: fprintf(stderr, "%s: size %d, #%d\n", u_errorName(*status), michael@0: valuesIndex, valuesCount); michael@0: #endif michael@0: return uloc_openKeywordList(valuesBuf, valuesIndex, status); michael@0: } michael@0: #if 0 michael@0: /* This code isn't needed, and given the documentation warnings the implementation is suspect */ michael@0: U_INTERNAL UBool U_EXPORT2 michael@0: ures_equal(const UResourceBundle* res1, const UResourceBundle* res2){ michael@0: if(res1==NULL || res2==NULL){ michael@0: return res1==res2; /* pointer comparision */ michael@0: } michael@0: if(res1->fKey==NULL|| res2->fKey==NULL){ michael@0: return (res1->fKey==res2->fKey); michael@0: }else{ michael@0: if(uprv_strcmp(res1->fKey, res2->fKey)!=0){ michael@0: return FALSE; michael@0: } michael@0: } michael@0: if(uprv_strcmp(res1->fData->fName, res2->fData->fName)!=0){ michael@0: return FALSE; michael@0: } michael@0: if(res1->fData->fPath == NULL|| res2->fData->fPath==NULL){ michael@0: return (res1->fData->fPath == res2->fData->fPath); michael@0: }else{ michael@0: if(uprv_strcmp(res1->fData->fPath, res2->fData->fPath)!=0){ michael@0: return FALSE; michael@0: } michael@0: } michael@0: if(uprv_strcmp(res1->fData->fParent->fName, res2->fData->fParent->fName)!=0){ michael@0: return FALSE; michael@0: } michael@0: if(uprv_strcmp(res1->fData->fParent->fPath, res2->fData->fParent->fPath)!=0){ michael@0: return FALSE; michael@0: } michael@0: if(uprv_strncmp(res1->fResPath, res2->fResPath, res1->fResPathLen)!=0){ michael@0: return FALSE; michael@0: } michael@0: if(res1->fRes != res2->fRes){ michael@0: return FALSE; michael@0: } michael@0: return TRUE; michael@0: } michael@0: U_INTERNAL UResourceBundle* U_EXPORT2 michael@0: ures_clone(const UResourceBundle* res, UErrorCode* status){ michael@0: UResourceBundle* bundle = NULL; michael@0: UResourceBundle* ret = NULL; michael@0: if(U_FAILURE(*status) || res == NULL){ michael@0: return NULL; michael@0: } michael@0: bundle = ures_open(res->fData->fPath, res->fData->fName, status); michael@0: if(res->fResPath!=NULL){ michael@0: ret = ures_findSubResource(bundle, res->fResPath, NULL, status); michael@0: ures_close(bundle); michael@0: }else{ michael@0: ret = bundle; michael@0: } michael@0: return ret; michael@0: } michael@0: U_INTERNAL const UResourceBundle* U_EXPORT2 michael@0: ures_getParentBundle(const UResourceBundle* res){ michael@0: if(res==NULL){ michael@0: return NULL; michael@0: } michael@0: return res->fParentRes; michael@0: } michael@0: #endif michael@0: michael@0: U_INTERNAL void U_EXPORT2 michael@0: ures_getVersionByKey(const UResourceBundle* res, const char *key, UVersionInfo ver, UErrorCode *status) { michael@0: const UChar *str; michael@0: int32_t len; michael@0: str = ures_getStringByKey(res, key, &len, status); michael@0: if(U_SUCCESS(*status)) { michael@0: u_versionFromUString(ver, str); michael@0: } michael@0: } michael@0: michael@0: /* eof */