michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 1997-2013, International Business Machines Corporation and michael@0: * others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: * michael@0: * File DCFMTSYM.CPP michael@0: * michael@0: * Modification History: michael@0: * michael@0: * Date Name Description michael@0: * 02/19/97 aliu Converted from java. michael@0: * 03/18/97 clhuang Implemented with C++ APIs. michael@0: * 03/27/97 helena Updated to pass the simple test after code review. michael@0: * 08/26/97 aliu Added currency/intl currency symbol support. michael@0: * 07/20/98 stephen Slightly modified initialization of monetarySeparator michael@0: ******************************************************************************** michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/dcfmtsym.h" michael@0: #include "unicode/ures.h" michael@0: #include "unicode/decimfmt.h" michael@0: #include "unicode/ucurr.h" michael@0: #include "unicode/choicfmt.h" michael@0: #include "unicode/unistr.h" michael@0: #include "unicode/numsys.h" michael@0: #include "unicode/unum.h" michael@0: #include "unicode/utf16.h" michael@0: #include "ucurrimp.h" michael@0: #include "cstring.h" michael@0: #include "locbased.h" michael@0: #include "uresimp.h" michael@0: #include "ureslocs.h" michael@0: michael@0: // ***************************************************************************** michael@0: // class DecimalFormatSymbols michael@0: // ***************************************************************************** michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DecimalFormatSymbols) michael@0: michael@0: static const char gNumberElements[] = "NumberElements"; michael@0: static const char gCurrencySpacingTag[] = "currencySpacing"; michael@0: static const char gBeforeCurrencyTag[] = "beforeCurrency"; michael@0: static const char gAfterCurrencyTag[] = "afterCurrency"; michael@0: static const char gCurrencyMatchTag[] = "currencyMatch"; michael@0: static const char gCurrencySudMatchTag[] = "surroundingMatch"; michael@0: static const char gCurrencyInsertBtnTag[] = "insertBetween"; michael@0: michael@0: michael@0: static const UChar INTL_CURRENCY_SYMBOL_STR[] = {0xa4, 0xa4, 0}; michael@0: michael@0: // ------------------------------------- michael@0: // Initializes this with the decimal format symbols in the default locale. michael@0: michael@0: DecimalFormatSymbols::DecimalFormatSymbols(UErrorCode& status) michael@0: : UObject(), michael@0: locale() michael@0: { michael@0: initialize(locale, status, TRUE); michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: // Initializes this with the decimal format symbols in the desired locale. michael@0: michael@0: DecimalFormatSymbols::DecimalFormatSymbols(const Locale& loc, UErrorCode& status) michael@0: : UObject(), michael@0: locale(loc) michael@0: { michael@0: initialize(locale, status); michael@0: } michael@0: michael@0: DecimalFormatSymbols::DecimalFormatSymbols() michael@0: : UObject(), michael@0: locale(Locale::getRoot()), michael@0: currPattern(NULL) { michael@0: *validLocale = *actualLocale = 0; michael@0: initialize(); michael@0: } michael@0: michael@0: DecimalFormatSymbols* michael@0: DecimalFormatSymbols::createWithLastResortData(UErrorCode& status) { michael@0: if (U_FAILURE(status)) { return NULL; } michael@0: DecimalFormatSymbols* sym = new DecimalFormatSymbols(); michael@0: if (sym == NULL) { michael@0: status = U_MEMORY_ALLOCATION_ERROR; michael@0: } michael@0: return sym; michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: DecimalFormatSymbols::~DecimalFormatSymbols() michael@0: { michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: // copy constructor michael@0: michael@0: DecimalFormatSymbols::DecimalFormatSymbols(const DecimalFormatSymbols &source) michael@0: : UObject(source) michael@0: { michael@0: *this = source; michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: // assignment operator michael@0: michael@0: DecimalFormatSymbols& michael@0: DecimalFormatSymbols::operator=(const DecimalFormatSymbols& rhs) michael@0: { michael@0: if (this != &rhs) { michael@0: for(int32_t i = 0; i < (int32_t)kFormatSymbolCount; ++i) { michael@0: // fastCopyFrom is safe, see docs on fSymbols michael@0: fSymbols[(ENumberFormatSymbol)i].fastCopyFrom(rhs.fSymbols[(ENumberFormatSymbol)i]); michael@0: } michael@0: for(int32_t i = 0; i < (int32_t)UNUM_CURRENCY_SPACING_COUNT; ++i) { michael@0: currencySpcBeforeSym[i].fastCopyFrom(rhs.currencySpcBeforeSym[i]); michael@0: currencySpcAfterSym[i].fastCopyFrom(rhs.currencySpcAfterSym[i]); michael@0: } michael@0: locale = rhs.locale; michael@0: uprv_strcpy(validLocale, rhs.validLocale); michael@0: uprv_strcpy(actualLocale, rhs.actualLocale); michael@0: } michael@0: return *this; michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: UBool michael@0: DecimalFormatSymbols::operator==(const DecimalFormatSymbols& that) const michael@0: { michael@0: if (this == &that) { michael@0: return TRUE; michael@0: } michael@0: for(int32_t i = 0; i < (int32_t)kFormatSymbolCount; ++i) { michael@0: if(fSymbols[(ENumberFormatSymbol)i] != that.fSymbols[(ENumberFormatSymbol)i]) { michael@0: return FALSE; michael@0: } michael@0: } michael@0: for(int32_t i = 0; i < (int32_t)UNUM_CURRENCY_SPACING_COUNT; ++i) { michael@0: if(currencySpcBeforeSym[i] != that.currencySpcBeforeSym[i]) { michael@0: return FALSE; michael@0: } michael@0: if(currencySpcAfterSym[i] != that.currencySpcAfterSym[i]) { michael@0: return FALSE; michael@0: } michael@0: } michael@0: return locale == that.locale && michael@0: uprv_strcmp(validLocale, that.validLocale) == 0 && michael@0: uprv_strcmp(actualLocale, that.actualLocale) == 0; michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: void michael@0: DecimalFormatSymbols::initialize(const Locale& loc, UErrorCode& status, UBool useLastResortData) michael@0: { michael@0: static const char *gNumberElementKeys[kFormatSymbolCount] = { michael@0: "decimal", michael@0: "group", michael@0: "list", michael@0: "percentSign", michael@0: NULL, /* Native zero digit is deprecated from CLDR - get it from the numbering system */ michael@0: NULL, /* Pattern digit character is deprecated from CLDR - use # by default always */ michael@0: "minusSign", michael@0: "plusSign", michael@0: NULL, /* currency symbol - We don't really try to load this directly from CLDR until we know the currency */ michael@0: NULL, /* intl currency symbol - We don't really try to load this directly from CLDR until we know the currency */ michael@0: "currencyDecimal", michael@0: "exponential", michael@0: "perMille", michael@0: NULL, /* Escape padding character - not in CLDR */ michael@0: "infinity", michael@0: "nan", michael@0: NULL, /* Significant digit symbol - not in CLDR */ michael@0: "currencyGroup", michael@0: NULL, /* one digit - get it from the numbering system */ michael@0: NULL, /* two digit - get it from the numbering system */ michael@0: NULL, /* three digit - get it from the numbering system */ michael@0: NULL, /* four digit - get it from the numbering system */ michael@0: NULL, /* five digit - get it from the numbering system */ michael@0: NULL, /* six digit - get it from the numbering system */ michael@0: NULL, /* seven digit - get it from the numbering system */ michael@0: NULL, /* eight digit - get it from the numbering system */ michael@0: NULL, /* nine digit - get it from the numbering system */ michael@0: }; michael@0: michael@0: static const char *gLatn = "latn"; michael@0: static const char *gSymbols = "symbols"; michael@0: const char *nsName; michael@0: const UChar *sym = NULL; michael@0: int32_t len = 0; michael@0: michael@0: *validLocale = *actualLocale = 0; michael@0: currPattern = NULL; michael@0: if (U_FAILURE(status)) michael@0: return; michael@0: michael@0: const char* locStr = loc.getName(); michael@0: LocalUResourceBundlePointer resource(ures_open(NULL, locStr, &status)); michael@0: LocalUResourceBundlePointer numberElementsRes( michael@0: ures_getByKeyWithFallback(resource.getAlias(), gNumberElements, NULL, &status)); michael@0: michael@0: if (U_FAILURE(status)) { michael@0: if ( useLastResortData ) { michael@0: status = U_USING_DEFAULT_WARNING; michael@0: initialize(); michael@0: } michael@0: return; michael@0: } michael@0: michael@0: // First initialize all the symbols to the fallbacks for anything we can't find michael@0: initialize(); michael@0: michael@0: // michael@0: // Next get the numbering system for this locale and set zero digit michael@0: // and the digit string based on the numbering system for the locale michael@0: // michael@0: michael@0: LocalPointer ns(NumberingSystem::createInstance(loc, status)); michael@0: if (U_SUCCESS(status) && ns->getRadix() == 10 && !ns->isAlgorithmic()) { michael@0: nsName = ns->getName(); michael@0: UnicodeString digitString(ns->getDescription()); michael@0: int32_t digitIndex = 0; michael@0: UChar32 digit = digitString.char32At(0); michael@0: fSymbols[kZeroDigitSymbol].setTo(digit); michael@0: for (int32_t i = kOneDigitSymbol; i <= kNineDigitSymbol; ++i) { michael@0: digitIndex += U16_LENGTH(digit); michael@0: digit = digitString.char32At(digitIndex); michael@0: fSymbols[i].setTo(digit); michael@0: } michael@0: } else { michael@0: nsName = gLatn; michael@0: } michael@0: michael@0: UBool isLatn = !uprv_strcmp(nsName,gLatn); michael@0: michael@0: UErrorCode nlStatus = U_ZERO_ERROR; michael@0: LocalUResourceBundlePointer nonLatnSymbols; michael@0: if ( !isLatn ) { michael@0: nonLatnSymbols.adoptInstead( michael@0: ures_getByKeyWithFallback(numberElementsRes.getAlias(), nsName, NULL, &nlStatus)); michael@0: ures_getByKeyWithFallback(nonLatnSymbols.getAlias(), gSymbols, nonLatnSymbols.getAlias(), &nlStatus); michael@0: } michael@0: michael@0: LocalUResourceBundlePointer latnSymbols( michael@0: ures_getByKeyWithFallback(numberElementsRes.getAlias(), gLatn, NULL, &status)); michael@0: ures_getByKeyWithFallback(latnSymbols.getAlias(), gSymbols, latnSymbols.getAlias(), &status); michael@0: michael@0: UBool kMonetaryDecimalSet = FALSE; michael@0: UBool kMonetaryGroupingSet = FALSE; michael@0: for(int32_t i = 0; i 0) { michael@0: char cc[4]={0}; michael@0: u_UCharsToChars(ucc, cc, uccLen); michael@0: /* An explicit currency was requested */ michael@0: LocalUResourceBundlePointer currencyResource(ures_open(U_ICUDATA_CURR, locStr, &localStatus)); michael@0: LocalUResourceBundlePointer currency( michael@0: ures_getByKeyWithFallback(currencyResource.getAlias(), "Currencies", NULL, &localStatus)); michael@0: ures_getByKeyWithFallback(currency.getAlias(), cc, currency.getAlias(), &localStatus); michael@0: if(U_SUCCESS(localStatus) && ures_getSize(currency.getAlias())>2) { // the length is 3 if more data is present michael@0: ures_getByIndex(currency.getAlias(), 2, currency.getAlias(), &localStatus); michael@0: int32_t currPatternLen = 0; michael@0: currPattern = michael@0: ures_getStringByIndex(currency.getAlias(), (int32_t)0, &currPatternLen, &localStatus); michael@0: UnicodeString decimalSep = michael@0: ures_getUnicodeStringByIndex(currency.getAlias(), (int32_t)1, &localStatus); michael@0: UnicodeString groupingSep = michael@0: ures_getUnicodeStringByIndex(currency.getAlias(), (int32_t)2, &localStatus); michael@0: if(U_SUCCESS(localStatus)){ michael@0: fSymbols[kMonetaryGroupingSeparatorSymbol] = groupingSep; michael@0: fSymbols[kMonetarySeparatorSymbol] = decimalSep; michael@0: //pattern.setTo(TRUE, currPattern, currPatternLen); michael@0: status = localStatus; michael@0: } michael@0: } michael@0: /* else An explicit currency was requested and is unknown or locale data is malformed. */ michael@0: /* ucurr_* API will get the correct value later on. */ michael@0: } michael@0: // else ignore the error if no currency michael@0: michael@0: // Currency Spacing. michael@0: localStatus = U_ZERO_ERROR; michael@0: LocalUResourceBundlePointer currencyResource(ures_open(U_ICUDATA_CURR, locStr, &localStatus)); michael@0: LocalUResourceBundlePointer currencySpcRes( michael@0: ures_getByKeyWithFallback(currencyResource.getAlias(), michael@0: gCurrencySpacingTag, NULL, &localStatus)); michael@0: michael@0: if (localStatus == U_USING_FALLBACK_WARNING || U_SUCCESS(localStatus)) { michael@0: const char* keywords[UNUM_CURRENCY_SPACING_COUNT] = { michael@0: gCurrencyMatchTag, gCurrencySudMatchTag, gCurrencyInsertBtnTag michael@0: }; michael@0: localStatus = U_ZERO_ERROR; michael@0: LocalUResourceBundlePointer dataRes( michael@0: ures_getByKeyWithFallback(currencySpcRes.getAlias(), michael@0: gBeforeCurrencyTag, NULL, &localStatus)); michael@0: if (localStatus == U_USING_FALLBACK_WARNING || U_SUCCESS(localStatus)) { michael@0: localStatus = U_ZERO_ERROR; michael@0: for (int32_t i = 0; i < UNUM_CURRENCY_SPACING_COUNT; i++) { michael@0: currencySpcBeforeSym[i] = michael@0: ures_getUnicodeStringByKey(dataRes.getAlias(), keywords[i], &localStatus); michael@0: } michael@0: } michael@0: dataRes.adoptInstead( michael@0: ures_getByKeyWithFallback(currencySpcRes.getAlias(), michael@0: gAfterCurrencyTag, NULL, &localStatus)); michael@0: if (localStatus == U_USING_FALLBACK_WARNING || U_SUCCESS(localStatus)) { michael@0: localStatus = U_ZERO_ERROR; michael@0: for (int32_t i = 0; i < UNUM_CURRENCY_SPACING_COUNT; i++) { michael@0: currencySpcAfterSym[i] = michael@0: ures_getUnicodeStringByKey(dataRes.getAlias(), keywords[i], &localStatus); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: DecimalFormatSymbols::initialize() { michael@0: /* michael@0: * These strings used to be in static arrays, but the HP/UX aCC compiler michael@0: * cannot initialize a static array with class constructors. michael@0: * markus 2000may25 michael@0: */ michael@0: fSymbols[kDecimalSeparatorSymbol] = (UChar)0x2e; // '.' decimal separator michael@0: fSymbols[kGroupingSeparatorSymbol].remove(); // group (thousands) separator michael@0: fSymbols[kPatternSeparatorSymbol] = (UChar)0x3b; // ';' pattern separator michael@0: fSymbols[kPercentSymbol] = (UChar)0x25; // '%' percent sign michael@0: fSymbols[kZeroDigitSymbol] = (UChar)0x30; // '0' native 0 digit michael@0: fSymbols[kOneDigitSymbol] = (UChar)0x31; // '1' native 1 digit michael@0: fSymbols[kTwoDigitSymbol] = (UChar)0x32; // '2' native 2 digit michael@0: fSymbols[kThreeDigitSymbol] = (UChar)0x33; // '3' native 3 digit michael@0: fSymbols[kFourDigitSymbol] = (UChar)0x34; // '4' native 4 digit michael@0: fSymbols[kFiveDigitSymbol] = (UChar)0x35; // '5' native 5 digit michael@0: fSymbols[kSixDigitSymbol] = (UChar)0x36; // '6' native 6 digit michael@0: fSymbols[kSevenDigitSymbol] = (UChar)0x37; // '7' native 7 digit michael@0: fSymbols[kEightDigitSymbol] = (UChar)0x38; // '8' native 8 digit michael@0: fSymbols[kNineDigitSymbol] = (UChar)0x39; // '9' native 9 digit michael@0: fSymbols[kDigitSymbol] = (UChar)0x23; // '#' pattern digit michael@0: fSymbols[kPlusSignSymbol] = (UChar)0x002b; // '+' plus sign michael@0: fSymbols[kMinusSignSymbol] = (UChar)0x2d; // '-' minus sign michael@0: fSymbols[kCurrencySymbol] = (UChar)0xa4; // 'OX' currency symbol michael@0: fSymbols[kIntlCurrencySymbol].setTo(TRUE, INTL_CURRENCY_SYMBOL_STR, 2); michael@0: fSymbols[kMonetarySeparatorSymbol] = (UChar)0x2e; // '.' monetary decimal separator michael@0: fSymbols[kExponentialSymbol] = (UChar)0x45; // 'E' exponential michael@0: fSymbols[kPerMillSymbol] = (UChar)0x2030; // '%o' per mill michael@0: fSymbols[kPadEscapeSymbol] = (UChar)0x2a; // '*' pad escape symbol michael@0: fSymbols[kInfinitySymbol] = (UChar)0x221e; // 'oo' infinite michael@0: fSymbols[kNaNSymbol] = (UChar)0xfffd; // SUB NaN michael@0: fSymbols[kSignificantDigitSymbol] = (UChar)0x0040; // '@' significant digit michael@0: fSymbols[kMonetaryGroupingSeparatorSymbol].remove(); // michael@0: } michael@0: michael@0: Locale michael@0: DecimalFormatSymbols::getLocale(ULocDataLocaleType type, UErrorCode& status) const { michael@0: U_LOCALE_BASED(locBased, *this); michael@0: return locBased.getLocale(type, status); michael@0: } michael@0: michael@0: const UnicodeString& michael@0: DecimalFormatSymbols::getPatternForCurrencySpacing(UCurrencySpacing type, michael@0: UBool beforeCurrency, michael@0: UErrorCode& status) const { michael@0: if (U_FAILURE(status)) { michael@0: return fNoSymbol; // always empty. michael@0: } michael@0: if (beforeCurrency) { michael@0: return currencySpcBeforeSym[(int32_t)type]; michael@0: } else { michael@0: return currencySpcAfterSym[(int32_t)type]; michael@0: } michael@0: } michael@0: michael@0: void michael@0: DecimalFormatSymbols::setPatternForCurrencySpacing(UCurrencySpacing type, michael@0: UBool beforeCurrency, michael@0: const UnicodeString& pattern) { michael@0: if (beforeCurrency) { michael@0: currencySpcBeforeSym[(int32_t)type] = pattern; michael@0: } else { michael@0: currencySpcAfterSym[(int32_t)type] = pattern; michael@0: } michael@0: } michael@0: U_NAMESPACE_END michael@0: michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */ michael@0: michael@0: //eof