]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/region.cpp
ICU-57149.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / region.cpp
CommitLineData
51004dcb
A
1/*
2*******************************************************************************
b331163b 3* Copyright (C) 2014-2015, International Business Machines Corporation and
51004dcb
A
4* others. All Rights Reserved.
5*******************************************************************************
6*
7*
8* File REGION.CPP
9*
10* Modification History:*
11* Date Name Description
12* 01/15/13 Emmons Original Port from ICU4J
13********************************************************************************
14*/
15
16/**
57a6839d 17 * \file
51004dcb
A
18 * \brief C++ API: Region classes (territory containment)
19 */
20
21#include "unicode/region.h"
22#include "unicode/utypes.h"
23#include "unicode/uobject.h"
24#include "unicode/unistr.h"
25#include "unicode/ures.h"
26#include "unicode/decimfmt.h"
27#include "ucln_in.h"
28#include "cstring.h"
b331163b 29#include "mutex.h"
51004dcb
A
30#include "uhash.h"
31#include "umutex.h"
32#include "uresimp.h"
33#include "region_impl.h"
34
35#if !UCONFIG_NO_FORMATTING
36
37
38U_CDECL_BEGIN
39
40static void U_CALLCONV
41deleteRegion(void *obj) {
42 delete (icu::Region *)obj;
43}
44
45/**
46 * Cleanup callback func
47 */
48static UBool U_CALLCONV region_cleanup(void)
49{
50 icu::Region::cleanupRegionData();
51
52 return TRUE;
53}
54
55U_CDECL_END
56
57U_NAMESPACE_BEGIN
58
b331163b 59static UInitOnce gRegionDataInitOnce = U_INITONCE_INITIALIZER;
51004dcb
A
60static UVector* availableRegions[URGN_LIMIT];
61
b331163b
A
62static UHashtable *regionAliases = NULL;
63static UHashtable *regionIDMap = NULL;
64static UHashtable *numericCodeMap = NULL;
2ca993e8 65static UVector *allRegions = NULL;
51004dcb
A
66
67static const UChar UNKNOWN_REGION_ID [] = { 0x5A, 0x5A, 0 }; /* "ZZ" */
68static const UChar OUTLYING_OCEANIA_REGION_ID [] = { 0x51, 0x4F, 0 }; /* "QO" */
69static const UChar WORLD_ID [] = { 0x30, 0x30, 0x31, 0 }; /* "001" */
2ca993e8 70static const UChar RANGE_MARKER = 0x7e; /* "~" */
51004dcb
A
71
72UOBJECT_DEFINE_RTTI_IMPLEMENTATION(RegionNameEnumeration)
73
74/*
75 * Initializes the region data from the ICU resource bundles. The region data
76 * contains the basic relationships such as which regions are known, what the numeric
77 * codes are, any known aliases, and the territory containment data.
57a6839d 78 *
51004dcb
A
79 * If the region data has already loaded, then this method simply returns without doing
80 * anything meaningful.
81 */
b331163b 82void Region::loadRegionData(UErrorCode &status) {
2ca993e8 83
b331163b
A
84 // Construct service objs first
85 LocalUHashtablePointer newRegionIDMap(uhash_open(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, &status));
86 LocalUHashtablePointer newNumericCodeMap(uhash_open(uhash_hashLong,uhash_compareLong,NULL,&status));
87 LocalUHashtablePointer newRegionAliases(uhash_open(uhash_hashUnicodeString,uhash_compareUnicodeString,NULL,&status));
b331163b 88 LocalPointer<DecimalFormat> df(new DecimalFormat(status), status);
51004dcb 89
b331163b
A
90 LocalPointer<UVector> continents(new UVector(uprv_deleteUObject, uhash_compareUnicodeString, status), status);
91 LocalPointer<UVector> groupings(new UVector(uprv_deleteUObject, uhash_compareUnicodeString, status), status);
2ca993e8 92 allRegions = new UVector(uprv_deleteUObject, uhash_compareUnicodeString, status);
51004dcb 93
b331163b 94 LocalUResourceBundlePointer metadata(ures_openDirect(NULL,"metadata",&status));
b331163b
A
95 LocalUResourceBundlePointer metadataAlias(ures_getByKey(metadata.getAlias(),"alias",NULL,&status));
96 LocalUResourceBundlePointer territoryAlias(ures_getByKey(metadataAlias.getAlias(),"territory",NULL,&status));
51004dcb 97
b331163b
A
98 LocalUResourceBundlePointer supplementalData(ures_openDirect(NULL,"supplementalData",&status));
99 LocalUResourceBundlePointer codeMappings(ures_getByKey(supplementalData.getAlias(),"codeMappings",NULL,&status));
57a6839d 100
2ca993e8
A
101 LocalUResourceBundlePointer idValidity(ures_getByKey(supplementalData.getAlias(),"idValidity",NULL,&status));
102 LocalUResourceBundlePointer regionList(ures_getByKey(idValidity.getAlias(),"region",NULL,&status));
103 LocalUResourceBundlePointer regionRegular(ures_getByKey(regionList.getAlias(),"regular",NULL,&status));
104 LocalUResourceBundlePointer regionMacro(ures_getByKey(regionList.getAlias(),"macroregion",NULL,&status));
105 LocalUResourceBundlePointer regionUnknown(ures_getByKey(regionList.getAlias(),"unknown",NULL,&status));
106
b331163b
A
107 LocalUResourceBundlePointer territoryContainment(ures_getByKey(supplementalData.getAlias(),"territoryContainment",NULL,&status));
108 LocalUResourceBundlePointer worldContainment(ures_getByKey(territoryContainment.getAlias(),"001",NULL,&status));
109 LocalUResourceBundlePointer groupingContainment(ures_getByKey(territoryContainment.getAlias(),"grouping",NULL,&status));
51004dcb 110
51004dcb 111 if (U_FAILURE(status)) {
51004dcb
A
112 return;
113 }
51004dcb 114
b331163b
A
115 // now, initialize
116 df->setParseIntegerOnly(TRUE);
117 uhash_setValueDeleter(newRegionIDMap.getAlias(), deleteRegion); // regionIDMap owns objs
118 uhash_setKeyDeleter(newRegionAliases.getAlias(), uprv_deleteUObject); // regionAliases owns the string keys
51004dcb 119
2ca993e8
A
120
121 while ( ures_hasNext(regionRegular.getAlias()) ) {
122 UnicodeString regionName = ures_getNextUnicodeString(regionRegular.getAlias(),NULL,&status);
123 int32_t rangeMarkerLocation = regionName.indexOf(RANGE_MARKER);
124 UChar buf[6];
125 regionName.extract(buf,6,status);
126 if ( rangeMarkerLocation > 0 ) {
127 UChar endRange = regionName.charAt(rangeMarkerLocation+1);
128 buf[rangeMarkerLocation] = 0;
129 while ( buf[rangeMarkerLocation-1] <= endRange ) {
130 LocalPointer<UnicodeString> newRegion(new UnicodeString(buf), status);
131 allRegions->addElement(newRegion.orphan(),status);
132 buf[rangeMarkerLocation-1]++;
133 }
134 } else {
135 LocalPointer<UnicodeString> newRegion(new UnicodeString(regionName), status);
136 allRegions->addElement(newRegion.orphan(),status);
137 }
138 }
139
140 while ( ures_hasNext(regionMacro.getAlias()) ) {
141 UnicodeString regionName = ures_getNextUnicodeString(regionMacro.getAlias(),NULL,&status);
142 int32_t rangeMarkerLocation = regionName.indexOf(RANGE_MARKER);
143 UChar buf[6];
144 regionName.extract(buf,6,status);
145 if ( rangeMarkerLocation > 0 ) {
146 UChar endRange = regionName.charAt(rangeMarkerLocation+1);
147 buf[rangeMarkerLocation] = 0;
148 while ( buf[rangeMarkerLocation-1] <= endRange ) {
149 LocalPointer<UnicodeString> newRegion(new UnicodeString(buf), status);
150 allRegions->addElement(newRegion.orphan(),status);
151 buf[rangeMarkerLocation-1]++;
152 }
153 } else {
154 LocalPointer<UnicodeString> newRegion(new UnicodeString(regionName), status);
155 allRegions->addElement(newRegion.orphan(),status);
156 }
157 }
158
159 while ( ures_hasNext(regionUnknown.getAlias()) ) {
160 LocalPointer<UnicodeString> regionName (new UnicodeString(ures_getNextUnicodeString(regionUnknown.getAlias(),NULL,&status),status));
161 allRegions->addElement(regionName.orphan(),status);
162 }
163
b331163b
A
164 while ( ures_hasNext(worldContainment.getAlias()) ) {
165 UnicodeString *continentName = new UnicodeString(ures_getNextUnicodeString(worldContainment.getAlias(),NULL,&status));
51004dcb
A
166 continents->addElement(continentName,status);
167 }
168
b331163b
A
169 while ( ures_hasNext(groupingContainment.getAlias()) ) {
170 UnicodeString *groupingName = new UnicodeString(ures_getNextUnicodeString(groupingContainment.getAlias(),NULL,&status));
51004dcb
A
171 groupings->addElement(groupingName,status);
172 }
173
2ca993e8 174 for ( int32_t i = 0 ; i < allRegions->size() ; i++ ) {
b331163b
A
175 LocalPointer<Region> r(new Region(), status);
176 if ( U_FAILURE(status) ) {
177 return;
178 }
2ca993e8
A
179 UnicodeString *regionName = (UnicodeString *)allRegions->elementAt(i);
180 r->idStr = *regionName;
181
51004dcb
A
182 r->idStr.extract(0,r->idStr.length(),r->id,sizeof(r->id),US_INV);
183 r->type = URGN_TERRITORY; // Only temporary - figure out the real type later once the aliases are known.
184
51004dcb
A
185 Formattable result;
186 UErrorCode ps = U_ZERO_ERROR;
187 df->parse(r->idStr,result,ps);
188 if ( U_SUCCESS(ps) ) {
189 r->code = result.getLong(); // Convert string to number
b331163b 190 uhash_iput(newNumericCodeMap.getAlias(),r->code,(void *)(r.getAlias()),&status);
51004dcb
A
191 r->type = URGN_SUBCONTINENT;
192 } else {
193 r->code = -1;
194 }
b331163b
A
195 void* idStrAlias = (void*)&(r->idStr); // about to orphan 'r'. Save this off.
196 uhash_put(newRegionIDMap.getAlias(),idStrAlias,(void *)(r.orphan()),&status); // regionIDMap takes ownership
51004dcb
A
197 }
198
51004dcb 199 // Process the territory aliases
b331163b
A
200 while ( ures_hasNext(territoryAlias.getAlias()) ) {
201 LocalUResourceBundlePointer res(ures_getNextResource(territoryAlias.getAlias(),NULL,&status));
202 const char *aliasFrom = ures_getKey(res.getAlias());
203 LocalPointer<UnicodeString> aliasFromStr(new UnicodeString(aliasFrom, -1, US_INV), status);
204 UnicodeString aliasTo = ures_getUnicodeStringByKey(res.getAlias(),"replacement",&status);
205 res.adoptInstead(NULL);
51004dcb 206
b331163b
A
207 const Region *aliasToRegion = (Region *) uhash_get(newRegionIDMap.getAlias(),&aliasTo);
208 Region *aliasFromRegion = (Region *)uhash_get(newRegionIDMap.getAlias(),aliasFromStr.getAlias());
51004dcb
A
209
210 if ( aliasToRegion != NULL && aliasFromRegion == NULL ) { // This is just an alias from some string to a region
b331163b 211 uhash_put(newRegionAliases.getAlias(),(void *)aliasFromStr.orphan(), (void *)aliasToRegion,&status);
51004dcb
A
212 } else {
213 if ( aliasFromRegion == NULL ) { // Deprecated region code not in the master codes list - so need to create a deprecated region for it.
b331163b
A
214 LocalPointer<Region> newRgn(new Region, status);
215 if ( U_SUCCESS(status) ) {
216 aliasFromRegion = newRgn.orphan();
217 } else {
218 return; // error out
219 }
51004dcb
A
220 aliasFromRegion->idStr.setTo(*aliasFromStr);
221 aliasFromRegion->idStr.extract(0,aliasFromRegion->idStr.length(),aliasFromRegion->id,sizeof(aliasFromRegion->id),US_INV);
b331163b 222 uhash_put(newRegionIDMap.getAlias(),(void *)&(aliasFromRegion->idStr),(void *)aliasFromRegion,&status);
51004dcb
A
223 Formattable result;
224 UErrorCode ps = U_ZERO_ERROR;
225 df->parse(aliasFromRegion->idStr,result,ps);
226 if ( U_SUCCESS(ps) ) {
227 aliasFromRegion->code = result.getLong(); // Convert string to number
b331163b 228 uhash_iput(newNumericCodeMap.getAlias(),aliasFromRegion->code,(void *)aliasFromRegion,&status);
51004dcb
A
229 } else {
230 aliasFromRegion->code = -1;
231 }
232 aliasFromRegion->type = URGN_DEPRECATED;
233 } else {
234 aliasFromRegion->type = URGN_DEPRECATED;
235 }
51004dcb 236
b331163b
A
237 {
238 LocalPointer<UVector> newPreferredValues(new UVector(uprv_deleteUObject, uhash_compareUnicodeString, status), status);
239 aliasFromRegion->preferredValues = newPreferredValues.orphan();
240 }
241 if( U_FAILURE(status)) {
242 return;
243 }
51004dcb 244 UnicodeString currentRegion;
b331163b 245 //currentRegion.remove(); TODO: was already 0 length?
51004dcb
A
246 for (int32_t i = 0 ; i < aliasTo.length() ; i++ ) {
247 if ( aliasTo.charAt(i) != 0x0020 ) {
248 currentRegion.append(aliasTo.charAt(i));
249 }
250 if ( aliasTo.charAt(i) == 0x0020 || i+1 == aliasTo.length() ) {
b331163b 251 Region *target = (Region *)uhash_get(newRegionIDMap.getAlias(),(void *)&currentRegion);
51004dcb 252 if (target) {
b331163b
A
253 LocalPointer<UnicodeString> preferredValue(new UnicodeString(target->idStr), status);
254 aliasFromRegion->preferredValues->addElement((void *)preferredValue.orphan(),status); // may add null if err
51004dcb
A
255 }
256 currentRegion.remove();
257 }
258 }
259 }
260 }
261
262 // Process the code mappings - This will allow us to assign numeric codes to most of the territories.
b331163b
A
263 while ( ures_hasNext(codeMappings.getAlias()) ) {
264 UResourceBundle *mapping = ures_getNextResource(codeMappings.getAlias(),NULL,&status);
51004dcb
A
265 if ( ures_getType(mapping) == URES_ARRAY && ures_getSize(mapping) == 3) {
266 UnicodeString codeMappingID = ures_getUnicodeStringByIndex(mapping,0,&status);
267 UnicodeString codeMappingNumber = ures_getUnicodeStringByIndex(mapping,1,&status);
268 UnicodeString codeMapping3Letter = ures_getUnicodeStringByIndex(mapping,2,&status);
269
b331163b 270 Region *r = (Region *)uhash_get(newRegionIDMap.getAlias(),(void *)&codeMappingID);
51004dcb
A
271 if ( r ) {
272 Formattable result;
273 UErrorCode ps = U_ZERO_ERROR;
274 df->parse(codeMappingNumber,result,ps);
275 if ( U_SUCCESS(ps) ) {
276 r->code = result.getLong(); // Convert string to number
b331163b 277 uhash_iput(newNumericCodeMap.getAlias(),r->code,(void *)r,&status);
51004dcb 278 }
b331163b
A
279 LocalPointer<UnicodeString> code3(new UnicodeString(codeMapping3Letter), status);
280 uhash_put(newRegionAliases.getAlias(),(void *)code3.orphan(), (void *)r,&status);
57a6839d 281 }
51004dcb
A
282 }
283 ures_close(mapping);
284 }
285
286 // Now fill in the special cases for WORLD, UNKNOWN, CONTINENTS, and GROUPINGS
287 Region *r;
b331163b
A
288 UnicodeString WORLD_ID_STRING(WORLD_ID);
289 r = (Region *) uhash_get(newRegionIDMap.getAlias(),(void *)&WORLD_ID_STRING);
51004dcb
A
290 if ( r ) {
291 r->type = URGN_WORLD;
292 }
293
b331163b
A
294 UnicodeString UNKNOWN_REGION_ID_STRING(UNKNOWN_REGION_ID);
295 r = (Region *) uhash_get(newRegionIDMap.getAlias(),(void *)&UNKNOWN_REGION_ID_STRING);
51004dcb
A
296 if ( r ) {
297 r->type = URGN_UNKNOWN;
298 }
299
300 for ( int32_t i = 0 ; i < continents->size() ; i++ ) {
b331163b 301 r = (Region *) uhash_get(newRegionIDMap.getAlias(),(void *)continents->elementAt(i));
51004dcb
A
302 if ( r ) {
303 r->type = URGN_CONTINENT;
304 }
305 }
51004dcb
A
306
307 for ( int32_t i = 0 ; i < groupings->size() ; i++ ) {
b331163b 308 r = (Region *) uhash_get(newRegionIDMap.getAlias(),(void *)groupings->elementAt(i));
51004dcb
A
309 if ( r ) {
310 r->type = URGN_GROUPING;
311 }
312 }
51004dcb
A
313
314 // Special case: The region code "QO" (Outlying Oceania) is a subcontinent code added by CLDR
315 // even though it looks like a territory code. Need to handle it here.
316
b331163b
A
317 UnicodeString OUTLYING_OCEANIA_REGION_ID_STRING(OUTLYING_OCEANIA_REGION_ID);
318 r = (Region *) uhash_get(newRegionIDMap.getAlias(),(void *)&OUTLYING_OCEANIA_REGION_ID_STRING);
51004dcb
A
319 if ( r ) {
320 r->type = URGN_SUBCONTINENT;
321 }
322
323 // Load territory containment info from the supplemental data.
b331163b
A
324 while ( ures_hasNext(territoryContainment.getAlias()) ) {
325 LocalUResourceBundlePointer mapping(ures_getNextResource(territoryContainment.getAlias(),NULL,&status));
326 if( U_FAILURE(status) ) {
327 return; // error out
328 }
329 const char *parent = ures_getKey(mapping.getAlias());
330 if (uprv_strcmp(parent, "containedGroupings") == 0 || uprv_strcmp(parent, "deprecated") == 0) {
331 continue; // handle new pseudo-parent types added in ICU data per cldrbug 7808; for now just skip.
332 // #11232 is to do something useful with these.
333 }
51004dcb 334 UnicodeString parentStr = UnicodeString(parent, -1 , US_INV);
b331163b 335 Region *parentRegion = (Region *) uhash_get(newRegionIDMap.getAlias(),(void *)&parentStr);
51004dcb 336
b331163b
A
337 for ( int j = 0 ; j < ures_getSize(mapping.getAlias()); j++ ) {
338 UnicodeString child = ures_getUnicodeStringByIndex(mapping.getAlias(),j,&status);
339 Region *childRegion = (Region *) uhash_get(newRegionIDMap.getAlias(),(void *)&child);
57a6839d 340 if ( parentRegion != NULL && childRegion != NULL ) {
51004dcb
A
341
342 // Add the child region to the set of regions contained by the parent
343 if (parentRegion->containedRegions == NULL) {
344 parentRegion->containedRegions = new UVector(uprv_deleteUObject, uhash_compareUnicodeString, status);
345 }
346
b331163b
A
347 LocalPointer<UnicodeString> childStr(new UnicodeString(), status);
348 if( U_FAILURE(status) ) {
349 return; // error out
350 }
51004dcb 351 childStr->fastCopyFrom(childRegion->idStr);
b331163b 352 parentRegion->containedRegions->addElement((void *)childStr.orphan(),status);
51004dcb
A
353
354 // Set the parent region to be the containing region of the child.
355 // Regions of type GROUPING can't be set as the parent, since another region
356 // such as a SUBCONTINENT, CONTINENT, or WORLD must always be the parent.
357 if ( parentRegion->type != URGN_GROUPING) {
358 childRegion->containingRegion = parentRegion;
359 }
360 }
361 }
57a6839d 362 }
51004dcb
A
363
364 // Create the availableRegions lists
b331163b
A
365 int32_t pos = UHASH_FIRST;
366 while ( const UHashElement* element = uhash_nextElement(newRegionIDMap.getAlias(),&pos)) {
51004dcb
A
367 Region *ar = (Region *)element->value.pointer;
368 if ( availableRegions[ar->type] == NULL ) {
b331163b
A
369 LocalPointer<UVector> newAr(new UVector(uprv_deleteUObject, uhash_compareUnicodeString, status), status);
370 availableRegions[ar->type] = newAr.orphan();
51004dcb 371 }
b331163b
A
372 LocalPointer<UnicodeString> arString(new UnicodeString(ar->idStr), status);
373 if( U_FAILURE(status) ) {
374 return; // error out
375 }
376 availableRegions[ar->type]->addElement((void *)arString.orphan(),status);
51004dcb 377 }
b331163b 378
51004dcb 379 ucln_i18n_registerCleanup(UCLN_I18N_REGION, region_cleanup);
b331163b
A
380 // copy hashtables
381 numericCodeMap = newNumericCodeMap.orphan();
382 regionIDMap = newRegionIDMap.orphan();
383 regionAliases = newRegionAliases.orphan();
51004dcb
A
384}
385
386void Region::cleanupRegionData() {
51004dcb
A
387 for (int32_t i = 0 ; i < URGN_LIMIT ; i++ ) {
388 if ( availableRegions[i] ) {
389 delete availableRegions[i];
390 }
391 }
392
393 if (regionAliases) {
394 uhash_close(regionAliases);
395 }
396
397 if (numericCodeMap) {
398 uhash_close(numericCodeMap);
399 }
400
401 if (regionIDMap) {
402 uhash_close(regionIDMap);
403 }
2ca993e8
A
404 if (allRegions) {
405 allRegions->removeAllElements(); // Don't need the temporary list anymore.
406 delete allRegions;
407 allRegions = NULL;
408 }
409
b331163b
A
410 regionAliases = numericCodeMap = regionIDMap = NULL;
411
412 gRegionDataInitOnce.reset();
51004dcb
A
413}
414
415Region::Region ()
416 : code(-1),
417 type(URGN_UNKNOWN),
418 containingRegion(NULL),
419 containedRegions(NULL),
420 preferredValues(NULL) {
421 id[0] = 0;
422}
423
424Region::~Region () {
425 if (containedRegions) {
426 delete containedRegions;
427 }
428 if (preferredValues) {
429 delete preferredValues;
430 }
431}
432
433/**
434 * Returns true if the two regions are equal.
b331163b 435 * Per PMC, just use pointer compare, since we have at most one instance of each Region.
51004dcb
A
436 */
437UBool
438Region::operator==(const Region &that) const {
439 return (idStr == that.idStr);
440}
441
442/**
443 * Returns true if the two regions are NOT equal; that is, if operator ==() returns false.
b331163b 444 * Per PMC, just use pointer compare, since we have at most one instance of each Region.
51004dcb
A
445 */
446UBool
447Region::operator!=(const Region &that) const {
448 return (idStr != that.idStr);
449}
57a6839d 450
51004dcb
A
451/**
452 * Returns a pointer to a Region using the given region code. The region code can be either 2-letter ISO code,
453 * 3-letter ISO code, UNM.49 numeric code, or other valid Unicode Region Code as defined by the LDML specification.
454 * The identifier will be canonicalized internally using the supplemental metadata as defined in the CLDR.
455 * If the region code is NULL or not recognized, the appropriate error code will be set ( U_ILLEGAL_ARGUMENT_ERROR )
456 */
457const Region* U_EXPORT2
458Region::getInstance(const char *region_code, UErrorCode &status) {
459
b331163b
A
460 umtx_initOnce(gRegionDataInitOnce, &loadRegionData, status);
461 if (U_FAILURE(status)) {
51004dcb
A
462 return NULL;
463 }
464
b331163b 465 if ( !region_code ) {
51004dcb
A
466 status = U_ILLEGAL_ARGUMENT_ERROR;
467 return NULL;
468 }
469
470 UnicodeString regionCodeString = UnicodeString(region_code, -1, US_INV);
471 Region *r = (Region *)uhash_get(regionIDMap,(void *)&regionCodeString);
472
473 if ( !r ) {
474 r = (Region *)uhash_get(regionAliases,(void *)&regionCodeString);
475 }
476
477 if ( !r ) { // Unknown region code
478 status = U_ILLEGAL_ARGUMENT_ERROR;
479 return NULL;
480 }
481
482 if ( r->type == URGN_DEPRECATED && r->preferredValues->size() == 1) {
b331163b 483 StringEnumeration *pv = r->getPreferredValues(status);
51004dcb
A
484 pv->reset(status);
485 const UnicodeString *ustr = pv->snext(status);
486 r = (Region *)uhash_get(regionIDMap,(void *)ustr);
487 delete pv;
488 }
489
490 return r;
491
492}
493
494/**
495 * Returns a pointer to a Region using the given numeric region code. If the numeric region code is not recognized,
496 * the appropriate error code will be set ( U_ILLEGAL_ARGUMENT_ERROR ).
497 */
57a6839d 498const Region* U_EXPORT2
51004dcb
A
499Region::getInstance (int32_t code, UErrorCode &status) {
500
b331163b
A
501 umtx_initOnce(gRegionDataInitOnce, &loadRegionData, status);
502 if (U_FAILURE(status)) {
51004dcb
A
503 return NULL;
504 }
505
506 Region *r = (Region *)uhash_iget(numericCodeMap,code);
507
508 if ( !r ) { // Just in case there's an alias that's numeric, try to find it.
2ca993e8
A
509 UnicodeString pat = UNICODE_STRING_SIMPLE("0");
510 LocalPointer<DecimalFormat> df(new DecimalFormat(pat,status), status);
b331163b
A
511 if( U_FAILURE(status) ) {
512 return NULL;
513 }
51004dcb
A
514 UnicodeString id;
515 id.remove();
b331163b
A
516 FieldPosition posIter;
517 df->format(code,id, posIter, status);
51004dcb
A
518 r = (Region *)uhash_get(regionAliases,&id);
519 }
520
b331163b
A
521 if( U_FAILURE(status) ) {
522 return NULL;
523 }
524
51004dcb
A
525 if ( !r ) {
526 status = U_ILLEGAL_ARGUMENT_ERROR;
527 return NULL;
528 }
529
530 if ( r->type == URGN_DEPRECATED && r->preferredValues->size() == 1) {
b331163b 531 StringEnumeration *pv = r->getPreferredValues(status);
51004dcb
A
532 pv->reset(status);
533 const UnicodeString *ustr = pv->snext(status);
534 r = (Region *)uhash_get(regionIDMap,(void *)ustr);
535 delete pv;
536 }
537
538 return r;
539}
540
541
542/**
543 * Returns an enumeration over the IDs of all known regions that match the given type.
544 */
545StringEnumeration* U_EXPORT2
b331163b
A
546Region::getAvailable(URegionType type, UErrorCode &status) {
547 umtx_initOnce(gRegionDataInitOnce, &loadRegionData, status); // returns immediately if U_FAILURE(status)
548 if (U_FAILURE(status)) {
549 return NULL;
550 }
51004dcb 551 return new RegionNameEnumeration(availableRegions[type],status);
51004dcb 552}
57a6839d 553
51004dcb
A
554/**
555 * Returns a pointer to the region that contains this region. Returns NULL if this region is code "001" (World)
556 * or "ZZ" (Unknown region). For example, calling this method with region "IT" (Italy) returns the
557 * region "039" (Southern Europe).
558 */
559const Region*
560Region::getContainingRegion() const {
b331163b
A
561 UErrorCode status = U_ZERO_ERROR;
562 umtx_initOnce(gRegionDataInitOnce, &loadRegionData, status);
51004dcb
A
563 return containingRegion;
564}
565
566/**
567 * Return a pointer to the region that geographically contains this region and matches the given type,
568 * moving multiple steps up the containment chain if necessary. Returns NULL if no containing region can be found
569 * that matches the given type. Note: The URegionTypes = "URGN_GROUPING", "URGN_DEPRECATED", or "URGN_UNKNOWN"
570 * are not appropriate for use in this API. NULL will be returned in this case. For example, calling this method
571 * with region "IT" (Italy) for type "URGN_CONTINENT" returns the region "150" ( Europe ).
572 */
573const Region*
574Region::getContainingRegion(URegionType type) const {
b331163b
A
575 UErrorCode status = U_ZERO_ERROR;
576 umtx_initOnce(gRegionDataInitOnce, &loadRegionData, status);
51004dcb
A
577 if ( containingRegion == NULL ) {
578 return NULL;
579 }
580
b331163b 581 return ( containingRegion->type == type )? containingRegion: containingRegion->getContainingRegion(type);
51004dcb
A
582}
583
584/**
585 * Return an enumeration over the IDs of all the regions that are immediate children of this region in the
586 * region hierarchy. These returned regions could be either macro regions, territories, or a mixture of the two,
587 * depending on the containment data as defined in CLDR. This API may return NULL if this region doesn't have
588 * any sub-regions. For example, calling this method with region "150" (Europe) returns an enumeration containing
589 * the various sub regions of Europe - "039" (Southern Europe) - "151" (Eastern Europe) - "154" (Northern Europe)
590 * and "155" (Western Europe).
591 */
592StringEnumeration*
b331163b
A
593Region::getContainedRegions(UErrorCode &status) const {
594 umtx_initOnce(gRegionDataInitOnce, &loadRegionData, status); // returns immediately if U_FAILURE(status)
595 if (U_FAILURE(status)) {
596 return NULL;
597 }
51004dcb
A
598 return new RegionNameEnumeration(containedRegions,status);
599}
600
601/**
602 * Returns an enumeration over the IDs of all the regions that are children of this region anywhere in the region
603 * hierarchy and match the given type. This API may return an empty enumeration if this region doesn't have any
604 * sub-regions that match the given type. For example, calling this method with region "150" (Europe) and type
605 * "URGN_TERRITORY" returns a set containing all the territories in Europe ( "FR" (France) - "IT" (Italy) - "DE" (Germany) etc. )
606 */
607StringEnumeration*
b331163b
A
608Region::getContainedRegions( URegionType type, UErrorCode &status ) const {
609 umtx_initOnce(gRegionDataInitOnce, &loadRegionData, status); // returns immediately if U_FAILURE(status)
610 if (U_FAILURE(status)) {
611 return NULL;
612 }
51004dcb 613
51004dcb 614 UVector *result = new UVector(NULL, uhash_compareChars, status);
57a6839d 615
b331163b 616 StringEnumeration *cr = getContainedRegions(status);
51004dcb
A
617
618 for ( int32_t i = 0 ; i < cr->count(status) ; i++ ) {
619 const char *id = cr->next(NULL,status);
620 const Region *r = Region::getInstance(id,status);
621 if ( r->getType() == type ) {
622 result->addElement((void *)&r->idStr,status);
623 } else {
b331163b 624 StringEnumeration *children = r->getContainedRegions(type, status);
51004dcb
A
625 for ( int32_t j = 0 ; j < children->count(status) ; j++ ) {
626 const char *id2 = children->next(NULL,status);
627 const Region *r2 = Region::getInstance(id2,status);
628 result->addElement((void *)&r2->idStr,status);
629 }
630 delete children;
631 }
632 }
633 delete cr;
634 StringEnumeration* resultEnumeration = new RegionNameEnumeration(result,status);
635 delete result;
636 return resultEnumeration;
637}
57a6839d 638
51004dcb
A
639/**
640 * Returns true if this region contains the supplied other region anywhere in the region hierarchy.
641 */
642UBool
643Region::contains(const Region &other) const {
b331163b
A
644 UErrorCode status = U_ZERO_ERROR;
645 umtx_initOnce(gRegionDataInitOnce, &loadRegionData, status);
51004dcb
A
646
647 if (!containedRegions) {
648 return FALSE;
649 }
650 if (containedRegions->contains((void *)&other.idStr)) {
651 return TRUE;
652 } else {
653 for ( int32_t i = 0 ; i < containedRegions->size() ; i++ ) {
654 UnicodeString *crStr = (UnicodeString *)containedRegions->elementAt(i);
655 Region *cr = (Region *) uhash_get(regionIDMap,(void *)crStr);
656 if ( cr && cr->contains(other) ) {
657 return TRUE;
658 }
659 }
660 }
661
662 return FALSE;
663}
664
665/**
666 * For deprecated regions, return an enumeration over the IDs of the regions that are the preferred replacement
667 * regions for this region. Returns NULL for a non-deprecated region. For example, calling this method with region
668 * "SU" (Soviet Union) would return a list of the regions containing "RU" (Russia), "AM" (Armenia), "AZ" (Azerbaijan), etc...
669 */
670StringEnumeration*
b331163b
A
671Region::getPreferredValues(UErrorCode &status) const {
672 umtx_initOnce(gRegionDataInitOnce, &loadRegionData, status); // returns immediately if U_FAILURE(status)
673 if (U_FAILURE(status) || type != URGN_DEPRECATED) {
51004dcb
A
674 return NULL;
675 }
b331163b 676 return new RegionNameEnumeration(preferredValues,status);
51004dcb 677}
57a6839d 678
51004dcb
A
679
680/**
681 * Return this region's canonical region code.
682 */
683const char*
684Region::getRegionCode() const {
685 return id;
686}
687
688int32_t
689Region::getNumericCode() const {
690 return code;
691}
692
693/**
694 * Returns the region type of this region.
695 */
696URegionType
697Region::getType() const {
698 return type;
699}
700
701RegionNameEnumeration::RegionNameEnumeration(UVector *fNameList, UErrorCode& status) {
702 pos=0;
703 if (fNameList && U_SUCCESS(status)) {
704 fRegionNames = new UVector(uprv_deleteUObject, uhash_compareUnicodeString, fNameList->size(),status);
705 for ( int32_t i = 0 ; i < fNameList->size() ; i++ ) {
706 UnicodeString* this_region_name = (UnicodeString *)fNameList->elementAt(i);
707 UnicodeString* new_region_name = new UnicodeString(*this_region_name);
57a6839d 708 fRegionNames->addElement((void *)new_region_name,status);
51004dcb
A
709 }
710 }
57a6839d 711 else {
51004dcb
A
712 fRegionNames = NULL;
713 }
714}
715
716const UnicodeString*
57a6839d 717RegionNameEnumeration::snext(UErrorCode& status) {
51004dcb
A
718 if (U_FAILURE(status) || (fRegionNames==NULL)) {
719 return NULL;
720 }
721 const UnicodeString* nextStr = (const UnicodeString *)fRegionNames->elementAt(pos);
722 if (nextStr!=NULL) {
723 pos++;
724 }
725 return nextStr;
726}
727
728void
729RegionNameEnumeration::reset(UErrorCode& /*status*/) {
730 pos=0;
731}
732
733int32_t
734RegionNameEnumeration::count(UErrorCode& /*status*/) const {
735 return (fRegionNames==NULL) ? 0 : fRegionNames->size();
736}
737
738RegionNameEnumeration::~RegionNameEnumeration() {
739 delete fRegionNames;
740}
741
742U_NAMESPACE_END
743
744#endif /* #if !UCONFIG_NO_FORMATTING */
745
746//eof