1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 ********************************************************************************
5 * Copyright (C) 2005-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 ********************************************************************************
8 *
9 * File WINNMFMT.CPP
10 *
11 ********************************************************************************
12 */
13
14 #include "unicode/utypes.h"
15
16 #if U_PLATFORM_USES_ONLY_WIN32_API
17
18 #if !UCONFIG_NO_FORMATTING
19
20 #include "winnmfmt.h"
21
22 #include "unicode/format.h"
23 #include "unicode/numfmt.h"
24 #include "unicode/locid.h"
25 #include "unicode/ustring.h"
26
27 #include "bytesinkutil.h"
28 #include "charstr.h"
29 #include "cmemory.h"
30 #include "uassert.h"
31 #include "ulocimp.h"
32 #include "locmap.h"
33
34 #ifndef WIN32_LEAN_AND_MEAN
35 # define WIN32_LEAN_AND_MEAN
36 #endif
37 # define VC_EXTRALEAN
38 # define NOUSER
39 # define NOSERVICE
40 # define NOIME
41 # define NOMCX
42 #include <windows.h>
43 #include <stdio.h>
44
45 U_NAMESPACE_BEGIN
46
47 union FormatInfo
48 {
49 NUMBERFMTW number;
50 CURRENCYFMTW currency;
51 };
52
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Win32NumberFormat)53 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Win32NumberFormat)
54
55 #define NEW_ARRAY(type,count) (type *) uprv_malloc((count) * sizeof(type))
56 #define DELETE_ARRAY(array) uprv_free((void *) (array))
57
58 #define STACK_BUFFER_SIZE 32
59
60 /*
61 * Turns a string of the form "3;2;0" into the grouping UINT
62 * needed for NUMBERFMT and CURRENCYFMT. If the string does not
63 * end in ";0" then the return value should be multiplied by 10.
64 * (e.g. "3" => 30, "3;2" => 320)
65 */
66 static UINT getGrouping(const wchar_t *grouping)
67 {
68 UINT g = 0;
69 const wchar_t *s;
70
71 for (s = grouping; *s != L'\0'; s += 1) {
72 if (*s > L'0' && *s < L'9') {
73 g = g * 10 + (*s - L'0');
74 } else if (*s != L';') {
75 break;
76 }
77 }
78
79 if (*s != L'0') {
80 g *= 10;
81 }
82
83 return g;
84 }
85
getNumberFormat(NUMBERFMTW * fmt,const wchar_t * windowsLocaleName)86 static void getNumberFormat(NUMBERFMTW *fmt, const wchar_t *windowsLocaleName)
87 {
88 wchar_t buf[10];
89
90 GetLocaleInfoEx(windowsLocaleName, LOCALE_RETURN_NUMBER|LOCALE_IDIGITS, (LPWSTR) &fmt->NumDigits, sizeof(UINT));
91 GetLocaleInfoEx(windowsLocaleName, LOCALE_RETURN_NUMBER|LOCALE_ILZERO, (LPWSTR) &fmt->LeadingZero, sizeof(UINT));
92
93 GetLocaleInfoEx(windowsLocaleName, LOCALE_SGROUPING, (LPWSTR)buf, 10);
94 fmt->Grouping = getGrouping(buf);
95
96 fmt->lpDecimalSep = NEW_ARRAY(wchar_t, 6);
97 GetLocaleInfoEx(windowsLocaleName, LOCALE_SDECIMAL, fmt->lpDecimalSep, 6);
98
99 fmt->lpThousandSep = NEW_ARRAY(wchar_t, 6);
100 GetLocaleInfoEx(windowsLocaleName, LOCALE_STHOUSAND, fmt->lpThousandSep, 6);
101
102 GetLocaleInfoEx(windowsLocaleName, LOCALE_RETURN_NUMBER|LOCALE_INEGNUMBER, (LPWSTR) &fmt->NegativeOrder, sizeof(UINT));
103 }
104
freeNumberFormat(NUMBERFMTW * fmt)105 static void freeNumberFormat(NUMBERFMTW *fmt)
106 {
107 if (fmt != nullptr) {
108 DELETE_ARRAY(fmt->lpThousandSep);
109 DELETE_ARRAY(fmt->lpDecimalSep);
110 }
111 }
112
getCurrencyFormat(CURRENCYFMTW * fmt,const wchar_t * windowsLocaleName)113 static void getCurrencyFormat(CURRENCYFMTW *fmt, const wchar_t *windowsLocaleName)
114 {
115 wchar_t buf[10];
116
117 GetLocaleInfoEx(windowsLocaleName, LOCALE_RETURN_NUMBER|LOCALE_ICURRDIGITS, (LPWSTR) &fmt->NumDigits, sizeof(UINT));
118 GetLocaleInfoEx(windowsLocaleName, LOCALE_RETURN_NUMBER|LOCALE_ILZERO, (LPWSTR) &fmt->LeadingZero, sizeof(UINT));
119
120 GetLocaleInfoEx(windowsLocaleName, LOCALE_SMONGROUPING, (LPWSTR)buf, sizeof(buf));
121 fmt->Grouping = getGrouping(buf);
122
123 fmt->lpDecimalSep = NEW_ARRAY(wchar_t, 6);
124 GetLocaleInfoEx(windowsLocaleName, LOCALE_SMONDECIMALSEP, fmt->lpDecimalSep, 6);
125
126 fmt->lpThousandSep = NEW_ARRAY(wchar_t, 6);
127 GetLocaleInfoEx(windowsLocaleName, LOCALE_SMONTHOUSANDSEP, fmt->lpThousandSep, 6);
128
129 GetLocaleInfoEx(windowsLocaleName, LOCALE_RETURN_NUMBER|LOCALE_INEGCURR, (LPWSTR) &fmt->NegativeOrder, sizeof(UINT));
130 GetLocaleInfoEx(windowsLocaleName, LOCALE_RETURN_NUMBER|LOCALE_ICURRENCY, (LPWSTR) &fmt->PositiveOrder, sizeof(UINT));
131
132 fmt->lpCurrencySymbol = NEW_ARRAY(wchar_t, 8);
133 GetLocaleInfoEx(windowsLocaleName, LOCALE_SCURRENCY, (LPWSTR) fmt->lpCurrencySymbol, 8);
134 }
135
freeCurrencyFormat(CURRENCYFMTW * fmt)136 static void freeCurrencyFormat(CURRENCYFMTW *fmt)
137 {
138 if (fmt != nullptr) {
139 DELETE_ARRAY(fmt->lpCurrencySymbol);
140 DELETE_ARRAY(fmt->lpThousandSep);
141 DELETE_ARRAY(fmt->lpDecimalSep);
142 }
143 }
144
145 // TODO: This is copied in both winnmfmt.cpp and windtfmt.cpp, but really should
146 // be factored out into a common helper for both.
GetEquivalentWindowsLocaleName(const Locale & locale,UnicodeString ** buffer)147 static UErrorCode GetEquivalentWindowsLocaleName(const Locale& locale, UnicodeString** buffer)
148 {
149 UErrorCode status = U_ZERO_ERROR;
150
151 // Convert from names like "en_CA" and "de_DE@collation=phonebook" to "en-CA" and "de-DE-u-co-phonebk".
152 CharString asciiBCP47Tag;
153 {
154 CharStringByteSink sink(&asciiBCP47Tag);
155 ulocimp_toLanguageTag(locale.getName(), sink, false, &status);
156 }
157
158 if (U_SUCCESS(status))
159 {
160 // Need it to be UTF-16, not 8-bit
161 // TODO: This seems like a good thing for a helper
162 wchar_t bcp47Tag[LOCALE_NAME_MAX_LENGTH] = {};
163 int32_t i;
164 for (i = 0; i < UPRV_LENGTHOF(bcp47Tag); i++)
165 {
166 if (asciiBCP47Tag[i] == '\0')
167 {
168 break;
169 }
170 else
171 {
172 // normally just copy the character
173 bcp47Tag[i] = static_cast<wchar_t>(asciiBCP47Tag[i]);
174 }
175 }
176
177 // Ensure it's null terminated
178 if (i < (UPRV_LENGTHOF(bcp47Tag) - 1))
179 {
180 bcp47Tag[i] = L'\0';
181 }
182 else
183 {
184 // Ran out of room.
185 bcp47Tag[UPRV_LENGTHOF(bcp47Tag) - 1] = L'\0';
186 }
187
188
189 wchar_t windowsLocaleName[LOCALE_NAME_MAX_LENGTH] = {};
190
191 // Note: On Windows versions below 10, there is no support for locale name aliases.
192 // This means that it will fail for locales where ICU has a completely different
193 // name (like ku vs ckb), and it will also not work for alternate sort locale
194 // names like "de-DE-u-co-phonebk".
195
196 // TODO: We could add some sort of exception table for cases like ku vs ckb.
197
198 int length = ResolveLocaleName(bcp47Tag, windowsLocaleName, UPRV_LENGTHOF(windowsLocaleName));
199
200 if (length > 0)
201 {
202 *buffer = new UnicodeString(windowsLocaleName);
203 }
204 else
205 {
206 status = U_UNSUPPORTED_ERROR;
207 }
208 }
209 return status;
210 }
211
Win32NumberFormat(const Locale & locale,UBool currency,UErrorCode & status)212 Win32NumberFormat::Win32NumberFormat(const Locale &locale, UBool currency, UErrorCode &status)
213 : NumberFormat(), fCurrency(currency), fFormatInfo(nullptr), fFractionDigitsSet(false), fWindowsLocaleName(nullptr)
214 {
215 if (!U_FAILURE(status)) {
216 fLCID = locale.getLCID();
217
218 GetEquivalentWindowsLocaleName(locale, &fWindowsLocaleName);
219 // Note: In the previous code, it would look up the LCID for the locale, and if
220 // the locale was not recognized then it would get an LCID of 0, which is a
221 // synonym for LOCALE_USER_DEFAULT on Windows.
222 // If the above method fails, then fWindowsLocaleName will remain as nullptr, and
223 // then we will pass nullptr to API GetLocaleInfoEx, which is the same as passing
224 // LOCALE_USER_DEFAULT.
225
226 // Resolve actual locale to be used later
227 UErrorCode tmpsts = U_ZERO_ERROR;
228 char tmpLocID[ULOC_FULLNAME_CAPACITY];
229 int32_t len = uloc_getLocaleForLCID(fLCID, tmpLocID, UPRV_LENGTHOF(tmpLocID) - 1, &tmpsts);
230 if (U_SUCCESS(tmpsts)) {
231 tmpLocID[len] = 0;
232 fLocale = Locale((const char*)tmpLocID);
233 }
234
235 const wchar_t *localeName = nullptr;
236
237 if (fWindowsLocaleName != nullptr)
238 {
239 localeName = reinterpret_cast<const wchar_t*>(toOldUCharPtr(fWindowsLocaleName->getTerminatedBuffer()));
240 }
241
242 fFormatInfo = (FormatInfo*)uprv_malloc(sizeof(FormatInfo));
243
244 if (fCurrency) {
245 getCurrencyFormat(&fFormatInfo->currency, localeName);
246 } else {
247 getNumberFormat(&fFormatInfo->number, localeName);
248 }
249 }
250 }
251
Win32NumberFormat(const Win32NumberFormat & other)252 Win32NumberFormat::Win32NumberFormat(const Win32NumberFormat &other)
253 : NumberFormat(other), fFormatInfo((FormatInfo*)uprv_malloc(sizeof(FormatInfo)))
254 {
255 if (fFormatInfo != nullptr) {
256 uprv_memset(fFormatInfo, 0, sizeof(*fFormatInfo));
257 }
258 *this = other;
259 }
260
~Win32NumberFormat()261 Win32NumberFormat::~Win32NumberFormat()
262 {
263 if (fFormatInfo != nullptr) {
264 if (fCurrency) {
265 freeCurrencyFormat(&fFormatInfo->currency);
266 } else {
267 freeNumberFormat(&fFormatInfo->number);
268 }
269
270 uprv_free(fFormatInfo);
271 }
272 delete fWindowsLocaleName;
273 }
274
operator =(const Win32NumberFormat & other)275 Win32NumberFormat &Win32NumberFormat::operator=(const Win32NumberFormat &other)
276 {
277 if (this == &other) { return *this; } // self-assignment: no-op
278 NumberFormat::operator=(other);
279
280 this->fCurrency = other.fCurrency;
281 this->fLocale = other.fLocale;
282 this->fLCID = other.fLCID;
283 this->fFractionDigitsSet = other.fFractionDigitsSet;
284 this->fWindowsLocaleName = other.fWindowsLocaleName == nullptr ? nullptr : new UnicodeString(*other.fWindowsLocaleName);
285
286 const wchar_t *localeName = nullptr;
287
288 if (fWindowsLocaleName != nullptr)
289 {
290 localeName = reinterpret_cast<const wchar_t*>(toOldUCharPtr(fWindowsLocaleName->getTerminatedBuffer()));
291 }
292
293 if (fCurrency) {
294 freeCurrencyFormat(&fFormatInfo->currency);
295 getCurrencyFormat(&fFormatInfo->currency, localeName);
296 } else {
297 freeNumberFormat(&fFormatInfo->number);
298 getNumberFormat(&fFormatInfo->number, localeName);
299 }
300
301 return *this;
302 }
303
clone() const304 Win32NumberFormat *Win32NumberFormat::clone() const
305 {
306 return new Win32NumberFormat(*this);
307 }
308
format(double number,UnicodeString & appendTo,FieldPosition &) const309 UnicodeString& Win32NumberFormat::format(double number, UnicodeString& appendTo, FieldPosition& /* pos */) const
310 {
311 return format(getMaximumFractionDigits(), appendTo, L"%.16f", number);
312 }
313
format(int32_t number,UnicodeString & appendTo,FieldPosition &) const314 UnicodeString& Win32NumberFormat::format(int32_t number, UnicodeString& appendTo, FieldPosition& /* pos */) const
315 {
316 return format(getMinimumFractionDigits(), appendTo, L"%I32d", number);
317 }
318
format(int64_t number,UnicodeString & appendTo,FieldPosition &) const319 UnicodeString& Win32NumberFormat::format(int64_t number, UnicodeString& appendTo, FieldPosition& /* pos */) const
320 {
321 return format(getMinimumFractionDigits(), appendTo, L"%I64d", number);
322 }
323
parse(const UnicodeString & text,Formattable & result,ParsePosition & parsePosition) const324 void Win32NumberFormat::parse(const UnicodeString& text, Formattable& result, ParsePosition& parsePosition) const
325 {
326 UErrorCode status = U_ZERO_ERROR;
327 NumberFormat *nf = fCurrency? NumberFormat::createCurrencyInstance(fLocale, status) : NumberFormat::createInstance(fLocale, status);
328
329 nf->parse(text, result, parsePosition);
330 delete nf;
331 }
setMaximumFractionDigits(int32_t newValue)332 void Win32NumberFormat::setMaximumFractionDigits(int32_t newValue)
333 {
334 fFractionDigitsSet = true;
335 NumberFormat::setMaximumFractionDigits(newValue);
336 }
337
setMinimumFractionDigits(int32_t newValue)338 void Win32NumberFormat::setMinimumFractionDigits(int32_t newValue)
339 {
340 fFractionDigitsSet = true;
341 NumberFormat::setMinimumFractionDigits(newValue);
342 }
343
format(int32_t numDigits,UnicodeString & appendTo,const wchar_t * fmt,...) const344 UnicodeString &Win32NumberFormat::format(int32_t numDigits, UnicodeString &appendTo, const wchar_t *fmt, ...) const
345 {
346 wchar_t nStackBuffer[STACK_BUFFER_SIZE];
347 wchar_t *nBuffer = nStackBuffer;
348 va_list args;
349 int result;
350
351 nBuffer[0] = 0x0000;
352
353 /* Due to the arguments causing a result to be <= 23 characters (+2 for nullptr and minus),
354 we don't need to reallocate the buffer. */
355 va_start(args, fmt);
356 result = _vsnwprintf(nBuffer, STACK_BUFFER_SIZE, fmt, args);
357 va_end(args);
358
359 /* Just to make sure of the above statement, we add this assert */
360 U_ASSERT(result >=0);
361 // The following code is not used because _vscwprintf isn't available on MinGW at the moment.
362 /*if (result < 0) {
363 int newLength;
364
365 va_start(args, fmt);
366 newLength = _vscwprintf(fmt, args);
367 va_end(args);
368
369 nBuffer = NEW_ARRAY(char16_t, newLength + 1);
370
371 va_start(args, fmt);
372 result = _vsnwprintf(nBuffer, newLength + 1, fmt, args);
373 va_end(args);
374 }*/
375
376 // vswprintf is sensitive to the locale set by setlocale. For some locales
377 // it doesn't use "." as the decimal separator, which is what GetNumberFormatW
378 // and GetCurrencyFormatW both expect to see.
379 //
380 // To fix this, we scan over the string and replace the first non-digits, except
381 // for a leading "-", with a "."
382 //
383 // Note: (nBuffer[0] == L'-') will evaluate to 1 if there is a leading '-' in the
384 // number, and 0 otherwise.
385 for (wchar_t *p = &nBuffer[nBuffer[0] == L'-']; *p != L'\0'; p += 1) {
386 if (*p < L'0' || *p > L'9') {
387 *p = L'.';
388 break;
389 }
390 }
391
392 wchar_t stackBuffer[STACK_BUFFER_SIZE];
393 wchar_t *buffer = stackBuffer;
394 FormatInfo formatInfo;
395
396 formatInfo = *fFormatInfo;
397 buffer[0] = 0x0000;
398
399 const wchar_t *localeName = nullptr;
400
401 if (fWindowsLocaleName != nullptr)
402 {
403 localeName = reinterpret_cast<const wchar_t*>(toOldUCharPtr(fWindowsLocaleName->getTerminatedBuffer()));
404 }
405
406 if (fCurrency) {
407 if (fFractionDigitsSet) {
408 formatInfo.currency.NumDigits = (UINT) numDigits;
409 }
410
411 if (!isGroupingUsed()) {
412 formatInfo.currency.Grouping = 0;
413 }
414
415 result = GetCurrencyFormatEx(localeName, 0, nBuffer, &formatInfo.currency, buffer, STACK_BUFFER_SIZE);
416
417 if (result == 0) {
418 DWORD lastError = GetLastError();
419
420 if (lastError == ERROR_INSUFFICIENT_BUFFER) {
421 int newLength = GetCurrencyFormatEx(localeName, 0, nBuffer, &formatInfo.currency, nullptr, 0);
422
423 buffer = NEW_ARRAY(wchar_t, newLength);
424 buffer[0] = 0x0000;
425 GetCurrencyFormatEx(localeName, 0, nBuffer, &formatInfo.currency, buffer, newLength);
426 }
427 }
428 } else {
429 if (fFractionDigitsSet) {
430 formatInfo.number.NumDigits = (UINT) numDigits;
431 }
432
433 if (!isGroupingUsed()) {
434 formatInfo.number.Grouping = 0;
435 }
436
437 result = GetNumberFormatEx(localeName, 0, nBuffer, &formatInfo.number, buffer, STACK_BUFFER_SIZE);
438
439 if (result == 0) {
440 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
441 int newLength = GetNumberFormatEx(localeName, 0, nBuffer, &formatInfo.number, nullptr, 0);
442
443 buffer = NEW_ARRAY(wchar_t, newLength);
444 buffer[0] = 0x0000;
445 GetNumberFormatEx(localeName, 0, nBuffer, &formatInfo.number, buffer, newLength);
446 }
447 }
448 }
449
450 appendTo.append((char16_t *)buffer, (int32_t) wcslen(buffer));
451
452 if (buffer != stackBuffer) {
453 DELETE_ARRAY(buffer);
454 }
455
456 /*if (nBuffer != nStackBuffer) {
457 DELETE_ARRAY(nBuffer);
458 }*/
459
460 return appendTo;
461 }
462
463 U_NAMESPACE_END
464
465 #endif /* #if !UCONFIG_NO_FORMATTING */
466
467 #endif // U_PLATFORM_USES_ONLY_WIN32_API
468