2006-01-12 19:53:58 +00:00
|
|
|
/*
|
|
|
|
********************************************************************************
|
|
|
|
* Copyright (C) 2005-2006, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
********************************************************************************
|
|
|
|
*
|
|
|
|
* File WINNMFMT.CPP
|
|
|
|
*
|
|
|
|
********************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#ifdef U_WINDOWS
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
|
|
|
#include "winnmfmt.h"
|
|
|
|
|
|
|
|
#include "unicode/format.h"
|
|
|
|
#include "unicode/numfmt.h"
|
|
|
|
#include "unicode/locid.h"
|
|
|
|
#include "unicode/ustring.h"
|
|
|
|
|
|
|
|
#include "cmemory.h"
|
|
|
|
#include "locmap.h"
|
|
|
|
|
|
|
|
# define WIN32_LEAN_AND_MEAN
|
|
|
|
# define VC_EXTRALEAN
|
|
|
|
# define NOUSER
|
|
|
|
# define NOSERVICE
|
|
|
|
# define NOIME
|
|
|
|
# define NOMCX
|
|
|
|
#include <windows.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
union FormatInfo
|
|
|
|
{
|
|
|
|
NUMBERFMTW number;
|
|
|
|
CURRENCYFMTW currency;
|
|
|
|
};
|
|
|
|
|
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Win32NumberFormat)
|
|
|
|
|
|
|
|
#define NEW_ARRAY(type,count) (type *) uprv_malloc((count) * sizeof(type))
|
|
|
|
#define DELETE_ARRAY(array) uprv_free((void *) (array))
|
|
|
|
|
|
|
|
#define STACK_BUFFER_SIZE 32
|
|
|
|
|
2006-01-23 23:25:00 +00:00
|
|
|
/*
|
|
|
|
* Turns a string of the form "3;2;0" into the grouping UINT
|
|
|
|
* needed for NUMBERFMT and CURRENCYFMT. If there's only one
|
|
|
|
* group in the string, that means that there's only one group,
|
|
|
|
* and the return value should be multiplied by 10. (e.g. "3" => 30)
|
|
|
|
*/
|
2006-01-12 19:53:58 +00:00
|
|
|
UINT getGrouping(const char *grouping)
|
|
|
|
{
|
|
|
|
UINT g = 0;
|
2006-01-23 23:25:00 +00:00
|
|
|
UINT c = 0;
|
2006-01-12 19:53:58 +00:00
|
|
|
|
|
|
|
for (const char *s = grouping; *s != '\0'; s += 1) {
|
|
|
|
if (*s > '0' && *s < '9') {
|
|
|
|
g = g * 10 + (*s - '0');
|
2006-01-23 23:25:00 +00:00
|
|
|
} else if (*s == ';') {
|
|
|
|
c += 1;
|
2006-01-12 19:53:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-23 23:25:00 +00:00
|
|
|
if (c == 0) {
|
|
|
|
g *= 10;
|
|
|
|
}
|
|
|
|
|
2006-01-12 19:53:58 +00:00
|
|
|
return g;
|
|
|
|
}
|
|
|
|
|
|
|
|
void getNumberFormat(NUMBERFMTW *fmt, int32_t lcid)
|
|
|
|
{
|
|
|
|
char buf[10];
|
|
|
|
|
2006-02-04 02:08:05 +00:00
|
|
|
GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_IDIGITS, (LPWSTR) &fmt->NumDigits, sizeof(UINT));
|
|
|
|
GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_ILZERO, (LPWSTR) &fmt->LeadingZero, sizeof(UINT));
|
2006-01-12 19:53:58 +00:00
|
|
|
|
|
|
|
GetLocaleInfoA(lcid, LOCALE_SGROUPING, buf, 10);
|
|
|
|
fmt->Grouping = getGrouping(buf);
|
|
|
|
|
|
|
|
fmt->lpDecimalSep = NEW_ARRAY(UChar, 6);
|
|
|
|
GetLocaleInfoW(lcid, LOCALE_SDECIMAL, fmt->lpDecimalSep, 6);
|
|
|
|
|
|
|
|
fmt->lpThousandSep = NEW_ARRAY(UChar, 6);
|
|
|
|
GetLocaleInfoW(lcid, LOCALE_STHOUSAND, fmt->lpThousandSep, 6);
|
|
|
|
|
2006-02-04 02:08:05 +00:00
|
|
|
GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_INEGNUMBER, (LPWSTR) &fmt->NegativeOrder, sizeof(UINT));
|
2006-01-12 19:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void freeNumberFormat(NUMBERFMTW *fmt)
|
|
|
|
{
|
|
|
|
DELETE_ARRAY(fmt->lpThousandSep);
|
|
|
|
DELETE_ARRAY(fmt->lpDecimalSep);
|
|
|
|
}
|
|
|
|
|
|
|
|
void getCurrencyFormat(CURRENCYFMTW *fmt, int32_t lcid)
|
|
|
|
{
|
|
|
|
char buf[10];
|
|
|
|
|
2006-01-23 23:25:00 +00:00
|
|
|
GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_ICURRDIGITS, (LPWSTR) &fmt->NumDigits, sizeof(UINT));
|
|
|
|
GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_ILZERO, (LPWSTR) &fmt->LeadingZero, sizeof(UINT));
|
2006-01-12 19:53:58 +00:00
|
|
|
|
2006-01-23 23:25:00 +00:00
|
|
|
GetLocaleInfoA(lcid, LOCALE_SMONGROUPING, buf, sizeof(buf));
|
2006-01-12 19:53:58 +00:00
|
|
|
fmt->Grouping = getGrouping(buf);
|
|
|
|
|
|
|
|
fmt->lpDecimalSep = NEW_ARRAY(UChar, 6);
|
|
|
|
GetLocaleInfoW(lcid, LOCALE_SMONDECIMALSEP, fmt->lpDecimalSep, 6);
|
|
|
|
|
|
|
|
fmt->lpThousandSep = NEW_ARRAY(UChar, 6);
|
|
|
|
GetLocaleInfoW(lcid, LOCALE_SMONTHOUSANDSEP, fmt->lpThousandSep, 6);
|
|
|
|
|
2006-01-23 23:25:00 +00:00
|
|
|
GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_INEGCURR, (LPWSTR) &fmt->NegativeOrder, sizeof(UINT));
|
|
|
|
GetLocaleInfoW(lcid, LOCALE_RETURN_NUMBER|LOCALE_ICURRENCY, (LPWSTR) &fmt->PositiveOrder, sizeof(UINT));
|
2006-01-12 19:53:58 +00:00
|
|
|
|
|
|
|
fmt->lpCurrencySymbol = NEW_ARRAY(UChar, 8);
|
|
|
|
GetLocaleInfoW(lcid, LOCALE_SCURRENCY, (LPWSTR) fmt->lpCurrencySymbol, 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
void freeCurrencyFormat(CURRENCYFMTW *fmt)
|
|
|
|
{
|
|
|
|
DELETE_ARRAY(fmt->lpCurrencySymbol);
|
|
|
|
DELETE_ARRAY(fmt->lpThousandSep);
|
|
|
|
DELETE_ARRAY(fmt->lpDecimalSep);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: keep locale too?
|
|
|
|
Win32NumberFormat::Win32NumberFormat(const Locale &locale, UBool currency, UErrorCode &status)
|
|
|
|
: NumberFormat(), fCurrency(currency), fFractionDigitsSet(FALSE)
|
|
|
|
{
|
|
|
|
if (!U_FAILURE(status)) {
|
|
|
|
fLCID = locale.getLCID();
|
|
|
|
|
|
|
|
fFormatInfo = new FormatInfo();
|
|
|
|
|
|
|
|
if (fCurrency) {
|
|
|
|
getCurrencyFormat(&fFormatInfo->currency, fLCID);
|
|
|
|
} else {
|
|
|
|
getNumberFormat(&fFormatInfo->number, fLCID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Win32NumberFormat::Win32NumberFormat(const Win32NumberFormat &other)
|
|
|
|
: NumberFormat(other)
|
|
|
|
{
|
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
|
|
|
|
Win32NumberFormat::~Win32NumberFormat()
|
|
|
|
{
|
|
|
|
if (fCurrency) {
|
|
|
|
freeCurrencyFormat(&fFormatInfo->currency);
|
|
|
|
} else {
|
|
|
|
freeNumberFormat(&fFormatInfo->number);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete fFormatInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
Win32NumberFormat &Win32NumberFormat::operator=(const Win32NumberFormat &other)
|
|
|
|
{
|
|
|
|
NumberFormat::operator=(other);
|
|
|
|
|
|
|
|
this->fCurrency = other.fCurrency;
|
|
|
|
this->fLCID = other.fLCID;
|
|
|
|
this->fFractionDigitsSet = other.fFractionDigitsSet;
|
|
|
|
|
|
|
|
this->fFormatInfo = new FormatInfo;
|
|
|
|
*this->fFormatInfo = *other.fFormatInfo;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Format *Win32NumberFormat::clone(void) const
|
|
|
|
{
|
|
|
|
return new Win32NumberFormat(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString& Win32NumberFormat::format(double number, UnicodeString& appendTo, FieldPosition& pos) const
|
|
|
|
{
|
|
|
|
return format(getMaximumFractionDigits(), appendTo, L"%f", number);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString& Win32NumberFormat::format(int32_t number, UnicodeString& appendTo, FieldPosition& pos) const
|
|
|
|
{
|
|
|
|
return format(getMinimumFractionDigits(), appendTo, L"%I32d", number);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString& Win32NumberFormat::format(int64_t number, UnicodeString& appendTo, FieldPosition& pos) const
|
|
|
|
{
|
|
|
|
return format(getMinimumFractionDigits(), appendTo, L"%I64d", number);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: cache Locale and NumberFormat? Could keep locale passed to constructor...
|
|
|
|
void Win32NumberFormat::parse(const UnicodeString& text, Formattable& result, ParsePosition& parsePosition) const
|
|
|
|
{
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
Locale loc(uprv_convertToPosix(fLCID, &status));
|
|
|
|
NumberFormat *nf = fCurrency? NumberFormat::createCurrencyInstance(loc, status) : NumberFormat::createInstance(loc, status);
|
|
|
|
|
|
|
|
nf->parse(text, result, parsePosition);
|
|
|
|
delete nf;
|
|
|
|
}
|
|
|
|
void Win32NumberFormat::setMaximumFractionDigits(int32_t newValue)
|
|
|
|
{
|
|
|
|
fFractionDigitsSet = TRUE;
|
|
|
|
NumberFormat::setMaximumFractionDigits(newValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Win32NumberFormat::setMinimumFractionDigits(int32_t newValue)
|
|
|
|
{
|
|
|
|
fFractionDigitsSet = TRUE;
|
|
|
|
NumberFormat::setMinimumFractionDigits(newValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString &Win32NumberFormat::format(int32_t numDigits, UnicodeString &appendTo, wchar_t *fmt, ...) const
|
|
|
|
{
|
|
|
|
wchar_t nStackBuffer[STACK_BUFFER_SIZE];
|
|
|
|
wchar_t *nBuffer = nStackBuffer;
|
|
|
|
va_list args;
|
|
|
|
int result;
|
|
|
|
|
2006-01-23 23:25:00 +00:00
|
|
|
nBuffer[0] = 0x0000;
|
|
|
|
|
2006-01-12 19:53:58 +00:00
|
|
|
va_start(args, fmt);
|
|
|
|
result = vswprintf(nBuffer, STACK_BUFFER_SIZE, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (result < 0) {
|
|
|
|
int newLength;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
newLength = _vscwprintf(fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
2006-01-23 23:25:00 +00:00
|
|
|
nBuffer = NEW_ARRAY(UChar, newLength + 1);
|
2006-01-12 19:53:58 +00:00
|
|
|
|
|
|
|
va_start(args, fmt);
|
2006-01-23 23:25:00 +00:00
|
|
|
result = vswprintf(nBuffer, newLength + 1, fmt, args);
|
2006-01-12 19:53:58 +00:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
UChar stackBuffer[STACK_BUFFER_SIZE];
|
|
|
|
UChar *buffer = stackBuffer;
|
|
|
|
FormatInfo formatInfo;
|
|
|
|
|
|
|
|
formatInfo = *fFormatInfo;
|
2006-01-23 23:25:00 +00:00
|
|
|
buffer[0] = 0x0000;
|
2006-01-12 19:53:58 +00:00
|
|
|
|
|
|
|
if (fCurrency) {
|
|
|
|
if (fFractionDigitsSet) {
|
|
|
|
formatInfo.currency.NumDigits = (UINT) numDigits;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isGroupingUsed()) {
|
|
|
|
formatInfo.currency.Grouping = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = GetCurrencyFormatW(fLCID, 0, nBuffer, &formatInfo.currency, buffer, STACK_BUFFER_SIZE);
|
|
|
|
|
|
|
|
if (result == 0) {
|
2006-01-23 23:25:00 +00:00
|
|
|
DWORD lastError = GetLastError();
|
|
|
|
|
|
|
|
if (lastError == ERROR_INSUFFICIENT_BUFFER) {
|
2006-01-12 19:53:58 +00:00
|
|
|
int newLength = GetCurrencyFormatW(fLCID, 0, nBuffer, &formatInfo.currency, NULL, 0);
|
|
|
|
|
|
|
|
buffer = NEW_ARRAY(UChar, newLength);
|
2006-01-23 23:25:00 +00:00
|
|
|
buffer[0] = 0x0000;
|
2006-01-12 19:53:58 +00:00
|
|
|
GetCurrencyFormatW(fLCID, 0, nBuffer, &formatInfo.currency, buffer, newLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (fFractionDigitsSet) {
|
|
|
|
formatInfo.number.NumDigits = (UINT) numDigits;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isGroupingUsed()) {
|
|
|
|
formatInfo.number.Grouping = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = GetNumberFormatW(fLCID, 0, nBuffer, &formatInfo.number, buffer, STACK_BUFFER_SIZE);
|
|
|
|
|
|
|
|
if (result == 0) {
|
|
|
|
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
|
|
|
|
int newLength = GetNumberFormatW(fLCID, 0, nBuffer, &formatInfo.number, NULL, 0);
|
|
|
|
|
|
|
|
buffer = NEW_ARRAY(UChar, newLength);
|
2006-01-23 23:25:00 +00:00
|
|
|
buffer[0] = 0x0000;
|
2006-01-12 19:53:58 +00:00
|
|
|
GetNumberFormatW(fLCID, 0, nBuffer, &formatInfo.number, buffer, newLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
appendTo.append(buffer, (int32_t) wcslen(buffer));
|
|
|
|
|
|
|
|
if (buffer != stackBuffer) {
|
|
|
|
DELETE_ARRAY(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nBuffer != nStackBuffer) {
|
|
|
|
DELETE_ARRAY(nBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|
|
|
|
|
|
|
|
#endif // #ifdef U_WINDOWS
|