clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name tzgnames.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/home/maurizio/node-v18.6.0/out -resource-dir /usr/local/lib/clang/16.0.0 -D V8_DEPRECATION_WARNINGS -D V8_IMMINENT_DEPRECATION_WARNINGS -D _GLIBCXX_USE_CXX11_ABI=1 -D NODE_OPENSSL_CONF_NAME=nodejs_conf -D NODE_OPENSSL_HAS_QUIC -D __STDC_FORMAT_MACROS -D OPENSSL_NO_PINSHARED -D OPENSSL_THREADS -D U_COMMON_IMPLEMENTATION=1 -D U_I18N_IMPLEMENTATION=1 -D U_IO_IMPLEMENTATION=1 -D U_TOOLUTIL_IMPLEMENTATION=1 -D U_ATTRIBUTE_DEPRECATED= -D _CRT_SECURE_NO_DEPRECATE= -D U_STATIC_IMPLEMENTATION=1 -D UCONFIG_NO_SERVICE=1 -D U_ENABLE_DYLOAD=0 -D U_HAVE_STD_STRING=1 -D UCONFIG_NO_BREAK_ITERATION=0 -I ../deps/icu-small/source/common -I ../deps/icu-small/source/i18n -I ../deps/icu-small/source/tools/toolutil -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8 -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/x86_64-redhat-linux -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/backward -internal-isystem /usr/local/lib/clang/16.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../x86_64-redhat-linux/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Wno-unused-parameter -Wno-deprecated-declarations -Wno-strict-aliasing -std=gnu++17 -fdeprecated-macro -fdebug-compilation-dir=/home/maurizio/node-v18.6.0/out -ferror-limit 19 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-08-22-142216-507842-1 -x c++ ../deps/icu-small/source/i18n/tzgnames.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | #include "unicode/utypes.h" |
11 | |
12 | #if !UCONFIG_NO_FORMATTING |
13 | |
14 | #include "tzgnames.h" |
15 | |
16 | #include "unicode/basictz.h" |
17 | #include "unicode/locdspnm.h" |
18 | #include "unicode/rbtz.h" |
19 | #include "unicode/simpleformatter.h" |
20 | #include "unicode/simpletz.h" |
21 | #include "unicode/strenum.h" |
22 | #include "unicode/vtzone.h" |
23 | |
24 | #include "bytesinkutil.h" |
25 | #include "charstr.h" |
26 | #include "cmemory.h" |
27 | #include "cstring.h" |
28 | #include "mutex.h" |
29 | #include "uhash.h" |
30 | #include "uassert.h" |
31 | #include "umutex.h" |
32 | #include "ulocimp.h" |
33 | #include "uresimp.h" |
34 | #include "ureslocs.h" |
35 | #include "zonemeta.h" |
36 | #include "tznames_impl.h" |
37 | #include "olsontz.h" |
38 | #include "ucln_in.h" |
39 | |
40 | U_NAMESPACE_BEGIN |
41 | |
42 | #define ZID_KEY_MAX 128 |
43 | |
44 | static const char gZoneStrings[] = "zoneStrings"; |
45 | |
46 | static const char gRegionFormatTag[] = "regionFormat"; |
47 | static const char gFallbackFormatTag[] = "fallbackFormat"; |
48 | |
49 | static const UChar gEmpty[] = {0x00}; |
50 | |
51 | static const UChar gDefRegionPattern[] = {0x7B, 0x30, 0x7D, 0x00}; |
52 | static const UChar gDefFallbackPattern[] = {0x7B, 0x31, 0x7D, 0x20, 0x28, 0x7B, 0x30, 0x7D, 0x29, 0x00}; |
53 | |
54 | static const double kDstCheckRange = (double)184*U_MILLIS_PER_DAY; |
55 | |
56 | |
57 | |
58 | U_CDECL_BEGIN |
59 | |
60 | typedef struct PartialLocationKey { |
61 | const UChar* tzID; |
62 | const UChar* mzID; |
63 | UBool isLong; |
64 | } PartialLocationKey; |
65 | |
66 | |
67 | |
68 | |
69 | static int32_t U_CALLCONV |
70 | hashPartialLocationKey(const UHashTok key) { |
71 | |
72 | PartialLocationKey *p = (PartialLocationKey *)key.pointer; |
73 | UnicodeString str(p->tzID); |
74 | str.append((UChar)0x26) |
75 | .append(p->mzID, -1) |
76 | .append((UChar)0x23) |
77 | .append((UChar)(p->isLong ? 0x4C : 0x53)); |
78 | return str.hashCode(); |
79 | } |
80 | |
81 | |
82 | |
83 | |
84 | static UBool U_CALLCONV |
85 | comparePartialLocationKey(const UHashTok key1, const UHashTok key2) { |
86 | PartialLocationKey *p1 = (PartialLocationKey *)key1.pointer; |
87 | PartialLocationKey *p2 = (PartialLocationKey *)key2.pointer; |
88 | |
89 | if (p1 == p2) { |
90 | return TRUE; |
91 | } |
92 | if (p1 == NULL || p2 == NULL) { |
93 | return FALSE; |
94 | } |
95 | |
96 | return (p1->tzID == p2->tzID && p1->mzID == p2->mzID && p1->isLong == p2->isLong); |
97 | } |
98 | |
99 | |
100 | |
101 | |
102 | static void U_CALLCONV |
103 | deleteGNameInfo(void *obj) { |
104 | uprv_free(obj); |
105 | } |
106 | |
107 | |
108 | |
109 | |
110 | typedef struct GNameInfo { |
111 | UTimeZoneGenericNameType type; |
112 | const UChar* tzID; |
113 | } ZNameInfo; |
114 | |
115 | |
116 | |
117 | |
118 | typedef struct GMatchInfo { |
119 | const GNameInfo* gnameInfo; |
120 | int32_t matchLength; |
121 | UTimeZoneFormatTimeType timeType; |
122 | } ZMatchInfo; |
123 | |
124 | U_CDECL_END |
125 | |
126 | |
127 | |
128 | |
129 | class TimeZoneGenericNameMatchInfo : public UMemory { |
130 | public: |
131 | TimeZoneGenericNameMatchInfo(UVector* matches); |
132 | ~TimeZoneGenericNameMatchInfo(); |
133 | |
134 | int32_t size() const; |
135 | UTimeZoneGenericNameType getGenericNameType(int32_t index) const; |
136 | int32_t getMatchLength(int32_t index) const; |
137 | UnicodeString& getTimeZoneID(int32_t index, UnicodeString& tzID) const; |
138 | |
139 | private: |
140 | UVector* fMatches; |
141 | }; |
142 | |
143 | TimeZoneGenericNameMatchInfo::TimeZoneGenericNameMatchInfo(UVector* matches) |
144 | : fMatches(matches) { |
145 | } |
146 | |
147 | TimeZoneGenericNameMatchInfo::~TimeZoneGenericNameMatchInfo() { |
148 | if (fMatches != NULL) { |
149 | delete fMatches; |
150 | } |
151 | } |
152 | |
153 | int32_t |
154 | TimeZoneGenericNameMatchInfo::size() const { |
155 | if (fMatches == NULL) { |
156 | return 0; |
157 | } |
158 | return fMatches->size(); |
159 | } |
160 | |
161 | UTimeZoneGenericNameType |
162 | TimeZoneGenericNameMatchInfo::getGenericNameType(int32_t index) const { |
163 | GMatchInfo *minfo = (GMatchInfo *)fMatches->elementAt(index); |
164 | if (minfo != NULL) { |
165 | return static_cast<UTimeZoneGenericNameType>(minfo->gnameInfo->type); |
166 | } |
167 | return UTZGNM_UNKNOWN; |
168 | } |
169 | |
170 | int32_t |
171 | TimeZoneGenericNameMatchInfo::getMatchLength(int32_t index) const { |
172 | ZMatchInfo *minfo = (ZMatchInfo *)fMatches->elementAt(index); |
173 | if (minfo != NULL) { |
174 | return minfo->matchLength; |
175 | } |
176 | return -1; |
177 | } |
178 | |
179 | UnicodeString& |
180 | TimeZoneGenericNameMatchInfo::getTimeZoneID(int32_t index, UnicodeString& tzID) const { |
181 | GMatchInfo *minfo = (GMatchInfo *)fMatches->elementAt(index); |
182 | if (minfo != NULL && minfo->gnameInfo->tzID != NULL) { |
183 | tzID.setTo(TRUE, minfo->gnameInfo->tzID, -1); |
184 | } else { |
185 | tzID.setToBogus(); |
186 | } |
187 | return tzID; |
188 | } |
189 | |
190 | |
191 | |
192 | |
193 | class GNameSearchHandler : public TextTrieMapSearchResultHandler { |
194 | public: |
195 | GNameSearchHandler(uint32_t types); |
196 | virtual ~GNameSearchHandler(); |
197 | |
198 | UBool handleMatch(int32_t matchLength, const CharacterNode *node, UErrorCode &status) override; |
199 | UVector* getMatches(int32_t& maxMatchLen); |
200 | |
201 | private: |
202 | uint32_t fTypes; |
203 | UVector* fResults; |
204 | int32_t fMaxMatchLen; |
205 | }; |
206 | |
207 | GNameSearchHandler::GNameSearchHandler(uint32_t types) |
208 | : fTypes(types), fResults(NULL), fMaxMatchLen(0) { |
209 | } |
210 | |
211 | GNameSearchHandler::~GNameSearchHandler() { |
212 | if (fResults != NULL) { |
213 | delete fResults; |
214 | } |
215 | } |
216 | |
217 | UBool |
218 | GNameSearchHandler::handleMatch(int32_t matchLength, const CharacterNode *node, UErrorCode &status) { |
219 | if (U_FAILURE(status)) { |
220 | return FALSE; |
221 | } |
222 | if (node->hasValues()) { |
223 | int32_t valuesCount = node->countValues(); |
224 | for (int32_t i = 0; i < valuesCount; i++) { |
225 | GNameInfo *nameinfo = (ZNameInfo *)node->getValue(i); |
226 | if (nameinfo == NULL) { |
227 | break; |
228 | } |
229 | if ((nameinfo->type & fTypes) != 0) { |
230 | |
231 | if (fResults == NULL) { |
232 | LocalPointer<UVector> lpResults(new UVector(uprv_free, NULL, status), status); |
233 | if (U_FAILURE(status)) { |
234 | return false; |
235 | } |
236 | fResults = lpResults.orphan(); |
237 | } |
238 | GMatchInfo *gmatch = (GMatchInfo *)uprv_malloc(sizeof(GMatchInfo)); |
239 | if (gmatch == NULL) { |
240 | status = U_MEMORY_ALLOCATION_ERROR; |
241 | return false; |
242 | } |
243 | |
244 | gmatch->gnameInfo = nameinfo; |
245 | gmatch->matchLength = matchLength; |
246 | gmatch->timeType = UTZFMT_TIME_TYPE_UNKNOWN; |
247 | fResults->adoptElement(gmatch, status); |
248 | if (U_FAILURE(status)) { |
249 | return false; |
250 | } |
251 | if (matchLength > fMaxMatchLen) { |
252 | fMaxMatchLen = matchLength; |
253 | } |
254 | } |
255 | } |
256 | } |
257 | return TRUE; |
258 | } |
259 | |
260 | UVector* |
261 | GNameSearchHandler::getMatches(int32_t& maxMatchLen) { |
262 | |
263 | UVector *results = fResults; |
264 | maxMatchLen = fMaxMatchLen; |
265 | |
266 | |
267 | fResults = NULL; |
268 | fMaxMatchLen = 0; |
269 | return results; |
270 | } |
271 | |
272 | static UMutex gLock; |
273 | |
274 | class TZGNCore : public UMemory { |
275 | public: |
276 | TZGNCore(const Locale& locale, UErrorCode& status); |
277 | virtual ~TZGNCore(); |
278 | |
279 | UnicodeString& getDisplayName(const TimeZone& tz, UTimeZoneGenericNameType type, |
280 | UDate date, UnicodeString& name) const; |
281 | |
282 | UnicodeString& getGenericLocationName(const UnicodeString& tzCanonicalID, UnicodeString& name) const; |
283 | |
284 | int32_t findBestMatch(const UnicodeString& text, int32_t start, uint32_t types, |
285 | UnicodeString& tzID, UTimeZoneFormatTimeType& timeType, UErrorCode& status) const; |
286 | |
287 | private: |
288 | Locale fLocale; |
289 | const TimeZoneNames* fTimeZoneNames; |
290 | UHashtable* fLocationNamesMap; |
291 | UHashtable* fPartialLocationNamesMap; |
292 | |
293 | SimpleFormatter fRegionFormat; |
294 | SimpleFormatter fFallbackFormat; |
295 | |
296 | LocaleDisplayNames* fLocaleDisplayNames; |
297 | ZNStringPool fStringPool; |
298 | |
299 | TextTrieMap fGNamesTrie; |
300 | UBool fGNamesTrieFullyLoaded; |
301 | |
302 | char fTargetRegion[ULOC_COUNTRY_CAPACITY]; |
303 | |
304 | void initialize(const Locale& locale, UErrorCode& status); |
305 | void cleanup(); |
306 | |
307 | void loadStrings(const UnicodeString& tzCanonicalID); |
308 | |
309 | const UChar* getGenericLocationName(const UnicodeString& tzCanonicalID); |
310 | |
311 | UnicodeString& formatGenericNonLocationName(const TimeZone& tz, UTimeZoneGenericNameType type, |
312 | UDate date, UnicodeString& name) const; |
313 | |
314 | UnicodeString& getPartialLocationName(const UnicodeString& tzCanonicalID, |
315 | const UnicodeString& mzID, UBool isLong, const UnicodeString& mzDisplayName, |
316 | UnicodeString& name) const; |
317 | |
318 | const UChar* getPartialLocationName(const UnicodeString& tzCanonicalID, |
319 | const UnicodeString& mzID, UBool isLong, const UnicodeString& mzDisplayName); |
320 | |
321 | TimeZoneGenericNameMatchInfo* findLocal(const UnicodeString& text, int32_t start, uint32_t types, UErrorCode& status) const; |
322 | |
323 | TimeZoneNames::MatchInfoCollection* findTimeZoneNames(const UnicodeString& text, int32_t start, uint32_t types, UErrorCode& status) const; |
324 | }; |
325 | |
326 | |
327 | |
328 | |
329 | |
330 | |
331 | |
332 | |
333 | |
334 | TZGNCore::TZGNCore(const Locale& locale, UErrorCode& status) |
335 | : fLocale(locale), |
336 | fTimeZoneNames(NULL), |
337 | fLocationNamesMap(NULL), |
338 | fPartialLocationNamesMap(NULL), |
339 | fLocaleDisplayNames(NULL), |
340 | fStringPool(status), |
341 | fGNamesTrie(TRUE, deleteGNameInfo), |
342 | fGNamesTrieFullyLoaded(FALSE) { |
343 | initialize(locale, status); |
344 | } |
345 | |
346 | TZGNCore::~TZGNCore() { |
347 | cleanup(); |
348 | } |
349 | |
350 | void |
351 | TZGNCore::initialize(const Locale& locale, UErrorCode& status) { |
352 | if (U_FAILURE(status)) { |
353 | return; |
354 | } |
355 | |
356 | |
357 | fTimeZoneNames = TimeZoneNames::createInstance(locale, status); |
358 | if (U_FAILURE(status)) { |
359 | return; |
360 | } |
361 | |
362 | |
363 | UnicodeString rpat(TRUE, gDefRegionPattern, -1); |
364 | UnicodeString fpat(TRUE, gDefFallbackPattern, -1); |
365 | |
366 | UErrorCode tmpsts = U_ZERO_ERROR; |
367 | UResourceBundle *zoneStrings = ures_open(U_ICUDATA_ZONE, locale.getName(), &tmpsts); |
368 | zoneStrings = ures_getByKeyWithFallback(zoneStrings, gZoneStrings, zoneStrings, &tmpsts); |
369 | |
370 | if (U_SUCCESS(tmpsts)) { |
371 | const UChar *regionPattern = ures_getStringByKeyWithFallback(zoneStrings, gRegionFormatTag, NULL, &tmpsts); |
372 | if (U_SUCCESS(tmpsts) && u_strlen(regionPattern) > 0) { |
373 | rpat.setTo(regionPattern, -1); |
374 | } |
375 | tmpsts = U_ZERO_ERROR; |
376 | const UChar *fallbackPattern = ures_getStringByKeyWithFallback(zoneStrings, gFallbackFormatTag, NULL, &tmpsts); |
377 | if (U_SUCCESS(tmpsts) && u_strlen(fallbackPattern) > 0) { |
378 | fpat.setTo(fallbackPattern, -1); |
379 | } |
380 | } |
381 | ures_close(zoneStrings); |
382 | |
383 | fRegionFormat.applyPatternMinMaxArguments(rpat, 1, 1, status); |
384 | fFallbackFormat.applyPatternMinMaxArguments(fpat, 2, 2, status); |
385 | if (U_FAILURE(status)) { |
386 | cleanup(); |
387 | return; |
388 | } |
389 | |
390 | |
391 | fLocaleDisplayNames = LocaleDisplayNames::createInstance(locale); |
392 | |
393 | |
394 | fLocationNamesMap = uhash_open(uhash_hashUChars, uhash_compareUChars, NULL, &status); |
395 | if (U_FAILURE(status)) { |
396 | cleanup(); |
397 | return; |
398 | } |
399 | |
400 | fPartialLocationNamesMap = uhash_open(hashPartialLocationKey, comparePartialLocationKey, NULL, &status); |
401 | if (U_FAILURE(status)) { |
402 | cleanup(); |
403 | return; |
404 | } |
405 | uhash_setKeyDeleter(fPartialLocationNamesMap, uprv_free); |
406 | |
407 | |
408 | |
409 | const char* region = fLocale.getCountry(); |
410 | int32_t regionLen = static_cast<int32_t>(uprv_strlen(region)); |
411 | if (regionLen == 0) { |
412 | CharString loc; |
413 | { |
414 | CharStringByteSink sink(&loc); |
415 | ulocimp_addLikelySubtags(fLocale.getName(), sink, &status); |
416 | } |
417 | |
418 | regionLen = uloc_getCountry(loc.data(), fTargetRegion, sizeof(fTargetRegion), &status); |
419 | if (U_SUCCESS(status)) { |
420 | fTargetRegion[regionLen] = 0; |
421 | } else { |
422 | cleanup(); |
423 | return; |
424 | } |
425 | } else if (regionLen < (int32_t)sizeof(fTargetRegion)) { |
426 | uprv_strcpy(fTargetRegion, region); |
427 | } else { |
428 | fTargetRegion[0] = 0; |
429 | } |
430 | |
431 | |
432 | TimeZone *tz = TimeZone::createDefault(); |
433 | const UChar *tzID = ZoneMeta::getCanonicalCLDRID(*tz); |
434 | if (tzID != NULL) { |
435 | loadStrings(UnicodeString(TRUE, tzID, -1)); |
436 | } |
437 | delete tz; |
438 | } |
439 | |
440 | void |
441 | TZGNCore::cleanup() { |
442 | if (fLocaleDisplayNames != NULL) { |
443 | delete fLocaleDisplayNames; |
444 | } |
445 | if (fTimeZoneNames != NULL) { |
446 | delete fTimeZoneNames; |
447 | } |
448 | |
449 | uhash_close(fLocationNamesMap); |
450 | uhash_close(fPartialLocationNamesMap); |
451 | } |
452 | |
453 | |
454 | UnicodeString& |
455 | TZGNCore::getDisplayName(const TimeZone& tz, UTimeZoneGenericNameType type, UDate date, UnicodeString& name) const { |
456 | name.setToBogus(); |
457 | switch (type) { |
| 2 | | Control jumps to 'case UTZGNM_SHORT:' at line 467 | |
|
458 | case UTZGNM_LOCATION: |
459 | { |
460 | const UChar* tzCanonicalID = ZoneMeta::getCanonicalCLDRID(tz); |
461 | if (tzCanonicalID != NULL) { |
462 | getGenericLocationName(UnicodeString(TRUE, tzCanonicalID, -1), name); |
463 | } |
464 | } |
465 | break; |
466 | case UTZGNM_LONG: |
467 | case UTZGNM_SHORT: |
468 | formatGenericNonLocationName(tz, type, date, name); |
| 3 | | Calling 'TZGNCore::formatGenericNonLocationName' | |
|
469 | if (name.isEmpty()) { |
470 | const UChar* tzCanonicalID = ZoneMeta::getCanonicalCLDRID(tz); |
471 | if (tzCanonicalID != NULL) { |
472 | getGenericLocationName(UnicodeString(TRUE, tzCanonicalID, -1), name); |
473 | } |
474 | } |
475 | break; |
476 | default: |
477 | break; |
478 | } |
479 | return name; |
480 | } |
481 | |
482 | UnicodeString& |
483 | TZGNCore::getGenericLocationName(const UnicodeString& tzCanonicalID, UnicodeString& name) const { |
484 | if (tzCanonicalID.isEmpty()) { |
485 | name.setToBogus(); |
486 | return name; |
487 | } |
488 | |
489 | const UChar *locname = NULL; |
490 | TZGNCore *nonConstThis = const_cast<TZGNCore *>(this); |
491 | umtx_lock(&gLock); |
492 | { |
493 | locname = nonConstThis->getGenericLocationName(tzCanonicalID); |
494 | } |
495 | umtx_unlock(&gLock); |
496 | |
497 | if (locname == NULL) { |
498 | name.setToBogus(); |
499 | } else { |
500 | name.setTo(locname, u_strlen(locname)); |
501 | } |
502 | |
503 | return name; |
504 | } |
505 | |
506 | |
507 | |
508 | |
509 | const UChar* |
510 | TZGNCore::getGenericLocationName(const UnicodeString& tzCanonicalID) { |
511 | U_ASSERT(!tzCanonicalID.isEmpty()); |
512 | if (tzCanonicalID.length() > ZID_KEY_MAX) { |
513 | return NULL; |
514 | } |
515 | |
516 | UErrorCode status = U_ZERO_ERROR; |
517 | UChar tzIDKey[ZID_KEY_MAX + 1]; |
518 | int32_t tzIDKeyLen = tzCanonicalID.extract(tzIDKey, ZID_KEY_MAX + 1, status); |
519 | U_ASSERT(status == U_ZERO_ERROR); |
520 | tzIDKey[tzIDKeyLen] = 0; |
521 | |
522 | const UChar *locname = (const UChar *)uhash_get(fLocationNamesMap, tzIDKey); |
523 | |
524 | if (locname != NULL) { |
525 | |
526 | if (locname == gEmpty) { |
527 | return NULL; |
528 | } |
529 | return locname; |
530 | } |
531 | |
532 | |
533 | UnicodeString name; |
534 | UnicodeString usCountryCode; |
535 | UBool isPrimary = FALSE; |
536 | |
537 | ZoneMeta::getCanonicalCountry(tzCanonicalID, usCountryCode, &isPrimary); |
538 | |
539 | if (!usCountryCode.isEmpty()) { |
540 | if (isPrimary) { |
541 | |
542 | char countryCode[ULOC_COUNTRY_CAPACITY]; |
543 | U_ASSERT(usCountryCode.length() < ULOC_COUNTRY_CAPACITY); |
544 | int32_t ccLen = usCountryCode.extract(0, usCountryCode.length(), countryCode, sizeof(countryCode), US_INV); |
545 | countryCode[ccLen] = 0; |
546 | |
547 | UnicodeString country; |
548 | fLocaleDisplayNames->regionDisplayName(countryCode, country); |
549 | fRegionFormat.format(country, name, status); |
550 | } else { |
551 | |
552 | |
553 | |
554 | |
555 | |
556 | |
557 | UnicodeString city; |
558 | fTimeZoneNames->getExemplarLocationName(tzCanonicalID, city); |
559 | fRegionFormat.format(city, name, status); |
560 | } |
561 | if (U_FAILURE(status)) { |
562 | return NULL; |
563 | } |
564 | } |
565 | |
566 | locname = name.isEmpty() ? NULL : fStringPool.get(name, status); |
567 | if (U_SUCCESS(status)) { |
568 | |
569 | const UChar* cacheID = ZoneMeta::findTimeZoneID(tzCanonicalID); |
570 | U_ASSERT(cacheID != NULL); |
571 | if (locname == NULL) { |
572 | |
573 | uhash_put(fLocationNamesMap, (void *)cacheID, (void *)gEmpty, &status); |
574 | } else { |
575 | uhash_put(fLocationNamesMap, (void *)cacheID, (void *)locname, &status); |
576 | if (U_FAILURE(status)) { |
577 | locname = NULL; |
578 | } else { |
579 | |
580 | GNameInfo *nameinfo = (ZNameInfo *)uprv_malloc(sizeof(GNameInfo)); |
581 | if (nameinfo != NULL) { |
582 | nameinfo->type = UTZGNM_LOCATION; |
583 | nameinfo->tzID = cacheID; |
584 | fGNamesTrie.put(locname, nameinfo, status); |
585 | } |
586 | } |
587 | } |
588 | } |
589 | |
590 | return locname; |
591 | } |
592 | |
593 | UnicodeString& |
594 | TZGNCore::formatGenericNonLocationName(const TimeZone& tz, UTimeZoneGenericNameType type, UDate date, UnicodeString& name) const { |
595 | U_ASSERT(type == UTZGNM_LONG || type == UTZGNM_SHORT); |
596 | name.setToBogus(); |
597 | |
598 | const UChar* uID = ZoneMeta::getCanonicalCLDRID(tz); |
599 | if (uID == NULL) { |
| 4 | | Assuming 'uID' is not equal to NULL | |
|
| |
600 | return name; |
601 | } |
602 | |
603 | UnicodeString tzID(TRUE, uID, -1); |
604 | |
605 | |
606 | UTimeZoneNameType nameType = (type == UTZGNM_LONG) ? UTZNM_LONG_GENERIC : UTZNM_SHORT_GENERIC; |
| |
607 | fTimeZoneNames->getTimeZoneDisplayName(tzID, nameType, name); |
608 | |
609 | if (!name.isEmpty()) { |
| |
610 | return name; |
611 | } |
612 | |
613 | |
614 | UChar mzIDBuf[32]; |
615 | UnicodeString mzID(mzIDBuf, 0, UPRV_LENGTHOF(mzIDBuf)); |
616 | fTimeZoneNames->getMetaZoneID(tzID, date, mzID); |
617 | if (!mzID.isEmpty()) { |
| |
618 | UErrorCode status = U_ZERO_ERROR; |
619 | UBool useStandard = FALSE; |
620 | int32_t raw, sav; |
621 | UChar tmpNameBuf[ZONE_NAME_U16_MAX]; |
622 | |
623 | tz.getOffset(date, FALSE, raw, sav, status); |
624 | if (U_FAILURE(status)) { |
| |
625 | return name; |
626 | } |
627 | |
628 | if (sav == 0) { |
| 10 | | Assuming 'sav' is equal to 0 | |
|
| |
629 | useStandard = TRUE; |
630 | |
631 | TimeZone *tmptz = tz.clone(); |
| 12 | | 'tmptz' initialized here | |
|
632 | |
633 | BasicTimeZone *btz = NULL; |
634 | if (dynamic_cast<OlsonTimeZone *>(tmptz) != NULL |
| 13 | | Assuming 'tmptz' is equal to NULL | |
|
| |
635 | || dynamic_cast<SimpleTimeZone *>(tmptz) != NULL |
636 | || dynamic_cast<RuleBasedTimeZone *>(tmptz) != NULL |
637 | || dynamic_cast<VTimeZone *>(tmptz) != NULL) { |
638 | btz = (BasicTimeZone*)tmptz; |
639 | } |
640 | |
641 | if (btz != NULL) { |
| |
642 | TimeZoneTransition before; |
643 | UBool beforTrs = btz->getPreviousTransition(date, TRUE, before); |
644 | if (beforTrs |
645 | && (date - before.getTime() < kDstCheckRange) |
646 | && before.getFrom()->getDSTSavings() != 0) { |
647 | useStandard = FALSE; |
648 | } else { |
649 | TimeZoneTransition after; |
650 | UBool afterTrs = btz->getNextTransition(date, FALSE, after); |
651 | if (afterTrs |
652 | && (after.getTime() - date < kDstCheckRange) |
653 | && after.getTo()->getDSTSavings() != 0) { |
654 | useStandard = FALSE; |
655 | } |
656 | } |
657 | } else { |
658 | |
659 | |
660 | tmptz->getOffset(date - kDstCheckRange, FALSE, raw, sav, status); |
| 16 | | Called C++ object pointer is null |
|
661 | if (sav != 0) { |
662 | useStandard = FALSE; |
663 | } else { |
664 | tmptz->getOffset(date + kDstCheckRange, FALSE, raw, sav, status); |
665 | if (sav != 0){ |
666 | useStandard = FALSE; |
667 | } |
668 | } |
669 | if (U_FAILURE(status)) { |
670 | delete tmptz; |
671 | return name; |
672 | } |
673 | } |
674 | delete tmptz; |
675 | } |
676 | if (useStandard) { |
677 | UTimeZoneNameType stdNameType = (nameType == UTZNM_LONG_GENERIC) |
678 | ? UTZNM_LONG_STANDARD : UTZNM_SHORT_STANDARD; |
679 | UnicodeString stdName(tmpNameBuf, 0, UPRV_LENGTHOF(tmpNameBuf)); |
680 | fTimeZoneNames->getDisplayName(tzID, stdNameType, date, stdName); |
681 | if (!stdName.isEmpty()) { |
682 | name.setTo(stdName); |
683 | |
684 | |
685 | |
686 | |
687 | |
688 | |
689 | UChar genNameBuf[ZONE_NAME_U16_MAX]; |
690 | UnicodeString mzGenericName(genNameBuf, 0, UPRV_LENGTHOF(genNameBuf)); |
691 | fTimeZoneNames->getMetaZoneDisplayName(mzID, nameType, mzGenericName); |
692 | if (stdName.caseCompare(mzGenericName, 0) == 0) { |
693 | name.setToBogus(); |
694 | } |
695 | } |
696 | } |
697 | if (name.isEmpty()) { |
698 | |
699 | UnicodeString mzName(tmpNameBuf, 0, UPRV_LENGTHOF(tmpNameBuf)); |
700 | fTimeZoneNames->getMetaZoneDisplayName(mzID, nameType, mzName); |
701 | if (!mzName.isEmpty()) { |
702 | |
703 | |
704 | |
705 | UChar idBuf[32]; |
706 | UnicodeString goldenID(idBuf, 0, UPRV_LENGTHOF(idBuf)); |
707 | fTimeZoneNames->getReferenceZoneID(mzID, fTargetRegion, goldenID); |
708 | if (!goldenID.isEmpty() && goldenID != tzID) { |
709 | TimeZone *goldenZone = TimeZone::createTimeZone(goldenID); |
710 | int32_t raw1, sav1; |
711 | |
712 | |
713 | |
714 | |
715 | |
716 | goldenZone->getOffset(date + raw + sav, TRUE, raw1, sav1, status); |
717 | delete goldenZone; |
718 | if (U_SUCCESS(status)) { |
719 | if (raw != raw1 || sav != sav1) { |
720 | |
721 | getPartialLocationName(tzID, mzID, (nameType == UTZNM_LONG_GENERIC), mzName, name); |
722 | } else { |
723 | name.setTo(mzName); |
724 | } |
725 | } |
726 | } else { |
727 | name.setTo(mzName); |
728 | } |
729 | } |
730 | } |
731 | } |
732 | return name; |
733 | } |
734 | |
735 | UnicodeString& |
736 | TZGNCore::getPartialLocationName(const UnicodeString& tzCanonicalID, |
737 | const UnicodeString& mzID, UBool isLong, const UnicodeString& mzDisplayName, |
738 | UnicodeString& name) const { |
739 | name.setToBogus(); |
740 | if (tzCanonicalID.isEmpty() || mzID.isEmpty() || mzDisplayName.isEmpty()) { |
741 | return name; |
742 | } |
743 | |
744 | const UChar *uplname = NULL; |
745 | TZGNCore *nonConstThis = const_cast<TZGNCore *>(this); |
746 | umtx_lock(&gLock); |
747 | { |
748 | uplname = nonConstThis->getPartialLocationName(tzCanonicalID, mzID, isLong, mzDisplayName); |
749 | } |
750 | umtx_unlock(&gLock); |
751 | |
752 | if (uplname == NULL) { |
753 | name.setToBogus(); |
754 | } else { |
755 | name.setTo(TRUE, uplname, -1); |
756 | } |
757 | return name; |
758 | } |
759 | |
760 | |
761 | |
762 | |
763 | const UChar* |
764 | TZGNCore::getPartialLocationName(const UnicodeString& tzCanonicalID, |
765 | const UnicodeString& mzID, UBool isLong, const UnicodeString& mzDisplayName) { |
766 | U_ASSERT(!tzCanonicalID.isEmpty()); |
767 | U_ASSERT(!mzID.isEmpty()); |
768 | U_ASSERT(!mzDisplayName.isEmpty()); |
769 | |
770 | PartialLocationKey key; |
771 | key.tzID = ZoneMeta::findTimeZoneID(tzCanonicalID); |
772 | key.mzID = ZoneMeta::findMetaZoneID(mzID); |
773 | key.isLong = isLong; |
774 | U_ASSERT(key.tzID != NULL && key.mzID != NULL); |
775 | |
776 | const UChar* uplname = (const UChar*)uhash_get(fPartialLocationNamesMap, (void *)&key); |
777 | if (uplname != NULL) { |
778 | return uplname; |
779 | } |
780 | |
781 | UnicodeString location; |
782 | UnicodeString usCountryCode; |
783 | ZoneMeta::getCanonicalCountry(tzCanonicalID, usCountryCode); |
784 | if (!usCountryCode.isEmpty()) { |
785 | char countryCode[ULOC_COUNTRY_CAPACITY]; |
786 | U_ASSERT(usCountryCode.length() < ULOC_COUNTRY_CAPACITY); |
787 | int32_t ccLen = usCountryCode.extract(0, usCountryCode.length(), countryCode, sizeof(countryCode), US_INV); |
788 | countryCode[ccLen] = 0; |
789 | |
790 | UnicodeString regionalGolden; |
791 | fTimeZoneNames->getReferenceZoneID(mzID, countryCode, regionalGolden); |
792 | if (tzCanonicalID == regionalGolden) { |
793 | |
794 | fLocaleDisplayNames->regionDisplayName(countryCode, location); |
795 | } else { |
796 | |
797 | fTimeZoneNames->getExemplarLocationName(tzCanonicalID, location); |
798 | } |
799 | } else { |
800 | fTimeZoneNames->getExemplarLocationName(tzCanonicalID, location); |
801 | if (location.isEmpty()) { |
802 | |
803 | |
804 | |
805 | location.setTo(tzCanonicalID); |
806 | } |
807 | } |
808 | |
809 | UErrorCode status = U_ZERO_ERROR; |
810 | UnicodeString name; |
811 | fFallbackFormat.format(location, mzDisplayName, name, status); |
812 | if (U_FAILURE(status)) { |
813 | return NULL; |
814 | } |
815 | |
816 | uplname = fStringPool.get(name, status); |
817 | if (U_SUCCESS(status)) { |
818 | |
819 | PartialLocationKey* cacheKey = (PartialLocationKey *)uprv_malloc(sizeof(PartialLocationKey)); |
820 | if (cacheKey != NULL) { |
821 | cacheKey->tzID = key.tzID; |
822 | cacheKey->mzID = key.mzID; |
823 | cacheKey->isLong = key.isLong; |
824 | uhash_put(fPartialLocationNamesMap, (void *)cacheKey, (void *)uplname, &status); |
825 | if (U_FAILURE(status)) { |
826 | uprv_free(cacheKey); |
827 | } else { |
828 | |
829 | GNameInfo *nameinfo = (ZNameInfo *)uprv_malloc(sizeof(GNameInfo)); |
830 | if (nameinfo != NULL) { |
831 | nameinfo->type = isLong ? UTZGNM_LONG : UTZGNM_SHORT; |
832 | nameinfo->tzID = key.tzID; |
833 | fGNamesTrie.put(uplname, nameinfo, status); |
834 | } |
835 | } |
836 | } |
837 | } |
838 | return uplname; |
839 | } |
840 | |
841 | |
842 | |
843 | |
844 | |
845 | void |
846 | TZGNCore::loadStrings(const UnicodeString& tzCanonicalID) { |
847 | |
848 | getGenericLocationName(tzCanonicalID); |
849 | |
850 | |
851 | UErrorCode status = U_ZERO_ERROR; |
852 | |
853 | const UnicodeString *mzID; |
854 | UnicodeString goldenID; |
855 | UnicodeString mzGenName; |
856 | UTimeZoneNameType genNonLocTypes[] = { |
857 | UTZNM_LONG_GENERIC, UTZNM_SHORT_GENERIC, |
858 | UTZNM_UNKNOWN |
859 | }; |
860 | |
861 | StringEnumeration *mzIDs = fTimeZoneNames->getAvailableMetaZoneIDs(tzCanonicalID, status); |
862 | while ((mzID = mzIDs->snext(status)) != NULL) { |
863 | if (U_FAILURE(status)) { |
864 | break; |
865 | } |
866 | |
867 | |
868 | |
869 | fTimeZoneNames->getReferenceZoneID(*mzID, fTargetRegion, goldenID); |
870 | if (tzCanonicalID != goldenID) { |
871 | for (int32_t i = 0; genNonLocTypes[i] != UTZNM_UNKNOWN; i++) { |
872 | fTimeZoneNames->getMetaZoneDisplayName(*mzID, genNonLocTypes[i], mzGenName); |
873 | if (!mzGenName.isEmpty()) { |
874 | |
875 | getPartialLocationName(tzCanonicalID, *mzID, |
876 | (genNonLocTypes[i] == UTZNM_LONG_GENERIC), mzGenName); |
877 | } |
878 | } |
879 | } |
880 | } |
881 | if (mzIDs != NULL) { |
882 | delete mzIDs; |
883 | } |
884 | } |
885 | |
886 | int32_t |
887 | TZGNCore::findBestMatch(const UnicodeString& text, int32_t start, uint32_t types, |
888 | UnicodeString& tzID, UTimeZoneFormatTimeType& timeType, UErrorCode& status) const { |
889 | timeType = UTZFMT_TIME_TYPE_UNKNOWN; |
890 | tzID.setToBogus(); |
891 | |
892 | if (U_FAILURE(status)) { |
893 | return 0; |
894 | } |
895 | |
896 | |
897 | TimeZoneNames::MatchInfoCollection *tznamesMatches = findTimeZoneNames(text, start, types, status); |
898 | if (U_FAILURE(status)) { |
899 | return 0; |
900 | } |
901 | |
902 | int32_t bestMatchLen = 0; |
903 | UTimeZoneFormatTimeType bestMatchTimeType = UTZFMT_TIME_TYPE_UNKNOWN; |
904 | UnicodeString bestMatchTzID; |
905 | |
906 | UBool isStandard = FALSE; |
907 | |
908 | if (tznamesMatches != NULL) { |
909 | UnicodeString mzID; |
910 | for (int32_t i = 0; i < tznamesMatches->size(); i++) { |
911 | int32_t len = tznamesMatches->getMatchLengthAt(i); |
912 | if (len > bestMatchLen) { |
913 | bestMatchLen = len; |
914 | if (!tznamesMatches->getTimeZoneIDAt(i, bestMatchTzID)) { |
915 | |
916 | if (tznamesMatches->getMetaZoneIDAt(i, mzID)) { |
917 | fTimeZoneNames->getReferenceZoneID(mzID, fTargetRegion, bestMatchTzID); |
918 | } |
919 | } |
920 | UTimeZoneNameType nameType = tznamesMatches->getNameTypeAt(i); |
921 | if (U_FAILURE(status)) { |
922 | break; |
923 | } |
924 | switch (nameType) { |
925 | case UTZNM_LONG_STANDARD: |
926 | |
927 | case UTZNM_SHORT_STANDARD: |
928 | isStandard = TRUE; |
929 | bestMatchTimeType = UTZFMT_TIME_TYPE_STANDARD; |
930 | break; |
931 | case UTZNM_LONG_DAYLIGHT: |
932 | case UTZNM_SHORT_DAYLIGHT: |
933 | bestMatchTimeType = UTZFMT_TIME_TYPE_DAYLIGHT; |
934 | break; |
935 | default: |
936 | bestMatchTimeType = UTZFMT_TIME_TYPE_UNKNOWN; |
937 | } |
938 | } |
939 | } |
940 | delete tznamesMatches; |
941 | if (U_FAILURE(status)) { |
942 | return 0; |
943 | } |
944 | |
945 | if (bestMatchLen == (text.length() - start)) { |
946 | |
947 | |
948 | |
949 | |
950 | |
951 | |
952 | |
953 | |
954 | |
955 | |
956 | |
957 | |
958 | |
959 | |
960 | |
961 | |
962 | |
963 | |
964 | |
965 | |
966 | |
967 | |
968 | |
969 | if (!isStandard) { |
970 | tzID.setTo(bestMatchTzID); |
971 | timeType = bestMatchTimeType; |
972 | return bestMatchLen; |
973 | } |
974 | } |
975 | } |
976 | |
977 | |
978 | TimeZoneGenericNameMatchInfo *localMatches = findLocal(text, start, types, status); |
979 | if (U_FAILURE(status)) { |
980 | return 0; |
981 | } |
982 | if (localMatches != NULL) { |
983 | for (int32_t i = 0; i < localMatches->size(); i++) { |
984 | int32_t len = localMatches->getMatchLength(i); |
985 | |
986 | |
987 | |
988 | |
989 | |
990 | |
991 | if (len >= bestMatchLen) { |
992 | bestMatchLen = localMatches->getMatchLength(i); |
993 | bestMatchTimeType = UTZFMT_TIME_TYPE_UNKNOWN; |
994 | localMatches->getTimeZoneID(i, bestMatchTzID); |
995 | } |
996 | } |
997 | delete localMatches; |
998 | } |
999 | |
1000 | if (bestMatchLen > 0) { |
1001 | timeType = bestMatchTimeType; |
1002 | tzID.setTo(bestMatchTzID); |
1003 | } |
1004 | return bestMatchLen; |
1005 | } |
1006 | |
1007 | TimeZoneGenericNameMatchInfo* |
1008 | TZGNCore::findLocal(const UnicodeString& text, int32_t start, uint32_t types, UErrorCode& status) const { |
1009 | GNameSearchHandler handler(types); |
1010 | |
1011 | TZGNCore *nonConstThis = const_cast<TZGNCore *>(this); |
1012 | |
1013 | umtx_lock(&gLock); |
1014 | { |
1015 | fGNamesTrie.search(text, start, (TextTrieMapSearchResultHandler *)&handler, status); |
1016 | } |
1017 | umtx_unlock(&gLock); |
1018 | |
1019 | if (U_FAILURE(status)) { |
1020 | return NULL; |
1021 | } |
1022 | |
1023 | TimeZoneGenericNameMatchInfo *gmatchInfo = NULL; |
1024 | |
1025 | int32_t maxLen = 0; |
1026 | UVector *results = handler.getMatches(maxLen); |
1027 | if (results != NULL && ((maxLen == (text.length() - start)) || fGNamesTrieFullyLoaded)) { |
1028 | |
1029 | gmatchInfo = new TimeZoneGenericNameMatchInfo(results); |
1030 | if (gmatchInfo == NULL) { |
1031 | status = U_MEMORY_ALLOCATION_ERROR; |
1032 | delete results; |
1033 | return NULL; |
1034 | } |
1035 | return gmatchInfo; |
1036 | } |
1037 | |
1038 | if (results != NULL) { |
1039 | delete results; |
1040 | } |
1041 | |
1042 | |
1043 | |
1044 | umtx_lock(&gLock); |
1045 | { |
1046 | if (!fGNamesTrieFullyLoaded) { |
1047 | StringEnumeration *tzIDs = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL, NULL, NULL, status); |
1048 | if (U_SUCCESS(status)) { |
1049 | const UnicodeString *tzID; |
1050 | while ((tzID = tzIDs->snext(status)) != NULL) { |
1051 | if (U_FAILURE(status)) { |
1052 | break; |
1053 | } |
1054 | nonConstThis->loadStrings(*tzID); |
1055 | } |
1056 | } |
1057 | if (tzIDs != NULL) { |
1058 | delete tzIDs; |
1059 | } |
1060 | |
1061 | if (U_SUCCESS(status)) { |
1062 | nonConstThis->fGNamesTrieFullyLoaded = TRUE; |
1063 | } |
1064 | } |
1065 | } |
1066 | umtx_unlock(&gLock); |
1067 | |
1068 | if (U_FAILURE(status)) { |
1069 | return NULL; |
1070 | } |
1071 | |
1072 | umtx_lock(&gLock); |
1073 | { |
1074 | |
1075 | fGNamesTrie.search(text, start, (TextTrieMapSearchResultHandler *)&handler, status); |
1076 | } |
1077 | umtx_unlock(&gLock); |
1078 | |
1079 | results = handler.getMatches(maxLen); |
1080 | if (results != NULL && maxLen > 0) { |
1081 | gmatchInfo = new TimeZoneGenericNameMatchInfo(results); |
1082 | if (gmatchInfo == NULL) { |
1083 | status = U_MEMORY_ALLOCATION_ERROR; |
1084 | delete results; |
1085 | return NULL; |
1086 | } |
1087 | } |
1088 | |
1089 | return gmatchInfo; |
1090 | } |
1091 | |
1092 | TimeZoneNames::MatchInfoCollection* |
1093 | TZGNCore::findTimeZoneNames(const UnicodeString& text, int32_t start, uint32_t types, UErrorCode& status) const { |
1094 | |
1095 | uint32_t nameTypes = 0; |
1096 | if (types & UTZGNM_LONG) { |
1097 | nameTypes |= (UTZNM_LONG_GENERIC | UTZNM_LONG_STANDARD); |
1098 | } |
1099 | if (types & UTZGNM_SHORT) { |
1100 | nameTypes |= (UTZNM_SHORT_GENERIC | UTZNM_SHORT_STANDARD); |
1101 | } |
1102 | |
1103 | if (types) { |
1104 | |
1105 | return fTimeZoneNames->find(text, start, nameTypes, status); |
1106 | } |
1107 | |
1108 | return NULL; |
1109 | } |
1110 | |
1111 | typedef struct TZGNCoreRef { |
1112 | TZGNCore* obj; |
1113 | int32_t refCount; |
1114 | double lastAccess; |
1115 | } TZGNCoreRef; |
1116 | |
1117 | |
1118 | static UMutex gTZGNLock; |
1119 | static UHashtable *gTZGNCoreCache = NULL; |
1120 | static UBool gTZGNCoreCacheInitialized = FALSE; |
1121 | |
1122 | |
1123 | |
1124 | static int32_t gAccessCount = 0; |
1125 | |
1126 | |
1127 | #define SWEEP_INTERVAL 100 |
1128 | |
1129 | |
1130 | |
1131 | |
1132 | |
1133 | #define CACHE_EXPIRATION 180000.0 |
1134 | |
1135 | U_CDECL_BEGIN |
1136 | |
1137 | |
1138 | |
1139 | static UBool U_CALLCONV tzgnCore_cleanup(void) |
1140 | { |
1141 | if (gTZGNCoreCache != NULL) { |
1142 | uhash_close(gTZGNCoreCache); |
1143 | gTZGNCoreCache = NULL; |
1144 | } |
1145 | gTZGNCoreCacheInitialized = FALSE; |
1146 | return TRUE; |
1147 | } |
1148 | |
1149 | |
1150 | |
1151 | |
1152 | static void U_CALLCONV |
1153 | deleteTZGNCoreRef(void *obj) { |
1154 | icu::TZGNCoreRef *entry = (icu::TZGNCoreRef*)obj; |
1155 | delete (icu::TZGNCore*) entry->obj; |
1156 | uprv_free(entry); |
1157 | } |
1158 | U_CDECL_END |
1159 | |
1160 | |
1161 | |
1162 | |
1163 | |
1164 | |
1165 | static void sweepCache() { |
1166 | int32_t pos = UHASH_FIRST; |
1167 | const UHashElement* elem; |
1168 | double now = (double)uprv_getUTCtime(); |
1169 | |
1170 | while ((elem = uhash_nextElement(gTZGNCoreCache, &pos)) != NULL) { |
1171 | TZGNCoreRef *entry = (TZGNCoreRef *)elem->value.pointer; |
1172 | if (entry->refCount <= 0 && (now - entry->lastAccess) > CACHE_EXPIRATION) { |
1173 | |
1174 | uhash_removeElement(gTZGNCoreCache, elem); |
1175 | } |
1176 | } |
1177 | } |
1178 | |
1179 | TimeZoneGenericNames::TimeZoneGenericNames() |
1180 | : fRef(0) { |
1181 | } |
1182 | |
1183 | TimeZoneGenericNames::~TimeZoneGenericNames() { |
1184 | umtx_lock(&gTZGNLock); |
1185 | { |
1186 | U_ASSERT(fRef->refCount > 0); |
1187 | |
1188 | fRef->refCount--; |
1189 | } |
1190 | umtx_unlock(&gTZGNLock); |
1191 | } |
1192 | |
1193 | TimeZoneGenericNames* |
1194 | TimeZoneGenericNames::createInstance(const Locale& locale, UErrorCode& status) { |
1195 | if (U_FAILURE(status)) { |
1196 | return NULL; |
1197 | } |
1198 | TimeZoneGenericNames* instance = new TimeZoneGenericNames(); |
1199 | if (instance == NULL) { |
1200 | status = U_MEMORY_ALLOCATION_ERROR; |
1201 | return NULL; |
1202 | } |
1203 | |
1204 | TZGNCoreRef *cacheEntry = NULL; |
1205 | { |
1206 | Mutex lock(&gTZGNLock); |
1207 | |
1208 | if (!gTZGNCoreCacheInitialized) { |
1209 | |
1210 | gTZGNCoreCache = uhash_open(uhash_hashChars, uhash_compareChars, NULL, &status); |
1211 | if (U_SUCCESS(status)) { |
1212 | uhash_setKeyDeleter(gTZGNCoreCache, uprv_free); |
1213 | uhash_setValueDeleter(gTZGNCoreCache, deleteTZGNCoreRef); |
1214 | gTZGNCoreCacheInitialized = TRUE; |
1215 | ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONEGENERICNAMES, tzgnCore_cleanup); |
1216 | } |
1217 | } |
1218 | if (U_FAILURE(status)) { |
1219 | return NULL; |
1220 | } |
1221 | |
1222 | |
1223 | const char *key = locale.getName(); |
1224 | cacheEntry = (TZGNCoreRef *)uhash_get(gTZGNCoreCache, key); |
1225 | if (cacheEntry == NULL) { |
1226 | TZGNCore *tzgnCore = NULL; |
1227 | char *newKey = NULL; |
1228 | |
1229 | tzgnCore = new TZGNCore(locale, status); |
1230 | if (tzgnCore == NULL) { |
1231 | status = U_MEMORY_ALLOCATION_ERROR; |
1232 | } |
1233 | if (U_SUCCESS(status)) { |
1234 | newKey = (char *)uprv_malloc(uprv_strlen(key) + 1); |
1235 | if (newKey == NULL) { |
1236 | status = U_MEMORY_ALLOCATION_ERROR; |
1237 | } else { |
1238 | uprv_strcpy(newKey, key); |
1239 | } |
1240 | } |
1241 | if (U_SUCCESS(status)) { |
1242 | cacheEntry = (TZGNCoreRef *)uprv_malloc(sizeof(TZGNCoreRef)); |
1243 | if (cacheEntry == NULL) { |
1244 | status = U_MEMORY_ALLOCATION_ERROR; |
1245 | } else { |
1246 | cacheEntry->obj = tzgnCore; |
1247 | cacheEntry->refCount = 1; |
1248 | cacheEntry->lastAccess = (double)uprv_getUTCtime(); |
1249 | |
1250 | uhash_put(gTZGNCoreCache, newKey, cacheEntry, &status); |
1251 | } |
1252 | } |
1253 | if (U_FAILURE(status)) { |
1254 | if (tzgnCore != NULL) { |
1255 | delete tzgnCore; |
1256 | } |
1257 | if (newKey != NULL) { |
1258 | uprv_free(newKey); |
1259 | } |
1260 | if (cacheEntry != NULL) { |
1261 | uprv_free(cacheEntry); |
1262 | } |
1263 | cacheEntry = NULL; |
1264 | } |
1265 | } else { |
1266 | |
1267 | cacheEntry->refCount++; |
1268 | cacheEntry->lastAccess = (double)uprv_getUTCtime(); |
1269 | } |
1270 | gAccessCount++; |
1271 | if (gAccessCount >= SWEEP_INTERVAL) { |
1272 | |
1273 | sweepCache(); |
1274 | gAccessCount = 0; |
1275 | } |
1276 | } |
1277 | |
1278 | if (cacheEntry == NULL) { |
1279 | delete instance; |
1280 | return NULL; |
1281 | } |
1282 | |
1283 | instance->fRef = cacheEntry; |
1284 | return instance; |
1285 | } |
1286 | |
1287 | bool |
1288 | TimeZoneGenericNames::operator==(const TimeZoneGenericNames& other) const { |
1289 | |
1290 | |
1291 | return fRef == other.fRef; |
1292 | } |
1293 | |
1294 | TimeZoneGenericNames* |
1295 | TimeZoneGenericNames::clone() const { |
1296 | TimeZoneGenericNames* other = new TimeZoneGenericNames(); |
1297 | if (other) { |
1298 | umtx_lock(&gTZGNLock); |
1299 | { |
1300 | |
1301 | fRef->refCount++; |
1302 | other->fRef = fRef; |
1303 | } |
1304 | umtx_unlock(&gTZGNLock); |
1305 | } |
1306 | return other; |
1307 | } |
1308 | |
1309 | UnicodeString& |
1310 | TimeZoneGenericNames::getDisplayName(const TimeZone& tz, UTimeZoneGenericNameType type, |
1311 | UDate date, UnicodeString& name) const { |
1312 | return fRef->obj->getDisplayName(tz, type, date, name); |
| 1 | Calling 'TZGNCore::getDisplayName' | |
|
1313 | } |
1314 | |
1315 | UnicodeString& |
1316 | TimeZoneGenericNames::getGenericLocationName(const UnicodeString& tzCanonicalID, UnicodeString& name) const { |
1317 | return fRef->obj->getGenericLocationName(tzCanonicalID, name); |
1318 | } |
1319 | |
1320 | int32_t |
1321 | TimeZoneGenericNames::findBestMatch(const UnicodeString& text, int32_t start, uint32_t types, |
1322 | UnicodeString& tzID, UTimeZoneFormatTimeType& timeType, UErrorCode& status) const { |
1323 | return fRef->obj->findBestMatch(text, start, types, tzID, timeType, status); |
1324 | } |
1325 | |
1326 | U_NAMESPACE_END |
1327 | #endif |