]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/winnmfmt.cpp
ICU-8.11.4.tar.gz
[apple/icu.git] / icuSources / i18n / winnmfmt.cpp
1 /*
2 ********************************************************************************
3 * Copyright (C) 2005-2006, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ********************************************************************************
6 *
7 * File WINNMFMT.CPP
8 *
9 ********************************************************************************
10 */
11
12 #include "unicode/utypes.h"
13
14 #ifdef U_WINDOWS
15
16 #if !UCONFIG_NO_FORMATTING
17
18 #include "winnmfmt.h"
19
20 #include "unicode/format.h"
21 #include "unicode/numfmt.h"
22 #include "unicode/locid.h"
23 #include "unicode/ustring.h"
24
25 #include "cmemory.h"
26 #include "uassert.h"
27 #include "locmap.h"
28
29 # define WIN32_LEAN_AND_MEAN
30 # define VC_EXTRALEAN
31 # define NOUSER
32 # define NOSERVICE
33 # define NOIME
34 # define NOMCX
35 #include <windows.h>
36 #include <stdio.h>
37
38 U_NAMESPACE_BEGIN
39
40 union FormatInfo
41 {
42 NUMBERFMTW number;
43 CURRENCYFMTW currency;
44 };
45
46 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Win32NumberFormat)
47
48 #define NEW_ARRAY(type,count) (type *) uprv_malloc((count) * sizeof(type))
49 #define DELETE_ARRAY(array) uprv_free((void *) (array))
50
51 #define STACK_BUFFER_SIZE 32
52
53 /*
54 * Turns a string of the form "3;2;0" into the grouping UINT
55 * needed for NUMBERFMT and CURRENCYFMT. If the string does not
56 * end in ";0" then the return value should be multiplied by 10.
57 * (e.g. "3" => 30, "3;2" => 320)
58 */
59 static UINT getGrouping(const char *grouping)
60 {
61 UINT g = 0;
62 const char *s;
63
64 for (s = grouping; *s != '\0'; s += 1) {
65 if (*s > '0' && *s < '9') {
66 g = g * 10 + (*s - '0');
67 } else if (*s != ';') {
68 break;
69 }
70 }
71
72 if (*s != '0') {
73 g *= 10;
74 }
75
76 return g;
77 }
78
79 static void getNumberFormat(NUMBERFMTW *fmt, int32_t lcid)
80 {
81 char buf[10];
82
83 GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_IDIGITS, (LPWSTR) &fmt->NumDigits, sizeof(UINT));
84 GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_ILZERO, (LPWSTR) &fmt->LeadingZero, sizeof(UINT));
85
86 GetLocaleInfoA(lcid, LOCALE_SGROUPING, buf, 10);
87 fmt->Grouping = getGrouping(buf);
88
89 fmt->lpDecimalSep = NEW_ARRAY(UChar, 6);
90 GetLocaleInfoW(lcid, LOCALE_SDECIMAL, fmt->lpDecimalSep, 6);
91
92 fmt->lpThousandSep = NEW_ARRAY(UChar, 6);
93 GetLocaleInfoW(lcid, LOCALE_STHOUSAND, fmt->lpThousandSep, 6);
94
95 GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_INEGNUMBER, (LPWSTR) &fmt->NegativeOrder, sizeof(UINT));
96 }
97
98 static void freeNumberFormat(NUMBERFMTW *fmt)
99 {
100 DELETE_ARRAY(fmt->lpThousandSep);
101 DELETE_ARRAY(fmt->lpDecimalSep);
102 }
103
104 static void getCurrencyFormat(CURRENCYFMTW *fmt, int32_t lcid)
105 {
106 char buf[10];
107
108 GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_ICURRDIGITS, (LPWSTR) &fmt->NumDigits, sizeof(UINT));
109 GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_ILZERO, (LPWSTR) &fmt->LeadingZero, sizeof(UINT));
110
111 GetLocaleInfoA(lcid, LOCALE_SMONGROUPING, buf, sizeof(buf));
112 fmt->Grouping = getGrouping(buf);
113
114 fmt->lpDecimalSep = NEW_ARRAY(UChar, 6);
115 GetLocaleInfoW(lcid, LOCALE_SMONDECIMALSEP, fmt->lpDecimalSep, 6);
116
117 fmt->lpThousandSep = NEW_ARRAY(UChar, 6);
118 GetLocaleInfoW(lcid, LOCALE_SMONTHOUSANDSEP, fmt->lpThousandSep, 6);
119
120 GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_INEGCURR, (LPWSTR) &fmt->NegativeOrder, sizeof(UINT));
121 GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_ICURRENCY, (LPWSTR) &fmt->PositiveOrder, sizeof(UINT));
122
123 fmt->lpCurrencySymbol = NEW_ARRAY(UChar, 8);
124 GetLocaleInfoW(lcid, LOCALE_SCURRENCY, (LPWSTR) fmt->lpCurrencySymbol, 8);
125 }
126
127 static void freeCurrencyFormat(CURRENCYFMTW *fmt)
128 {
129 DELETE_ARRAY(fmt->lpCurrencySymbol);
130 DELETE_ARRAY(fmt->lpThousandSep);
131 DELETE_ARRAY(fmt->lpDecimalSep);
132 }
133
134 // TODO: keep locale too?
135 Win32NumberFormat::Win32NumberFormat(const Locale &locale, UBool currency, UErrorCode &status)
136 : NumberFormat(), fCurrency(currency), fFractionDigitsSet(FALSE)
137 {
138 if (!U_FAILURE(status)) {
139 fLCID = locale.getLCID();
140
141 fFormatInfo = (FormatInfo*)uprv_malloc(sizeof(FormatInfo));
142
143 if (fCurrency) {
144 getCurrencyFormat(&fFormatInfo->currency, fLCID);
145 } else {
146 getNumberFormat(&fFormatInfo->number, fLCID);
147 }
148 }
149 }
150
151 Win32NumberFormat::Win32NumberFormat(const Win32NumberFormat &other)
152 : NumberFormat(other)
153 {
154 *this = other;
155 }
156
157 Win32NumberFormat::~Win32NumberFormat()
158 {
159 if (fCurrency) {
160 freeCurrencyFormat(&fFormatInfo->currency);
161 } else {
162 freeNumberFormat(&fFormatInfo->number);
163 }
164
165 uprv_free(fFormatInfo);
166 }
167
168 Win32NumberFormat &Win32NumberFormat::operator=(const Win32NumberFormat &other)
169 {
170 NumberFormat::operator=(other);
171
172 this->fCurrency = other.fCurrency;
173 this->fLCID = other.fLCID;
174 this->fFractionDigitsSet = other.fFractionDigitsSet;
175
176 if (fCurrency) {
177 freeCurrencyFormat(&fFormatInfo->currency);
178 getCurrencyFormat(&fFormatInfo->currency, fLCID);
179 } else {
180 freeNumberFormat(&fFormatInfo->number);
181 getNumberFormat(&fFormatInfo->number, fLCID);
182 }
183
184 return *this;
185 }
186
187 Format *Win32NumberFormat::clone(void) const
188 {
189 return new Win32NumberFormat(*this);
190 }
191
192 UnicodeString& Win32NumberFormat::format(double number, UnicodeString& appendTo, FieldPosition& pos) const
193 {
194 return format(getMaximumFractionDigits(), appendTo, L"%.16f", number);
195 }
196
197 UnicodeString& Win32NumberFormat::format(int32_t number, UnicodeString& appendTo, FieldPosition& pos) const
198 {
199 return format(getMinimumFractionDigits(), appendTo, L"%I32d", number);
200 }
201
202 UnicodeString& Win32NumberFormat::format(int64_t number, UnicodeString& appendTo, FieldPosition& pos) const
203 {
204 return format(getMinimumFractionDigits(), appendTo, L"%I64d", number);
205 }
206
207 // TODO: cache Locale and NumberFormat? Could keep locale passed to constructor...
208 void Win32NumberFormat::parse(const UnicodeString& text, Formattable& result, ParsePosition& parsePosition) const
209 {
210 UErrorCode status = U_ZERO_ERROR;
211 Locale loc(uprv_convertToPosix(fLCID, &status));
212 NumberFormat *nf = fCurrency? NumberFormat::createCurrencyInstance(loc, status) : NumberFormat::createInstance(loc, status);
213
214 nf->parse(text, result, parsePosition);
215 delete nf;
216 }
217 void Win32NumberFormat::setMaximumFractionDigits(int32_t newValue)
218 {
219 fFractionDigitsSet = TRUE;
220 NumberFormat::setMaximumFractionDigits(newValue);
221 }
222
223 void Win32NumberFormat::setMinimumFractionDigits(int32_t newValue)
224 {
225 fFractionDigitsSet = TRUE;
226 NumberFormat::setMinimumFractionDigits(newValue);
227 }
228
229 UnicodeString &Win32NumberFormat::format(int32_t numDigits, UnicodeString &appendTo, wchar_t *fmt, ...) const
230 {
231 wchar_t nStackBuffer[STACK_BUFFER_SIZE];
232 wchar_t *nBuffer = nStackBuffer;
233 va_list args;
234 int result;
235
236 nBuffer[0] = 0x0000;
237
238 /* Due to the arguments causing a result to be <= 23 characters (+2 for NULL and minus),
239 we don't need to reallocate the buffer. */
240 va_start(args, fmt);
241 result = _vsnwprintf(nBuffer, STACK_BUFFER_SIZE, fmt, args);
242 va_end(args);
243
244 /* Just to make sure of the above statement, we add this assert */
245 U_ASSERT(result >=0);
246 // The following code is not used because _vscwprintf isn't available on MinGW at the moment.
247 /*if (result < 0) {
248 int newLength;
249
250 va_start(args, fmt);
251 newLength = _vscwprintf(fmt, args);
252 va_end(args);
253
254 nBuffer = NEW_ARRAY(UChar, newLength + 1);
255
256 va_start(args, fmt);
257 result = _vsnwprintf(nBuffer, newLength + 1, fmt, args);
258 va_end(args);
259 }*/
260
261 // vswprintf is sensitive to the locale set by setlocale. For some locales
262 // it doesn't use "." as the decimal separator, which is what GetNumberFormatW
263 // and GetCurrencyFormatW both expect to see.
264 //
265 // To fix this, we scan over the string and replace the first non-digits, except
266 // for a leading "-", with a "."
267 //
268 // Note: (nBuffer[0] == L'-') will evaluate to 1 if there is a leading '-' in the
269 // number, and 0 otherwise.
270 for (wchar_t *p = &nBuffer[nBuffer[0] == L'-']; *p != L'\0'; p += 1) {
271 if (*p < L'0' || *p > L'9') {
272 *p = L'.';
273 break;
274 }
275 }
276
277 UChar stackBuffer[STACK_BUFFER_SIZE];
278 UChar *buffer = stackBuffer;
279 FormatInfo formatInfo;
280
281 formatInfo = *fFormatInfo;
282 buffer[0] = 0x0000;
283
284 if (fCurrency) {
285 if (fFractionDigitsSet) {
286 formatInfo.currency.NumDigits = (UINT) numDigits;
287 }
288
289 if (!isGroupingUsed()) {
290 formatInfo.currency.Grouping = 0;
291 }
292
293 result = GetCurrencyFormatW(fLCID, 0, nBuffer, &formatInfo.currency, buffer, STACK_BUFFER_SIZE);
294
295 if (result == 0) {
296 DWORD lastError = GetLastError();
297
298 if (lastError == ERROR_INSUFFICIENT_BUFFER) {
299 int newLength = GetCurrencyFormatW(fLCID, 0, nBuffer, &formatInfo.currency, NULL, 0);
300
301 buffer = NEW_ARRAY(UChar, newLength);
302 buffer[0] = 0x0000;
303 GetCurrencyFormatW(fLCID, 0, nBuffer, &formatInfo.currency, buffer, newLength);
304 }
305 }
306 } else {
307 if (fFractionDigitsSet) {
308 formatInfo.number.NumDigits = (UINT) numDigits;
309 }
310
311 if (!isGroupingUsed()) {
312 formatInfo.number.Grouping = 0;
313 }
314
315 result = GetNumberFormatW(fLCID, 0, nBuffer, &formatInfo.number, buffer, STACK_BUFFER_SIZE);
316
317 if (result == 0) {
318 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
319 int newLength = GetNumberFormatW(fLCID, 0, nBuffer, &formatInfo.number, NULL, 0);
320
321 buffer = NEW_ARRAY(UChar, newLength);
322 buffer[0] = 0x0000;
323 GetNumberFormatW(fLCID, 0, nBuffer, &formatInfo.number, buffer, newLength);
324 }
325 }
326 }
327
328 appendTo.append(buffer, (int32_t) wcslen(buffer));
329
330 if (buffer != stackBuffer) {
331 DELETE_ARRAY(buffer);
332 }
333
334 /*if (nBuffer != nStackBuffer) {
335 DELETE_ARRAY(nBuffer);
336 }*/
337
338 return appendTo;
339 }
340
341 U_NAMESPACE_END
342
343 #endif /* #if !UCONFIG_NO_FORMATTING */
344
345 #endif // #ifdef U_WINDOWS