static void printOutBundle(FILE *out, UConverter *converter, UResourceBundle *resource, int32_t indent, const char *pname, UErrorCode *status) { static const UChar cr[] = { '\n' }; /* int32_t noOfElements = ures_getSize(resource);*/ int32_t i = 0; const char *key = ures_getKey(resource); switch(ures_getType(resource)) { case RES_STRING : { int32_t len=0; const UChar* thestr = ures_getString(resource, &len, status); UChar *string = quotedString(thestr); /* TODO: String truncation */ if(trunc && len > truncsize) { char msg[128]; printIndent(out, converter, indent); sprintf(msg, "// WARNING: this resource, size %li is truncated to %li\n", (long)len, (long)(truncsize/2)); printCString(out, converter, msg, -1); len = truncsize/2; } printIndent(out, converter, indent); if(key != NULL) { static const UChar openStr[] = { 0x0020, 0x007B, 0x0020, 0x0022 }; /* " { \"" */ static const UChar closeStr[] = { 0x0022, 0x0020, 0x007D }; /* "\" }" */ printCString(out, converter, key, (int32_t)uprv_strlen(key)); printString(out, converter, openStr, (int32_t)(sizeof(openStr)/sizeof(*openStr))); printString(out, converter, string, len); printString(out, converter, closeStr, (int32_t)(sizeof(closeStr) / sizeof(*closeStr))); } else { static const UChar openStr[] = { 0x0022 }; /* "\"" */ static const UChar closeStr[] = { 0x0022, 0x002C }; /* "\"," */ printString(out, converter, openStr, (int32_t)(sizeof(openStr) / sizeof(*openStr))); printString(out, converter, string, (int32_t)(u_strlen(string))); printString(out, converter, closeStr, (int32_t)(sizeof(closeStr) / sizeof(*closeStr))); } if(verbose) { printCString(out, converter, "// STRING", -1); } printString(out, converter, cr, (int32_t)(sizeof(cr) / sizeof(*cr))); uprv_free(string); } break; case RES_INT : { static const UChar openStr[] = { 0x003A, 0x0069, 0x006E, 0x0074, 0x0020, 0x007B, 0x0020 }; /* ":int { " */ static const UChar closeStr[] = { 0x0020, 0x007D }; /* " }" */ UChar num[20]; printIndent(out, converter, indent); if(key != NULL) { printCString(out, converter, key, -1); } printString(out, converter, openStr, (int32_t)(sizeof(openStr) / sizeof(*openStr))); uprv_itou(num, 20, ures_getInt(resource, status), 10, 0); printString(out, converter, num, u_strlen(num)); printString(out, converter, closeStr, (int32_t)(sizeof(closeStr) / sizeof(*closeStr))); if(verbose) { printCString(out, converter, "// INT", -1); } printString(out, converter, cr, (int32_t)(sizeof(cr) / sizeof(*cr))); break; } case RES_BINARY : { int32_t len = 0; const int8_t *data = (const int8_t *)ures_getBinary(resource, &len, status); if(trunc && len > truncsize) { char msg[128]; printIndent(out, converter, indent); sprintf(msg, "// WARNING: this resource, size %li is truncated to %li\n", (long)len, (long)(truncsize/2)); printCString(out, converter, msg, -1); len = truncsize; } if(U_SUCCESS(*status)) { static const UChar openStr[] = { 0x003A, 0x0062, 0x0069, 0x006E, 0x0061, 0x0072, 0x0079, 0x0020, 0x007B, 0x0020 }; /* ":binary { " */ static const UChar closeStr[] = { 0x0020, 0x007D, 0x0020 }; /* " } " */ printIndent(out, converter, indent); if(key != NULL) { printCString(out, converter, key, -1); } printString(out, converter, openStr, (int32_t)(sizeof(openStr) / sizeof(*openStr))); for(i = 0; i<len; i++) { printHex(out, converter, *data++); } printString(out, converter, closeStr, (int32_t)(sizeof(closeStr) / sizeof(*closeStr))); if(verbose) { printCString(out, converter, " // BINARY", -1); } printString(out, converter, cr, (int32_t)(sizeof(cr) / sizeof(*cr))); } else { reportError(pname, status, "getting binary value"); } } break; case RES_INT_VECTOR : { int32_t len = 0; const int32_t *data = ures_getIntVector(resource, &len, status); if(U_SUCCESS(*status)) { static const UChar openStr[] = { 0x003A, 0x0069, 0x006E, 0x0074, 0x0076, 0x0065, 0x0063, 0x0074, 0x006F, 0x0072, 0x0020, 0x007B, 0x0020 }; /* ":intvector { " */ static const UChar closeStr[] = { 0x0020, 0x007D, 0x0020 }; /* " } " */ UChar num[20]; printIndent(out, converter, indent); if(key != NULL) { printCString(out, converter, key, -1); } printString(out, converter, openStr, (int32_t)(sizeof(openStr) / sizeof(*openStr))); for(i = 0; i < len - 1; i++) { int32_t numLen = uprv_itou(num, 20, data[i], 10, 0); num[numLen++] = 0x002C; /* ',' */ num[numLen++] = 0x0020; /* ' ' */ num[numLen] = 0; printString(out, converter, num, u_strlen(num)); } if(len > 0) { uprv_itou(num, 20, data[len - 1], 10, 0); printString(out, converter, num, u_strlen(num)); } printString(out, converter, closeStr, (int32_t)(sizeof(closeStr) / sizeof(*closeStr))); if(verbose) { printCString(out, converter, "// INTVECTOR", -1); } printString(out, converter, cr, (int32_t)(sizeof(cr) / sizeof(*cr))); } else { reportError(pname, status, "getting int vector"); } } break; case RES_TABLE : case RES_ARRAY : { static const UChar openStr[] = { 0x007B }; /* "{" */ static const UChar closeStr[] = { 0x007D, '\n' }; /* "}\n" */ UResourceBundle *t = NULL; ures_resetIterator(resource); printIndent(out, converter, indent); if(key != NULL) { printCString(out, converter, key, -1); } printString(out, converter, openStr, (int32_t)(sizeof(openStr) / sizeof(*openStr))); if(verbose) { if(ures_getType(resource) == RES_TABLE) { printCString(out, converter, "// TABLE", -1); } else { printCString(out, converter, "// ARRAY", -1); } } printString(out, converter, cr, (int32_t)(sizeof(cr) / sizeof(*cr))); if(suppressAliases == FALSE) { while(U_SUCCESS(*status) && ures_hasNext(resource)) { t = ures_getNextResource(resource, t, status); if(U_SUCCESS(*status)) { printOutBundle(out, converter, t, indent+indentsize, pname, status); } else { reportError(pname, status, "While processing table"); *status = U_ZERO_ERROR; } } } else { /* we have to use low level access to do this */ Resource r = RES_BOGUS; for(i = 0; i < ures_getSize(resource); i++) { /* need to know if it's an alias */ if(ures_getType(resource) == RES_TABLE) { r = derb_getTableItem(resource->fResData.pRoot, resource->fRes, (int16_t)i); key = derb_getTableKey(resource->fResData.pRoot, resource->fRes, (int16_t)i); } else { r = derb_getArrayItem(resource->fResData.pRoot, resource->fRes, i); } if(U_SUCCESS(*status)) { if(RES_GET_TYPE(r) == RES_ALIAS) { printOutAlias(out, converter, resource, r, key, indent+indentsize, pname, status); } else { t = ures_getByIndex(resource, i, t, status); printOutBundle(out, converter, t, indent+indentsize, pname, status); } } else { reportError(pname, status, "While processing table"); *status = U_ZERO_ERROR; } } } printIndent(out, converter, indent); printString(out, converter, closeStr, (int32_t)(sizeof(closeStr) / sizeof(*closeStr))); ures_close(t); } break; default: break; } }
U_CDECL_END static void U_CALLCONV initFromResourceBundle(UErrorCode& sts) { U_NAMESPACE_USE ucln_common_registerCleanup(UCLN_COMMON_LOCALE_KEY_TYPE, uloc_key_type_cleanup); gLocExtKeyMap = uhash_open(uhash_hashIChars, uhash_compareIChars, NULL, &sts); LocalUResourceBundlePointer keyTypeDataRes(ures_openDirect(NULL, "keyTypeData", &sts)); LocalUResourceBundlePointer keyMapRes(ures_getByKey(keyTypeDataRes.getAlias(), "keyMap", NULL, &sts)); LocalUResourceBundlePointer typeMapRes(ures_getByKey(keyTypeDataRes.getAlias(), "typeMap", NULL, &sts)); if (U_FAILURE(sts)) { return; } UErrorCode tmpSts = U_ZERO_ERROR; LocalUResourceBundlePointer typeAliasRes(ures_getByKey(keyTypeDataRes.getAlias(), "typeAlias", NULL, &tmpSts)); tmpSts = U_ZERO_ERROR; LocalUResourceBundlePointer bcpTypeAliasRes(ures_getByKey(keyTypeDataRes.getAlias(), "bcpTypeAlias", NULL, &tmpSts)); // initialize vectors storing dynamically allocated objects gKeyTypeStringPool = new UVector(uloc_deleteKeyTypeStringPoolEntry, NULL, sts); if (gKeyTypeStringPool == NULL) { if (U_SUCCESS(sts)) { sts = U_MEMORY_ALLOCATION_ERROR; } } if (U_FAILURE(sts)) { return; } gLocExtKeyDataEntries = new UVector(uloc_deleteKeyDataEntry, NULL, sts); if (gLocExtKeyDataEntries == NULL) { if (U_SUCCESS(sts)) { sts = U_MEMORY_ALLOCATION_ERROR; } } if (U_FAILURE(sts)) { return; } gLocExtTypeEntries = new UVector(uloc_deleteTypeEntry, NULL, sts); if (gLocExtTypeEntries == NULL) { if (U_SUCCESS(sts)) { sts = U_MEMORY_ALLOCATION_ERROR; } } if (U_FAILURE(sts)) { return; } // iterate through keyMap resource LocalUResourceBundlePointer keyMapEntry; while (ures_hasNext(keyMapRes.getAlias())) { keyMapEntry.adoptInstead(ures_getNextResource(keyMapRes.getAlias(), keyMapEntry.orphan(), &sts)); if (U_FAILURE(sts)) { break; } const char* legacyKeyId = ures_getKey(keyMapEntry.getAlias()); int32_t bcpKeyIdLen = 0; const UChar* uBcpKeyId = ures_getString(keyMapEntry.getAlias(), &bcpKeyIdLen, &sts); if (U_FAILURE(sts)) { break; } // empty value indicates that BCP key is same with the legacy key. const char* bcpKeyId = legacyKeyId; if (bcpKeyIdLen > 0) { char* bcpKeyIdBuf = (char*)uprv_malloc(bcpKeyIdLen + 1); if (bcpKeyIdBuf == NULL) { sts = U_MEMORY_ALLOCATION_ERROR; break; } u_UCharsToChars(uBcpKeyId, bcpKeyIdBuf, bcpKeyIdLen); bcpKeyIdBuf[bcpKeyIdLen] = 0; gKeyTypeStringPool->addElement(bcpKeyIdBuf, sts); if (U_FAILURE(sts)) { break; } bcpKeyId = bcpKeyIdBuf; } UBool isTZ = uprv_strcmp(legacyKeyId, "timezone") == 0; UHashtable* typeDataMap = uhash_open(uhash_hashIChars, uhash_compareIChars, NULL, &sts); if (U_FAILURE(sts)) { break; } uint32_t specialTypes = SPECIALTYPE_NONE; LocalUResourceBundlePointer typeAliasResByKey; LocalUResourceBundlePointer bcpTypeAliasResByKey; if (typeAliasRes.isValid()) { tmpSts = U_ZERO_ERROR; typeAliasResByKey.adoptInstead(ures_getByKey(typeAliasRes.getAlias(), legacyKeyId, NULL, &tmpSts)); if (U_FAILURE(tmpSts)) { typeAliasResByKey.orphan(); } } if (bcpTypeAliasRes.isValid()) { tmpSts = U_ZERO_ERROR; bcpTypeAliasResByKey.adoptInstead(ures_getByKey(bcpTypeAliasRes.getAlias(), bcpKeyId, NULL, &tmpSts)); if (U_FAILURE(tmpSts)) { bcpTypeAliasResByKey.orphan(); } } // look up type map for the key, and walk through the mapping data tmpSts = U_ZERO_ERROR; LocalUResourceBundlePointer typeMapResByKey(ures_getByKey(typeMapRes.getAlias(), legacyKeyId, NULL, &tmpSts)); if (U_FAILURE(tmpSts)) { // type map for each key must exist U_ASSERT(FALSE); } else { LocalUResourceBundlePointer typeMapEntry; while (ures_hasNext(typeMapResByKey.getAlias())) { typeMapEntry.adoptInstead(ures_getNextResource(typeMapResByKey.getAlias(), typeMapEntry.orphan(), &sts)); if (U_FAILURE(sts)) { break; } const char* legacyTypeId = ures_getKey(typeMapEntry.getAlias()); // special types if (uprv_strcmp(legacyTypeId, "CODEPOINTS") == 0) { specialTypes |= SPECIALTYPE_CODEPOINTS; continue; } if (uprv_strcmp(legacyTypeId, "REORDER_CODE") == 0) { specialTypes |= SPECIALTYPE_REORDER_CODE; continue; } if (uprv_strcmp(legacyTypeId, "RG_KEY_VALUE") == 0) { specialTypes |= SPECIALTYPE_RG_KEY_VALUE; continue; } if (isTZ) { // a timezone key uses a colon instead of a slash in the resource. // e.g. America:Los_Angeles if (uprv_strchr(legacyTypeId, ':') != NULL) { int32_t legacyTypeIdLen = uprv_strlen(legacyTypeId); char* legacyTypeIdBuf = (char*)uprv_malloc(legacyTypeIdLen + 1); if (legacyTypeIdBuf == NULL) { sts = U_MEMORY_ALLOCATION_ERROR; break; } const char* p = legacyTypeId; char* q = legacyTypeIdBuf; while (*p) { if (*p == ':') { *q++ = '/'; } else { *q++ = *p; } p++; } *q = 0; gKeyTypeStringPool->addElement(legacyTypeIdBuf, sts); if (U_FAILURE(sts)) { break; } legacyTypeId = legacyTypeIdBuf; } } int32_t bcpTypeIdLen = 0; const UChar* uBcpTypeId = ures_getString(typeMapEntry.getAlias(), &bcpTypeIdLen, &sts); if (U_FAILURE(sts)) { break; } // empty value indicates that BCP type is same with the legacy type. const char* bcpTypeId = legacyTypeId; if (bcpTypeIdLen > 0) { char* bcpTypeIdBuf = (char*)uprv_malloc(bcpTypeIdLen + 1); if (bcpTypeIdBuf == NULL) { sts = U_MEMORY_ALLOCATION_ERROR; break; } u_UCharsToChars(uBcpTypeId, bcpTypeIdBuf, bcpTypeIdLen); bcpTypeIdBuf[bcpTypeIdLen] = 0; gKeyTypeStringPool->addElement(bcpTypeIdBuf, sts); if (U_FAILURE(sts)) { break; } bcpTypeId = bcpTypeIdBuf; } // Note: legacy type value should never be // equivalent to bcp type value of a different // type under the same key. So we use a single // map for lookup. LocExtType* t = (LocExtType*)uprv_malloc(sizeof(LocExtType)); if (t == NULL) { sts = U_MEMORY_ALLOCATION_ERROR; break; } t->bcpId = bcpTypeId; t->legacyId = legacyTypeId; gLocExtTypeEntries->addElement((void*)t, sts); if (U_FAILURE(sts)) { break; } uhash_put(typeDataMap, (void*)legacyTypeId, t, &sts); if (bcpTypeId != legacyTypeId) { // different type value uhash_put(typeDataMap, (void*)bcpTypeId, t, &sts); } if (U_FAILURE(sts)) { break; } // also put aliases in the map if (typeAliasResByKey.isValid()) { LocalUResourceBundlePointer typeAliasDataEntry; ures_resetIterator(typeAliasResByKey.getAlias()); while (ures_hasNext(typeAliasResByKey.getAlias()) && U_SUCCESS(sts)) { int32_t toLen; typeAliasDataEntry.adoptInstead(ures_getNextResource(typeAliasResByKey.getAlias(), typeAliasDataEntry.orphan(), &sts)); const UChar* to = ures_getString(typeAliasDataEntry.getAlias(), &toLen, &sts); if (U_FAILURE(sts)) { break; } // check if this is an alias of canoncal legacy type if (uprv_compareInvWithUChar(NULL, legacyTypeId, -1, to, toLen) == 0) { const char* from = ures_getKey(typeAliasDataEntry.getAlias()); if (isTZ) { // replace colon with slash if necessary if (uprv_strchr(from, ':') != NULL) { int32_t fromLen = uprv_strlen(from); char* fromBuf = (char*)uprv_malloc(fromLen + 1); if (fromBuf == NULL) { sts = U_MEMORY_ALLOCATION_ERROR; break; } const char* p = from; char* q = fromBuf; while (*p) { if (*p == ':') { *q++ = '/'; } else { *q++ = *p; } p++; } *q = 0; gKeyTypeStringPool->addElement(fromBuf, sts); if (U_FAILURE(sts)) { break; } from = fromBuf; } } uhash_put(typeDataMap, (void*)from, t, &sts); } } if (U_FAILURE(sts)) { break; } } if (bcpTypeAliasResByKey.isValid()) { LocalUResourceBundlePointer bcpTypeAliasDataEntry; ures_resetIterator(bcpTypeAliasResByKey.getAlias()); while (ures_hasNext(bcpTypeAliasResByKey.getAlias()) && U_SUCCESS(sts)) { int32_t toLen; bcpTypeAliasDataEntry.adoptInstead(ures_getNextResource(bcpTypeAliasResByKey.getAlias(), bcpTypeAliasDataEntry.orphan(), &sts)); const UChar* to = ures_getString(bcpTypeAliasDataEntry.getAlias(), &toLen, &sts); if (U_FAILURE(sts)) { break; } // check if this is an alias of bcp type if (uprv_compareInvWithUChar(NULL, bcpTypeId, -1, to, toLen) == 0) { const char* from = ures_getKey(bcpTypeAliasDataEntry.getAlias()); uhash_put(typeDataMap, (void*)from, t, &sts); } } if (U_FAILURE(sts)) { break; } } } } if (U_FAILURE(sts)) { break; } LocExtKeyData* keyData = (LocExtKeyData*)uprv_malloc(sizeof(LocExtKeyData)); if (keyData == NULL) { sts = U_MEMORY_ALLOCATION_ERROR; break; } keyData->bcpId = bcpKeyId; keyData->legacyId = legacyKeyId; keyData->specialTypes = specialTypes; keyData->typeMap = typeDataMap; gLocExtKeyDataEntries->addElement((void*)keyData, sts); if (U_FAILURE(sts)) { break; } uhash_put(gLocExtKeyMap, (void*)legacyKeyId, keyData, &sts); if (legacyKeyId != bcpKeyId) { // different key value uhash_put(gLocExtKeyMap, (void*)bcpKeyId, keyData, &sts); } if (U_FAILURE(sts)) { break; } } }
void printOutBundle(UFILE *out, UResourceBundle *resource, int32_t indent, UErrorCode *status) { int32_t i = 0; const char *key = ures_getKey(resource); switch(ures_getType(resource)) { case URES_STRING : { int32_t len=0; const UChar*thestr = ures_getString(resource, &len, status); UChar *string = quotedString(thestr); /* TODO: String truncation */ /* if(trunc && len > truncsize) { printIndent(out, indent); u_fprintf(out, "// WARNING: this string, size %d is truncated to %d\n", len, truncsize/2); len = truncsize/2; } */ printIndent(out, indent); if(key != NULL) { u_fprintf(out, "%s { \"%S\" } ", key, string); } else { u_fprintf(out, "\"%S\",", string); } if(VERBOSE) { u_fprintf(out, " // STRING"); } u_fprintf(out, "\n"); free(string); } break; case URES_INT : printIndent(out, indent); if(key != NULL) { u_fprintf(out, "%s", key); } u_fprintf(out, ":int { %li } ", ures_getInt(resource, status)); if(VERBOSE) { u_fprintf(out, " // INT"); } u_fprintf(out, "\n"); break; case URES_BINARY : { int32_t len = 0; const int8_t *data = (const int8_t *)ures_getBinary(resource, &len, status); if(trunc && len > truncsize) { printIndent(out, indent); u_fprintf(out, "// WARNING: this resource, size %li is truncated to %li\n", len, truncsize/2); len = truncsize/2; } if(U_SUCCESS(*status)) { printIndent(out, indent); if(key != NULL) { u_fprintf(out, "%s", key); } u_fprintf(out, ":binary { "); for(i = 0; i<len; i++) { printHex(out, data++); } u_fprintf(out, " }"); if(VERBOSE) { u_fprintf(out, " // BINARY"); } u_fprintf(out, "\n"); } else { reportError(status); } } break; case URES_INT_VECTOR : { int32_t len = 0; const int32_t *data = ures_getIntVector(resource, &len, status); if(U_SUCCESS(*status)) { printIndent(out, indent); if(key != NULL) { u_fprintf(out, "%s", key); } u_fprintf(out, ":intvector { "); for(i = 0; i<len-1; i++) { u_fprintf(out, "%d, ", data[i]); } if(len > 0) { u_fprintf(out, "%d ", data[len-1]); } u_fprintf(out, "}"); if(VERBOSE) { u_fprintf(out, " // INTVECTOR"); } u_fprintf(out, "\n"); } else { reportError(status); } } break; case URES_TABLE : case URES_ARRAY : { UResourceBundle *t = NULL; ures_resetIterator(resource); printIndent(out, indent); if(key != NULL) { u_fprintf(out, "%s ", key); } u_fprintf(out, "{"); if(VERBOSE) { if(ures_getType(resource) == URES_TABLE) { u_fprintf(out, " // TABLE"); } else { u_fprintf(out, " // ARRAY"); } } u_fprintf(out, "\n"); while(ures_hasNext(resource)) { t = ures_getNextResource(resource, t, status); printOutBundle(out, t, indent+indentsize, status); } printIndent(out, indent); u_fprintf(out, "}\n"); ures_close(t); } break; default: break; } }
void ResourceBundle::resetIterator(void) { ures_resetIterator(fResource); }