]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/dcfmtsym.cpp
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / i18n / dcfmtsym.cpp
1 /*
2 *******************************************************************************
3 * Copyright (C) 1997-2003, International Business Machines Corporation and *
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.
16 * 07/20/98 stephen Slightly modified initialization of monetarySeparator
17 ********************************************************************************
18 */
19
20 #include "unicode/utypes.h"
21
22 #if !UCONFIG_NO_FORMATTING
23
24 #include "unicode/dcfmtsym.h"
25 #include "unicode/resbund.h"
26 #include "unicode/decimfmt.h"
27 #include "unicode/ucurr.h"
28 #include "unicode/choicfmt.h"
29
30 // *****************************************************************************
31 // class DecimalFormatSymbols
32 // *****************************************************************************
33
34 U_NAMESPACE_BEGIN
35
36 const char DecimalFormatSymbols::fgClassID=0;
37
38 const char DecimalFormatSymbols::fgNumberElements[] = "NumberElements";
39
40 static const UChar INTL_CURRENCY_SYMBOL_STR[] = {0xa4, 0xa4, 0};
41
42 // -------------------------------------
43 // Initializes this with the decimal format symbols in the default locale.
44
45 DecimalFormatSymbols::DecimalFormatSymbols(UErrorCode& status)
46 : UObject()
47 {
48 initialize(Locale::getDefault(), status, TRUE);
49 }
50
51 // -------------------------------------
52 // Initializes this with the decimal format symbols in the desired locale.
53
54 DecimalFormatSymbols::DecimalFormatSymbols(const Locale& loc, UErrorCode& status)
55 : UObject()
56 {
57 initialize(loc, status);
58 }
59
60 // -------------------------------------
61
62 DecimalFormatSymbols::~DecimalFormatSymbols()
63 {
64 }
65
66 // -------------------------------------
67 // copy constructor
68
69 DecimalFormatSymbols::DecimalFormatSymbols(const DecimalFormatSymbols &source)
70 : UObject(source)
71 {
72 int i;
73 for(i = 0; i < (int)kFormatSymbolCount; ++i) {
74 // fastCopyFrom is safe, see docs on fSymbols
75 fSymbols[(ENumberFormatSymbol)i].fastCopyFrom(source.fSymbols[(ENumberFormatSymbol)i]);
76 }
77 }
78
79 // -------------------------------------
80 // assignment operator
81
82 DecimalFormatSymbols&
83 DecimalFormatSymbols::operator=(const DecimalFormatSymbols& rhs)
84 {
85 if (this != &rhs)
86 {
87 int i;
88 for(i = 0; i < (int)kFormatSymbolCount; ++i) {
89 // fastCopyFrom is safe, see docs on fSymbols
90 fSymbols[(ENumberFormatSymbol)i].fastCopyFrom(rhs.fSymbols[(ENumberFormatSymbol)i]);
91 }
92 }
93 return *this;
94 }
95
96 // -------------------------------------
97
98 UBool
99 DecimalFormatSymbols::operator==(const DecimalFormatSymbols& that) const
100 {
101 if (this == &that) {
102 return TRUE;
103 }
104
105 int i;
106 for(i = 0; i < (int)kFormatSymbolCount; ++i) {
107 if(fSymbols[(ENumberFormatSymbol)i] != that.fSymbols[(ENumberFormatSymbol)i]) {
108 return FALSE;
109 }
110 }
111 return TRUE;
112 }
113
114 // -------------------------------------
115
116 void
117 DecimalFormatSymbols::initialize(const Locale& loc, UErrorCode& status,
118 UBool useLastResortData)
119 {
120 if (U_FAILURE(status)) return;
121
122 this->locale = loc;
123
124 ResourceBundle resource((char *)0, loc, status);
125 if (U_FAILURE(status))
126 {
127 // Initializes with last resort data if necessary.
128 if (useLastResortData)
129 {
130 status = U_USING_FALLBACK_WARNING;
131 initialize();
132 }
133 return;
134 }
135
136 // Gets the number element array.
137 int32_t i = 0;
138 ResourceBundle numberElementsRes = resource.get(fgNumberElements, status);
139 int32_t numberElementsLength = numberElementsRes.getSize();
140
141 // If the array size is too small, something is wrong with the resource
142 // bundle, returns the failure error code.
143 if (numberElementsLength < 11) {
144 status = U_INVALID_FORMAT_ERROR;
145 return;
146 }
147
148 UnicodeString numberElements[kFormatSymbolCount];
149 for(i = 0; i<numberElementsLength; i++) {
150 numberElements[i].fastCopyFrom(numberElementsRes.getStringEx(i, status));
151 }
152
153 if (U_FAILURE(status)) {
154 return;
155 }
156
157 initialize(numberElements, numberElementsLength);
158
159
160 // Obtain currency data from the currency API. This is strictly
161 // for backward compatibility; we don't use DecimalFormatSymbols
162 // for currency data anymore.
163 UErrorCode ec = U_ZERO_ERROR; // don't propagate failures out
164 const char* l = loc.getName();
165 const UChar* curriso = ucurr_forLocale(l, &ec);
166 UBool isChoiceFormat;
167 int32_t len;
168 const UChar* currname = ucurr_getName(curriso, l, UCURR_SYMBOL_NAME,
169 &isChoiceFormat, &len, &ec);
170 if (U_SUCCESS(ec)) {
171 fSymbols[kIntlCurrencySymbol] = curriso;
172
173 // If this is a ChoiceFormat currency, then format an
174 // arbitrary value; pick something != 1; more common.
175 fSymbols[kCurrencySymbol].truncate(0);
176 if (isChoiceFormat) {
177 ChoiceFormat f(currname, ec);
178 if (U_SUCCESS(ec)) {
179 f.format(2.0, fSymbols[kCurrencySymbol]);
180 } else {
181 fSymbols[kCurrencySymbol] = fSymbols[kIntlCurrencySymbol];
182 }
183 } else {
184 fSymbols[kCurrencySymbol] = UnicodeString(currname);
185 }
186 }
187 /* else use the default values. */
188 }
189
190 // Initializes the DecimalFormatSymbol instance with the data obtained
191 // from ResourceBundle in the desired locale.
192
193 void
194 DecimalFormatSymbols::initialize(const UnicodeString* numberElements, int32_t numberElementsLength)
195 {
196 fSymbols[kDecimalSeparatorSymbol].fastCopyFrom(numberElements[0]);
197 fSymbols[kGroupingSeparatorSymbol].fastCopyFrom(numberElements[1]);
198 fSymbols[kPatternSeparatorSymbol].fastCopyFrom(numberElements[2]);
199 fSymbols[kPercentSymbol].fastCopyFrom(numberElements[3]);
200 fSymbols[kZeroDigitSymbol].fastCopyFrom(numberElements[4]);
201 fSymbols[kDigitSymbol].fastCopyFrom(numberElements[5]);
202 fSymbols[kMinusSignSymbol].fastCopyFrom(numberElements[6]);
203 fSymbols[kPlusSignSymbol] = (UChar)0x002b; // '+' Hard coded for now; get from resource later
204 fSymbols[kExponentialSymbol].fastCopyFrom(numberElements[7]);
205 fSymbols[kPerMillSymbol].fastCopyFrom(numberElements[8]);
206 fSymbols[kPadEscapeSymbol] = (UChar)0x002a; // '*' Hard coded for now; get from resource later
207 fSymbols[kInfinitySymbol].fastCopyFrom(numberElements[9]);
208 fSymbols[kNaNSymbol].fastCopyFrom(numberElements[10]);
209
210 // If there is a currency decimal, use it.
211 fSymbols[kMonetarySeparatorSymbol].fastCopyFrom(numberElements[numberElementsLength >= 12 ? 11 : 0]);
212
213 // Default values until it's set later on.
214 fSymbols[kCurrencySymbol] = (UChar)0xa4; // 'OX' currency symbol
215 fSymbols[kIntlCurrencySymbol] = INTL_CURRENCY_SYMBOL_STR;
216 }
217
218 // initialize with default values
219 void
220 DecimalFormatSymbols::initialize() {
221 /*
222 * These strings used to be in static arrays, but the HP/UX aCC compiler
223 * cannot initialize a static array with class constructors.
224 * markus 2000may25
225 */
226 fSymbols[kDecimalSeparatorSymbol] = (UChar)0x2e; // '.' decimal separator
227 fSymbols[kGroupingSeparatorSymbol].remove(); // group (thousands) separator
228 fSymbols[kPatternSeparatorSymbol] = (UChar)0x3b; // ';' pattern separator
229 fSymbols[kPercentSymbol] = (UChar)0x25; // '%' percent sign
230 fSymbols[kZeroDigitSymbol] = (UChar)0x30; // '0' native 0 digit
231 fSymbols[kDigitSymbol] = (UChar)0x23; // '#' pattern digit
232 fSymbols[kMinusSignSymbol] = (UChar)0x2d; // '-' minus sign
233 fSymbols[kPlusSignSymbol] = (UChar)0x002b; // '+' plus sign
234 fSymbols[kCurrencySymbol] = (UChar)0xa4; // 'OX' currency symbol
235 fSymbols[kIntlCurrencySymbol] = INTL_CURRENCY_SYMBOL_STR;
236 fSymbols[kMonetarySeparatorSymbol] = (UChar)0x2e; // '.' monetary decimal separator
237 fSymbols[kExponentialSymbol] = (UChar)0x45; // 'E' exponential
238 fSymbols[kPerMillSymbol] = (UChar)0x2030; // '%o' per mill
239 fSymbols[kPadEscapeSymbol] = (UChar)0x2a; // '*' pad escape symbol
240 fSymbols[kInfinitySymbol] = (UChar)0x221e; // 'oo' infinite
241 fSymbols[kNaNSymbol] = (UChar)0xfffd; // SUB NaN
242 }
243
244 U_NAMESPACE_END
245
246 #endif /* #if !UCONFIG_NO_FORMATTING */
247
248 //eof