]>
Commit | Line | Data |
---|---|---|
1 | // © 2016 and later: Unicode, Inc. and others. | |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
3 | /******************************************************************************* | |
4 | * Copyright (C) 2008-2016, International Business Machines Corporation and | |
5 | * others. All Rights Reserved. | |
6 | ******************************************************************************* | |
7 | * | |
8 | * File DTITVINF.CPP | |
9 | * | |
10 | ******************************************************************************* | |
11 | */ | |
12 | ||
13 | #include "unicode/dtitvinf.h" | |
14 | ||
15 | ||
16 | #if !UCONFIG_NO_FORMATTING | |
17 | ||
18 | //TODO: define it in compiler time | |
19 | //#define DTITVINF_DEBUG 1 | |
20 | ||
21 | ||
22 | #ifdef DTITVINF_DEBUG | |
23 | #include <iostream> | |
24 | #endif | |
25 | ||
26 | #include "cmemory.h" | |
27 | #include "cstring.h" | |
28 | #include "unicode/msgfmt.h" | |
29 | #include "unicode/uloc.h" | |
30 | #include "unicode/ures.h" | |
31 | #include "dtitv_impl.h" | |
32 | #include "charstr.h" | |
33 | #include "hash.h" | |
34 | #include "gregoimp.h" | |
35 | #include "uresimp.h" | |
36 | #include "hash.h" | |
37 | #include "gregoimp.h" | |
38 | #include "uresimp.h" | |
39 | ||
40 | ||
41 | U_NAMESPACE_BEGIN | |
42 | ||
43 | ||
44 | #ifdef DTITVINF_DEBUG | |
45 | #define PRINTMESG(msg) UPRV_BLOCK_MACRO_BEGIN { \ | |
46 | std::cout << "(" << __FILE__ << ":" << __LINE__ << ") " << msg << "\n"; \ | |
47 | } UPRV_BLOCK_MACRO_END | |
48 | #endif | |
49 | ||
50 | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DateIntervalInfo) | |
51 | ||
52 | static const char gCalendarTag[]="calendar"; | |
53 | static const char gGenericTag[]="generic"; | |
54 | static const char gGregorianTag[]="gregorian"; | |
55 | static const char gIntervalDateTimePatternTag[]="intervalFormats"; | |
56 | static const char gFallbackPatternTag[]="fallback"; | |
57 | ||
58 | // {0} | |
59 | static const UChar gFirstPattern[] = {LEFT_CURLY_BRACKET, DIGIT_ZERO, RIGHT_CURLY_BRACKET}; | |
60 | // {1} | |
61 | static const UChar gSecondPattern[] = {LEFT_CURLY_BRACKET, DIGIT_ONE, RIGHT_CURLY_BRACKET}; | |
62 | ||
63 | // default fall-back | |
64 | static const UChar gDefaultFallbackPattern[] = {LEFT_CURLY_BRACKET, DIGIT_ZERO, RIGHT_CURLY_BRACKET, SPACE, EN_DASH, SPACE, LEFT_CURLY_BRACKET, DIGIT_ONE, RIGHT_CURLY_BRACKET, 0}; | |
65 | ||
66 | DateIntervalInfo::DateIntervalInfo(UErrorCode& status) | |
67 | : fFallbackIntervalPattern(gDefaultFallbackPattern), | |
68 | fFirstDateInPtnIsLaterDate(false), | |
69 | fIntervalPatterns(NULL) | |
70 | { | |
71 | fIntervalPatterns = initHash(status); | |
72 | } | |
73 | ||
74 | ||
75 | ||
76 | DateIntervalInfo::DateIntervalInfo(const Locale& locale, UErrorCode& status) | |
77 | : fFallbackIntervalPattern(gDefaultFallbackPattern), | |
78 | fFirstDateInPtnIsLaterDate(false), | |
79 | fIntervalPatterns(NULL) | |
80 | { | |
81 | initializeData(locale, status); | |
82 | } | |
83 | ||
84 | ||
85 | ||
86 | void | |
87 | DateIntervalInfo::setIntervalPattern(const UnicodeString& skeleton, | |
88 | UCalendarDateFields lrgDiffCalUnit, | |
89 | const UnicodeString& intervalPattern, | |
90 | UErrorCode& status) { | |
91 | ||
92 | if ( lrgDiffCalUnit == UCAL_HOUR_OF_DAY ) { | |
93 | setIntervalPatternInternally(skeleton, UCAL_AM_PM, intervalPattern, status); | |
94 | setIntervalPatternInternally(skeleton, UCAL_HOUR, intervalPattern, status); | |
95 | } else if ( lrgDiffCalUnit == UCAL_DAY_OF_MONTH || | |
96 | lrgDiffCalUnit == UCAL_DAY_OF_WEEK ) { | |
97 | setIntervalPatternInternally(skeleton, UCAL_DATE, intervalPattern, status); | |
98 | } else { | |
99 | setIntervalPatternInternally(skeleton, lrgDiffCalUnit, intervalPattern, status); | |
100 | } | |
101 | } | |
102 | ||
103 | ||
104 | void | |
105 | DateIntervalInfo::setFallbackIntervalPattern( | |
106 | const UnicodeString& fallbackPattern, | |
107 | UErrorCode& status) { | |
108 | if ( U_FAILURE(status) ) { | |
109 | return; | |
110 | } | |
111 | int32_t firstPatternIndex = fallbackPattern.indexOf(gFirstPattern, | |
112 | UPRV_LENGTHOF(gFirstPattern), 0); | |
113 | int32_t secondPatternIndex = fallbackPattern.indexOf(gSecondPattern, | |
114 | UPRV_LENGTHOF(gSecondPattern), 0); | |
115 | if ( firstPatternIndex == -1 || secondPatternIndex == -1 ) { | |
116 | status = U_ILLEGAL_ARGUMENT_ERROR; | |
117 | return; | |
118 | } | |
119 | if ( firstPatternIndex > secondPatternIndex ) { | |
120 | fFirstDateInPtnIsLaterDate = true; | |
121 | } | |
122 | fFallbackIntervalPattern = fallbackPattern; | |
123 | } | |
124 | ||
125 | ||
126 | ||
127 | DateIntervalInfo::DateIntervalInfo(const DateIntervalInfo& dtitvinf) | |
128 | : UObject(dtitvinf), | |
129 | fIntervalPatterns(NULL) | |
130 | { | |
131 | *this = dtitvinf; | |
132 | } | |
133 | ||
134 | ||
135 | ||
136 | DateIntervalInfo& | |
137 | DateIntervalInfo::operator=(const DateIntervalInfo& dtitvinf) { | |
138 | if ( this == &dtitvinf ) { | |
139 | return *this; | |
140 | } | |
141 | ||
142 | UErrorCode status = U_ZERO_ERROR; | |
143 | deleteHash(fIntervalPatterns); | |
144 | fIntervalPatterns = initHash(status); | |
145 | copyHash(dtitvinf.fIntervalPatterns, fIntervalPatterns, status); | |
146 | if ( U_FAILURE(status) ) { | |
147 | return *this; | |
148 | } | |
149 | ||
150 | fFallbackIntervalPattern = dtitvinf.fFallbackIntervalPattern; | |
151 | fFirstDateInPtnIsLaterDate = dtitvinf.fFirstDateInPtnIsLaterDate; | |
152 | return *this; | |
153 | } | |
154 | ||
155 | ||
156 | DateIntervalInfo* | |
157 | DateIntervalInfo::clone() const { | |
158 | return new DateIntervalInfo(*this); | |
159 | } | |
160 | ||
161 | ||
162 | DateIntervalInfo::~DateIntervalInfo() { | |
163 | deleteHash(fIntervalPatterns); | |
164 | fIntervalPatterns = NULL; | |
165 | } | |
166 | ||
167 | ||
168 | UBool | |
169 | DateIntervalInfo::operator==(const DateIntervalInfo& other) const { | |
170 | UBool equal = ( | |
171 | fFallbackIntervalPattern == other.fFallbackIntervalPattern && | |
172 | fFirstDateInPtnIsLaterDate == other.fFirstDateInPtnIsLaterDate ); | |
173 | ||
174 | if ( equal == TRUE ) { | |
175 | equal = fIntervalPatterns->equals(*(other.fIntervalPatterns)); | |
176 | } | |
177 | ||
178 | return equal; | |
179 | } | |
180 | ||
181 | ||
182 | UnicodeString& | |
183 | DateIntervalInfo::getIntervalPattern(const UnicodeString& skeleton, | |
184 | UCalendarDateFields field, | |
185 | UnicodeString& result, | |
186 | UErrorCode& status) const { | |
187 | if ( U_FAILURE(status) ) { | |
188 | return result; | |
189 | } | |
190 | ||
191 | const UnicodeString* patternsOfOneSkeleton = (UnicodeString*) fIntervalPatterns->get(skeleton); | |
192 | if ( patternsOfOneSkeleton != NULL ) { | |
193 | IntervalPatternIndex index = calendarFieldToIntervalIndex(field, status); | |
194 | if ( U_FAILURE(status) ) { | |
195 | return result; | |
196 | } | |
197 | const UnicodeString& intervalPattern = patternsOfOneSkeleton[index]; | |
198 | if ( !intervalPattern.isEmpty() ) { | |
199 | result = intervalPattern; | |
200 | } | |
201 | } | |
202 | return result; | |
203 | } | |
204 | ||
205 | ||
206 | UBool | |
207 | DateIntervalInfo::getDefaultOrder() const { | |
208 | return fFirstDateInPtnIsLaterDate; | |
209 | } | |
210 | ||
211 | ||
212 | UnicodeString& | |
213 | DateIntervalInfo::getFallbackIntervalPattern(UnicodeString& result) const { | |
214 | result = fFallbackIntervalPattern; | |
215 | return result; | |
216 | } | |
217 | ||
218 | #define ULOC_LOCALE_IDENTIFIER_CAPACITY (ULOC_FULLNAME_CAPACITY + 1 + ULOC_KEYWORD_AND_VALUES_CAPACITY) | |
219 | ||
220 | ||
221 | static const int32_t PATH_PREFIX_LENGTH = 17; | |
222 | static const UChar PATH_PREFIX[] = {SOLIDUS, CAP_L, CAP_O, CAP_C, CAP_A, CAP_L, CAP_E, SOLIDUS, | |
223 | LOW_C, LOW_A, LOW_L, LOW_E, LOW_N, LOW_D, LOW_A, LOW_R, SOLIDUS}; | |
224 | static const int32_t PATH_SUFFIX_LENGTH = 16; | |
225 | static const UChar PATH_SUFFIX[] = {SOLIDUS, LOW_I, LOW_N, LOW_T, LOW_E, LOW_R, LOW_V, LOW_A, | |
226 | LOW_L, CAP_F, LOW_O, LOW_R, LOW_M, LOW_A, LOW_T, LOW_S}; | |
227 | ||
228 | /** | |
229 | * Sink for enumerating all of the date interval skeletons. | |
230 | */ | |
231 | struct DateIntervalInfo::DateIntervalSink : public ResourceSink { | |
232 | ||
233 | // Output data | |
234 | DateIntervalInfo &dateIntervalInfo; | |
235 | ||
236 | // Next calendar type | |
237 | UnicodeString nextCalendarType; | |
238 | ||
239 | UBool onlyProcessShortDateSkeletons; | |
240 | UBool stripRightToLeftMarks; | |
241 | ||
242 | DateIntervalSink(DateIntervalInfo &diInfo, const char *currentCalendarType, UBool onlyProcessShortDateSkeletons, UBool stripRightToLeftMarks) | |
243 | : dateIntervalInfo(diInfo), nextCalendarType(currentCalendarType, -1, US_INV), onlyProcessShortDateSkeletons(onlyProcessShortDateSkeletons), stripRightToLeftMarks(stripRightToLeftMarks) { } | |
244 | virtual ~DateIntervalSink(); | |
245 | ||
246 | virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/, UErrorCode &errorCode) { | |
247 | if (U_FAILURE(errorCode)) { return; } | |
248 | ||
249 | // Iterate over all the calendar entries and only pick the 'intervalFormats' table. | |
250 | ResourceTable dateIntervalData = value.getTable(errorCode); | |
251 | if (U_FAILURE(errorCode)) { return; } | |
252 | for (int32_t i = 0; dateIntervalData.getKeyAndValue(i, key, value); i++) { | |
253 | if (uprv_strcmp(key, gIntervalDateTimePatternTag) != 0) { | |
254 | continue; | |
255 | } | |
256 | ||
257 | // Handle aliases and tables. Ignore the rest. | |
258 | if (value.getType() == URES_ALIAS) { | |
259 | // Get the calendar type for the alias path. | |
260 | const UnicodeString &aliasPath = value.getAliasUnicodeString(errorCode); | |
261 | if (U_FAILURE(errorCode)) { return; } | |
262 | ||
263 | nextCalendarType.remove(); | |
264 | getCalendarTypeFromPath(aliasPath, nextCalendarType, errorCode); | |
265 | ||
266 | if (U_FAILURE(errorCode)) { | |
267 | resetNextCalendarType(); | |
268 | } | |
269 | break; | |
270 | ||
271 | } else if (value.getType() == URES_TABLE) { | |
272 | // Iterate over all the skeletons in the 'intervalFormat' table. | |
273 | ResourceTable skeletonData = value.getTable(errorCode); | |
274 | if (U_FAILURE(errorCode)) { return; } | |
275 | for (int32_t j = 0; skeletonData.getKeyAndValue(j, key, value); j++) { | |
276 | if (shouldProcessSkeleton(key) && value.getType() == URES_TABLE) { | |
277 | // Process the skeleton | |
278 | processSkeletonTable(key, value, errorCode); | |
279 | if (U_FAILURE(errorCode)) { return; } | |
280 | } | |
281 | } | |
282 | break; | |
283 | } | |
284 | } | |
285 | } | |
286 | ||
287 | /** | |
288 | * Processes the patterns for a skeleton table | |
289 | */ | |
290 | void processSkeletonTable(const char *key, ResourceValue &value, UErrorCode &errorCode) { | |
291 | if (U_FAILURE(errorCode)) { return; } | |
292 | ||
293 | // Iterate over all the patterns in the current skeleton table | |
294 | const char *currentSkeleton = key; | |
295 | ResourceTable patternData = value.getTable(errorCode); | |
296 | if (U_FAILURE(errorCode)) { return; } | |
297 | for (int32_t k = 0; patternData.getKeyAndValue(k, key, value); k++) { | |
298 | if (value.getType() == URES_STRING) { | |
299 | // Process the key | |
300 | UCalendarDateFields calendarField = validateAndProcessPatternLetter(key); | |
301 | ||
302 | // If the calendar field has a valid value | |
303 | if (calendarField < UCAL_FIELD_COUNT) { | |
304 | // Set the interval pattern | |
305 | setIntervalPatternIfAbsent(currentSkeleton, calendarField, value, errorCode); | |
306 | if (U_FAILURE(errorCode)) { return; } | |
307 | } | |
308 | } | |
309 | } | |
310 | } | |
311 | ||
312 | UBool shouldProcessSkeleton(const char *key) { | |
313 | if (!onlyProcessShortDateSkeletons) { | |
314 | return TRUE; | |
315 | } | |
316 | ||
317 | // if we're only processing short date skeletons, look at the skeleton string: if it contains at least | |
318 | // two of y, M, and d, and it has fewer than 3 Ms, it's a short date skeleton | |
319 | int32_t yCount = 0, mCount = 0, dCount = 0; | |
320 | const char *p = key; | |
321 | while (*p != '\0') { | |
322 | switch (*p++) { | |
323 | case 'y': ++yCount; break; | |
324 | case 'M': ++mCount; break; | |
325 | case 'd': ++dCount; break; | |
326 | } | |
327 | } | |
328 | if (mCount < 3 && ((yCount > 0 && mCount > 0) || (yCount > 0 && dCount > 0) || (mCount > 0 && dCount > 0))) { | |
329 | return TRUE; | |
330 | } else { | |
331 | return FALSE; | |
332 | } | |
333 | } | |
334 | ||
335 | /** | |
336 | * Extracts the calendar type from the path. | |
337 | */ | |
338 | static void getCalendarTypeFromPath(const UnicodeString &path, UnicodeString &calendarType, | |
339 | UErrorCode &errorCode) { | |
340 | if (U_FAILURE(errorCode)) { return; } | |
341 | ||
342 | if (!path.startsWith(PATH_PREFIX, PATH_PREFIX_LENGTH) || !path.endsWith(PATH_SUFFIX, PATH_SUFFIX_LENGTH)) { | |
343 | errorCode = U_INVALID_FORMAT_ERROR; | |
344 | return; | |
345 | } | |
346 | ||
347 | path.extractBetween(PATH_PREFIX_LENGTH, path.length() - PATH_SUFFIX_LENGTH, calendarType); | |
348 | } | |
349 | ||
350 | /** | |
351 | * Validates and processes the pattern letter | |
352 | */ | |
353 | UCalendarDateFields validateAndProcessPatternLetter(const char *patternLetter) { | |
354 | // Check that patternLetter is just one letter | |
355 | char c0; | |
356 | if ((c0 = patternLetter[0]) != 0 && patternLetter[1] == 0) { | |
357 | // Check that the pattern letter is accepted | |
358 | if (c0 == 'G') { | |
359 | return UCAL_ERA; | |
360 | } else if (c0 == 'y') { | |
361 | return UCAL_YEAR; | |
362 | } else if (c0 == 'M') { | |
363 | return UCAL_MONTH; | |
364 | } else if (c0 == 'd') { | |
365 | return UCAL_DATE; | |
366 | } else if (c0 == 'a') { | |
367 | return UCAL_AM_PM; | |
368 | } else if (c0 == 'h' || c0 == 'H') { | |
369 | return UCAL_HOUR; | |
370 | } else if (c0 == 'm') { | |
371 | return UCAL_MINUTE; | |
372 | }// TODO(ticket:12190): Why icu4c doesn't accept the calendar field "s" but icu4j does? | |
373 | } | |
374 | return UCAL_FIELD_COUNT; | |
375 | } | |
376 | ||
377 | /** | |
378 | * Stores the interval pattern for the current skeleton in the internal data structure | |
379 | * if it's not present. | |
380 | */ | |
381 | void setIntervalPatternIfAbsent(const char *currentSkeleton, UCalendarDateFields lrgDiffCalUnit, | |
382 | const ResourceValue &value, UErrorCode &errorCode) { | |
383 | // Check if the pattern has already been stored on the data structure | |
384 | IntervalPatternIndex index = | |
385 | dateIntervalInfo.calendarFieldToIntervalIndex(lrgDiffCalUnit, errorCode); | |
386 | if (U_FAILURE(errorCode)) { return; } | |
387 | ||
388 | UnicodeString skeleton(currentSkeleton, -1, US_INV); | |
389 | UnicodeString* patternsOfOneSkeleton = | |
390 | (UnicodeString*)(dateIntervalInfo.fIntervalPatterns->get(skeleton)); | |
391 | ||
392 | if (patternsOfOneSkeleton == NULL || patternsOfOneSkeleton[index].isEmpty()) { | |
393 | UnicodeString pattern = value.getUnicodeString(errorCode); | |
394 | if (stripRightToLeftMarks) { | |
395 | // if the pattern string came from a right-to-left locale, it might contain Unicode right-to-left | |
396 | // marks to ensure proper display in a RTL context; we want to strip these if the interval formatter's | |
397 | // actual locale is a LTR locale | |
398 | pattern.findAndReplace(UnicodeString(u'\u200f'), UnicodeString()); | |
399 | } | |
400 | dateIntervalInfo.setIntervalPatternInternally(skeleton, lrgDiffCalUnit, | |
401 | pattern, errorCode); | |
402 | } | |
403 | } | |
404 | ||
405 | const UnicodeString &getNextCalendarType() { | |
406 | return nextCalendarType; | |
407 | } | |
408 | ||
409 | void resetNextCalendarType() { | |
410 | nextCalendarType.setToBogus(); | |
411 | } | |
412 | }; | |
413 | ||
414 | // Virtual destructors must be defined out of line. | |
415 | DateIntervalInfo::DateIntervalSink::~DateIntervalSink() {} | |
416 | ||
417 | ||
418 | ||
419 | void | |
420 | DateIntervalInfo::initializeData(const Locale& locale, UErrorCode& status) | |
421 | { | |
422 | fIntervalPatterns = initHash(status); | |
423 | if (U_FAILURE(status)) { | |
424 | return; | |
425 | } | |
426 | const char *locName = locale.getName(); | |
427 | ||
428 | // Get the correct calendar type | |
429 | const char * calendarTypeToUse = gGregorianTag; // initial default | |
430 | char calendarType[ULOC_KEYWORDS_CAPACITY]; // to be filled in with the type to use, if all goes well | |
431 | char localeWithCalendarKey[ULOC_LOCALE_IDENTIFIER_CAPACITY]; | |
432 | // obtain a locale that always has the calendar key value that should be used | |
433 | (void)ures_getFunctionalEquivalent(localeWithCalendarKey, ULOC_LOCALE_IDENTIFIER_CAPACITY, NULL, | |
434 | "calendar", "calendar", locName, NULL, FALSE, &status); | |
435 | localeWithCalendarKey[ULOC_LOCALE_IDENTIFIER_CAPACITY-1] = 0; // ensure null termination | |
436 | // now get the calendar key value from that locale | |
437 | int32_t calendarTypeLen = uloc_getKeywordValue(localeWithCalendarKey, "calendar", calendarType, | |
438 | ULOC_KEYWORDS_CAPACITY, &status); | |
439 | if (U_SUCCESS(status) && calendarTypeLen < ULOC_KEYWORDS_CAPACITY) { | |
440 | calendarTypeToUse = calendarType; | |
441 | } | |
442 | status = U_ZERO_ERROR; | |
443 | ||
444 | // Instantiate the resource bundles | |
445 | UBool hasCountryResource = FALSE; | |
446 | UResourceBundle *rb, *countryRB, *calBundle, *countryCalBundle; | |
447 | rb = ures_open(NULL, locName, &status); | |
448 | countryRB = ures_openWithCountryFallback(NULL, locName, &hasCountryResource, &status); | |
449 | if (U_FAILURE(status)) { | |
450 | return; | |
451 | } | |
452 | calBundle = ures_getByKeyWithFallback(rb, gCalendarTag, NULL, &status); | |
453 | countryCalBundle = ures_getByKeyWithFallback(countryRB, gCalendarTag, NULL, &status); | |
454 | ||
455 | ||
456 | if (U_SUCCESS(status)) { | |
457 | UResourceBundle *calTypeBundle, *itvDtPtnResource; | |
458 | ||
459 | // Get the fallback pattern | |
460 | const UChar* resStr = nullptr; | |
461 | int32_t resStrLen = 0; | |
462 | calTypeBundle = ures_getByKeyWithFallback(calBundle, calendarTypeToUse, NULL, &status); | |
463 | itvDtPtnResource = ures_getByKeyWithFallback(calTypeBundle, | |
464 | gIntervalDateTimePatternTag, NULL, &status); | |
465 | // TODO(ICU-20400): After the fixing, we should find the "fallback" from | |
466 | // the rb directly by the path "calendar/${calendar}/intervalFormats/fallback". | |
467 | if ( U_SUCCESS(status) ) { | |
468 | resStr = ures_getStringByKeyWithFallback(itvDtPtnResource, gFallbackPatternTag, | |
469 | &resStrLen, &status); | |
470 | if ( U_FAILURE(status) ) { | |
471 | // Try to find "fallback" from "generic" to work around the bug in | |
472 | // ures_getByKeyWithFallback | |
473 | UErrorCode localStatus = U_ZERO_ERROR; | |
474 | UResourceBundle *genericCalBundle = | |
475 | ures_getByKeyWithFallback(calBundle, gGenericTag, NULL, &localStatus); | |
476 | UResourceBundle *genericItvDtPtnResource = | |
477 | ures_getByKeyWithFallback( | |
478 | genericCalBundle, gIntervalDateTimePatternTag, NULL, &localStatus); | |
479 | resStr = ures_getStringByKeyWithFallback( | |
480 | genericItvDtPtnResource, gFallbackPatternTag, &resStrLen, &localStatus); | |
481 | ures_close(genericItvDtPtnResource); | |
482 | ures_close(genericCalBundle); | |
483 | if ( U_SUCCESS(localStatus) ) { | |
484 | status = U_USING_FALLBACK_WARNING;; | |
485 | } | |
486 | } | |
487 | } | |
488 | ||
489 | if ( U_SUCCESS(status) && (resStr != nullptr)) { | |
490 | UnicodeString pattern = UnicodeString(TRUE, resStr, resStrLen); | |
491 | setFallbackIntervalPattern(pattern, status); | |
492 | } | |
493 | ures_close(itvDtPtnResource); | |
494 | ures_close(calTypeBundle); | |
495 | ||
496 | ||
497 | UResourceBundle *curCalBundle = hasCountryResource ? countryCalBundle : calBundle; | |
498 | ||
499 | while (curCalBundle != NULL) { | |
500 | // Instantiate the sink | |
501 | DateIntervalSink sink(*this, calendarTypeToUse, curCalBundle != calBundle, !locale.isRightToLeft()); | |
502 | const UnicodeString &calendarTypeToUseUString = sink.getNextCalendarType(); | |
503 | ||
504 | // Already loaded calendar types | |
505 | Hashtable loadedCalendarTypes(FALSE, status); | |
506 | ||
507 | if (U_SUCCESS(status)) { | |
508 | while (!calendarTypeToUseUString.isBogus()) { | |
509 | // Set an error when a loop is detected | |
510 | if (loadedCalendarTypes.geti(calendarTypeToUseUString) == 1) { | |
511 | status = U_INVALID_FORMAT_ERROR; | |
512 | break; | |
513 | } | |
514 | ||
515 | // Register the calendar type to avoid loops | |
516 | loadedCalendarTypes.puti(calendarTypeToUseUString, 1, status); | |
517 | if (U_FAILURE(status)) { break; } | |
518 | ||
519 | // Get the calendar string | |
520 | CharString calTypeBuffer; | |
521 | calTypeBuffer.appendInvariantChars(calendarTypeToUseUString, status); | |
522 | if (U_FAILURE(status)) { break; } | |
523 | const char *calType = calTypeBuffer.data(); | |
524 | ||
525 | // Reset the next calendar type to load. | |
526 | sink.resetNextCalendarType(); | |
527 | ||
528 | // Get all resources for this calendar type | |
529 | ures_getAllItemsWithFallback(curCalBundle, calType, sink, status); | |
530 | } | |
531 | } | |
532 | ||
533 | curCalBundle = (curCalBundle == calBundle) ? NULL : calBundle; | |
534 | } | |
535 | } | |
536 | ||
537 | // Close the opened resource bundles | |
538 | ures_close(calBundle); | |
539 | ures_close(rb); | |
540 | ures_close(countryCalBundle); | |
541 | ures_close(countryRB); | |
542 | } | |
543 | ||
544 | void | |
545 | DateIntervalInfo::setIntervalPatternInternally(const UnicodeString& skeleton, | |
546 | UCalendarDateFields lrgDiffCalUnit, | |
547 | const UnicodeString& intervalPattern, | |
548 | UErrorCode& status) { | |
549 | IntervalPatternIndex index = calendarFieldToIntervalIndex(lrgDiffCalUnit,status); | |
550 | if ( U_FAILURE(status) ) { | |
551 | return; | |
552 | } | |
553 | UnicodeString* patternsOfOneSkeleton = (UnicodeString*)(fIntervalPatterns->get(skeleton)); | |
554 | UBool emptyHash = false; | |
555 | if ( patternsOfOneSkeleton == NULL ) { | |
556 | patternsOfOneSkeleton = new UnicodeString[kIPI_MAX_INDEX]; | |
557 | emptyHash = true; | |
558 | } | |
559 | ||
560 | patternsOfOneSkeleton[index] = intervalPattern; | |
561 | if ( emptyHash == TRUE ) { | |
562 | fIntervalPatterns->put(skeleton, patternsOfOneSkeleton, status); | |
563 | } | |
564 | } | |
565 | ||
566 | ||
567 | ||
568 | void | |
569 | DateIntervalInfo::parseSkeleton(const UnicodeString& skeleton, | |
570 | int32_t* skeletonFieldWidth) { | |
571 | const int8_t PATTERN_CHAR_BASE = 0x41; | |
572 | int32_t i; | |
573 | for ( i = 0; i < skeleton.length(); ++i ) { | |
574 | // it is an ASCII char in skeleton | |
575 | int8_t ch = (int8_t)skeleton.charAt(i); | |
576 | ++skeletonFieldWidth[ch - PATTERN_CHAR_BASE]; | |
577 | } | |
578 | } | |
579 | ||
580 | ||
581 | ||
582 | UBool | |
583 | DateIntervalInfo::stringNumeric(int32_t fieldWidth, int32_t anotherFieldWidth, | |
584 | char patternLetter) { | |
585 | if ( patternLetter == 'M' ) { | |
586 | if ( (fieldWidth <= 2 && anotherFieldWidth > 2) || | |
587 | (fieldWidth > 2 && anotherFieldWidth <= 2 )) { | |
588 | return true; | |
589 | } | |
590 | } | |
591 | return false; | |
592 | } | |
593 | ||
594 | ||
595 | ||
596 | const UnicodeString* | |
597 | DateIntervalInfo::getBestSkeleton(const UnicodeString& skeleton, | |
598 | int8_t& bestMatchDistanceInfo) const { | |
599 | #ifdef DTITVINF_DEBUG | |
600 | char result[1000]; | |
601 | char result_1[1000]; | |
602 | char mesg[2000]; | |
603 | skeleton.extract(0, skeleton.length(), result, "UTF-8"); | |
604 | sprintf(mesg, "in getBestSkeleton: skeleton: %s; \n", result); | |
605 | PRINTMESG(mesg) | |
606 | #endif | |
607 | ||
608 | ||
609 | int32_t inputSkeletonFieldWidth[] = | |
610 | { | |
611 | // A B C D E F G H I J K L M N O | |
612 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
613 | // P Q R S T U V W X Y Z | |
614 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
615 | // a b c d e f g h i j k l m n o | |
616 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
617 | // p q r s t u v w x y z | |
618 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
619 | }; | |
620 | ||
621 | int32_t skeletonFieldWidth[] = | |
622 | { | |
623 | // A B C D E F G H I J K L M N O | |
624 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
625 | // P Q R S T U V W X Y Z | |
626 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
627 | // a b c d e f g h i j k l m n o | |
628 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
629 | // p q r s t u v w x y z | |
630 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
631 | }; | |
632 | ||
633 | const int32_t DIFFERENT_FIELD = 0x1000; | |
634 | const int32_t STRING_NUMERIC_DIFFERENCE = 0x100; | |
635 | const int32_t BASE = 0x41; | |
636 | const UChar CHAR_V = 0x0076; | |
637 | const UChar CHAR_Z = 0x007A; | |
638 | ||
639 | // hack for 'v' and 'z'. | |
640 | // resource bundle only have time skeletons ending with 'v', | |
641 | // but not for time skeletons ending with 'z'. | |
642 | UBool replaceZWithV = false; | |
643 | const UnicodeString* inputSkeleton = &skeleton; | |
644 | UnicodeString copySkeleton; | |
645 | if ( skeleton.indexOf(CHAR_Z) != -1 ) { | |
646 | copySkeleton = skeleton; | |
647 | copySkeleton.findAndReplace(UnicodeString(CHAR_Z), UnicodeString(CHAR_V)); | |
648 | inputSkeleton = ©Skeleton; | |
649 | replaceZWithV = true; | |
650 | } | |
651 | ||
652 | parseSkeleton(*inputSkeleton, inputSkeletonFieldWidth); | |
653 | int32_t bestDistance = MAX_POSITIVE_INT; | |
654 | const UnicodeString* bestSkeleton = NULL; | |
655 | ||
656 | // 0 means exact the same skeletons; | |
657 | // 1 means having the same field, but with different length, | |
658 | // 2 means only z/v differs | |
659 | // -1 means having different field. | |
660 | bestMatchDistanceInfo = 0; | |
661 | int8_t fieldLength = UPRV_LENGTHOF(skeletonFieldWidth); | |
662 | ||
663 | int32_t pos = UHASH_FIRST; | |
664 | const UHashElement* elem = NULL; | |
665 | while ( (elem = fIntervalPatterns->nextElement(pos)) != NULL ) { | |
666 | const UHashTok keyTok = elem->key; | |
667 | UnicodeString* newSkeleton = (UnicodeString*)keyTok.pointer; | |
668 | #ifdef DTITVINF_DEBUG | |
669 | skeleton->extract(0, skeleton->length(), result, "UTF-8"); | |
670 | sprintf(mesg, "available skeletons: skeleton: %s; \n", result); | |
671 | PRINTMESG(mesg) | |
672 | #endif | |
673 | ||
674 | // clear skeleton field width | |
675 | int8_t i; | |
676 | for ( i = 0; i < fieldLength; ++i ) { | |
677 | skeletonFieldWidth[i] = 0; | |
678 | } | |
679 | parseSkeleton(*newSkeleton, skeletonFieldWidth); | |
680 | // calculate distance | |
681 | int32_t distance = 0; | |
682 | int8_t fieldDifference = 1; | |
683 | for ( i = 0; i < fieldLength; ++i ) { | |
684 | int32_t inputFieldWidth = inputSkeletonFieldWidth[i]; | |
685 | int32_t fieldWidth = skeletonFieldWidth[i]; | |
686 | if ( inputFieldWidth == fieldWidth ) { | |
687 | continue; | |
688 | } | |
689 | if ( inputFieldWidth == 0 ) { | |
690 | fieldDifference = -1; | |
691 | distance += DIFFERENT_FIELD; | |
692 | } else if ( fieldWidth == 0 ) { | |
693 | fieldDifference = -1; | |
694 | distance += DIFFERENT_FIELD; | |
695 | } else if (stringNumeric(inputFieldWidth, fieldWidth, | |
696 | (char)(i+BASE) ) ) { | |
697 | distance += STRING_NUMERIC_DIFFERENCE; | |
698 | } else { | |
699 | distance += (inputFieldWidth > fieldWidth) ? | |
700 | (inputFieldWidth - fieldWidth) : | |
701 | (fieldWidth - inputFieldWidth); | |
702 | } | |
703 | } | |
704 | if ( distance < bestDistance ) { | |
705 | bestSkeleton = newSkeleton; | |
706 | bestDistance = distance; | |
707 | bestMatchDistanceInfo = fieldDifference; | |
708 | } | |
709 | if ( distance == 0 ) { | |
710 | bestMatchDistanceInfo = 0; | |
711 | break; | |
712 | } | |
713 | } | |
714 | if ( replaceZWithV && bestMatchDistanceInfo != -1 ) { | |
715 | bestMatchDistanceInfo = 2; | |
716 | } | |
717 | return bestSkeleton; | |
718 | } | |
719 | ||
720 | ||
721 | ||
722 | DateIntervalInfo::IntervalPatternIndex | |
723 | DateIntervalInfo::calendarFieldToIntervalIndex(UCalendarDateFields field, | |
724 | UErrorCode& status) { | |
725 | if ( U_FAILURE(status) ) { | |
726 | return kIPI_MAX_INDEX; | |
727 | } | |
728 | IntervalPatternIndex index = kIPI_MAX_INDEX; | |
729 | switch ( field ) { | |
730 | case UCAL_ERA: | |
731 | index = kIPI_ERA; | |
732 | break; | |
733 | case UCAL_YEAR: | |
734 | index = kIPI_YEAR; | |
735 | break; | |
736 | case UCAL_MONTH: | |
737 | index = kIPI_MONTH; | |
738 | break; | |
739 | case UCAL_DATE: | |
740 | case UCAL_DAY_OF_WEEK: | |
741 | //case UCAL_DAY_OF_MONTH: | |
742 | index = kIPI_DATE; | |
743 | break; | |
744 | case UCAL_AM_PM: | |
745 | index = kIPI_AM_PM; | |
746 | break; | |
747 | case UCAL_HOUR: | |
748 | case UCAL_HOUR_OF_DAY: | |
749 | index = kIPI_HOUR; | |
750 | break; | |
751 | case UCAL_MINUTE: | |
752 | index = kIPI_MINUTE; | |
753 | break; | |
754 | case UCAL_SECOND: | |
755 | index = kIPI_SECOND; | |
756 | break; | |
757 | default: | |
758 | status = U_ILLEGAL_ARGUMENT_ERROR; | |
759 | } | |
760 | return index; | |
761 | } | |
762 | ||
763 | ||
764 | ||
765 | void | |
766 | DateIntervalInfo::deleteHash(Hashtable* hTable) | |
767 | { | |
768 | if ( hTable == NULL ) { | |
769 | return; | |
770 | } | |
771 | int32_t pos = UHASH_FIRST; | |
772 | const UHashElement* element = NULL; | |
773 | while ( (element = hTable->nextElement(pos)) != NULL ) { | |
774 | const UHashTok valueTok = element->value; | |
775 | const UnicodeString* value = (UnicodeString*)valueTok.pointer; | |
776 | delete[] value; | |
777 | } | |
778 | delete fIntervalPatterns; | |
779 | } | |
780 | ||
781 | ||
782 | U_CDECL_BEGIN | |
783 | ||
784 | /** | |
785 | * set hash table value comparator | |
786 | * | |
787 | * @param val1 one value in comparison | |
788 | * @param val2 the other value in comparison | |
789 | * @return TRUE if 2 values are the same, FALSE otherwise | |
790 | */ | |
791 | static UBool U_CALLCONV dtitvinfHashTableValueComparator(UHashTok val1, UHashTok val2); | |
792 | ||
793 | static UBool | |
794 | U_CALLCONV dtitvinfHashTableValueComparator(UHashTok val1, UHashTok val2) { | |
795 | const UnicodeString* pattern1 = (UnicodeString*)val1.pointer; | |
796 | const UnicodeString* pattern2 = (UnicodeString*)val2.pointer; | |
797 | UBool ret = TRUE; | |
798 | int8_t i; | |
799 | for ( i = 0; i < DateIntervalInfo::kMaxIntervalPatternIndex && ret == TRUE; ++i ) { | |
800 | ret = (pattern1[i] == pattern2[i]); | |
801 | } | |
802 | return ret; | |
803 | } | |
804 | ||
805 | U_CDECL_END | |
806 | ||
807 | ||
808 | Hashtable* | |
809 | DateIntervalInfo::initHash(UErrorCode& status) { | |
810 | if ( U_FAILURE(status) ) { | |
811 | return NULL; | |
812 | } | |
813 | Hashtable* hTable; | |
814 | if ( (hTable = new Hashtable(FALSE, status)) == NULL ) { | |
815 | status = U_MEMORY_ALLOCATION_ERROR; | |
816 | return NULL; | |
817 | } | |
818 | if ( U_FAILURE(status) ) { | |
819 | delete hTable; | |
820 | return NULL; | |
821 | } | |
822 | hTable->setValueComparator(dtitvinfHashTableValueComparator); | |
823 | return hTable; | |
824 | } | |
825 | ||
826 | ||
827 | void | |
828 | DateIntervalInfo::copyHash(const Hashtable* source, | |
829 | Hashtable* target, | |
830 | UErrorCode& status) { | |
831 | if ( U_FAILURE(status) ) { | |
832 | return; | |
833 | } | |
834 | int32_t pos = UHASH_FIRST; | |
835 | const UHashElement* element = NULL; | |
836 | if ( source ) { | |
837 | while ( (element = source->nextElement(pos)) != NULL ) { | |
838 | const UHashTok keyTok = element->key; | |
839 | const UnicodeString* key = (UnicodeString*)keyTok.pointer; | |
840 | const UHashTok valueTok = element->value; | |
841 | const UnicodeString* value = (UnicodeString*)valueTok.pointer; | |
842 | UnicodeString* copy = new UnicodeString[kIPI_MAX_INDEX]; | |
843 | int8_t i; | |
844 | for ( i = 0; i < kIPI_MAX_INDEX; ++i ) { | |
845 | copy[i] = value[i]; | |
846 | } | |
847 | target->put(UnicodeString(*key), copy, status); | |
848 | if ( U_FAILURE(status) ) { | |
849 | return; | |
850 | } | |
851 | } | |
852 | } | |
853 | } | |
854 | ||
855 | ||
856 | U_NAMESPACE_END | |
857 | ||
858 | #endif |