michael@0: /* michael@0: ******************************************************************************** michael@0: * Copyright (C) 2005-2013, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ******************************************************************************** michael@0: * michael@0: * File WINNMFMT.CPP michael@0: * michael@0: ******************************************************************************** michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if U_PLATFORM_USES_ONLY_WIN32_API michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "winnmfmt.h" michael@0: michael@0: #include "unicode/format.h" michael@0: #include "unicode/numfmt.h" michael@0: #include "unicode/locid.h" michael@0: #include "unicode/ustring.h" michael@0: michael@0: #include "cmemory.h" michael@0: #include "uassert.h" michael@0: #include "locmap.h" michael@0: michael@0: # define WIN32_LEAN_AND_MEAN michael@0: # define VC_EXTRALEAN michael@0: # define NOUSER michael@0: # define NOSERVICE michael@0: # define NOIME michael@0: # define NOMCX michael@0: #include michael@0: #include michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: union FormatInfo michael@0: { michael@0: NUMBERFMTW number; michael@0: CURRENCYFMTW currency; michael@0: }; michael@0: michael@0: UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Win32NumberFormat) michael@0: michael@0: #define NEW_ARRAY(type,count) (type *) uprv_malloc((count) * sizeof(type)) michael@0: #define DELETE_ARRAY(array) uprv_free((void *) (array)) michael@0: michael@0: #define STACK_BUFFER_SIZE 32 michael@0: michael@0: /* michael@0: * Turns a string of the form "3;2;0" into the grouping UINT michael@0: * needed for NUMBERFMT and CURRENCYFMT. If the string does not michael@0: * end in ";0" then the return value should be multiplied by 10. michael@0: * (e.g. "3" => 30, "3;2" => 320) michael@0: */ michael@0: static UINT getGrouping(const char *grouping) michael@0: { michael@0: UINT g = 0; michael@0: const char *s; michael@0: michael@0: for (s = grouping; *s != '\0'; s += 1) { michael@0: if (*s > '0' && *s < '9') { michael@0: g = g * 10 + (*s - '0'); michael@0: } else if (*s != ';') { michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (*s != '0') { michael@0: g *= 10; michael@0: } michael@0: michael@0: return g; michael@0: } michael@0: michael@0: static void getNumberFormat(NUMBERFMTW *fmt, int32_t lcid) michael@0: { michael@0: char buf[10]; michael@0: michael@0: GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_IDIGITS, (LPWSTR) &fmt->NumDigits, sizeof(UINT)); michael@0: GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_ILZERO, (LPWSTR) &fmt->LeadingZero, sizeof(UINT)); michael@0: michael@0: GetLocaleInfoA(lcid, LOCALE_SGROUPING, buf, 10); michael@0: fmt->Grouping = getGrouping(buf); michael@0: michael@0: fmt->lpDecimalSep = NEW_ARRAY(UChar, 6); michael@0: GetLocaleInfoW(lcid, LOCALE_SDECIMAL, fmt->lpDecimalSep, 6); michael@0: michael@0: fmt->lpThousandSep = NEW_ARRAY(UChar, 6); michael@0: GetLocaleInfoW(lcid, LOCALE_STHOUSAND, fmt->lpThousandSep, 6); michael@0: michael@0: GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_INEGNUMBER, (LPWSTR) &fmt->NegativeOrder, sizeof(UINT)); michael@0: } michael@0: michael@0: static void freeNumberFormat(NUMBERFMTW *fmt) michael@0: { michael@0: if (fmt != NULL) { michael@0: DELETE_ARRAY(fmt->lpThousandSep); michael@0: DELETE_ARRAY(fmt->lpDecimalSep); michael@0: } michael@0: } michael@0: michael@0: static void getCurrencyFormat(CURRENCYFMTW *fmt, int32_t lcid) michael@0: { michael@0: char buf[10]; michael@0: michael@0: GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_ICURRDIGITS, (LPWSTR) &fmt->NumDigits, sizeof(UINT)); michael@0: GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_ILZERO, (LPWSTR) &fmt->LeadingZero, sizeof(UINT)); michael@0: michael@0: GetLocaleInfoA(lcid, LOCALE_SMONGROUPING, buf, sizeof(buf)); michael@0: fmt->Grouping = getGrouping(buf); michael@0: michael@0: fmt->lpDecimalSep = NEW_ARRAY(UChar, 6); michael@0: GetLocaleInfoW(lcid, LOCALE_SMONDECIMALSEP, fmt->lpDecimalSep, 6); michael@0: michael@0: fmt->lpThousandSep = NEW_ARRAY(UChar, 6); michael@0: GetLocaleInfoW(lcid, LOCALE_SMONTHOUSANDSEP, fmt->lpThousandSep, 6); michael@0: michael@0: GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_INEGCURR, (LPWSTR) &fmt->NegativeOrder, sizeof(UINT)); michael@0: GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_ICURRENCY, (LPWSTR) &fmt->PositiveOrder, sizeof(UINT)); michael@0: michael@0: fmt->lpCurrencySymbol = NEW_ARRAY(UChar, 8); michael@0: GetLocaleInfoW(lcid, LOCALE_SCURRENCY, (LPWSTR) fmt->lpCurrencySymbol, 8); michael@0: } michael@0: michael@0: static void freeCurrencyFormat(CURRENCYFMTW *fmt) michael@0: { michael@0: if (fmt != NULL) { michael@0: DELETE_ARRAY(fmt->lpCurrencySymbol); michael@0: DELETE_ARRAY(fmt->lpThousandSep); michael@0: DELETE_ARRAY(fmt->lpDecimalSep); michael@0: } michael@0: } michael@0: michael@0: Win32NumberFormat::Win32NumberFormat(const Locale &locale, UBool currency, UErrorCode &status) michael@0: : NumberFormat(), fCurrency(currency), fFractionDigitsSet(FALSE), fFormatInfo(NULL) michael@0: { michael@0: if (!U_FAILURE(status)) { michael@0: fLCID = locale.getLCID(); michael@0: michael@0: // Resolve actual locale to be used later michael@0: UErrorCode tmpsts = U_ZERO_ERROR; michael@0: char tmpLocID[ULOC_FULLNAME_CAPACITY]; michael@0: int32_t len = uloc_getLocaleForLCID(fLCID, tmpLocID, sizeof(tmpLocID)/sizeof(tmpLocID[0]) - 1, &tmpsts); michael@0: if (U_SUCCESS(tmpsts)) { michael@0: tmpLocID[len] = 0; michael@0: fLocale = Locale((const char*)tmpLocID); michael@0: } michael@0: michael@0: fFormatInfo = (FormatInfo*)uprv_malloc(sizeof(FormatInfo)); michael@0: michael@0: if (fCurrency) { michael@0: getCurrencyFormat(&fFormatInfo->currency, fLCID); michael@0: } else { michael@0: getNumberFormat(&fFormatInfo->number, fLCID); michael@0: } michael@0: } michael@0: } michael@0: michael@0: Win32NumberFormat::Win32NumberFormat(const Win32NumberFormat &other) michael@0: : NumberFormat(other), fFormatInfo((FormatInfo*)uprv_malloc(sizeof(FormatInfo))) michael@0: { michael@0: if (fFormatInfo != NULL) { michael@0: uprv_memset(fFormatInfo, 0, sizeof(*fFormatInfo)); michael@0: } michael@0: *this = other; michael@0: } michael@0: michael@0: Win32NumberFormat::~Win32NumberFormat() michael@0: { michael@0: if (fFormatInfo != NULL) { michael@0: if (fCurrency) { michael@0: freeCurrencyFormat(&fFormatInfo->currency); michael@0: } else { michael@0: freeNumberFormat(&fFormatInfo->number); michael@0: } michael@0: michael@0: uprv_free(fFormatInfo); michael@0: } michael@0: } michael@0: michael@0: Win32NumberFormat &Win32NumberFormat::operator=(const Win32NumberFormat &other) michael@0: { michael@0: NumberFormat::operator=(other); michael@0: michael@0: this->fCurrency = other.fCurrency; michael@0: this->fLocale = other.fLocale; michael@0: this->fLCID = other.fLCID; michael@0: this->fFractionDigitsSet = other.fFractionDigitsSet; michael@0: michael@0: if (fCurrency) { michael@0: freeCurrencyFormat(&fFormatInfo->currency); michael@0: getCurrencyFormat(&fFormatInfo->currency, fLCID); michael@0: } else { michael@0: freeNumberFormat(&fFormatInfo->number); michael@0: getNumberFormat(&fFormatInfo->number, fLCID); michael@0: } michael@0: michael@0: return *this; michael@0: } michael@0: michael@0: Format *Win32NumberFormat::clone(void) const michael@0: { michael@0: return new Win32NumberFormat(*this); michael@0: } michael@0: michael@0: UnicodeString& Win32NumberFormat::format(double number, UnicodeString& appendTo, FieldPosition& pos) const michael@0: { michael@0: return format(getMaximumFractionDigits(), appendTo, L"%.16f", number); michael@0: } michael@0: michael@0: UnicodeString& Win32NumberFormat::format(int32_t number, UnicodeString& appendTo, FieldPosition& pos) const michael@0: { michael@0: return format(getMinimumFractionDigits(), appendTo, L"%I32d", number); michael@0: } michael@0: michael@0: UnicodeString& Win32NumberFormat::format(int64_t number, UnicodeString& appendTo, FieldPosition& pos) const michael@0: { michael@0: return format(getMinimumFractionDigits(), appendTo, L"%I64d", number); michael@0: } michael@0: michael@0: void Win32NumberFormat::parse(const UnicodeString& text, Formattable& result, ParsePosition& parsePosition) const michael@0: { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: NumberFormat *nf = fCurrency? NumberFormat::createCurrencyInstance(fLocale, status) : NumberFormat::createInstance(fLocale, status); michael@0: michael@0: nf->parse(text, result, parsePosition); michael@0: delete nf; michael@0: } michael@0: void Win32NumberFormat::setMaximumFractionDigits(int32_t newValue) michael@0: { michael@0: fFractionDigitsSet = TRUE; michael@0: NumberFormat::setMaximumFractionDigits(newValue); michael@0: } michael@0: michael@0: void Win32NumberFormat::setMinimumFractionDigits(int32_t newValue) michael@0: { michael@0: fFractionDigitsSet = TRUE; michael@0: NumberFormat::setMinimumFractionDigits(newValue); michael@0: } michael@0: michael@0: UnicodeString &Win32NumberFormat::format(int32_t numDigits, UnicodeString &appendTo, wchar_t *fmt, ...) const michael@0: { michael@0: wchar_t nStackBuffer[STACK_BUFFER_SIZE]; michael@0: wchar_t *nBuffer = nStackBuffer; michael@0: va_list args; michael@0: int result; michael@0: michael@0: nBuffer[0] = 0x0000; michael@0: michael@0: /* Due to the arguments causing a result to be <= 23 characters (+2 for NULL and minus), michael@0: we don't need to reallocate the buffer. */ michael@0: va_start(args, fmt); michael@0: result = _vsnwprintf(nBuffer, STACK_BUFFER_SIZE, fmt, args); michael@0: va_end(args); michael@0: michael@0: /* Just to make sure of the above statement, we add this assert */ michael@0: U_ASSERT(result >=0); michael@0: // The following code is not used because _vscwprintf isn't available on MinGW at the moment. michael@0: /*if (result < 0) { michael@0: int newLength; michael@0: michael@0: va_start(args, fmt); michael@0: newLength = _vscwprintf(fmt, args); michael@0: va_end(args); michael@0: michael@0: nBuffer = NEW_ARRAY(UChar, newLength + 1); michael@0: michael@0: va_start(args, fmt); michael@0: result = _vsnwprintf(nBuffer, newLength + 1, fmt, args); michael@0: va_end(args); michael@0: }*/ michael@0: michael@0: // vswprintf is sensitive to the locale set by setlocale. For some locales michael@0: // it doesn't use "." as the decimal separator, which is what GetNumberFormatW michael@0: // and GetCurrencyFormatW both expect to see. michael@0: // michael@0: // To fix this, we scan over the string and replace the first non-digits, except michael@0: // for a leading "-", with a "." michael@0: // michael@0: // Note: (nBuffer[0] == L'-') will evaluate to 1 if there is a leading '-' in the michael@0: // number, and 0 otherwise. michael@0: for (wchar_t *p = &nBuffer[nBuffer[0] == L'-']; *p != L'\0'; p += 1) { michael@0: if (*p < L'0' || *p > L'9') { michael@0: *p = L'.'; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: UChar stackBuffer[STACK_BUFFER_SIZE]; michael@0: UChar *buffer = stackBuffer; michael@0: FormatInfo formatInfo; michael@0: michael@0: formatInfo = *fFormatInfo; michael@0: buffer[0] = 0x0000; michael@0: michael@0: if (fCurrency) { michael@0: if (fFractionDigitsSet) { michael@0: formatInfo.currency.NumDigits = (UINT) numDigits; michael@0: } michael@0: michael@0: if (!isGroupingUsed()) { michael@0: formatInfo.currency.Grouping = 0; michael@0: } michael@0: michael@0: result = GetCurrencyFormatW(fLCID, 0, nBuffer, &formatInfo.currency, buffer, STACK_BUFFER_SIZE); michael@0: michael@0: if (result == 0) { michael@0: DWORD lastError = GetLastError(); michael@0: michael@0: if (lastError == ERROR_INSUFFICIENT_BUFFER) { michael@0: int newLength = GetCurrencyFormatW(fLCID, 0, nBuffer, &formatInfo.currency, NULL, 0); michael@0: michael@0: buffer = NEW_ARRAY(UChar, newLength); michael@0: buffer[0] = 0x0000; michael@0: GetCurrencyFormatW(fLCID, 0, nBuffer, &formatInfo.currency, buffer, newLength); michael@0: } michael@0: } michael@0: } else { michael@0: if (fFractionDigitsSet) { michael@0: formatInfo.number.NumDigits = (UINT) numDigits; michael@0: } michael@0: michael@0: if (!isGroupingUsed()) { michael@0: formatInfo.number.Grouping = 0; michael@0: } michael@0: michael@0: result = GetNumberFormatW(fLCID, 0, nBuffer, &formatInfo.number, buffer, STACK_BUFFER_SIZE); michael@0: michael@0: if (result == 0) { michael@0: if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { michael@0: int newLength = GetNumberFormatW(fLCID, 0, nBuffer, &formatInfo.number, NULL, 0); michael@0: michael@0: buffer = NEW_ARRAY(UChar, newLength); michael@0: buffer[0] = 0x0000; michael@0: GetNumberFormatW(fLCID, 0, nBuffer, &formatInfo.number, buffer, newLength); michael@0: } michael@0: } michael@0: } michael@0: michael@0: appendTo.append(buffer, (int32_t) wcslen(buffer)); michael@0: michael@0: if (buffer != stackBuffer) { michael@0: DELETE_ARRAY(buffer); michael@0: } michael@0: michael@0: /*if (nBuffer != nStackBuffer) { michael@0: DELETE_ARRAY(nBuffer); michael@0: }*/ michael@0: michael@0: return appendTo; michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */ michael@0: michael@0: #endif // U_PLATFORM_USES_ONLY_WIN32_API