OLD | NEW |
1 /* | 1 /* |
2 ******************************************************************************* | 2 ******************************************************************************* |
3 * Copyright (C) 1997-2014, International Business Machines Corporation and | 3 * Copyright (C) 1997-2015, International Business Machines Corporation and |
4 * others. All Rights Reserved. | 4 * others. All Rights Reserved. |
5 ******************************************************************************* | 5 ******************************************************************************* |
6 * | 6 * |
7 * File NUMFMT.CPP | 7 * File NUMFMT.CPP |
8 * | 8 * |
9 * Modification History: | 9 * Modification History: |
10 * | 10 * |
11 * Date Name Description | 11 * Date Name Description |
12 * 02/19/97 aliu Converted from java. | 12 * 02/19/97 aliu Converted from java. |
13 * 03/18/97 clhuang Implemented with C++ APIs. | 13 * 03/18/97 clhuang Implemented with C++ APIs. |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 gLastResortScientificPat, // UNUM_SCIENTIFIC | 113 gLastResortScientificPat, // UNUM_SCIENTIFIC |
114 NULL, // UNUM_SPELLOUT | 114 NULL, // UNUM_SPELLOUT |
115 NULL, // UNUM_ORDINAL | 115 NULL, // UNUM_ORDINAL |
116 NULL, // UNUM_DURATION | 116 NULL, // UNUM_DURATION |
117 NULL, // UNUM_NUMBERING_SYSTEM | 117 NULL, // UNUM_NUMBERING_SYSTEM |
118 NULL, // UNUM_PATTERN_RULEBASED | 118 NULL, // UNUM_PATTERN_RULEBASED |
119 gLastResortIsoCurrencyPat, // UNUM_CURRENCY_ISO | 119 gLastResortIsoCurrencyPat, // UNUM_CURRENCY_ISO |
120 gLastResortPluralCurrencyPat, // UNUM_CURRENCY_PLURAL | 120 gLastResortPluralCurrencyPat, // UNUM_CURRENCY_PLURAL |
121 gLastResortAccountingCurrencyPat, // UNUM_CURRENCY_ACCOUNTING | 121 gLastResortAccountingCurrencyPat, // UNUM_CURRENCY_ACCOUNTING |
122 gLastResortCurrencyPat, // UNUM_CASH_CURRENCY | 122 gLastResortCurrencyPat, // UNUM_CASH_CURRENCY |
| 123 NULL, // UNUM_DECIMAL_COMPACT_SHORT |
| 124 NULL, // UNUM_DECIMAL_COMPACT_LONG |
| 125 gLastResortCurrencyPat, // UNUM_CURRENCY_STANDARD |
123 }; | 126 }; |
124 | 127 |
125 // Keys used for accessing resource bundles | 128 // Keys used for accessing resource bundles |
126 | 129 |
127 static const char *gNumberElements = "NumberElements"; | 130 static const char *gNumberElements = "NumberElements"; |
128 static const char *gLatn = "latn"; | 131 static const char *gLatn = "latn"; |
129 static const char *gPatterns = "patterns"; | 132 static const char *gPatterns = "patterns"; |
130 static const char *gFormatKeys[UNUM_FORMAT_STYLE_COUNT] = { | 133 static const char *gFormatKeys[UNUM_FORMAT_STYLE_COUNT] = { |
131 NULL, // UNUM_PATTERN_DECIMAL | 134 NULL, // UNUM_PATTERN_DECIMAL |
132 "decimalFormat", // UNUM_DECIMAL | 135 "decimalFormat", // UNUM_DECIMAL |
133 "currencyFormat", // UNUM_CURRENCY | 136 "currencyFormat", // UNUM_CURRENCY |
134 "percentFormat", // UNUM_PERCENT | 137 "percentFormat", // UNUM_PERCENT |
135 "scientificFormat", // UNUM_SCIENTIFIC | 138 "scientificFormat", // UNUM_SCIENTIFIC |
136 NULL, // UNUM_SPELLOUT | 139 NULL, // UNUM_SPELLOUT |
137 NULL, // UNUM_ORDINAL | 140 NULL, // UNUM_ORDINAL |
138 NULL, // UNUM_DURATION | 141 NULL, // UNUM_DURATION |
139 NULL, // UNUM_NUMBERING_SYSTEM | 142 NULL, // UNUM_NUMBERING_SYSTEM |
140 NULL, // UNUM_PATTERN_RULEBASED | 143 NULL, // UNUM_PATTERN_RULEBASED |
141 // For UNUM_CURRENCY_ISO and UNUM_CURRENCY_PLURAL, | 144 // For UNUM_CURRENCY_ISO and UNUM_CURRENCY_PLURAL, |
142 // the pattern is the same as the pattern of UNUM_CURRENCY | 145 // the pattern is the same as the pattern of UNUM_CURRENCY |
143 // except for replacing the single currency sign with | 146 // except for replacing the single currency sign with |
144 // double currency sign or triple currency sign. | 147 // double currency sign or triple currency sign. |
145 "currencyFormat", // UNUM_CURRENCY_ISO | 148 "currencyFormat", // UNUM_CURRENCY_ISO |
146 "currencyFormat", // UNUM_CURRENCY_PLURAL | 149 "currencyFormat", // UNUM_CURRENCY_PLURAL |
147 "accountingFormat", // UNUM_CURRENCY_ACCOUNTING | 150 "accountingFormat", // UNUM_CURRENCY_ACCOUNTING |
148 "currencyFormat" // UNUM_CASH_CURRENCY | 151 "currencyFormat", // UNUM_CASH_CURRENCY |
| 152 NULL, // UNUM_DECIMAL_COMPACT_SHORT |
| 153 NULL, // UNUM_DECIMAL_COMPACT_LONG |
| 154 "currencyFormat", // UNUM_CURRENCY_STANDARD |
149 }; | 155 }; |
150 | 156 |
151 // Static hashtable cache of NumberingSystem objects used by NumberFormat | 157 // Static hashtable cache of NumberingSystem objects used by NumberFormat |
152 static UHashtable * NumberingSystem_cache = NULL; | 158 static UHashtable * NumberingSystem_cache = NULL; |
153 static UMutex nscacheMutex = U_MUTEX_INITIALIZER; | 159 static UMutex nscacheMutex = U_MUTEX_INITIALIZER; |
154 static icu::UInitOnce gNSCacheInitOnce = U_INITONCE_INITIALIZER; | 160 static icu::UInitOnce gNSCacheInitOnce = U_INITONCE_INITIALIZER; |
155 | 161 |
156 #if !UCONFIG_NO_SERVICE | 162 #if !UCONFIG_NO_SERVICE |
157 static icu::ICULocaleService* gService = NULL; | 163 static icu::ICULocaleService* gService = NULL; |
158 static icu::UInitOnce gServiceInitOnce = U_INITONCE_INITIALIZER; | 164 static icu::UInitOnce gServiceInitOnce = U_INITONCE_INITIALIZER; |
(...skipping 565 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
724 ParsePosition& pos) const { | 730 ParsePosition& pos) const { |
725 // Default implementation only -- subclasses should override | 731 // Default implementation only -- subclasses should override |
726 Formattable parseResult; | 732 Formattable parseResult; |
727 int32_t start = pos.getIndex(); | 733 int32_t start = pos.getIndex(); |
728 parse(text, parseResult, pos); | 734 parse(text, parseResult, pos); |
729 if (pos.getIndex() != start) { | 735 if (pos.getIndex() != start) { |
730 UChar curr[4]; | 736 UChar curr[4]; |
731 UErrorCode ec = U_ZERO_ERROR; | 737 UErrorCode ec = U_ZERO_ERROR; |
732 getEffectiveCurrency(curr, ec); | 738 getEffectiveCurrency(curr, ec); |
733 if (U_SUCCESS(ec)) { | 739 if (U_SUCCESS(ec)) { |
734 LocalPointer<CurrencyAmount> currAmt(new CurrencyAmount(parseResult,
curr, ec)); | 740 LocalPointer<CurrencyAmount> currAmt(new CurrencyAmount(parseResult,
curr, ec), ec); |
735 if (U_FAILURE(ec)) { | 741 if (U_FAILURE(ec)) { |
736 pos.setIndex(start); // indicate failure | 742 pos.setIndex(start); // indicate failure |
737 } else { | 743 } else { |
738 return currAmt.orphan(); | 744 return currAmt.orphan(); |
739 } | 745 } |
740 } | 746 } |
741 } | 747 } |
742 return NULL; | 748 return NULL; |
743 } | 749 } |
744 | 750 |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1013 { | 1019 { |
1014 ICULocaleService *service = getNumberFormatService(); | 1020 ICULocaleService *service = getNumberFormatService(); |
1015 if (service) { | 1021 if (service) { |
1016 return service->getAvailableLocales(); | 1022 return service->getAvailableLocales(); |
1017 } | 1023 } |
1018 return NULL; // no way to return error condition | 1024 return NULL; // no way to return error condition |
1019 } | 1025 } |
1020 #endif /* UCONFIG_NO_SERVICE */ | 1026 #endif /* UCONFIG_NO_SERVICE */ |
1021 // ------------------------------------- | 1027 // ------------------------------------- |
1022 | 1028 |
| 1029 enum { kKeyValueLenMax = 32 }; |
| 1030 |
1023 NumberFormat* | 1031 NumberFormat* |
1024 NumberFormat::internalCreateInstance(const Locale& loc, UNumberFormatStyle kind,
UErrorCode& status) { | 1032 NumberFormat::internalCreateInstance(const Locale& loc, UNumberFormatStyle kind,
UErrorCode& status) { |
| 1033 if (kind == UNUM_CURRENCY) { |
| 1034 char cfKeyValue[kKeyValueLenMax] = {0}; |
| 1035 UErrorCode kvStatus = U_ZERO_ERROR; |
| 1036 int32_t kLen = loc.getKeywordValue("cf", cfKeyValue, kKeyValueLenMax, kv
Status); |
| 1037 if (U_SUCCESS(kvStatus) && kLen > 0 && uprv_strcmp(cfKeyValue,"account")
==0) { |
| 1038 kind = UNUM_CURRENCY_ACCOUNTING; |
| 1039 } |
| 1040 } |
1025 #if !UCONFIG_NO_SERVICE | 1041 #if !UCONFIG_NO_SERVICE |
1026 if (haveService()) { | 1042 if (haveService()) { |
1027 return (NumberFormat*)gService->get(loc, kind, status); | 1043 return (NumberFormat*)gService->get(loc, kind, status); |
1028 } | 1044 } |
1029 #endif | 1045 #endif |
1030 return makeInstance(loc, kind, status); | 1046 return makeInstance(loc, kind, status); |
1031 } | 1047 } |
1032 | 1048 |
1033 NumberFormat* U_EXPORT2 | 1049 NumberFormat* U_EXPORT2 |
1034 NumberFormat::createInstance(const Locale& loc, UNumberFormatStyle kind, UErrorC
ode& status) { | 1050 NumberFormat::createInstance(const Locale& loc, UNumberFormatStyle kind, UErrorC
ode& status) { |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1315 switch (style) { | 1331 switch (style) { |
1316 case UNUM_DECIMAL: | 1332 case UNUM_DECIMAL: |
1317 curr = FALSE; | 1333 curr = FALSE; |
1318 // fall-through | 1334 // fall-through |
1319 | 1335 |
1320 case UNUM_CURRENCY: | 1336 case UNUM_CURRENCY: |
1321 case UNUM_CURRENCY_ISO: // do not support plural formatting here | 1337 case UNUM_CURRENCY_ISO: // do not support plural formatting here |
1322 case UNUM_CURRENCY_PLURAL: | 1338 case UNUM_CURRENCY_PLURAL: |
1323 case UNUM_CURRENCY_ACCOUNTING: | 1339 case UNUM_CURRENCY_ACCOUNTING: |
1324 case UNUM_CASH_CURRENCY: | 1340 case UNUM_CASH_CURRENCY: |
| 1341 case UNUM_CURRENCY_STANDARD: |
1325 f = new Win32NumberFormat(desiredLocale, curr, status); | 1342 f = new Win32NumberFormat(desiredLocale, curr, status); |
1326 | 1343 |
1327 if (U_SUCCESS(status)) { | 1344 if (U_SUCCESS(status)) { |
1328 return f; | 1345 return f; |
1329 } | 1346 } |
1330 | 1347 |
1331 delete f; | 1348 delete f; |
1332 break; | 1349 break; |
1333 default: | 1350 default: |
1334 break; | 1351 break; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1368 } | 1385 } |
1369 | 1386 |
1370 LocalPointer<DecimalFormatSymbols> symbolsToAdopt; | 1387 LocalPointer<DecimalFormatSymbols> symbolsToAdopt; |
1371 UnicodeString pattern; | 1388 UnicodeString pattern; |
1372 LocalUResourceBundlePointer ownedResource(ures_open(NULL, desiredLocale.getN
ame(), &status)); | 1389 LocalUResourceBundlePointer ownedResource(ures_open(NULL, desiredLocale.getN
ame(), &status)); |
1373 if (U_FAILURE(status)) { | 1390 if (U_FAILURE(status)) { |
1374 return NULL; | 1391 return NULL; |
1375 } | 1392 } |
1376 else { | 1393 else { |
1377 // Loads the decimal symbols of the desired locale. | 1394 // Loads the decimal symbols of the desired locale. |
1378 symbolsToAdopt.adoptInstead(new DecimalFormatSymbols(desiredLocale, stat
us)); | 1395 symbolsToAdopt.adoptInsteadAndCheckErrorCode(new DecimalFormatSymbols(de
siredLocale, status), status); |
1379 if (symbolsToAdopt.isNull()) { | 1396 if (U_FAILURE(status)) { |
1380 status = U_MEMORY_ALLOCATION_ERROR; | |
1381 return NULL; | 1397 return NULL; |
1382 } | 1398 } |
1383 | 1399 |
1384 UResourceBundle *resource = ownedResource.orphan(); | 1400 UResourceBundle *resource = ownedResource.orphan(); |
1385 UResourceBundle *numElements = ures_getByKeyWithFallback(resource, gNumb
erElements, NULL, &status); | 1401 UResourceBundle *numElements = ures_getByKeyWithFallback(resource, gNumb
erElements, NULL, &status); |
1386 resource = ures_getByKeyWithFallback(numElements, ns->getName(), resourc
e, &status); | 1402 resource = ures_getByKeyWithFallback(numElements, ns->getName(), resourc
e, &status); |
1387 resource = ures_getByKeyWithFallback(resource, gPatterns, resource, &sta
tus); | 1403 resource = ures_getByKeyWithFallback(resource, gPatterns, resource, &sta
tus); |
1388 ownedResource.adoptInstead(resource); | 1404 ownedResource.adoptInstead(resource); |
1389 | 1405 |
1390 int32_t patLen = 0; | 1406 int32_t patLen = 0; |
1391 const UChar *patResStr = ures_getStringByKeyWithFallback(resource, gForm
atKeys[style], &patLen, &status); | 1407 const UChar *patResStr = ures_getStringByKeyWithFallback(resource, gForm
atKeys[style], &patLen, &status); |
1392 | 1408 |
1393 // Didn't find a pattern specific to the numbering system, so fall back
to "latn" | 1409 // Didn't find a pattern specific to the numbering system, so fall back
to "latn" |
1394 if ( status == U_MISSING_RESOURCE_ERROR && uprv_strcmp(gLatn,ns->getName
())) { | 1410 if ( status == U_MISSING_RESOURCE_ERROR && uprv_strcmp(gLatn,ns->getName
())) { |
1395 status = U_ZERO_ERROR; | 1411 status = U_ZERO_ERROR; |
1396 resource = ures_getByKeyWithFallback(numElements, gLatn, resource, &
status); | 1412 resource = ures_getByKeyWithFallback(numElements, gLatn, resource, &
status); |
1397 resource = ures_getByKeyWithFallback(resource, gPatterns, resource,
&status); | 1413 resource = ures_getByKeyWithFallback(resource, gPatterns, resource,
&status); |
1398 patResStr = ures_getStringByKeyWithFallback(resource, gFormatKeys[st
yle], &patLen, &status); | 1414 patResStr = ures_getStringByKeyWithFallback(resource, gFormatKeys[st
yle], &patLen, &status); |
1399 } | 1415 } |
1400 | 1416 |
1401 ures_close(numElements); | 1417 ures_close(numElements); |
1402 | 1418 |
1403 // Creates the specified decimal format style of the desired locale. | 1419 // Creates the specified decimal format style of the desired locale. |
1404 pattern.setTo(TRUE, patResStr, patLen); | 1420 pattern.setTo(TRUE, patResStr, patLen); |
1405 } | 1421 } |
1406 if (U_FAILURE(status)) { | 1422 if (U_FAILURE(status)) { |
1407 return NULL; | 1423 return NULL; |
1408 } | 1424 } |
1409 if(style==UNUM_CURRENCY || style == UNUM_CURRENCY_ISO || style == UNUM_CURRE
NCY_ACCOUNTING | 1425 if(style==UNUM_CURRENCY || style == UNUM_CURRENCY_ISO || style == UNUM_CURRE
NCY_ACCOUNTING |
1410 || style == UNUM_CASH_CURRENCY){ | 1426 || style == UNUM_CASH_CURRENCY || style == UNUM_CURRENCY_STANDARD){ |
1411 const UChar* currPattern = symbolsToAdopt->getCurrencyPattern(); | 1427 const UChar* currPattern = symbolsToAdopt->getCurrencyPattern(); |
1412 if(currPattern!=NULL){ | 1428 if(currPattern!=NULL){ |
1413 pattern.setTo(currPattern, u_strlen(currPattern)); | 1429 pattern.setTo(currPattern, u_strlen(currPattern)); |
1414 } | 1430 } |
1415 } | 1431 } |
1416 | 1432 |
1417 | 1433 |
1418 NumberFormat *f; | 1434 NumberFormat *f; |
1419 if (ns->isAlgorithmic()) { | 1435 if (ns->isAlgorithmic()) { |
1420 UnicodeString nsDesc; | 1436 UnicodeString nsDesc; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1488 return NULL; | 1504 return NULL; |
1489 } | 1505 } |
1490 return f; | 1506 return f; |
1491 } | 1507 } |
1492 | 1508 |
1493 U_NAMESPACE_END | 1509 U_NAMESPACE_END |
1494 | 1510 |
1495 #endif /* #if !UCONFIG_NO_FORMATTING */ | 1511 #endif /* #if !UCONFIG_NO_FORMATTING */ |
1496 | 1512 |
1497 //eof | 1513 //eof |
OLD | NEW |