]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/dcfmtsym.cpp
ICU-511.27.tar.gz
[apple/icu.git] / icuSources / i18n / dcfmtsym.cpp
CommitLineData
b75a7d8f
A
1/*
2*******************************************************************************
4388f060 3* Copyright (C) 1997-2011, International Business Machines Corporation and *
b75a7d8f
A
4* others. All Rights Reserved. *
5*******************************************************************************
6*
7* File DCFMTSYM.CPP
8*
9* Modification History:
10*
11* Date Name Description
12* 02/19/97 aliu Converted from java.
13* 03/18/97 clhuang Implemented with C++ APIs.
14* 03/27/97 helena Updated to pass the simple test after code review.
15* 08/26/97 aliu Added currency/intl currency symbol support.
374ca955 16* 07/20/98 stephen Slightly modified initialization of monetarySeparator
b75a7d8f
A
17********************************************************************************
18*/
729e4ab9 19
b75a7d8f
A
20#include "unicode/utypes.h"
21
22#if !UCONFIG_NO_FORMATTING
23
24#include "unicode/dcfmtsym.h"
374ca955 25#include "unicode/ures.h"
b75a7d8f
A
26#include "unicode/decimfmt.h"
27#include "unicode/ucurr.h"
28#include "unicode/choicfmt.h"
729e4ab9
A
29#include "unicode/unistr.h"
30#include "unicode/numsys.h"
4388f060 31#include "unicode/unum.h"
374ca955
A
32#include "ucurrimp.h"
33#include "cstring.h"
34#include "locbased.h"
73c04bcf 35#include "uresimp.h"
729e4ab9
A
36#include "ureslocs.h"
37
b75a7d8f
A
38// *****************************************************************************
39// class DecimalFormatSymbols
40// *****************************************************************************
729e4ab9 41
b75a7d8f
A
42U_NAMESPACE_BEGIN
43
374ca955 44UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DecimalFormatSymbols)
b75a7d8f 45
374ca955 46static const char gNumberElements[] = "NumberElements";
729e4ab9
A
47static const char gCurrencySpacingTag[] = "currencySpacing";
48static const char gBeforeCurrencyTag[] = "beforeCurrency";
49static const char gAfterCurrencyTag[] = "afterCurrency";
50static const char gCurrencyMatchTag[] = "currencyMatch";
51static const char gCurrencySudMatchTag[] = "surroundingMatch";
52static const char gCurrencyInsertBtnTag[] = "insertBetween";
53
b75a7d8f
A
54
55static const UChar INTL_CURRENCY_SYMBOL_STR[] = {0xa4, 0xa4, 0};
56
57// -------------------------------------
58// Initializes this with the decimal format symbols in the default locale.
729e4ab9 59
b75a7d8f 60DecimalFormatSymbols::DecimalFormatSymbols(UErrorCode& status)
374ca955
A
61 : UObject(),
62 locale()
b75a7d8f 63{
374ca955 64 initialize(locale, status, TRUE);
b75a7d8f 65}
729e4ab9 66
b75a7d8f
A
67// -------------------------------------
68// Initializes this with the decimal format symbols in the desired locale.
729e4ab9 69
b75a7d8f 70DecimalFormatSymbols::DecimalFormatSymbols(const Locale& loc, UErrorCode& status)
374ca955
A
71 : UObject(),
72 locale(loc)
b75a7d8f 73{
374ca955 74 initialize(locale, status);
b75a7d8f 75}
729e4ab9 76
b75a7d8f
A
77// -------------------------------------
78
79DecimalFormatSymbols::~DecimalFormatSymbols()
80{
81}
82
83// -------------------------------------
84// copy constructor
85
86DecimalFormatSymbols::DecimalFormatSymbols(const DecimalFormatSymbols &source)
87 : UObject(source)
88{
374ca955 89 *this = source;
b75a7d8f
A
90}
91
92// -------------------------------------
93// assignment operator
94
95DecimalFormatSymbols&
96DecimalFormatSymbols::operator=(const DecimalFormatSymbols& rhs)
97{
374ca955
A
98 if (this != &rhs) {
99 for(int32_t i = 0; i < (int32_t)kFormatSymbolCount; ++i) {
b75a7d8f
A
100 // fastCopyFrom is safe, see docs on fSymbols
101 fSymbols[(ENumberFormatSymbol)i].fastCopyFrom(rhs.fSymbols[(ENumberFormatSymbol)i]);
102 }
4388f060 103 for(int32_t i = 0; i < (int32_t)UNUM_CURRENCY_SPACING_COUNT; ++i) {
729e4ab9
A
104 currencySpcBeforeSym[i].fastCopyFrom(rhs.currencySpcBeforeSym[i]);
105 currencySpcAfterSym[i].fastCopyFrom(rhs.currencySpcAfterSym[i]);
106 }
374ca955
A
107 locale = rhs.locale;
108 uprv_strcpy(validLocale, rhs.validLocale);
109 uprv_strcpy(actualLocale, rhs.actualLocale);
b75a7d8f
A
110 }
111 return *this;
112}
113
114// -------------------------------------
115
116UBool
117DecimalFormatSymbols::operator==(const DecimalFormatSymbols& that) const
118{
119 if (this == &that) {
120 return TRUE;
121 }
374ca955 122 for(int32_t i = 0; i < (int32_t)kFormatSymbolCount; ++i) {
b75a7d8f
A
123 if(fSymbols[(ENumberFormatSymbol)i] != that.fSymbols[(ENumberFormatSymbol)i]) {
124 return FALSE;
125 }
126 }
4388f060 127 for(int32_t i = 0; i < (int32_t)UNUM_CURRENCY_SPACING_COUNT; ++i) {
729e4ab9
A
128 if(currencySpcBeforeSym[i] != that.currencySpcBeforeSym[i]) {
129 return FALSE;
130 }
131 if(currencySpcAfterSym[i] != that.currencySpcAfterSym[i]) {
132 return FALSE;
133 }
134 }
374ca955
A
135 return locale == that.locale &&
136 uprv_strcmp(validLocale, that.validLocale) == 0 &&
137 uprv_strcmp(actualLocale, that.actualLocale) == 0;
b75a7d8f 138}
729e4ab9 139
b75a7d8f 140// -------------------------------------
729e4ab9 141
b75a7d8f 142void
729e4ab9 143DecimalFormatSymbols::initialize(const Locale& loc, UErrorCode& status, UBool useLastResortData)
b75a7d8f 144{
729e4ab9
A
145 static const char *gNumberElementKeys[kFormatSymbolCount] = {
146 "decimal",
147 "group",
148 "list",
149 "percentSign",
150 NULL, /* Native zero digit is deprecated from CLDR - get it from the numbering system */
151 NULL, /* Pattern digit character is deprecated from CLDR - use # by default always */
152 "minusSign",
153 "plusSign",
154 NULL, /* currency symbol - We don't really try to load this directly from CLDR until we know the currency */
155 NULL, /* intl currency symbol - We don't really try to load this directly from CLDR until we know the currency */
156 "currencyDecimal",
157 "exponential",
158 "perMille",
159 NULL, /* Escape padding character - not in CLDR */
160 "infinity",
161 "nan",
162 NULL, /* Significant digit symbol - not in CLDR */
163 "currencyGroup",
164 NULL, /* one digit - get it from the numbering system */
165 NULL, /* two digit - get it from the numbering system */
166 NULL, /* three digit - get it from the numbering system */
167 NULL, /* four digit - get it from the numbering system */
168 NULL, /* five digit - get it from the numbering system */
169 NULL, /* six digit - get it from the numbering system */
170 NULL, /* seven digit - get it from the numbering system */
171 NULL, /* eight digit - get it from the numbering system */
172 NULL, /* nine digit - get it from the numbering system */
173 };
174
175 static const char *gLatn = "latn";
176 static const char *gSymbols = "symbols";
177 const char *nsName;
178 const UChar *sym = NULL;
179 int32_t len = 0;
180
374ca955 181 *validLocale = *actualLocale = 0;
73c04bcf 182 currPattern = NULL;
374ca955
A
183 if (U_FAILURE(status))
184 return;
729e4ab9 185
374ca955
A
186 const char* locStr = loc.getName();
187 UResourceBundle *resource = ures_open((char *)0, locStr, &status);
729e4ab9
A
188 UResourceBundle *numberElementsRes = ures_getByKeyWithFallback(resource, gNumberElements, NULL, &status);
189
190 if (U_FAILURE(status)) {
191 if ( useLastResortData ) {
4388f060 192 status = U_USING_DEFAULT_WARNING;
b75a7d8f
A
193 initialize();
194 }
729e4ab9
A
195 return;
196 } else {
197
198 // First initialize all the symbols to the fallbacks for anything we can't find
199 initialize();
b75a7d8f 200
729e4ab9
A
201 //
202 // Next get the numbering system for this locale and set zero digit
203 // and the digit string based on the numbering system for the locale
204 //
205
206 NumberingSystem* ns = NumberingSystem::createInstance(loc,status);
207 if (U_SUCCESS(status) && ns->getRadix() == 10 && !ns->isAlgorithmic()) {
208 nsName = ns->getName();
4388f060
A
209 UnicodeString digitString(ns->getDescription());
210 setSymbol(kZeroDigitSymbol, digitString.tempSubString(0, 1), FALSE);
211 setSymbol(kOneDigitSymbol, digitString.tempSubString(1, 1), FALSE);
212 setSymbol(kTwoDigitSymbol, digitString.tempSubString(2, 1), FALSE);
213 setSymbol(kThreeDigitSymbol, digitString.tempSubString(3, 1), FALSE);
214 setSymbol(kFourDigitSymbol, digitString.tempSubString(4, 1), FALSE);
215 setSymbol(kFiveDigitSymbol, digitString.tempSubString(5, 1), FALSE);
216 setSymbol(kSixDigitSymbol, digitString.tempSubString(6, 1), FALSE);
217 setSymbol(kSevenDigitSymbol, digitString.tempSubString(7, 1), FALSE);
218 setSymbol(kEightDigitSymbol, digitString.tempSubString(8, 1), FALSE);
219 setSymbol(kNineDigitSymbol, digitString.tempSubString(9, 1), FALSE);
729e4ab9
A
220 } else {
221 nsName = gLatn;
374ca955 222 }
729e4ab9
A
223
224 UBool isLatn = !uprv_strcmp(nsName,gLatn);
225
226 UErrorCode nlStatus = U_ZERO_ERROR;
227 UResourceBundle *nonLatnSymbols = NULL;
228 if ( !isLatn ) {
229 nonLatnSymbols = ures_getByKeyWithFallback(numberElementsRes, nsName, NULL, &nlStatus);
230 nonLatnSymbols = ures_getByKeyWithFallback(nonLatnSymbols, gSymbols, nonLatnSymbols, &nlStatus);
374ca955 231 }
b75a7d8f 232
729e4ab9
A
233 UResourceBundle *latnSymbols = ures_getByKeyWithFallback(numberElementsRes, gLatn, NULL, &status);
234 latnSymbols = ures_getByKeyWithFallback(latnSymbols, gSymbols, latnSymbols, &status);
235
236 UBool kMonetaryDecimalSet = FALSE;
237 UBool kMonetaryGroupingSet = FALSE;
238 for(int32_t i = 0; i<kFormatSymbolCount; i++) {
239 if ( gNumberElementKeys[i] != NULL ) {
240 UErrorCode localStatus = U_ZERO_ERROR;
241 if ( !isLatn ) {
242 sym = ures_getStringByKeyWithFallback(nonLatnSymbols,gNumberElementKeys[i],&len,&localStatus);
243 // If we can't find the symbol in the numbering system specific resources,
244 // use the "latn" numbering system as the fallback.
245 if ( U_FAILURE(localStatus) ) {
246 localStatus = U_ZERO_ERROR;
247 sym = ures_getStringByKeyWithFallback(latnSymbols,gNumberElementKeys[i],&len,&localStatus);
248 }
249 } else {
250 sym = ures_getStringByKeyWithFallback(latnSymbols,gNumberElementKeys[i],&len,&localStatus);
251 }
252
253 if ( U_SUCCESS(localStatus) ) {
4388f060 254 setSymbol((ENumberFormatSymbol)i, UnicodeString(TRUE, sym, len));
729e4ab9
A
255 if ( i == kMonetarySeparatorSymbol ) {
256 kMonetaryDecimalSet = TRUE;
257 } else if ( i == kMonetaryGroupingSeparatorSymbol ) {
258 kMonetaryGroupingSet = TRUE;
259 }
374ca955 260 }
b75a7d8f 261 }
729e4ab9
A
262 }
263
264 ures_close(latnSymbols);
265 if ( !isLatn ) {
266 ures_close(nonLatnSymbols);
267 }
268
269 // If monetary decimal or grouping were not explicitly set, then set them to be the
270 // same as their non-monetary counterparts.
271
272 if ( !kMonetaryDecimalSet ) {
273 setSymbol(kMonetarySeparatorSymbol,fSymbols[kDecimalSeparatorSymbol]);
274 }
275 if ( !kMonetaryGroupingSet ) {
276 setSymbol(kMonetaryGroupingSeparatorSymbol,fSymbols[kGroupingSeparatorSymbol]);
277 }
374ca955 278
729e4ab9
A
279 if (ns) {
280 delete ns;
b75a7d8f 281 }
729e4ab9
A
282
283 // Obtain currency data from the currency API. This is strictly
284 // for backward compatibility; we don't use DecimalFormatSymbols
285 // for currency data anymore.
286 UErrorCode internalStatus = U_ZERO_ERROR; // don't propagate failures out
287 UChar curriso[4];
288 UnicodeString tempStr;
289 ucurr_forLocale(locStr, curriso, 4, &internalStatus);
290
291 // Reuse numberElements[0] as a temporary buffer
292 uprv_getStaticCurrencyName(curriso, locStr, tempStr, internalStatus);
293 if (U_SUCCESS(internalStatus)) {
4388f060 294 fSymbols[kIntlCurrencySymbol].setTo(curriso, -1);
729e4ab9
A
295 fSymbols[kCurrencySymbol] = tempStr;
296 }
297 /* else use the default values. */
298
299 U_LOCALE_BASED(locBased, *this);
300 locBased.setLocaleIDs(ures_getLocaleByType(numberElementsRes,
301 ULOC_VALID_LOCALE, &status),
302 ures_getLocaleByType(numberElementsRes,
303 ULOC_ACTUAL_LOCALE, &status));
304
73c04bcf 305 //load the currency data
729e4ab9 306 UChar ucc[4]={0}; //Currency Codes are always 3 chars long
73c04bcf
A
307 int32_t uccLen = 4;
308 const char* locName = loc.getName();
46f4442e
A
309 UErrorCode localStatus = U_ZERO_ERROR;
310 uccLen = ucurr_forLocale(locName, ucc, uccLen, &localStatus);
729e4ab9 311
46f4442e 312 if(U_SUCCESS(localStatus) && uccLen > 0) {
73c04bcf
A
313 char cc[4]={0};
314 u_UCharsToChars(ucc, cc, uccLen);
315 /* An explicit currency was requested */
729e4ab9
A
316 UResourceBundle *currencyResource = ures_open(U_ICUDATA_CURR, locStr, &localStatus);
317 UResourceBundle *currency = ures_getByKeyWithFallback(currencyResource, "Currencies", NULL, &localStatus);
73c04bcf
A
318 currency = ures_getByKeyWithFallback(currency, cc, currency, &localStatus);
319 if(U_SUCCESS(localStatus) && ures_getSize(currency)>2) { // the length is 3 if more data is present
320 currency = ures_getByIndex(currency, 2, currency, &localStatus);
321 int32_t currPatternLen = 0;
322 currPattern = ures_getStringByIndex(currency, (int32_t)0, &currPatternLen, &localStatus);
4388f060
A
323 UnicodeString decimalSep = ures_getUnicodeStringByIndex(currency, (int32_t)1, &localStatus);
324 UnicodeString groupingSep = ures_getUnicodeStringByIndex(currency, (int32_t)2, &localStatus);
73c04bcf
A
325 if(U_SUCCESS(localStatus)){
326 fSymbols[kMonetaryGroupingSeparatorSymbol] = groupingSep;
327 fSymbols[kMonetarySeparatorSymbol] = decimalSep;
328 //pattern.setTo(TRUE, currPattern, currPatternLen);
329 status = localStatus;
330 }
331 }
332 ures_close(currency);
729e4ab9 333 ures_close(currencyResource);
73c04bcf
A
334 /* else An explicit currency was requested and is unknown or locale data is malformed. */
335 /* ucurr_* API will get the correct value later on. */
73c04bcf 336 }
729e4ab9 337 // else ignore the error if no currency
b75a7d8f 338
729e4ab9
A
339 // Currency Spacing.
340 localStatus = U_ZERO_ERROR;
341 UResourceBundle *currencyResource = ures_open(U_ICUDATA_CURR, locStr, &localStatus);
342 UResourceBundle *currencySpcRes = ures_getByKeyWithFallback(currencyResource,
343 gCurrencySpacingTag, NULL, &localStatus);
b75a7d8f 344
729e4ab9 345 if (localStatus == U_USING_FALLBACK_WARNING || U_SUCCESS(localStatus)) {
4388f060 346 const char* keywords[UNUM_CURRENCY_SPACING_COUNT] = {
729e4ab9
A
347 gCurrencyMatchTag, gCurrencySudMatchTag, gCurrencyInsertBtnTag
348 };
349 localStatus = U_ZERO_ERROR;
350 UResourceBundle *dataRes = ures_getByKeyWithFallback(currencySpcRes,
351 gBeforeCurrencyTag, NULL, &localStatus);
352 if (localStatus == U_USING_FALLBACK_WARNING || U_SUCCESS(localStatus)) {
353 localStatus = U_ZERO_ERROR;
4388f060
A
354 for (int32_t i = 0; i < UNUM_CURRENCY_SPACING_COUNT; i++) {
355 currencySpcBeforeSym[i] = ures_getUnicodeStringByKey(dataRes, keywords[i], &localStatus);
729e4ab9
A
356 }
357 ures_close(dataRes);
358 }
359 dataRes = ures_getByKeyWithFallback(currencySpcRes,
360 gAfterCurrencyTag, NULL, &localStatus);
361 if (localStatus == U_USING_FALLBACK_WARNING || U_SUCCESS(localStatus)) {
362 localStatus = U_ZERO_ERROR;
4388f060
A
363 for (int32_t i = 0; i < UNUM_CURRENCY_SPACING_COUNT; i++) {
364 currencySpcAfterSym[i] = ures_getUnicodeStringByKey(dataRes, keywords[i], &localStatus);
729e4ab9
A
365 }
366 ures_close(dataRes);
367 }
368 ures_close(currencySpcRes);
369 ures_close(currencyResource);
374ca955
A
370 }
371 }
729e4ab9
A
372 ures_close(resource);
373 ures_close(numberElementsRes);
b75a7d8f 374
b75a7d8f
A
375}
376
b75a7d8f
A
377void
378DecimalFormatSymbols::initialize() {
379 /*
380 * These strings used to be in static arrays, but the HP/UX aCC compiler
381 * cannot initialize a static array with class constructors.
382 * markus 2000may25
383 */
384 fSymbols[kDecimalSeparatorSymbol] = (UChar)0x2e; // '.' decimal separator
385 fSymbols[kGroupingSeparatorSymbol].remove(); // group (thousands) separator
386 fSymbols[kPatternSeparatorSymbol] = (UChar)0x3b; // ';' pattern separator
387 fSymbols[kPercentSymbol] = (UChar)0x25; // '%' percent sign
388 fSymbols[kZeroDigitSymbol] = (UChar)0x30; // '0' native 0 digit
729e4ab9
A
389 fSymbols[kOneDigitSymbol] = (UChar)0x31; // '1' native 1 digit
390 fSymbols[kTwoDigitSymbol] = (UChar)0x32; // '2' native 2 digit
391 fSymbols[kThreeDigitSymbol] = (UChar)0x33; // '3' native 3 digit
392 fSymbols[kFourDigitSymbol] = (UChar)0x34; // '4' native 4 digit
393 fSymbols[kFiveDigitSymbol] = (UChar)0x35; // '5' native 5 digit
394 fSymbols[kSixDigitSymbol] = (UChar)0x36; // '6' native 6 digit
395 fSymbols[kSevenDigitSymbol] = (UChar)0x37; // '7' native 7 digit
396 fSymbols[kEightDigitSymbol] = (UChar)0x38; // '8' native 8 digit
397 fSymbols[kNineDigitSymbol] = (UChar)0x39; // '9' native 9 digit
b75a7d8f 398 fSymbols[kDigitSymbol] = (UChar)0x23; // '#' pattern digit
b75a7d8f 399 fSymbols[kPlusSignSymbol] = (UChar)0x002b; // '+' plus sign
374ca955 400 fSymbols[kMinusSignSymbol] = (UChar)0x2d; // '-' minus sign
b75a7d8f 401 fSymbols[kCurrencySymbol] = (UChar)0xa4; // 'OX' currency symbol
4388f060 402 fSymbols[kIntlCurrencySymbol].setTo(TRUE, INTL_CURRENCY_SYMBOL_STR, 2);
b75a7d8f
A
403 fSymbols[kMonetarySeparatorSymbol] = (UChar)0x2e; // '.' monetary decimal separator
404 fSymbols[kExponentialSymbol] = (UChar)0x45; // 'E' exponential
405 fSymbols[kPerMillSymbol] = (UChar)0x2030; // '%o' per mill
406 fSymbols[kPadEscapeSymbol] = (UChar)0x2a; // '*' pad escape symbol
407 fSymbols[kInfinitySymbol] = (UChar)0x221e; // 'oo' infinite
408 fSymbols[kNaNSymbol] = (UChar)0xfffd; // SUB NaN
374ca955 409 fSymbols[kSignificantDigitSymbol] = (UChar)0x0040; // '@' significant digit
729e4ab9 410 fSymbols[kMonetaryGroupingSeparatorSymbol].remove(); //
374ca955
A
411}
412
729e4ab9 413Locale
374ca955
A
414DecimalFormatSymbols::getLocale(ULocDataLocaleType type, UErrorCode& status) const {
415 U_LOCALE_BASED(locBased, *this);
416 return locBased.getLocale(type, status);
b75a7d8f
A
417}
418
729e4ab9 419const UnicodeString&
4388f060 420DecimalFormatSymbols::getPatternForCurrencySpacing(UCurrencySpacing type,
729e4ab9
A
421 UBool beforeCurrency,
422 UErrorCode& status) const {
423 if (U_FAILURE(status)) {
424 return fNoSymbol; // always empty.
425 }
426 if (beforeCurrency) {
427 return currencySpcBeforeSym[(int32_t)type];
428 } else {
429 return currencySpcAfterSym[(int32_t)type];
430 }
431}
432
433void
4388f060 434DecimalFormatSymbols::setPatternForCurrencySpacing(UCurrencySpacing type,
729e4ab9
A
435 UBool beforeCurrency,
436 const UnicodeString& pattern) {
437 if (beforeCurrency) {
438 currencySpcBeforeSym[(int32_t)type] = pattern;
439 } else {
440 currencySpcAfterSym[(int32_t)type] = pattern;
441 }
442}
b75a7d8f
A
443U_NAMESPACE_END
444
445#endif /* #if !UCONFIG_NO_FORMATTING */
446
447//eof