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