From 7c982543854c48bafca5874300326cd2285c0ed6 Mon Sep 17 00:00:00 2001 From: Claire Ho Date: Tue, 24 Feb 2009 00:50:46 +0000 Subject: [PATCH] ICU-6736 merge APIs for currencySpacing from branch to trunk. X-SVN-Rev: 25472 --- icu4c/source/i18n/dcfmtsym.cpp | 108 +++++++++++++++++++++++---- icu4c/source/i18n/unicode/dcfmtsym.h | 61 +++++++++++++-- 2 files changed, 148 insertions(+), 21 deletions(-) diff --git a/icu4c/source/i18n/dcfmtsym.cpp b/icu4c/source/i18n/dcfmtsym.cpp index 525f104b65..a4ff269106 100644 --- a/icu4c/source/i18n/dcfmtsym.cpp +++ b/icu4c/source/i18n/dcfmtsym.cpp @@ -16,7 +16,7 @@ * 07/20/98 stephen Slightly modified initialization of monetarySeparator ******************************************************************************** */ - + #include "unicode/utypes.h" #if !UCONFIG_NO_FORMATTING @@ -35,35 +35,39 @@ // ***************************************************************************** // class DecimalFormatSymbols // ***************************************************************************** - + U_NAMESPACE_BEGIN UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DecimalFormatSymbols) static const char gNumberElements[] = "NumberElements"; +static const char gCurrencyFormatsTag[] = "currencyFormats"; +static const char gCurrencySpacingTag[] = "currencySpacing"; +static const char gBeforeCurrencyTag[] = "beforeCurrency"; +static const char gAfterCurrencyTag[] = "afterCurrency"; static const UChar INTL_CURRENCY_SYMBOL_STR[] = {0xa4, 0xa4, 0}; // ------------------------------------- // Initializes this with the decimal format symbols in the default locale. - + DecimalFormatSymbols::DecimalFormatSymbols(UErrorCode& status) : UObject(), locale() { initialize(locale, status, TRUE); } - + // ------------------------------------- // Initializes this with the decimal format symbols in the desired locale. - + DecimalFormatSymbols::DecimalFormatSymbols(const Locale& loc, UErrorCode& status) : UObject(), locale(loc) { initialize(locale, status); } - + // ------------------------------------- DecimalFormatSymbols::~DecimalFormatSymbols() @@ -90,6 +94,12 @@ DecimalFormatSymbols::operator=(const DecimalFormatSymbols& rhs) // fastCopyFrom is safe, see docs on fSymbols fSymbols[(ENumberFormatSymbol)i].fastCopyFrom(rhs.fSymbols[(ENumberFormatSymbol)i]); } + /* + for(int32_t i = 0; i < (int32_t)kCurrencySpacingCount; ++i) { + currencySpcBeforeSym[i].fastCopyFrom(rhs.currencySpcBeforeSym[i]); + currencySpcAfterSym[i].fastCopyFrom(rhs.currencySpcAfterSym[i]); + } + */ locale = rhs.locale; uprv_strcpy(validLocale, rhs.validLocale); uprv_strcpy(actualLocale, rhs.actualLocale); @@ -110,13 +120,23 @@ DecimalFormatSymbols::operator==(const DecimalFormatSymbols& that) const return FALSE; } } + /* + for(int32_t i = 0; i < (int32_t)kCurrencySpacingCount; ++i) { + if(currencySpcBeforeSym[i] != that.currencySpcBeforeSym[i]) { + return FALSE; + } + if(currencySpcAfterSym[i] != that.currencySpcAfterSym[i]) { + return FALSE; + } + } + */ return locale == that.locale && uprv_strcmp(validLocale, that.validLocale) == 0 && uprv_strcmp(actualLocale, that.actualLocale) == 0; } - + // ------------------------------------- - + void DecimalFormatSymbols::initialize(const Locale& loc, UErrorCode& status, UBool useLastResortData) @@ -125,7 +145,7 @@ DecimalFormatSymbols::initialize(const Locale& loc, UErrorCode& status, currPattern = NULL; if (U_FAILURE(status)) return; - + const char* locStr = loc.getName(); UResourceBundle *resource = ures_open((char *)0, locStr, &status); UResourceBundle *numberElementsRes = ures_getByKey(resource, gNumberElements, NULL, &status); @@ -172,7 +192,7 @@ DecimalFormatSymbols::initialize(const Locale& loc, UErrorCode& status, if (ns) { delete ns; } - + // Obtain currency data from the currency API. This is strictly // for backward compatibility; we don't use DecimalFormatSymbols // for currency data anymore. @@ -197,7 +217,7 @@ DecimalFormatSymbols::initialize(const Locale& loc, UErrorCode& status, ULOC_ACTUAL_LOCALE, &status)); } //load the currency data - UChar ucc[4]={0}; //Currency Codes are always 3 chars long + UChar ucc[4]={0}; //Currency Codes are always 3 chars long int32_t uccLen = 4; const char* locName = loc.getName(); UErrorCode localStatus = U_ZERO_ERROR; @@ -227,8 +247,46 @@ DecimalFormatSymbols::initialize(const Locale& loc, UErrorCode& status, } // else ignore the error if no currency } - ures_close(resource); ures_close(numberElementsRes); + + // Currency Spacing. + /* + UErrorCode localStatus = U_ZERO_ERROR; + UResourceBundle *currencyFormRes = ures_getByKeyWithFallback(resource, + gCurrencyFormatsTag, NULL, &localStatus); + if (localStatus == U_USING_FALLBACK_WARNING || U_SUCCESS(localStatus)) { + localStatus = U_ZERO_ERROR; + UResourceBundle *currencySpcRes = ures_getByKeyWithFallback(currencyFormRes, + gCurrencySpacingTag, NULL, &localStatus); + + if (localStatus == U_USING_FALLBACK_WARNING || U_SUCCESS(localStatus)) { + localStatus = U_ZERO_ERROR; + UResourceBundle *dataRes = ures_getByKeyWithFallback(currencySpcRes, + gBeforeCurrencyTag, NULL, &localStatus); + if (localStatus == U_USING_FALLBACK_WARNING || U_SUCCESS(localStatus)) { + localStatus = U_ZERO_ERROR; + for (int32_t i = 0; i < kCurrencySpacingCount; i++) { + currencySpcBeforeSym[i] = ures_getStringByIndex(dataRes, i, + NULL, &localStatus); + } + ures_close(dataRes); + } + dataRes = ures_getByKeyWithFallback(currencySpcRes, + gAfterCurrencyTag, NULL, &localStatus); + if (localStatus == U_USING_FALLBACK_WARNING || U_SUCCESS(localStatus)) { + localStatus = U_ZERO_ERROR; + for (int32_t i = 0; i < kCurrencySpacingCount; i++) { + currencySpcAfterSym[i] = ures_getStringByIndex(dataRes, i, + NULL, &localStatus); + } + ures_close(dataRes); + } + ures_close(currencySpcRes); + } + ures_close(currencyFormRes); + } + */ + ures_close(resource); } // Initializes the DecimalFormatSymbol instance with the data obtained @@ -296,12 +354,36 @@ DecimalFormatSymbols::initialize() { fSymbols[kSignificantDigitSymbol] = (UChar)0x0040; // '@' significant digit } -Locale +Locale DecimalFormatSymbols::getLocale(ULocDataLocaleType type, UErrorCode& status) const { U_LOCALE_BASED(locBased, *this); return locBased.getLocale(type, status); } +const UnicodeString& +DecimalFormatSymbols::getPatternForCurrencySpacing(ECurrencySpacing type, + UBool beforeCurrency, + UErrorCode& status) const { + if (U_FAILURE(status)) { + return fNoSymbol; // always empty. + } + if (beforeCurrency) { + return currencySpcBeforeSym[(int32_t)type]; + } else { + return currencySpcAfterSym[(int32_t)type]; + } +} + +void +DecimalFormatSymbols::setPatternForCurrencySpacing(ECurrencySpacing type, + UBool beforeCurrency, + const UnicodeString& pattern) { + if (beforeCurrency) { + currencySpcBeforeSym[(int32_t)type] = pattern; + } else { + currencySpcAfterSym[(int32_t)type] = pattern; + } +} U_NAMESPACE_END #endif /* #if !UCONFIG_NO_FORMATTING */ diff --git a/icu4c/source/i18n/unicode/dcfmtsym.h b/icu4c/source/i18n/unicode/dcfmtsym.h index bc5547d4cd..aa40ade5d3 100644 --- a/icu4c/source/i18n/unicode/dcfmtsym.h +++ b/icu4c/source/i18n/unicode/dcfmtsym.h @@ -1,19 +1,19 @@ /* ******************************************************************************** -* Copyright (C) 1997-2007, International Business Machines +* Copyright (C) 1997-2009, International Business Machines * Corporation and others. All Rights Reserved. ******************************************************************************** * * File DCFMTSYM.H * * Modification History: -* +* * Date Name Description * 02/19/97 aliu Converted from java. * 03/18/97 clhuang Updated per C++ implementation. * 03/27/97 helena Updated to pass the simple test after code review. * 08/26/97 aliu Added currency/intl currency symbol support. -* 07/22/98 stephen Changed to match C++ style +* 07/22/98 stephen Changed to match C++ style * currencySymbol -> fCurrencySymbol * Constants changed from CAPS to kCaps * 06/24/99 helena Integrated Alan's NF enhancements and Java2 bug fixes @@ -21,10 +21,10 @@ * functions. ******************************************************************************** */ - + #ifndef DCFMTSYM_H #define DCFMTSYM_H - + #include "unicode/utypes.h" #if !UCONFIG_NO_FORMATTING @@ -33,7 +33,7 @@ #include "unicode/locid.h" /** - * \file + * \file * \brief C++ API: Symbols for formatting numbers. */ @@ -121,7 +121,7 @@ public: /** Significant digit symbol * @stable ICU 3.0 */ kSignificantDigitSymbol, - /** The monetary grouping separator + /** The monetary grouping separator * @stable ICU 3.6 */ kMonetaryGroupingSeparatorSymbol, @@ -129,6 +129,17 @@ public: kFormatSymbolCount }; + /** + * Constants for specifying currency spacing + * @draft ICU 4.2 + */ + enum ECurrencySpacing { + kCurrencyMatch, + kSurroundingMatch, + kInsert, + kCurrencySpacingCount + }; + /** * Create a DecimalFormatSymbols object for the given locale. * @@ -222,6 +233,37 @@ public: */ Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const; + /** + * Get pattern string for 'CurrencySpacing' that can be applied to + * currency format. + * This API gets the CurrencySpacing data from ResourceBundle. The pattern can + * be empty if there is no data from current locale and its parent locales. + * + * @param type : kCurrencyMatch, kSurroundingMatch or kInsert. + * @param beforeCurrency : true if the pattern is for before currency symbol. + * false if the pattern is for after currency symbol. + * @param status: Input/output parameter, set to success or + * failure code upon return. + * @return pattern string for currencyMatch, surroundingMatch or spaceInsert. + * Return empty string if there is no data for this locale and its parent + * locales. + */ + const UnicodeString& getPatternForCurrencySpacing(ECurrencySpacing type, + UBool beforeCurrency, + UErrorCode& status) const; + /** + * Set pattern string for 'CurrencySpacing' that can be applied to + * currency format. + * + * @param type : kCurrencyMatch, kSurroundingMatch or kInsert. + * @param beforeCurrency : true if the pattern is for before currency symbol. + * false if the pattern is for after currency symbol. + * @param pattern : pattern string to override current setting. + */ + void setPatternForCurrencySpacing(ECurrencySpacing type, + UBool beforeCurrency, + const UnicodeString& pattern); + /** * ICU "poor man's RTTI", returns a UClassID for the actual class. * @@ -254,7 +296,7 @@ private: /** * Initialize the symbols from the given array of UnicodeStrings. * The array must be of the correct size. - * + * * @param numberElements the number format symbols * @param numberElementsLength length of numberElements */ @@ -316,6 +358,9 @@ private: char actualLocale[ULOC_FULLNAME_CAPACITY]; char validLocale[ULOC_FULLNAME_CAPACITY]; const UChar* currPattern; + + UnicodeString currencySpcBeforeSym[kCurrencySpacingCount]; + UnicodeString currencySpcAfterSym[kCurrencySpacingCount]; }; // -------------------------------------