michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2013, International Business Machines Corporation and michael@0: * others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: * michael@0: * michael@0: * File REGION.CPP michael@0: * michael@0: * Modification History:* michael@0: * Date Name Description michael@0: * 01/15/13 Emmons Original Port from ICU4J michael@0: ******************************************************************************** michael@0: */ michael@0: michael@0: /** michael@0: * \file michael@0: * \brief C++ API: Region classes (territory containment) michael@0: */ michael@0: michael@0: #include "unicode/region.h" michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/uobject.h" michael@0: #include "unicode/unistr.h" michael@0: #include "unicode/ures.h" michael@0: #include "unicode/decimfmt.h" michael@0: #include "ucln_in.h" michael@0: #include "cstring.h" michael@0: #include "uhash.h" michael@0: #include "umutex.h" michael@0: #include "uresimp.h" michael@0: #include "region_impl.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: michael@0: U_CDECL_BEGIN michael@0: michael@0: static void U_CALLCONV michael@0: deleteRegion(void *obj) { michael@0: delete (icu::Region *)obj; michael@0: } michael@0: michael@0: /** michael@0: * Cleanup callback func michael@0: */ michael@0: static UBool U_CALLCONV region_cleanup(void) michael@0: { michael@0: icu::Region::cleanupRegionData(); michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: U_CDECL_END michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: static UMutex gRegionDataLock = U_MUTEX_INITIALIZER; michael@0: static UBool regionDataIsLoaded = false; michael@0: static UVector* availableRegions[URGN_LIMIT]; michael@0: michael@0: static UHashtable *regionAliases; michael@0: static UHashtable *regionIDMap; michael@0: static UHashtable *numericCodeMap; michael@0: michael@0: static const UChar UNKNOWN_REGION_ID [] = { 0x5A, 0x5A, 0 }; /* "ZZ" */ michael@0: static const UChar OUTLYING_OCEANIA_REGION_ID [] = { 0x51, 0x4F, 0 }; /* "QO" */ michael@0: static const UChar WORLD_ID [] = { 0x30, 0x30, 0x31, 0 }; /* "001" */ michael@0: michael@0: UOBJECT_DEFINE_RTTI_IMPLEMENTATION(RegionNameEnumeration) michael@0: michael@0: /* michael@0: * Initializes the region data from the ICU resource bundles. The region data michael@0: * contains the basic relationships such as which regions are known, what the numeric michael@0: * codes are, any known aliases, and the territory containment data. michael@0: * michael@0: * If the region data has already loaded, then this method simply returns without doing michael@0: * anything meaningful. michael@0: */ michael@0: void Region::loadRegionData() { michael@0: michael@0: if (regionDataIsLoaded) { michael@0: return; michael@0: } michael@0: michael@0: umtx_lock(&gRegionDataLock); michael@0: michael@0: if (regionDataIsLoaded) { // In case another thread gets to it before we do... michael@0: umtx_unlock(&gRegionDataLock); michael@0: return; michael@0: } michael@0: michael@0: michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: michael@0: UResourceBundle* regionCodes = NULL; michael@0: UResourceBundle* territoryAlias = NULL; michael@0: UResourceBundle* codeMappings = NULL; michael@0: UResourceBundle* worldContainment = NULL; michael@0: UResourceBundle* territoryContainment = NULL; michael@0: UResourceBundle* groupingContainment = NULL; michael@0: michael@0: DecimalFormat *df = new DecimalFormat(status); michael@0: if (U_FAILURE(status)) { michael@0: umtx_unlock(&gRegionDataLock); michael@0: return; michael@0: } michael@0: df->setParseIntegerOnly(TRUE); michael@0: michael@0: regionIDMap = uhash_open(uhash_hashUnicodeString,uhash_compareUnicodeString,NULL,&status); michael@0: uhash_setValueDeleter(regionIDMap, deleteRegion); michael@0: michael@0: numericCodeMap = uhash_open(uhash_hashLong,uhash_compareLong,NULL,&status); michael@0: michael@0: regionAliases = uhash_open(uhash_hashUnicodeString,uhash_compareUnicodeString,NULL,&status); michael@0: uhash_setKeyDeleter(regionAliases,uprv_deleteUObject); michael@0: michael@0: UResourceBundle *rb = ures_openDirect(NULL,"metadata",&status); michael@0: regionCodes = ures_getByKey(rb,"regionCodes",NULL,&status); michael@0: territoryAlias = ures_getByKey(rb,"territoryAlias",NULL,&status); michael@0: michael@0: UResourceBundle *rb2 = ures_openDirect(NULL,"supplementalData",&status); michael@0: codeMappings = ures_getByKey(rb2,"codeMappings",NULL,&status); michael@0: michael@0: territoryContainment = ures_getByKey(rb2,"territoryContainment",NULL,&status); michael@0: worldContainment = ures_getByKey(territoryContainment,"001",NULL,&status); michael@0: groupingContainment = ures_getByKey(territoryContainment,"grouping",NULL,&status); michael@0: michael@0: UVector *continents = new UVector(uprv_deleteUObject, uhash_compareUnicodeString, status); michael@0: michael@0: while ( ures_hasNext(worldContainment) ) { michael@0: UnicodeString *continentName = new UnicodeString(ures_getNextUnicodeString(worldContainment,NULL,&status)); michael@0: continents->addElement(continentName,status); michael@0: } michael@0: michael@0: UVector *groupings = new UVector(uprv_deleteUObject, uhash_compareUnicodeString, status); michael@0: while ( ures_hasNext(groupingContainment) ) { michael@0: UnicodeString *groupingName = new UnicodeString(ures_getNextUnicodeString(groupingContainment,NULL,&status)); michael@0: groupings->addElement(groupingName,status); michael@0: } michael@0: michael@0: while ( ures_hasNext(regionCodes) ) { michael@0: UnicodeString regionID = ures_getNextUnicodeString(regionCodes,NULL,&status); michael@0: Region *r = new Region(); michael@0: r->idStr = regionID; michael@0: r->idStr.extract(0,r->idStr.length(),r->id,sizeof(r->id),US_INV); michael@0: r->type = URGN_TERRITORY; // Only temporary - figure out the real type later once the aliases are known. michael@0: michael@0: uhash_put(regionIDMap,(void *)&(r->idStr),(void *)r,&status); michael@0: Formattable result; michael@0: UErrorCode ps = U_ZERO_ERROR; michael@0: df->parse(r->idStr,result,ps); michael@0: if ( U_SUCCESS(ps) ) { michael@0: r->code = result.getLong(); // Convert string to number michael@0: uhash_iput(numericCodeMap,r->code,(void *)r,&status); michael@0: r->type = URGN_SUBCONTINENT; michael@0: } else { michael@0: r->code = -1; michael@0: } michael@0: } michael@0: michael@0: michael@0: // Process the territory aliases michael@0: while ( ures_hasNext(territoryAlias) ) { michael@0: UResourceBundle *res = ures_getNextResource(territoryAlias,NULL,&status); michael@0: const char *aliasFrom = ures_getKey(res); michael@0: UnicodeString* aliasFromStr = new UnicodeString(aliasFrom, -1, US_INV); michael@0: UnicodeString aliasTo = ures_getUnicodeString(res,&status); michael@0: ures_close(res); michael@0: michael@0: Region *aliasToRegion = (Region *) uhash_get(regionIDMap,&aliasTo); michael@0: Region *aliasFromRegion = (Region *)uhash_get(regionIDMap,aliasFromStr); michael@0: michael@0: if ( aliasToRegion != NULL && aliasFromRegion == NULL ) { // This is just an alias from some string to a region michael@0: uhash_put(regionAliases,(void *)aliasFromStr, (void *)aliasToRegion,&status); michael@0: } else { michael@0: if ( aliasFromRegion == NULL ) { // Deprecated region code not in the master codes list - so need to create a deprecated region for it. michael@0: aliasFromRegion = new Region(); michael@0: aliasFromRegion->idStr.setTo(*aliasFromStr); michael@0: aliasFromRegion->idStr.extract(0,aliasFromRegion->idStr.length(),aliasFromRegion->id,sizeof(aliasFromRegion->id),US_INV); michael@0: uhash_put(regionIDMap,(void *)&(aliasFromRegion->idStr),(void *)aliasFromRegion,&status); michael@0: Formattable result; michael@0: UErrorCode ps = U_ZERO_ERROR; michael@0: df->parse(aliasFromRegion->idStr,result,ps); michael@0: if ( U_SUCCESS(ps) ) { michael@0: aliasFromRegion->code = result.getLong(); // Convert string to number michael@0: uhash_iput(numericCodeMap,aliasFromRegion->code,(void *)aliasFromRegion,&status); michael@0: } else { michael@0: aliasFromRegion->code = -1; michael@0: } michael@0: aliasFromRegion->type = URGN_DEPRECATED; michael@0: } else { michael@0: aliasFromRegion->type = URGN_DEPRECATED; michael@0: } michael@0: delete aliasFromStr; michael@0: michael@0: aliasFromRegion->preferredValues = new UVector(uprv_deleteUObject, uhash_compareUnicodeString, status); michael@0: UnicodeString currentRegion; michael@0: currentRegion.remove(); michael@0: for (int32_t i = 0 ; i < aliasTo.length() ; i++ ) { michael@0: if ( aliasTo.charAt(i) != 0x0020 ) { michael@0: currentRegion.append(aliasTo.charAt(i)); michael@0: } michael@0: if ( aliasTo.charAt(i) == 0x0020 || i+1 == aliasTo.length() ) { michael@0: Region *target = (Region *)uhash_get(regionIDMap,(void *)¤tRegion); michael@0: if (target) { michael@0: UnicodeString *preferredValue = new UnicodeString(target->idStr); michael@0: aliasFromRegion->preferredValues->addElement((void *)preferredValue,status); michael@0: } michael@0: currentRegion.remove(); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Process the code mappings - This will allow us to assign numeric codes to most of the territories. michael@0: while ( ures_hasNext(codeMappings) ) { michael@0: UResourceBundle *mapping = ures_getNextResource(codeMappings,NULL,&status); michael@0: if ( ures_getType(mapping) == URES_ARRAY && ures_getSize(mapping) == 3) { michael@0: UnicodeString codeMappingID = ures_getUnicodeStringByIndex(mapping,0,&status); michael@0: UnicodeString codeMappingNumber = ures_getUnicodeStringByIndex(mapping,1,&status); michael@0: UnicodeString codeMapping3Letter = ures_getUnicodeStringByIndex(mapping,2,&status); michael@0: michael@0: Region *r = (Region *)uhash_get(regionIDMap,(void *)&codeMappingID); michael@0: if ( r ) { michael@0: Formattable result; michael@0: UErrorCode ps = U_ZERO_ERROR; michael@0: df->parse(codeMappingNumber,result,ps); michael@0: if ( U_SUCCESS(ps) ) { michael@0: r->code = result.getLong(); // Convert string to number michael@0: uhash_iput(numericCodeMap,r->code,(void *)r,&status); michael@0: } michael@0: UnicodeString *code3 = new UnicodeString(codeMapping3Letter); michael@0: uhash_put(regionAliases,(void *)code3, (void *)r,&status); michael@0: } michael@0: } michael@0: ures_close(mapping); michael@0: } michael@0: michael@0: // Now fill in the special cases for WORLD, UNKNOWN, CONTINENTS, and GROUPINGS michael@0: Region *r; michael@0: UnicodeString WORLD_ID_STRING(WORLD_ID); michael@0: r = (Region *) uhash_get(regionIDMap,(void *)&WORLD_ID_STRING); michael@0: if ( r ) { michael@0: r->type = URGN_WORLD; michael@0: } michael@0: michael@0: UnicodeString UNKNOWN_REGION_ID_STRING(UNKNOWN_REGION_ID); michael@0: r = (Region *) uhash_get(regionIDMap,(void *)&UNKNOWN_REGION_ID_STRING); michael@0: if ( r ) { michael@0: r->type = URGN_UNKNOWN; michael@0: } michael@0: michael@0: for ( int32_t i = 0 ; i < continents->size() ; i++ ) { michael@0: r = (Region *) uhash_get(regionIDMap,(void *)continents->elementAt(i)); michael@0: if ( r ) { michael@0: r->type = URGN_CONTINENT; michael@0: } michael@0: } michael@0: delete continents; michael@0: michael@0: for ( int32_t i = 0 ; i < groupings->size() ; i++ ) { michael@0: r = (Region *) uhash_get(regionIDMap,(void *)groupings->elementAt(i)); michael@0: if ( r ) { michael@0: r->type = URGN_GROUPING; michael@0: } michael@0: } michael@0: delete groupings; michael@0: michael@0: // Special case: The region code "QO" (Outlying Oceania) is a subcontinent code added by CLDR michael@0: // even though it looks like a territory code. Need to handle it here. michael@0: michael@0: UnicodeString OUTLYING_OCEANIA_REGION_ID_STRING(OUTLYING_OCEANIA_REGION_ID); michael@0: r = (Region *) uhash_get(regionIDMap,(void *)&OUTLYING_OCEANIA_REGION_ID_STRING); michael@0: if ( r ) { michael@0: r->type = URGN_SUBCONTINENT; michael@0: } michael@0: michael@0: // Load territory containment info from the supplemental data. michael@0: while ( ures_hasNext(territoryContainment) ) { michael@0: UResourceBundle *mapping = ures_getNextResource(territoryContainment,NULL,&status); michael@0: const char *parent = ures_getKey(mapping); michael@0: UnicodeString parentStr = UnicodeString(parent, -1 , US_INV); michael@0: Region *parentRegion = (Region *) uhash_get(regionIDMap,(void *)&parentStr); michael@0: michael@0: for ( int j = 0 ; j < ures_getSize(mapping); j++ ) { michael@0: UnicodeString child = ures_getUnicodeStringByIndex(mapping,j,&status); michael@0: Region *childRegion = (Region *) uhash_get(regionIDMap,(void *)&child); michael@0: if ( parentRegion != NULL && childRegion != NULL ) { michael@0: michael@0: // Add the child region to the set of regions contained by the parent michael@0: if (parentRegion->containedRegions == NULL) { michael@0: parentRegion->containedRegions = new UVector(uprv_deleteUObject, uhash_compareUnicodeString, status); michael@0: } michael@0: michael@0: UnicodeString *childStr = new UnicodeString(); michael@0: childStr->fastCopyFrom(childRegion->idStr); michael@0: parentRegion->containedRegions->addElement((void *)childStr,status); michael@0: michael@0: // Set the parent region to be the containing region of the child. michael@0: // Regions of type GROUPING can't be set as the parent, since another region michael@0: // such as a SUBCONTINENT, CONTINENT, or WORLD must always be the parent. michael@0: if ( parentRegion->type != URGN_GROUPING) { michael@0: childRegion->containingRegion = parentRegion; michael@0: } michael@0: } michael@0: } michael@0: ures_close(mapping); michael@0: } michael@0: michael@0: // Create the availableRegions lists michael@0: int32_t pos = -1; michael@0: while ( const UHashElement* element = uhash_nextElement(regionIDMap,&pos)) { michael@0: Region *ar = (Region *)element->value.pointer; michael@0: if ( availableRegions[ar->type] == NULL ) { michael@0: availableRegions[ar->type] = new UVector(uprv_deleteUObject, uhash_compareUnicodeString, status); michael@0: } michael@0: UnicodeString *arString = new UnicodeString(ar->idStr); michael@0: availableRegions[ar->type]->addElement((void *)arString,status); michael@0: } michael@0: michael@0: ures_close(territoryContainment); michael@0: ures_close(worldContainment); michael@0: ures_close(groupingContainment); michael@0: michael@0: ures_close(codeMappings); michael@0: ures_close(rb2); michael@0: ures_close(territoryAlias); michael@0: ures_close(regionCodes); michael@0: ures_close(rb); michael@0: michael@0: delete df; michael@0: michael@0: ucln_i18n_registerCleanup(UCLN_I18N_REGION, region_cleanup); michael@0: michael@0: regionDataIsLoaded = true; michael@0: umtx_unlock(&gRegionDataLock); michael@0: michael@0: } michael@0: michael@0: void Region::cleanupRegionData() { michael@0: michael@0: for (int32_t i = 0 ; i < URGN_LIMIT ; i++ ) { michael@0: if ( availableRegions[i] ) { michael@0: delete availableRegions[i]; michael@0: } michael@0: } michael@0: michael@0: if (regionAliases) { michael@0: uhash_close(regionAliases); michael@0: } michael@0: michael@0: if (numericCodeMap) { michael@0: uhash_close(numericCodeMap); michael@0: } michael@0: michael@0: if (regionIDMap) { michael@0: uhash_close(regionIDMap); michael@0: } michael@0: } michael@0: michael@0: Region::Region () michael@0: : code(-1), michael@0: type(URGN_UNKNOWN), michael@0: containingRegion(NULL), michael@0: containedRegions(NULL), michael@0: preferredValues(NULL) { michael@0: id[0] = 0; michael@0: } michael@0: michael@0: Region::~Region () { michael@0: if (containedRegions) { michael@0: delete containedRegions; michael@0: } michael@0: if (preferredValues) { michael@0: delete preferredValues; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Returns true if the two regions are equal. michael@0: */ michael@0: UBool michael@0: Region::operator==(const Region &that) const { michael@0: return (idStr == that.idStr); michael@0: } michael@0: michael@0: /** michael@0: * Returns true if the two regions are NOT equal; that is, if operator ==() returns false. michael@0: */ michael@0: UBool michael@0: Region::operator!=(const Region &that) const { michael@0: return (idStr != that.idStr); michael@0: } michael@0: michael@0: /** michael@0: * Returns a pointer to a Region using the given region code. The region code can be either 2-letter ISO code, michael@0: * 3-letter ISO code, UNM.49 numeric code, or other valid Unicode Region Code as defined by the LDML specification. michael@0: * The identifier will be canonicalized internally using the supplemental metadata as defined in the CLDR. michael@0: * If the region code is NULL or not recognized, the appropriate error code will be set ( U_ILLEGAL_ARGUMENT_ERROR ) michael@0: */ michael@0: const Region* U_EXPORT2 michael@0: Region::getInstance(const char *region_code, UErrorCode &status) { michael@0: michael@0: if ( !region_code ) { michael@0: status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: loadRegionData(); michael@0: michael@0: if (regionIDMap == NULL) { michael@0: status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: UnicodeString regionCodeString = UnicodeString(region_code, -1, US_INV); michael@0: Region *r = (Region *)uhash_get(regionIDMap,(void *)®ionCodeString); michael@0: michael@0: if ( !r ) { michael@0: r = (Region *)uhash_get(regionAliases,(void *)®ionCodeString); michael@0: } michael@0: michael@0: if ( !r ) { // Unknown region code michael@0: status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: if ( r->type == URGN_DEPRECATED && r->preferredValues->size() == 1) { michael@0: StringEnumeration *pv = r->getPreferredValues(); michael@0: pv->reset(status); michael@0: const UnicodeString *ustr = pv->snext(status); michael@0: r = (Region *)uhash_get(regionIDMap,(void *)ustr); michael@0: delete pv; michael@0: } michael@0: michael@0: return r; michael@0: michael@0: } michael@0: michael@0: /** michael@0: * Returns a pointer to a Region using the given numeric region code. If the numeric region code is not recognized, michael@0: * the appropriate error code will be set ( U_ILLEGAL_ARGUMENT_ERROR ). michael@0: */ michael@0: const Region* U_EXPORT2 michael@0: Region::getInstance (int32_t code, UErrorCode &status) { michael@0: michael@0: loadRegionData(); michael@0: michael@0: if (numericCodeMap == NULL) { michael@0: status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: Region *r = (Region *)uhash_iget(numericCodeMap,code); michael@0: michael@0: if ( !r ) { // Just in case there's an alias that's numeric, try to find it. michael@0: UErrorCode fs = U_ZERO_ERROR; michael@0: UnicodeString pat = UNICODE_STRING_SIMPLE("00#"); michael@0: DecimalFormat *df = new DecimalFormat(pat,fs); michael@0: michael@0: UnicodeString id; michael@0: id.remove(); michael@0: df->format(code,id); michael@0: delete df; michael@0: r = (Region *)uhash_get(regionAliases,&id); michael@0: } michael@0: michael@0: if ( !r ) { michael@0: status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: if ( r->type == URGN_DEPRECATED && r->preferredValues->size() == 1) { michael@0: StringEnumeration *pv = r->getPreferredValues(); michael@0: pv->reset(status); michael@0: const UnicodeString *ustr = pv->snext(status); michael@0: r = (Region *)uhash_get(regionIDMap,(void *)ustr); michael@0: delete pv; michael@0: } michael@0: michael@0: return r; michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Returns an enumeration over the IDs of all known regions that match the given type. michael@0: */ michael@0: StringEnumeration* U_EXPORT2 michael@0: Region::getAvailable(URegionType type) { michael@0: michael@0: loadRegionData(); michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: return new RegionNameEnumeration(availableRegions[type],status); michael@0: michael@0: return NULL; michael@0: } michael@0: michael@0: /** michael@0: * Returns a pointer to the region that contains this region. Returns NULL if this region is code "001" (World) michael@0: * or "ZZ" (Unknown region). For example, calling this method with region "IT" (Italy) returns the michael@0: * region "039" (Southern Europe). michael@0: */ michael@0: const Region* michael@0: Region::getContainingRegion() const { michael@0: loadRegionData(); michael@0: return containingRegion; michael@0: } michael@0: michael@0: /** michael@0: * Return a pointer to the region that geographically contains this region and matches the given type, michael@0: * moving multiple steps up the containment chain if necessary. Returns NULL if no containing region can be found michael@0: * that matches the given type. Note: The URegionTypes = "URGN_GROUPING", "URGN_DEPRECATED", or "URGN_UNKNOWN" michael@0: * are not appropriate for use in this API. NULL will be returned in this case. For example, calling this method michael@0: * with region "IT" (Italy) for type "URGN_CONTINENT" returns the region "150" ( Europe ). michael@0: */ michael@0: const Region* michael@0: Region::getContainingRegion(URegionType type) const { michael@0: loadRegionData(); michael@0: if ( containingRegion == NULL ) { michael@0: return NULL; michael@0: } michael@0: michael@0: if ( containingRegion->type == type ) { michael@0: return containingRegion; michael@0: } else { michael@0: return containingRegion->getContainingRegion(type); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Return an enumeration over the IDs of all the regions that are immediate children of this region in the michael@0: * region hierarchy. These returned regions could be either macro regions, territories, or a mixture of the two, michael@0: * depending on the containment data as defined in CLDR. This API may return NULL if this region doesn't have michael@0: * any sub-regions. For example, calling this method with region "150" (Europe) returns an enumeration containing michael@0: * the various sub regions of Europe - "039" (Southern Europe) - "151" (Eastern Europe) - "154" (Northern Europe) michael@0: * and "155" (Western Europe). michael@0: */ michael@0: StringEnumeration* michael@0: Region::getContainedRegions() const { michael@0: loadRegionData(); michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: return new RegionNameEnumeration(containedRegions,status); michael@0: } michael@0: michael@0: /** michael@0: * Returns an enumeration over the IDs of all the regions that are children of this region anywhere in the region michael@0: * hierarchy and match the given type. This API may return an empty enumeration if this region doesn't have any michael@0: * sub-regions that match the given type. For example, calling this method with region "150" (Europe) and type michael@0: * "URGN_TERRITORY" returns a set containing all the territories in Europe ( "FR" (France) - "IT" (Italy) - "DE" (Germany) etc. ) michael@0: */ michael@0: StringEnumeration* michael@0: Region::getContainedRegions( URegionType type ) const { michael@0: loadRegionData(); michael@0: michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: UVector *result = new UVector(NULL, uhash_compareChars, status); michael@0: michael@0: StringEnumeration *cr = getContainedRegions(); michael@0: michael@0: for ( int32_t i = 0 ; i < cr->count(status) ; i++ ) { michael@0: const char *id = cr->next(NULL,status); michael@0: const Region *r = Region::getInstance(id,status); michael@0: if ( r->getType() == type ) { michael@0: result->addElement((void *)&r->idStr,status); michael@0: } else { michael@0: StringEnumeration *children = r->getContainedRegions(type); michael@0: for ( int32_t j = 0 ; j < children->count(status) ; j++ ) { michael@0: const char *id2 = children->next(NULL,status); michael@0: const Region *r2 = Region::getInstance(id2,status); michael@0: result->addElement((void *)&r2->idStr,status); michael@0: } michael@0: delete children; michael@0: } michael@0: } michael@0: delete cr; michael@0: StringEnumeration* resultEnumeration = new RegionNameEnumeration(result,status); michael@0: delete result; michael@0: return resultEnumeration; michael@0: } michael@0: michael@0: /** michael@0: * Returns true if this region contains the supplied other region anywhere in the region hierarchy. michael@0: */ michael@0: UBool michael@0: Region::contains(const Region &other) const { michael@0: loadRegionData(); michael@0: michael@0: if (!containedRegions) { michael@0: return FALSE; michael@0: } michael@0: if (containedRegions->contains((void *)&other.idStr)) { michael@0: return TRUE; michael@0: } else { michael@0: for ( int32_t i = 0 ; i < containedRegions->size() ; i++ ) { michael@0: UnicodeString *crStr = (UnicodeString *)containedRegions->elementAt(i); michael@0: Region *cr = (Region *) uhash_get(regionIDMap,(void *)crStr); michael@0: if ( cr && cr->contains(other) ) { michael@0: return TRUE; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return FALSE; michael@0: } michael@0: michael@0: /** michael@0: * For deprecated regions, return an enumeration over the IDs of the regions that are the preferred replacement michael@0: * regions for this region. Returns NULL for a non-deprecated region. For example, calling this method with region michael@0: * "SU" (Soviet Union) would return a list of the regions containing "RU" (Russia), "AM" (Armenia), "AZ" (Azerbaijan), etc... michael@0: */ michael@0: StringEnumeration* michael@0: Region::getPreferredValues() const { michael@0: loadRegionData(); michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: if ( type == URGN_DEPRECATED ) { michael@0: return new RegionNameEnumeration(preferredValues,status); michael@0: } else { michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Return this region's canonical region code. michael@0: */ michael@0: const char* michael@0: Region::getRegionCode() const { michael@0: return id; michael@0: } michael@0: michael@0: int32_t michael@0: Region::getNumericCode() const { michael@0: return code; michael@0: } michael@0: michael@0: /** michael@0: * Returns the region type of this region. michael@0: */ michael@0: URegionType michael@0: Region::getType() const { michael@0: return type; michael@0: } michael@0: michael@0: RegionNameEnumeration::RegionNameEnumeration(UVector *fNameList, UErrorCode& status) { michael@0: pos=0; michael@0: if (fNameList && U_SUCCESS(status)) { michael@0: fRegionNames = new UVector(uprv_deleteUObject, uhash_compareUnicodeString, fNameList->size(),status); michael@0: for ( int32_t i = 0 ; i < fNameList->size() ; i++ ) { michael@0: UnicodeString* this_region_name = (UnicodeString *)fNameList->elementAt(i); michael@0: UnicodeString* new_region_name = new UnicodeString(*this_region_name); michael@0: fRegionNames->addElement((void *)new_region_name,status); michael@0: } michael@0: } michael@0: else { michael@0: fRegionNames = NULL; michael@0: } michael@0: } michael@0: michael@0: const UnicodeString* michael@0: RegionNameEnumeration::snext(UErrorCode& status) { michael@0: if (U_FAILURE(status) || (fRegionNames==NULL)) { michael@0: return NULL; michael@0: } michael@0: const UnicodeString* nextStr = (const UnicodeString *)fRegionNames->elementAt(pos); michael@0: if (nextStr!=NULL) { michael@0: pos++; michael@0: } michael@0: return nextStr; michael@0: } michael@0: michael@0: void michael@0: RegionNameEnumeration::reset(UErrorCode& /*status*/) { michael@0: pos=0; michael@0: } michael@0: michael@0: int32_t michael@0: RegionNameEnumeration::count(UErrorCode& /*status*/) const { michael@0: return (fRegionNames==NULL) ? 0 : fRegionNames->size(); michael@0: } michael@0: michael@0: RegionNameEnumeration::~RegionNameEnumeration() { michael@0: delete fRegionNames; michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */ michael@0: michael@0: //eof