1999-08-16 21:50:52 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
2011-03-22 16:58:58 +00:00
|
|
|
* Copyright (C) 1996-2011, International Business Machines
|
1999-11-23 01:30:04 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 06/24/99 helena Integrated Alan's NF enhancements and Java2 bug fixes
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/unum.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/uloc.h"
|
|
|
|
#include "unicode/numfmt.h"
|
|
|
|
#include "unicode/decimfmt.h"
|
2001-11-07 00:01:56 +00:00
|
|
|
#include "unicode/rbnf.h"
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/ustring.h"
|
|
|
|
#include "unicode/fmtable.h"
|
|
|
|
#include "unicode/dcfmtsym.h"
|
2004-04-27 21:47:09 +00:00
|
|
|
#include "unicode/curramt.h"
|
2004-03-25 17:42:28 +00:00
|
|
|
#include "uassert.h"
|
2002-07-12 21:42:24 +00:00
|
|
|
#include "cpputils.h"
|
2010-10-12 05:11:41 +00:00
|
|
|
#include "cstring.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-02-10 02:16:19 +00:00
|
|
|
|
2003-06-23 22:15:00 +00:00
|
|
|
U_NAMESPACE_USE
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI UNumberFormat* U_EXPORT2
|
2001-08-16 00:55:16 +00:00
|
|
|
unum_open( UNumberFormatStyle style,
|
|
|
|
const UChar* pattern,
|
|
|
|
int32_t patternLength,
|
|
|
|
const char* locale,
|
|
|
|
UParseError* parseErr,
|
2011-04-29 00:00:47 +00:00
|
|
|
UErrorCode* status) {
|
|
|
|
if(U_FAILURE(*status)) {
|
|
|
|
return NULL;
|
2003-06-23 22:15:00 +00:00
|
|
|
}
|
2004-03-12 20:15:13 +00:00
|
|
|
|
2011-04-29 00:00:47 +00:00
|
|
|
NumberFormat *retVal = NULL;
|
2004-08-09 07:07:33 +00:00
|
|
|
|
2004-03-12 20:15:13 +00:00
|
|
|
switch(style) {
|
|
|
|
case UNUM_DECIMAL:
|
|
|
|
case UNUM_CURRENCY:
|
|
|
|
case UNUM_PERCENT:
|
|
|
|
case UNUM_SCIENTIFIC:
|
2011-04-29 00:00:47 +00:00
|
|
|
retVal = NumberFormat::createInstance(Locale(locale), style, *status);
|
2004-08-09 07:07:33 +00:00
|
|
|
break;
|
2004-03-12 20:15:13 +00:00
|
|
|
|
|
|
|
case UNUM_PATTERN_DECIMAL: {
|
2003-06-23 22:15:00 +00:00
|
|
|
UParseError tErr;
|
2003-08-01 22:28:18 +00:00
|
|
|
/* UnicodeString can handle the case when patternLength = -1. */
|
|
|
|
const UnicodeString pat(pattern, patternLength);
|
2004-08-09 07:07:33 +00:00
|
|
|
|
2003-06-23 22:15:00 +00:00
|
|
|
if(parseErr==NULL){
|
|
|
|
parseErr = &tErr;
|
|
|
|
}
|
2004-08-09 07:07:33 +00:00
|
|
|
|
2011-04-29 00:00:47 +00:00
|
|
|
DecimalFormatSymbols *syms = new DecimalFormatSymbols(Locale(locale), *status);
|
|
|
|
if(syms == NULL) {
|
2003-06-23 22:15:00 +00:00
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
2011-04-29 00:00:47 +00:00
|
|
|
return NULL;
|
2003-06-23 22:15:00 +00:00
|
|
|
}
|
2010-03-30 21:46:11 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
2010-06-10 17:53:26 +00:00
|
|
|
delete syms;
|
2011-04-29 00:00:47 +00:00
|
|
|
return NULL;
|
2010-03-30 21:46:11 +00:00
|
|
|
}
|
2004-08-09 07:07:33 +00:00
|
|
|
|
2011-04-29 00:00:47 +00:00
|
|
|
retVal = new DecimalFormat(pat, syms, *parseErr, *status);
|
|
|
|
if(retVal == NULL) {
|
2003-06-23 22:15:00 +00:00
|
|
|
delete syms;
|
|
|
|
}
|
2011-04-29 00:00:47 +00:00
|
|
|
} break;
|
2004-03-12 20:15:13 +00:00
|
|
|
|
|
|
|
#if U_HAVE_RBNF
|
|
|
|
case UNUM_PATTERN_RULEBASED: {
|
|
|
|
UParseError tErr;
|
|
|
|
/* UnicodeString can handle the case when patternLength = -1. */
|
|
|
|
const UnicodeString pat(pattern, patternLength);
|
2003-06-23 22:15:00 +00:00
|
|
|
|
2004-03-12 20:15:13 +00:00
|
|
|
if(parseErr==NULL){
|
|
|
|
parseErr = &tErr;
|
|
|
|
}
|
|
|
|
|
2011-04-29 00:00:47 +00:00
|
|
|
retVal = new RuleBasedNumberFormat(pat, Locale(locale), *parseErr, *status);
|
2004-03-12 20:15:13 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case UNUM_SPELLOUT:
|
2011-04-29 00:00:47 +00:00
|
|
|
retVal = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale(locale), *status);
|
2004-08-09 07:07:33 +00:00
|
|
|
break;
|
2004-03-12 20:15:13 +00:00
|
|
|
|
|
|
|
case UNUM_ORDINAL:
|
2011-04-29 00:00:47 +00:00
|
|
|
retVal = new RuleBasedNumberFormat(URBNF_ORDINAL, Locale(locale), *status);
|
2004-08-09 07:07:33 +00:00
|
|
|
break;
|
2004-03-12 20:15:13 +00:00
|
|
|
|
|
|
|
case UNUM_DURATION:
|
2011-04-29 00:00:47 +00:00
|
|
|
retVal = new RuleBasedNumberFormat(URBNF_DURATION, Locale(locale), *status);
|
2004-08-09 07:07:33 +00:00
|
|
|
break;
|
2009-01-22 04:46:35 +00:00
|
|
|
|
|
|
|
case UNUM_NUMBERING_SYSTEM:
|
2011-04-29 00:00:47 +00:00
|
|
|
retVal = new RuleBasedNumberFormat(URBNF_NUMBERING_SYSTEM, Locale(locale), *status);
|
2009-01-22 04:46:35 +00:00
|
|
|
break;
|
2004-03-12 20:15:13 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
2004-08-09 07:07:33 +00:00
|
|
|
*status = U_UNSUPPORTED_ERROR;
|
2011-04-29 00:00:47 +00:00
|
|
|
return NULL;
|
2003-06-23 22:15:00 +00:00
|
|
|
}
|
2004-08-09 07:07:33 +00:00
|
|
|
|
2011-04-29 00:00:47 +00:00
|
|
|
if(retVal == NULL && U_SUCCESS(*status)) {
|
2004-08-09 07:07:33 +00:00
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
2004-03-12 20:15:13 +00:00
|
|
|
}
|
2004-08-09 07:07:33 +00:00
|
|
|
|
2011-04-29 00:00:47 +00:00
|
|
|
return reinterpret_cast<UNumberFormat *>(retVal);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
1999-08-16 21:50:52 +00:00
|
|
|
unum_close(UNumberFormat* fmt)
|
|
|
|
{
|
2003-06-23 22:15:00 +00:00
|
|
|
delete (NumberFormat*) fmt;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI UNumberFormat* U_EXPORT2
|
1999-08-16 21:50:52 +00:00
|
|
|
unum_clone(const UNumberFormat *fmt,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
2003-06-23 22:15:00 +00:00
|
|
|
if(U_FAILURE(*status))
|
|
|
|
return 0;
|
|
|
|
|
2004-03-12 20:15:13 +00:00
|
|
|
Format *res = 0;
|
2010-05-19 17:29:33 +00:00
|
|
|
const NumberFormat* nf = reinterpret_cast<const NumberFormat*>(fmt);
|
|
|
|
const DecimalFormat* df = dynamic_cast<const DecimalFormat*>(nf);
|
|
|
|
if (df != NULL) {
|
|
|
|
res = df->clone();
|
2004-03-12 20:15:13 +00:00
|
|
|
} else {
|
2010-05-19 17:29:33 +00:00
|
|
|
const RuleBasedNumberFormat* rbnf = dynamic_cast<const RuleBasedNumberFormat*>(nf);
|
|
|
|
U_ASSERT(rbnf != NULL);
|
|
|
|
res = rbnf->clone();
|
2004-03-12 20:15:13 +00:00
|
|
|
}
|
|
|
|
|
2003-06-23 22:15:00 +00:00
|
|
|
if(res == 0) {
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (UNumberFormat*) res;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
1999-08-16 21:50:52 +00:00
|
|
|
unum_format( const UNumberFormat* fmt,
|
2003-11-04 23:10:46 +00:00
|
|
|
int32_t number,
|
1999-08-16 21:50:52 +00:00
|
|
|
UChar* result,
|
2003-11-04 23:10:46 +00:00
|
|
|
int32_t resultLength,
|
1999-08-16 21:50:52 +00:00
|
|
|
UFieldPosition *pos,
|
2003-11-04 23:10:46 +00:00
|
|
|
UErrorCode* status)
|
|
|
|
{
|
|
|
|
return unum_formatInt64(fmt, number, result, resultLength, pos, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
unum_formatInt64(const UNumberFormat* fmt,
|
|
|
|
int64_t number,
|
|
|
|
UChar* result,
|
|
|
|
int32_t resultLength,
|
|
|
|
UFieldPosition *pos,
|
|
|
|
UErrorCode* status)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-06-23 22:15:00 +00:00
|
|
|
if(U_FAILURE(*status))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
UnicodeString res;
|
|
|
|
if(!(result==NULL && resultLength==0)) {
|
|
|
|
// NULL destination for pure preflighting: empty dummy string
|
|
|
|
// otherwise, alias the destination buffer
|
|
|
|
res.setTo(result, 0, resultLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
FieldPosition fp;
|
|
|
|
|
|
|
|
if(pos != 0)
|
|
|
|
fp.setField(pos->field);
|
|
|
|
|
2004-04-15 18:15:17 +00:00
|
|
|
((const NumberFormat*)fmt)->format(number, res, fp);
|
2003-06-23 22:15:00 +00:00
|
|
|
|
|
|
|
if(pos != 0) {
|
|
|
|
pos->beginIndex = fp.getBeginIndex();
|
|
|
|
pos->endIndex = fp.getEndIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.extract(result, resultLength, *status);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
1999-08-16 21:50:52 +00:00
|
|
|
unum_formatDouble( const UNumberFormat* fmt,
|
|
|
|
double number,
|
|
|
|
UChar* result,
|
|
|
|
int32_t resultLength,
|
|
|
|
UFieldPosition *pos, /* 0 if ignore */
|
|
|
|
UErrorCode* status)
|
|
|
|
{
|
2001-10-08 23:26:58 +00:00
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) return -1;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-11-16 01:14:40 +00:00
|
|
|
UnicodeString res;
|
|
|
|
if(!(result==NULL && resultLength==0)) {
|
|
|
|
// NULL destination for pure preflighting: empty dummy string
|
|
|
|
// otherwise, alias the destination buffer
|
|
|
|
res.setTo(result, 0, resultLength);
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
FieldPosition fp;
|
|
|
|
|
|
|
|
if(pos != 0)
|
|
|
|
fp.setField(pos->field);
|
|
|
|
|
2004-04-15 18:15:17 +00:00
|
|
|
((const NumberFormat*)fmt)->format(number, res, fp);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
if(pos != 0) {
|
|
|
|
pos->beginIndex = fp.getBeginIndex();
|
|
|
|
pos->endIndex = fp.getEndIndex();
|
|
|
|
}
|
|
|
|
|
2001-09-05 23:39:38 +00:00
|
|
|
return res.extract(result, resultLength, *status);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2010-10-12 05:11:41 +00:00
|
|
|
|
|
|
|
U_DRAFT int32_t U_EXPORT2
|
|
|
|
unum_formatDecimal(const UNumberFormat* fmt,
|
|
|
|
const char * number,
|
|
|
|
int32_t length,
|
|
|
|
UChar* result,
|
|
|
|
int32_t resultLength,
|
|
|
|
UFieldPosition *pos, /* 0 if ignore */
|
|
|
|
UErrorCode* status) {
|
|
|
|
|
|
|
|
if(U_FAILURE(*status)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((result == NULL && resultLength != 0) || resultLength < 0) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
FieldPosition fp;
|
|
|
|
if(pos != 0) {
|
|
|
|
fp.setField(pos->field);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length < 0) {
|
|
|
|
length = uprv_strlen(number);
|
|
|
|
}
|
|
|
|
StringPiece numSP(number, length);
|
|
|
|
Formattable numFmtbl(numSP, *status);
|
|
|
|
|
|
|
|
UnicodeString resultStr;
|
|
|
|
if (resultLength > 0) {
|
|
|
|
// Alias the destination buffer.
|
|
|
|
resultStr.setTo(result, 0, resultLength);
|
|
|
|
}
|
|
|
|
((const NumberFormat*)fmt)->format(numFmtbl, resultStr, fp, *status);
|
|
|
|
if(pos != 0) {
|
|
|
|
pos->beginIndex = fp.getBeginIndex();
|
|
|
|
pos->endIndex = fp.getEndIndex();
|
|
|
|
}
|
|
|
|
return resultStr.extract(result, resultLength, *status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-08-19 08:59:28 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2004-04-15 18:15:17 +00:00
|
|
|
unum_formatDoubleCurrency(const UNumberFormat* fmt,
|
|
|
|
double number,
|
|
|
|
UChar* currency,
|
|
|
|
UChar* result,
|
|
|
|
int32_t resultLength,
|
|
|
|
UFieldPosition* pos, /* ignored if 0 */
|
|
|
|
UErrorCode* status) {
|
|
|
|
if (U_FAILURE(*status)) return -1;
|
|
|
|
|
|
|
|
UnicodeString res;
|
|
|
|
if (!(result==NULL && resultLength==0)) {
|
|
|
|
// NULL destination for pure preflighting: empty dummy string
|
|
|
|
// otherwise, alias the destination buffer
|
|
|
|
res.setTo(result, 0, resultLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
FieldPosition fp;
|
|
|
|
if (pos != 0) {
|
|
|
|
fp.setField(pos->field);
|
|
|
|
}
|
2008-01-16 22:58:59 +00:00
|
|
|
CurrencyAmount *tempCurrAmnt = new CurrencyAmount(number, currency, *status);
|
|
|
|
// Check for null pointer.
|
|
|
|
if (tempCurrAmnt == NULL) {
|
2008-02-23 19:15:18 +00:00
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return -1;
|
2008-01-16 22:58:59 +00:00
|
|
|
}
|
|
|
|
Formattable n(tempCurrAmnt);
|
2004-04-15 18:15:17 +00:00
|
|
|
((const NumberFormat*)fmt)->format(n, res, fp, *status);
|
|
|
|
|
|
|
|
if (pos != 0) {
|
|
|
|
pos->beginIndex = fp.getBeginIndex();
|
|
|
|
pos->endIndex = fp.getEndIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.extract(result, resultLength, *status);
|
|
|
|
}
|
|
|
|
|
2003-11-04 23:10:46 +00:00
|
|
|
static void
|
|
|
|
parseRes(Formattable& res,
|
2004-04-15 18:15:17 +00:00
|
|
|
const UNumberFormat* fmt,
|
|
|
|
const UChar* text,
|
|
|
|
int32_t textLength,
|
|
|
|
int32_t *parsePos /* 0 = start */,
|
|
|
|
UBool parseCurrency,
|
|
|
|
UErrorCode *status)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-06-23 22:15:00 +00:00
|
|
|
if(U_FAILURE(*status))
|
2003-11-04 23:10:46 +00:00
|
|
|
return;
|
2003-06-23 22:15:00 +00:00
|
|
|
|
|
|
|
int32_t len = (textLength == -1 ? u_strlen(text) : textLength);
|
|
|
|
const UnicodeString src((UChar*)text, len, len);
|
|
|
|
ParsePosition pp;
|
|
|
|
|
|
|
|
if(parsePos != 0)
|
|
|
|
pp.setIndex(*parsePos);
|
|
|
|
|
2004-04-15 18:15:17 +00:00
|
|
|
if (parseCurrency) {
|
|
|
|
((const NumberFormat*)fmt)->parseCurrency(src, res, pp);
|
|
|
|
} else {
|
|
|
|
((const NumberFormat*)fmt)->parse(src, res, pp);
|
|
|
|
}
|
2003-06-23 22:15:00 +00:00
|
|
|
|
2009-02-19 08:41:08 +00:00
|
|
|
if(pp.getErrorIndex() != -1) {
|
|
|
|
*status = U_PARSE_ERROR;
|
|
|
|
if(parsePos != 0) {
|
2003-06-23 22:15:00 +00:00
|
|
|
*parsePos = pp.getErrorIndex();
|
|
|
|
}
|
2009-02-19 08:41:08 +00:00
|
|
|
} else if(parsePos != 0) {
|
|
|
|
*parsePos = pp.getIndex();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2003-11-04 23:10:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
unum_parse( const UNumberFormat* fmt,
|
|
|
|
const UChar* text,
|
|
|
|
int32_t textLength,
|
|
|
|
int32_t *parsePos /* 0 = start */,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
|
|
|
Formattable res;
|
2004-04-15 18:15:17 +00:00
|
|
|
parseRes(res, fmt, text, textLength, parsePos, FALSE, status);
|
2004-03-27 04:49:02 +00:00
|
|
|
return res.getLong(*status);
|
2003-11-04 23:10:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int64_t U_EXPORT2
|
|
|
|
unum_parseInt64( const UNumberFormat* fmt,
|
|
|
|
const UChar* text,
|
|
|
|
int32_t textLength,
|
|
|
|
int32_t *parsePos /* 0 = start */,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
|
|
|
Formattable res;
|
2004-04-15 18:15:17 +00:00
|
|
|
parseRes(res, fmt, text, textLength, parsePos, FALSE, status);
|
2004-03-27 04:49:02 +00:00
|
|
|
return res.getInt64(*status);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI double U_EXPORT2
|
1999-08-16 21:50:52 +00:00
|
|
|
unum_parseDouble( const UNumberFormat* fmt,
|
|
|
|
const UChar* text,
|
|
|
|
int32_t textLength,
|
|
|
|
int32_t *parsePos /* 0 = start */,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
2003-11-04 23:10:46 +00:00
|
|
|
Formattable res;
|
2004-04-15 18:15:17 +00:00
|
|
|
parseRes(res, fmt, text, textLength, parsePos, FALSE, status);
|
|
|
|
return res.getDouble(*status);
|
|
|
|
}
|
|
|
|
|
2010-10-12 05:11:41 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
unum_parseDecimal(const UNumberFormat* fmt,
|
|
|
|
const UChar* text,
|
|
|
|
int32_t textLength,
|
|
|
|
int32_t *parsePos /* 0 = start */,
|
|
|
|
char *outBuf,
|
|
|
|
int32_t outBufLength,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((outBuf == NULL && outBufLength != 0) || outBufLength < 0) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Formattable res;
|
|
|
|
parseRes(res, fmt, text, textLength, parsePos, FALSE, status);
|
|
|
|
StringPiece sp = res.getDecimalNumber(*status);
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return -1;
|
|
|
|
} else if (sp.size() > outBufLength) {
|
|
|
|
*status = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
} else if (sp.size() == outBufLength) {
|
|
|
|
uprv_strncpy(outBuf, sp.data(), sp.size());
|
|
|
|
*status = U_STRING_NOT_TERMINATED_WARNING;
|
|
|
|
} else {
|
|
|
|
uprv_strcpy(outBuf, sp.data());
|
|
|
|
}
|
|
|
|
return sp.size();
|
|
|
|
}
|
|
|
|
|
2007-08-19 08:59:28 +00:00
|
|
|
U_CAPI double U_EXPORT2
|
2004-04-15 18:15:17 +00:00
|
|
|
unum_parseDoubleCurrency(const UNumberFormat* fmt,
|
|
|
|
const UChar* text,
|
|
|
|
int32_t textLength,
|
|
|
|
int32_t* parsePos, /* 0 = start */
|
|
|
|
UChar* currency,
|
|
|
|
UErrorCode* status) {
|
|
|
|
Formattable res;
|
|
|
|
parseRes(res, fmt, text, textLength, parsePos, TRUE, status);
|
|
|
|
currency[0] = 0;
|
2010-05-19 17:29:33 +00:00
|
|
|
const CurrencyAmount* c;
|
2004-04-27 21:47:09 +00:00
|
|
|
if (res.getType() == Formattable::kObject &&
|
2010-05-19 17:29:33 +00:00
|
|
|
(c = dynamic_cast<const CurrencyAmount*>(res.getObject())) != NULL) {
|
2004-04-27 21:47:09 +00:00
|
|
|
u_strcpy(currency, c->getISOCurrency());
|
2004-04-15 18:15:17 +00:00
|
|
|
}
|
2004-03-27 04:49:02 +00:00
|
|
|
return res.getDouble(*status);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI const char* U_EXPORT2
|
1999-08-16 21:50:52 +00:00
|
|
|
unum_getAvailable(int32_t index)
|
|
|
|
{
|
2003-06-23 22:15:00 +00:00
|
|
|
return uloc_getAvailable(index);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
1999-08-16 21:50:52 +00:00
|
|
|
unum_countAvailable()
|
|
|
|
{
|
2003-06-23 22:15:00 +00:00
|
|
|
return uloc_countAvailable();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
1999-08-16 21:50:52 +00:00
|
|
|
unum_getAttribute(const UNumberFormat* fmt,
|
|
|
|
UNumberFormatAttribute attr)
|
|
|
|
{
|
2010-05-19 17:29:33 +00:00
|
|
|
const NumberFormat* nf = reinterpret_cast<const NumberFormat*>(fmt);
|
2011-05-04 12:23:42 +00:00
|
|
|
if ( attr == UNUM_LENIENT_PARSE ) {
|
|
|
|
// Supported for all subclasses
|
|
|
|
return nf->isLenient();
|
|
|
|
}
|
|
|
|
// The remaining attributea are only supported for DecimalFormat
|
2010-05-19 17:29:33 +00:00
|
|
|
const DecimalFormat* df = dynamic_cast<const DecimalFormat*>(nf);
|
|
|
|
if (df != NULL) {
|
2003-06-23 22:15:00 +00:00
|
|
|
switch(attr) {
|
|
|
|
case UNUM_PARSE_INT_ONLY:
|
2004-03-25 17:42:28 +00:00
|
|
|
return df->isParseIntegerOnly();
|
2003-06-23 22:15:00 +00:00
|
|
|
|
|
|
|
case UNUM_GROUPING_USED:
|
2004-03-25 17:42:28 +00:00
|
|
|
return df->isGroupingUsed();
|
2003-06-23 22:15:00 +00:00
|
|
|
|
|
|
|
case UNUM_DECIMAL_ALWAYS_SHOWN:
|
2004-03-25 17:42:28 +00:00
|
|
|
return df->isDecimalSeparatorAlwaysShown();
|
2003-06-23 22:15:00 +00:00
|
|
|
|
|
|
|
case UNUM_MAX_INTEGER_DIGITS:
|
2004-03-25 17:42:28 +00:00
|
|
|
return df->getMaximumIntegerDigits();
|
2003-06-23 22:15:00 +00:00
|
|
|
|
|
|
|
case UNUM_MIN_INTEGER_DIGITS:
|
2004-03-25 17:42:28 +00:00
|
|
|
return df->getMinimumIntegerDigits();
|
2003-06-23 22:15:00 +00:00
|
|
|
|
|
|
|
case UNUM_INTEGER_DIGITS:
|
|
|
|
// TBD: what should this return?
|
2004-03-25 17:42:28 +00:00
|
|
|
return df->getMinimumIntegerDigits();
|
2003-06-23 22:15:00 +00:00
|
|
|
|
|
|
|
case UNUM_MAX_FRACTION_DIGITS:
|
2004-03-25 17:42:28 +00:00
|
|
|
return df->getMaximumFractionDigits();
|
2003-06-23 22:15:00 +00:00
|
|
|
|
|
|
|
case UNUM_MIN_FRACTION_DIGITS:
|
2004-03-25 17:42:28 +00:00
|
|
|
return df->getMinimumFractionDigits();
|
2003-06-23 22:15:00 +00:00
|
|
|
|
|
|
|
case UNUM_FRACTION_DIGITS:
|
|
|
|
// TBD: what should this return?
|
2004-03-25 17:42:28 +00:00
|
|
|
return df->getMinimumFractionDigits();
|
|
|
|
|
2004-04-12 23:38:01 +00:00
|
|
|
case UNUM_SIGNIFICANT_DIGITS_USED:
|
|
|
|
return df->areSignificantDigitsUsed();
|
|
|
|
|
2004-03-25 17:42:28 +00:00
|
|
|
case UNUM_MAX_SIGNIFICANT_DIGITS:
|
|
|
|
return df->getMaximumSignificantDigits();
|
|
|
|
|
|
|
|
case UNUM_MIN_SIGNIFICANT_DIGITS:
|
|
|
|
return df->getMinimumSignificantDigits();
|
2003-06-23 22:15:00 +00:00
|
|
|
|
|
|
|
case UNUM_MULTIPLIER:
|
2004-03-25 17:42:28 +00:00
|
|
|
return df->getMultiplier();
|
2003-06-23 22:15:00 +00:00
|
|
|
|
|
|
|
case UNUM_GROUPING_SIZE:
|
2004-03-25 17:42:28 +00:00
|
|
|
return df->getGroupingSize();
|
2003-06-23 22:15:00 +00:00
|
|
|
|
|
|
|
case UNUM_ROUNDING_MODE:
|
2004-03-31 22:10:06 +00:00
|
|
|
return df->getRoundingMode();
|
2003-06-23 22:15:00 +00:00
|
|
|
|
|
|
|
case UNUM_FORMAT_WIDTH:
|
2004-03-31 22:10:06 +00:00
|
|
|
return df->getFormatWidth();
|
2003-06-23 22:15:00 +00:00
|
|
|
|
|
|
|
case UNUM_PADDING_POSITION:
|
2004-03-31 22:10:06 +00:00
|
|
|
return df->getPadPosition();
|
2003-06-23 22:15:00 +00:00
|
|
|
|
2004-07-18 10:22:23 +00:00
|
|
|
case UNUM_SECONDARY_GROUPING_SIZE:
|
|
|
|
return df->getSecondaryGroupingSize();
|
2011-05-04 12:23:42 +00:00
|
|
|
|
2003-06-23 22:15:00 +00:00
|
|
|
default:
|
2004-07-18 10:22:23 +00:00
|
|
|
/* enums out of sync? unsupported enum? */
|
2003-06-23 22:15:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-03-12 20:15:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
1999-08-16 21:50:52 +00:00
|
|
|
unum_setAttribute( UNumberFormat* fmt,
|
|
|
|
UNumberFormatAttribute attr,
|
|
|
|
int32_t newValue)
|
|
|
|
{
|
2010-05-19 17:29:33 +00:00
|
|
|
NumberFormat* nf = reinterpret_cast<NumberFormat*>(fmt);
|
2011-05-04 12:23:42 +00:00
|
|
|
if ( attr == UNUM_LENIENT_PARSE ) {
|
|
|
|
// Supported for all subclasses
|
|
|
|
return nf->setLenient(newValue != 0);
|
|
|
|
}
|
|
|
|
// The remaining attributea are only supported for DecimalFormat
|
2010-05-19 17:29:33 +00:00
|
|
|
DecimalFormat* df = dynamic_cast<DecimalFormat*>(nf);
|
|
|
|
if (df != NULL) {
|
2003-06-23 22:15:00 +00:00
|
|
|
switch(attr) {
|
|
|
|
case UNUM_PARSE_INT_ONLY:
|
2004-04-12 23:38:01 +00:00
|
|
|
df->setParseIntegerOnly(newValue!=0);
|
2003-06-23 22:15:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_GROUPING_USED:
|
2004-04-12 23:38:01 +00:00
|
|
|
df->setGroupingUsed(newValue!=0);
|
2003-06-23 22:15:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_DECIMAL_ALWAYS_SHOWN:
|
2004-04-12 23:38:01 +00:00
|
|
|
df->setDecimalSeparatorAlwaysShown(newValue!=0);
|
2003-06-23 22:15:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_MAX_INTEGER_DIGITS:
|
2004-03-25 17:42:28 +00:00
|
|
|
df->setMaximumIntegerDigits(newValue);
|
2003-06-23 22:15:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_MIN_INTEGER_DIGITS:
|
2004-03-25 17:42:28 +00:00
|
|
|
df->setMinimumIntegerDigits(newValue);
|
2003-06-23 22:15:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_INTEGER_DIGITS:
|
2004-03-25 17:42:28 +00:00
|
|
|
df->setMinimumIntegerDigits(newValue);
|
|
|
|
df->setMaximumIntegerDigits(newValue);
|
2003-06-23 22:15:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_MAX_FRACTION_DIGITS:
|
2004-03-25 17:42:28 +00:00
|
|
|
df->setMaximumFractionDigits(newValue);
|
2003-06-23 22:15:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_MIN_FRACTION_DIGITS:
|
2004-03-25 17:42:28 +00:00
|
|
|
df->setMinimumFractionDigits(newValue);
|
2003-06-23 22:15:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_FRACTION_DIGITS:
|
2004-03-25 17:42:28 +00:00
|
|
|
df->setMinimumFractionDigits(newValue);
|
|
|
|
df->setMaximumFractionDigits(newValue);
|
|
|
|
break;
|
|
|
|
|
2004-04-12 23:38:01 +00:00
|
|
|
case UNUM_SIGNIFICANT_DIGITS_USED:
|
|
|
|
df->setSignificantDigitsUsed(newValue!=0);
|
|
|
|
break;
|
|
|
|
|
2004-03-25 17:42:28 +00:00
|
|
|
case UNUM_MAX_SIGNIFICANT_DIGITS:
|
|
|
|
df->setMaximumSignificantDigits(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_MIN_SIGNIFICANT_DIGITS:
|
|
|
|
df->setMinimumSignificantDigits(newValue);
|
2003-06-23 22:15:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_MULTIPLIER:
|
2004-03-25 17:42:28 +00:00
|
|
|
df->setMultiplier(newValue);
|
2003-06-23 22:15:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_GROUPING_SIZE:
|
2004-03-25 17:42:28 +00:00
|
|
|
df->setGroupingSize(newValue);
|
2003-06-23 22:15:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_ROUNDING_MODE:
|
2004-03-25 17:42:28 +00:00
|
|
|
df->setRoundingMode((DecimalFormat::ERoundingMode)newValue);
|
2003-06-23 22:15:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_FORMAT_WIDTH:
|
2004-03-25 17:42:28 +00:00
|
|
|
df->setFormatWidth(newValue);
|
2003-06-23 22:15:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_PADDING_POSITION:
|
2004-03-25 17:42:28 +00:00
|
|
|
/** The position at which padding will take place. */
|
|
|
|
df->setPadPosition((DecimalFormat::EPadPosition)newValue);
|
2003-06-23 22:15:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_SECONDARY_GROUPING_SIZE:
|
2004-03-25 17:42:28 +00:00
|
|
|
df->setSecondaryGroupingSize(newValue);
|
2003-06-23 22:15:00 +00:00
|
|
|
break;
|
2011-05-04 12:23:42 +00:00
|
|
|
|
2003-06-23 22:15:00 +00:00
|
|
|
default:
|
|
|
|
/* Shouldn't get here anyway */
|
|
|
|
break;
|
|
|
|
}
|
2004-03-12 20:15:13 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI double U_EXPORT2
|
1999-08-16 21:50:52 +00:00
|
|
|
unum_getDoubleAttribute(const UNumberFormat* fmt,
|
|
|
|
UNumberFormatAttribute attr)
|
|
|
|
{
|
2010-05-19 17:29:33 +00:00
|
|
|
const NumberFormat* nf = reinterpret_cast<const NumberFormat*>(fmt);
|
|
|
|
const DecimalFormat* df = dynamic_cast<const DecimalFormat*>(nf);
|
|
|
|
if (df != NULL && attr == UNUM_ROUNDING_INCREMENT) {
|
|
|
|
return df->getRoundingIncrement();
|
2003-06-23 22:15:00 +00:00
|
|
|
} else {
|
|
|
|
return -1.0;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
1999-08-16 21:50:52 +00:00
|
|
|
unum_setDoubleAttribute( UNumberFormat* fmt,
|
|
|
|
UNumberFormatAttribute attr,
|
|
|
|
double newValue)
|
|
|
|
{
|
2010-05-19 17:29:33 +00:00
|
|
|
NumberFormat* nf = reinterpret_cast<NumberFormat*>(fmt);
|
|
|
|
DecimalFormat* df = dynamic_cast<DecimalFormat*>(nf);
|
|
|
|
if (df != NULL && attr == UNUM_ROUNDING_INCREMENT) {
|
|
|
|
df->setRoundingIncrement(newValue);
|
2003-06-23 22:15:00 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2001-03-28 23:23:23 +00:00
|
|
|
unum_getTextAttribute(const UNumberFormat* fmt,
|
1999-08-16 21:50:52 +00:00
|
|
|
UNumberFormatTextAttribute tag,
|
2001-03-28 23:23:23 +00:00
|
|
|
UChar* result,
|
|
|
|
int32_t resultLength,
|
|
|
|
UErrorCode* status)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-06-23 22:15:00 +00:00
|
|
|
if(U_FAILURE(*status))
|
|
|
|
return -1;
|
2004-11-12 07:07:41 +00:00
|
|
|
|
2003-06-23 22:15:00 +00:00
|
|
|
UnicodeString res;
|
|
|
|
if(!(result==NULL && resultLength==0)) {
|
|
|
|
// NULL destination for pure preflighting: empty dummy string
|
|
|
|
// otherwise, alias the destination buffer
|
|
|
|
res.setTo(result, 0, resultLength);
|
|
|
|
}
|
2004-11-12 07:07:41 +00:00
|
|
|
|
2010-05-19 17:29:33 +00:00
|
|
|
const NumberFormat* nf = reinterpret_cast<const NumberFormat*>(fmt);
|
|
|
|
const DecimalFormat* df = dynamic_cast<const DecimalFormat*>(nf);
|
|
|
|
if (df != NULL) {
|
2004-11-12 07:07:41 +00:00
|
|
|
switch(tag) {
|
|
|
|
case UNUM_POSITIVE_PREFIX:
|
|
|
|
df->getPositivePrefix(res);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_POSITIVE_SUFFIX:
|
|
|
|
df->getPositiveSuffix(res);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_NEGATIVE_PREFIX:
|
|
|
|
df->getNegativePrefix(res);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_NEGATIVE_SUFFIX:
|
|
|
|
df->getNegativeSuffix(res);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_PADDING_CHARACTER:
|
|
|
|
res = df->getPadCharacterString();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_CURRENCY_CODE:
|
|
|
|
res = UnicodeString(df->getCurrency());
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
*status = U_UNSUPPORTED_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
2004-03-12 20:15:13 +00:00
|
|
|
} else {
|
2010-05-19 17:29:33 +00:00
|
|
|
const RuleBasedNumberFormat* rbnf = dynamic_cast<const RuleBasedNumberFormat*>(nf);
|
|
|
|
U_ASSERT(rbnf != NULL);
|
2004-11-12 07:07:41 +00:00
|
|
|
if (tag == UNUM_DEFAULT_RULESET) {
|
|
|
|
res = rbnf->getDefaultRuleSetName();
|
|
|
|
} else if (tag == UNUM_PUBLIC_RULESETS) {
|
|
|
|
int32_t count = rbnf->getNumberOfRuleSetNames();
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
res += rbnf->getRuleSetName(i);
|
|
|
|
res += (UChar)0x003b; // semicolon
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*status = U_UNSUPPORTED_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
2003-06-23 22:15:00 +00:00
|
|
|
}
|
2004-11-12 07:07:41 +00:00
|
|
|
|
2003-06-23 22:15:00 +00:00
|
|
|
return res.extract(result, resultLength, *status);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
1999-08-16 21:50:52 +00:00
|
|
|
unum_setTextAttribute( UNumberFormat* fmt,
|
|
|
|
UNumberFormatTextAttribute tag,
|
|
|
|
const UChar* newValue,
|
|
|
|
int32_t newValueLength,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
2003-06-23 22:15:00 +00:00
|
|
|
if(U_FAILURE(*status))
|
|
|
|
return;
|
2010-05-19 17:29:33 +00:00
|
|
|
|
2011-07-07 18:46:19 +00:00
|
|
|
UnicodeString val(newValue, newValueLength);
|
2010-05-19 17:29:33 +00:00
|
|
|
NumberFormat* nf = reinterpret_cast<NumberFormat*>(fmt);
|
|
|
|
DecimalFormat* df = dynamic_cast<DecimalFormat*>(nf);
|
|
|
|
if (df != NULL) {
|
2004-03-12 20:15:13 +00:00
|
|
|
switch(tag) {
|
|
|
|
case UNUM_POSITIVE_PREFIX:
|
2004-03-25 17:42:28 +00:00
|
|
|
df->setPositivePrefix(val);
|
2003-06-23 22:15:00 +00:00
|
|
|
break;
|
|
|
|
|
2004-03-12 20:15:13 +00:00
|
|
|
case UNUM_POSITIVE_SUFFIX:
|
2004-03-25 17:42:28 +00:00
|
|
|
df->setPositiveSuffix(val);
|
2003-06-23 22:15:00 +00:00
|
|
|
break;
|
|
|
|
|
2004-03-12 20:15:13 +00:00
|
|
|
case UNUM_NEGATIVE_PREFIX:
|
2004-03-25 17:42:28 +00:00
|
|
|
df->setNegativePrefix(val);
|
2003-06-23 22:15:00 +00:00
|
|
|
break;
|
|
|
|
|
2004-03-12 20:15:13 +00:00
|
|
|
case UNUM_NEGATIVE_SUFFIX:
|
2004-03-25 17:42:28 +00:00
|
|
|
df->setNegativeSuffix(val);
|
2003-06-23 22:15:00 +00:00
|
|
|
break;
|
|
|
|
|
2004-03-12 20:15:13 +00:00
|
|
|
case UNUM_PADDING_CHARACTER:
|
2011-07-07 18:46:19 +00:00
|
|
|
df->setPadCharacter(val);
|
2003-06-23 22:15:00 +00:00
|
|
|
break;
|
|
|
|
|
2004-03-12 20:15:13 +00:00
|
|
|
case UNUM_CURRENCY_CODE:
|
2011-07-07 18:46:19 +00:00
|
|
|
df->setCurrency(val.getTerminatedBuffer(), *status);
|
2003-06-23 22:15:00 +00:00
|
|
|
break;
|
|
|
|
|
2004-03-12 20:15:13 +00:00
|
|
|
default:
|
2003-06-23 22:15:00 +00:00
|
|
|
*status = U_UNSUPPORTED_ERROR;
|
|
|
|
break;
|
2004-03-12 20:15:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-05-19 17:29:33 +00:00
|
|
|
RuleBasedNumberFormat* rbnf = dynamic_cast<RuleBasedNumberFormat*>(nf);
|
|
|
|
U_ASSERT(rbnf != NULL);
|
2004-03-12 20:15:13 +00:00
|
|
|
if (tag == UNUM_DEFAULT_RULESET) {
|
2011-07-07 18:46:19 +00:00
|
|
|
rbnf->setDefaultRuleSet(val, *status);
|
2004-03-12 20:15:13 +00:00
|
|
|
} else {
|
2010-05-19 17:29:33 +00:00
|
|
|
*status = U_UNSUPPORTED_ERROR;
|
2004-03-12 20:15:13 +00:00
|
|
|
}
|
2003-06-23 22:15:00 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
1999-08-16 21:50:52 +00:00
|
|
|
unum_toPattern( const UNumberFormat* fmt,
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool isPatternLocalized,
|
1999-08-16 21:50:52 +00:00
|
|
|
UChar* result,
|
|
|
|
int32_t resultLength,
|
|
|
|
UErrorCode* status)
|
|
|
|
{
|
2003-06-23 22:15:00 +00:00
|
|
|
if(U_FAILURE(*status))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
UnicodeString pat;
|
|
|
|
if(!(result==NULL && resultLength==0)) {
|
|
|
|
// NULL destination for pure preflighting: empty dummy string
|
|
|
|
// otherwise, alias the destination buffer
|
|
|
|
pat.setTo(result, 0, resultLength);
|
|
|
|
}
|
2004-03-12 20:15:13 +00:00
|
|
|
|
2010-05-19 17:29:33 +00:00
|
|
|
const NumberFormat* nf = reinterpret_cast<const NumberFormat*>(fmt);
|
|
|
|
const DecimalFormat* df = dynamic_cast<const DecimalFormat*>(nf);
|
|
|
|
if (df != NULL) {
|
2004-03-12 20:15:13 +00:00
|
|
|
if(isPatternLocalized)
|
2004-03-25 17:42:28 +00:00
|
|
|
df->toLocalizedPattern(pat);
|
2004-03-12 20:15:13 +00:00
|
|
|
else
|
2004-03-25 17:42:28 +00:00
|
|
|
df->toPattern(pat);
|
2004-03-12 20:15:13 +00:00
|
|
|
} else {
|
2010-05-19 17:29:33 +00:00
|
|
|
const RuleBasedNumberFormat* rbnf = dynamic_cast<const RuleBasedNumberFormat*>(nf);
|
|
|
|
U_ASSERT(rbnf != NULL);
|
|
|
|
pat = rbnf->getRules();
|
2004-03-12 20:15:13 +00:00
|
|
|
}
|
2003-06-23 22:15:00 +00:00
|
|
|
return pat.extract(result, resultLength, *status);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2000-05-25 01:10:55 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2004-03-27 07:21:32 +00:00
|
|
|
unum_getSymbol(const UNumberFormat *fmt,
|
2000-05-25 01:10:55 +00:00
|
|
|
UNumberFormatSymbol symbol,
|
|
|
|
UChar *buffer,
|
|
|
|
int32_t size,
|
2003-06-23 22:15:00 +00:00
|
|
|
UErrorCode *status)
|
|
|
|
{
|
|
|
|
if(status==NULL || U_FAILURE(*status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2011-03-22 16:58:58 +00:00
|
|
|
if(fmt==NULL || symbol< 0 || symbol>=UNUM_FORMAT_SYMBOL_COUNT) {
|
2003-06-23 22:15:00 +00:00
|
|
|
*status=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
2010-05-19 17:29:33 +00:00
|
|
|
const NumberFormat *nf = reinterpret_cast<const NumberFormat *>(fmt);
|
|
|
|
const DecimalFormat *dcf = dynamic_cast<const DecimalFormat *>(nf);
|
|
|
|
if (dcf == NULL) {
|
2004-03-12 20:15:13 +00:00
|
|
|
*status = U_UNSUPPORTED_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-19 17:29:33 +00:00
|
|
|
return dcf->
|
2002-10-15 00:36:11 +00:00
|
|
|
getDecimalFormatSymbols()->
|
|
|
|
getConstSymbol((DecimalFormatSymbols::ENumberFormatSymbol)symbol).
|
|
|
|
extract(buffer, size, *status);
|
2000-05-25 01:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
unum_setSymbol(UNumberFormat *fmt,
|
|
|
|
UNumberFormatSymbol symbol,
|
|
|
|
const UChar *value,
|
|
|
|
int32_t length,
|
2003-06-23 22:15:00 +00:00
|
|
|
UErrorCode *status)
|
2001-08-16 00:55:16 +00:00
|
|
|
{
|
2003-06-23 22:15:00 +00:00
|
|
|
if(status==NULL || U_FAILURE(*status)) {
|
|
|
|
return;
|
|
|
|
}
|
2011-03-22 16:58:58 +00:00
|
|
|
if(fmt==NULL || symbol< 0 || symbol>=UNUM_FORMAT_SYMBOL_COUNT || value==NULL || length<-1) {
|
2003-06-23 22:15:00 +00:00
|
|
|
*status=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2010-05-19 17:29:33 +00:00
|
|
|
NumberFormat *nf = reinterpret_cast<NumberFormat *>(fmt);
|
|
|
|
DecimalFormat *dcf = dynamic_cast<DecimalFormat *>(nf);
|
|
|
|
if (dcf == NULL) {
|
2004-03-12 20:15:13 +00:00
|
|
|
*status = U_UNSUPPORTED_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-19 17:29:33 +00:00
|
|
|
DecimalFormatSymbols symbols(*dcf->getDecimalFormatSymbols());
|
2003-06-23 22:15:00 +00:00
|
|
|
symbols.setSymbol((DecimalFormatSymbols::ENumberFormatSymbol)symbol,
|
|
|
|
UnicodeString(value, length)); /* UnicodeString can handle the case when length = -1. */
|
2010-05-19 17:29:33 +00:00
|
|
|
dcf->setDecimalFormatSymbols(symbols);
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2010-05-19 17:29:33 +00:00
|
|
|
unum_applyPattern( UNumberFormat *fmt,
|
2001-08-16 00:55:16 +00:00
|
|
|
UBool localized,
|
|
|
|
const UChar *pattern,
|
|
|
|
int32_t patternLength,
|
|
|
|
UParseError *parseError,
|
|
|
|
UErrorCode* status)
|
2000-07-20 23:47:59 +00:00
|
|
|
{
|
2003-06-23 22:15:00 +00:00
|
|
|
UErrorCode tStatus = U_ZERO_ERROR;
|
|
|
|
UParseError tParseError;
|
|
|
|
|
|
|
|
if(parseError == NULL){
|
|
|
|
parseError = &tParseError;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(status==NULL){
|
|
|
|
status = &tStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t len = (patternLength == -1 ? u_strlen(pattern) : patternLength);
|
|
|
|
const UnicodeString pat((UChar*)pattern, len, len);
|
2010-05-19 17:29:33 +00:00
|
|
|
|
2003-06-23 22:15:00 +00:00
|
|
|
// Verify if the object passed is a DecimalFormat object
|
2010-05-19 17:29:33 +00:00
|
|
|
NumberFormat* nf = reinterpret_cast<NumberFormat*>(fmt);
|
|
|
|
DecimalFormat* df = dynamic_cast<DecimalFormat*>(nf);
|
|
|
|
if (df != NULL) {
|
2004-03-12 20:15:13 +00:00
|
|
|
if(localized) {
|
2010-05-19 17:29:33 +00:00
|
|
|
df->applyLocalizedPattern(pat,*parseError, *status);
|
2004-03-12 20:15:13 +00:00
|
|
|
} else {
|
2010-05-19 17:29:33 +00:00
|
|
|
df->applyPattern(pat,*parseError, *status);
|
2004-03-12 20:15:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*status = U_UNSUPPORTED_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2000-07-20 23:47:59 +00:00
|
|
|
}
|
|
|
|
|
2003-11-14 21:58:52 +00:00
|
|
|
U_CAPI const char* U_EXPORT2
|
|
|
|
unum_getLocaleByType(const UNumberFormat *fmt,
|
|
|
|
ULocDataLocaleType type,
|
|
|
|
UErrorCode* status)
|
|
|
|
{
|
2004-01-20 23:06:38 +00:00
|
|
|
if (fmt == NULL) {
|
|
|
|
if (U_SUCCESS(*status)) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-03-25 17:42:28 +00:00
|
|
|
return ((const Format*)fmt)->getLocaleID(type, *status);
|
2003-11-14 21:58:52 +00:00
|
|
|
}
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|