From 484b5774151226d2e45e02f3c47ff3d94f255931 Mon Sep 17 00:00:00 2001 From: Vladimir Weinstein Date: Tue, 30 Jul 2002 20:49:30 +0000 Subject: [PATCH] ICU-1948 Fixed incorrectly assigning keys to directly accessed resources X-SVN-Rev: 9440 --- icu4c/source/common/uresbund.c | 12 +++++++----- icu4c/source/common/uresdata.c | 11 +++++++---- icu4c/source/common/uresdata.h | 2 +- icu4c/source/test/cintltst/creststn.c | 22 +++++++++++++++++++++- 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/icu4c/source/common/uresbund.c b/icu4c/source/common/uresbund.c index 64ca829eeec..408d3315850 100644 --- a/icu4c/source/common/uresbund.c +++ b/icu4c/source/common/uresbund.c @@ -546,6 +546,7 @@ static UResourceBundle *init_resb_result(const ResourceData *rdata, Resource r, /* first, open the bundle with real data */ UResourceBundle *mainRes = ures_openDirect(path, locale, status); UResourceBundle *result = NULL; + const char* temp = NULL; if(keyPath == NULL) { /* no key path. This means that we are going to @@ -557,7 +558,7 @@ static UResourceBundle *init_resb_result(const ResourceData *rdata, Resource r, */ const char* aKey = parent->fResPath; if(aKey) { - r = res_findResource(&(mainRes->fResData), mainRes->fRes, &aKey); + r = res_findResource(&(mainRes->fResData), mainRes->fRes, &aKey, &temp); } else { r = mainRes->fRes; } @@ -566,7 +567,7 @@ static UResourceBundle *init_resb_result(const ResourceData *rdata, Resource r, * current key, if there is a key associated */ aKey = key; - r = res_findResource(&(mainRes->fResData), r, &aKey); + r = res_findResource(&(mainRes->fResData), r, &aKey, &temp); } else if(index != -1) { /* if there is no key, but there is an index, try to get by the index */ /* here we have either a table or an array, so get the element */ @@ -592,7 +593,7 @@ static UResourceBundle *init_resb_result(const ResourceData *rdata, Resource r, */ result = mainRes; while(*keyPath) { - r = res_findResource(&(result->fResData), result->fRes, (const char**)&keyPath); + r = res_findResource(&(result->fResData), result->fRes, (const char**)&keyPath, &temp); if(r == RES_BOGUS) { *status = U_MISSING_RESOURCE_ERROR; result = resB; @@ -1089,6 +1090,7 @@ ures_findSubResource(const UResourceBundle *resB, const char* path, UResourceBun Resource res = RES_BOGUS; UResourceBundle *result = fillIn; const char *pathToResource = path; + const char *key; if(status == NULL || U_FAILURE(*status)) { return result; @@ -1096,10 +1098,10 @@ ures_findSubResource(const UResourceBundle *resB, const char* path, UResourceBun /* here we do looping and circular alias checking */ - res = res_findResource(&(resB->fResData), resB->fRes, &pathToResource); + res = res_findResource(&(resB->fResData), resB->fRes, &pathToResource, &key); if(res != RES_BOGUS) { - result = init_resb_result(&(resB->fResData), res, path, -1, resB->fData, resB, 0, fillIn, status); + result = init_resb_result(&(resB->fResData), res, key, -1, resB->fData, resB, 0, fillIn, status); } else { *status = U_MISSING_RESOURCE_ERROR; } diff --git a/icu4c/source/common/uresdata.c b/icu4c/source/common/uresdata.c index 71600d55694..f7e22126081 100644 --- a/icu4c/source/common/uresdata.c +++ b/icu4c/source/common/uresdata.c @@ -115,7 +115,7 @@ _res_findTableItem(const Resource *pRoot, const Resource res, const char *key) { } static Resource -_res_findTableItemN(const Resource *pRoot, const Resource res, const char *key, int32_t keyLen) { +_res_findTableItemN(const Resource *pRoot, const Resource res, const char *key, int32_t keyLen, const char **realKey) { uint16_t *p=(uint16_t *)RES_GET_POINTER(pRoot, res); uint16_t i, start, limit; @@ -138,9 +138,11 @@ _res_findTableItemN(const Resource *pRoot, const Resource res, const char *key, /* did we really find it? */ if(uprv_strncmp(key, RES_GET_KEY(pRoot, p[start]), keyLen)==0) { + *realKey = RES_GET_KEY(pRoot, p[start]); limit=*(p-1); /* itemCount */ return ((Resource *)(p+limit+(~limit&1)))[start]; } else { + *realKey = NULL; return RES_BOGUS; /* not found */ } } @@ -317,7 +319,7 @@ res_getArrayItem(const ResourceData *pResData, Resource array, const int32_t ind } U_CFUNC Resource -res_findResource(const ResourceData *pResData, Resource r, const char** path) { +res_findResource(const ResourceData *pResData, Resource r, const char** path, const char** key) { /* we pass in a path. CollationElements/Sequence or zoneStrings/3/2 etc. * iterates over a path and stops when a scalar resource is found. This * CAN be an alias. Path gets set to the part that has not yet been processed. @@ -349,17 +351,18 @@ res_findResource(const ResourceData *pResData, Resource r, const char** path) { /* if the resource is a table */ /* try the key based access */ if(type == RES_TABLE) { - t2 = _res_findTableItemN(pResData->pRoot, t1, pathP, keyLen); + t2 = _res_findTableItemN(pResData->pRoot, t1, pathP, keyLen, key); if(t2 == RES_BOGUS) { /* if we fail to get the resource by key, maybe we got an index */ indexR = uprv_strtol(pathP, &closeIndex, 10); if(closeIndex != pathP) { /* if we indeed have an index, try to get the item by index */ - t2 = _res_getTableItem(pResData->pRoot, t1, (uint16_t)indexR); + t2 = res_getTableItemByIndex(pResData, t1, indexR, key); } } } else if(type == RES_ARRAY) { t2 = _res_getArrayItem(pResData->pRoot, t1, indexR); + *key = NULL; } else { /* can't do much here, except setting t2 to bogus */ t2 = RES_BOGUS; } diff --git a/icu4c/source/common/uresdata.h b/icu4c/source/common/uresdata.h index d442247d35c..92acc0f8578 100644 --- a/icu4c/source/common/uresdata.h +++ b/icu4c/source/common/uresdata.h @@ -113,6 +113,6 @@ U_CFUNC int32_t res_getTableSize(const ResourceData *pResData, Resource table); U_CFUNC Resource res_getArrayItem(const ResourceData *pResData, Resource array, const int32_t indexS); U_CFUNC Resource res_getTableItemByIndex(const ResourceData *pResData, Resource table, int32_t indexS, const char ** key); U_CFUNC Resource res_getTableItemByKey(const ResourceData *pResData, Resource table, int32_t *indexS, const char* * key); -U_CFUNC Resource res_findResource(const ResourceData *pResData, Resource r, const char** path); +U_CFUNC Resource res_findResource(const ResourceData *pResData, Resource r, const char** path, const char** key); #endif diff --git a/icu4c/source/test/cintltst/creststn.c b/icu4c/source/test/cintltst/creststn.c index a9ae8950668..b9788a0fb47 100644 --- a/icu4c/source/test/cintltst/creststn.c +++ b/icu4c/source/test/cintltst/creststn.c @@ -1810,23 +1810,38 @@ static void TestResourceLevelAliasing(void) { static void TestDirectAccess(void) { UErrorCode status = U_ZERO_ERROR; UResourceBundle *t = NULL, *t2 = NULL; + const char* key = NULL; t = ures_findResource("en/zoneStrings/3/2", t, &status); if(U_FAILURE(status)) { log_err("Couldn't access indexed resource, error %s\n", u_errorName(status)); status = U_ZERO_ERROR; + } else { + key = ures_getKey(t); + if(key != NULL) { + log_err("Got a strange key, expected NULL, got %s\n", key); + } } - t = ures_findResource("en/zoneStrings/3", t, &status); if(U_FAILURE(status)) { log_err("Couldn't access indexed resource, error %s\n", u_errorName(status)); status = U_ZERO_ERROR; + } else { + key = ures_getKey(t); + if(key != NULL) { + log_err("Got a strange key, expected NULL, got %s\n", key); + } } t = ures_findResource("sh/CollationElements/Sequence", t, &status); if(U_FAILURE(status)) { log_err("Couldn't access keyed resource, error %s\n", u_errorName(status)); status = U_ZERO_ERROR; + } else { + key = ures_getKey(t); + if(strcmp(key, "Sequence")!=0) { + log_err("Got a strange key, expected 'Sequence', got %s\n", key); + } } t2 = ures_open(NULL, "sh", &status); @@ -1839,6 +1854,11 @@ static void TestDirectAccess(void) { if(U_FAILURE(status)) { log_err("Couldn't access keyed resource, error %s\n", u_errorName(status)); status = U_ZERO_ERROR; + } else { + key = ures_getKey(t); + if(strcmp(key, "Sequence")!=0) { + log_err("Got a strange key, expected 'Sequence', got %s\n", key); + } } ures_close(t);