1999-08-16 21:50:52 +00:00
|
|
|
/*
|
1999-11-22 20:25:35 +00:00
|
|
|
*******************************************************************************
|
2013-06-26 06:31:09 +00:00
|
|
|
* Copyright (C) 1997-2013, International Business Machines Corporation and *
|
1999-11-22 20:25:35 +00:00
|
|
|
* others. All Rights Reserved. *
|
|
|
|
*******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*
|
|
|
|
* File FMTABLE.CPP
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 03/25/97 clhuang Initial Implementation.
|
|
|
|
********************************************************************************
|
|
|
|
*/
|
2002-09-20 01:54:48 +00:00
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
2011-01-04 07:42:32 +00:00
|
|
|
#include <math.h>
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/fmtable.h"
|
2004-04-15 06:19:55 +00:00
|
|
|
#include "unicode/ustring.h"
|
2004-04-27 21:47:09 +00:00
|
|
|
#include "unicode/measure.h"
|
|
|
|
#include "unicode/curramt.h"
|
2013-06-26 06:31:09 +00:00
|
|
|
#include "unicode/uformattable.h"
|
2010-05-20 21:16:44 +00:00
|
|
|
#include "charstr.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "cmemory.h"
|
2010-02-26 02:29:00 +00:00
|
|
|
#include "cstring.h"
|
|
|
|
#include "decNumber.h"
|
|
|
|
#include "digitlst.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// *****************************************************************************
|
|
|
|
// class Formattable
|
|
|
|
// *****************************************************************************
|
2001-03-06 19:44:45 +00:00
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2003-08-31 20:53:46 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Formattable)
|
2002-06-29 00:04:16 +00:00
|
|
|
|
2012-11-29 01:09:50 +00:00
|
|
|
#include "fmtableimp.h"
|
2012-05-30 00:41:57 +00:00
|
|
|
|
2004-04-27 21:47:09 +00:00
|
|
|
//-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
|
|
|
|
|
|
|
|
// NOTE: As of 3.0, there are limitations to the UObject API. It does
|
2010-05-19 17:29:33 +00:00
|
|
|
// not (yet) support cloning, operator=, nor operator==. To
|
2004-04-27 21:47:09 +00:00
|
|
|
// work around this, I implement some simple inlines here. Later
|
|
|
|
// these can be modified or removed. [alan]
|
|
|
|
|
|
|
|
// NOTE: These inlines assume that all fObjects are in fact instances
|
|
|
|
// of the Measure class, which is true as of 3.0. [alan]
|
|
|
|
|
|
|
|
// Return TRUE if *a == *b.
|
2006-01-14 09:24:33 +00:00
|
|
|
static inline UBool objectEquals(const UObject* a, const UObject* b) {
|
2004-04-27 21:47:09 +00:00
|
|
|
// LATER: return *a == *b;
|
|
|
|
return *((const Measure*) a) == *((const Measure*) b);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a clone of *a.
|
2006-01-14 09:24:33 +00:00
|
|
|
static inline UObject* objectClone(const UObject* a) {
|
2004-04-27 21:47:09 +00:00
|
|
|
// LATER: return a->clone();
|
|
|
|
return ((const Measure*) a)->clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return TRUE if *a is an instance of Measure.
|
2006-01-14 09:24:33 +00:00
|
|
|
static inline UBool instanceOfMeasure(const UObject* a) {
|
2010-05-19 17:29:33 +00:00
|
|
|
return dynamic_cast<const Measure*>(a) != NULL;
|
2004-04-27 21:47:09 +00:00
|
|
|
}
|
|
|
|
|
2006-01-14 09:24:33 +00:00
|
|
|
/**
|
|
|
|
* Creates a new Formattable array and copies the values from the specified
|
|
|
|
* original.
|
|
|
|
* @param array the original array
|
|
|
|
* @param count the original array count
|
|
|
|
* @return the new Formattable array.
|
|
|
|
*/
|
2010-02-26 02:29:00 +00:00
|
|
|
static Formattable* createArrayCopy(const Formattable* array, int32_t count) {
|
2006-01-14 09:24:33 +00:00
|
|
|
Formattable *result = new Formattable[count];
|
2008-01-11 19:42:41 +00:00
|
|
|
if (result != NULL) {
|
2008-02-23 19:15:18 +00:00
|
|
|
for (int32_t i=0; i<count; ++i)
|
|
|
|
result[i] = array[i]; // Don't memcpy!
|
2008-01-11 19:42:41 +00:00
|
|
|
}
|
2006-01-14 09:24:33 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-04-27 21:47:09 +00:00
|
|
|
//-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
|
|
|
|
|
2004-03-27 04:49:02 +00:00
|
|
|
/**
|
|
|
|
* Set 'ec' to 'err' only if 'ec' is not already set to a failing UErrorCode.
|
|
|
|
*/
|
2010-02-26 02:29:00 +00:00
|
|
|
static void setError(UErrorCode& ec, UErrorCode err) {
|
2004-03-27 04:49:02 +00:00
|
|
|
if (U_SUCCESS(ec)) {
|
|
|
|
ec = err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-26 02:29:00 +00:00
|
|
|
//
|
|
|
|
// Common initialization code, shared by constructors.
|
|
|
|
// Put everything into a known state.
|
|
|
|
//
|
|
|
|
void Formattable::init() {
|
|
|
|
fValue.fInt64 = 0;
|
|
|
|
fType = kLong;
|
|
|
|
fDecimalStr = NULL;
|
|
|
|
fDecimalNum = NULL;
|
|
|
|
fBogus.setToBogus();
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// default constructor.
|
|
|
|
// Creates a formattable object with a long value 0.
|
2001-03-06 19:44:45 +00:00
|
|
|
|
2010-02-26 02:29:00 +00:00
|
|
|
Formattable::Formattable() {
|
|
|
|
init();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Creates a formattable object with a Date instance.
|
|
|
|
|
2000-08-15 18:25:20 +00:00
|
|
|
Formattable::Formattable(UDate date, ISDATE /*isDate*/)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2010-02-26 02:29:00 +00:00
|
|
|
init();
|
|
|
|
fType = kDate;
|
1999-08-16 21:50:52 +00:00
|
|
|
fValue.fDate = date;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Creates a formattable object with a double value.
|
|
|
|
|
|
|
|
Formattable::Formattable(double value)
|
|
|
|
{
|
2010-02-26 02:29:00 +00:00
|
|
|
init();
|
|
|
|
fType = kDouble;
|
1999-08-16 21:50:52 +00:00
|
|
|
fValue.fDouble = value;
|
|
|
|
}
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
2010-02-26 02:29:00 +00:00
|
|
|
// Creates a formattable object with an int32_t value.
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
Formattable::Formattable(int32_t value)
|
|
|
|
{
|
2010-02-26 02:29:00 +00:00
|
|
|
init();
|
2003-10-31 22:47:25 +00:00
|
|
|
fValue.fInt64 = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
2010-02-26 02:29:00 +00:00
|
|
|
// Creates a formattable object with an int64_t value.
|
2003-10-31 22:47:25 +00:00
|
|
|
|
|
|
|
Formattable::Formattable(int64_t value)
|
|
|
|
{
|
2010-02-26 02:29:00 +00:00
|
|
|
init();
|
|
|
|
fType = kInt64;
|
2003-10-31 22:47:25 +00:00
|
|
|
fValue.fInt64 = value;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-03-06 19:44:45 +00:00
|
|
|
|
2010-02-26 02:29:00 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Creates a formattable object with a decimal number value from a string.
|
|
|
|
|
|
|
|
Formattable::Formattable(const StringPiece &number, UErrorCode &status) {
|
|
|
|
init();
|
|
|
|
setDecimalNumber(number, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Creates a formattable object with a UnicodeString instance.
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
Formattable::Formattable(const UnicodeString& stringToCopy)
|
|
|
|
{
|
2010-02-26 02:29:00 +00:00
|
|
|
init();
|
|
|
|
fType = kString;
|
1999-08-16 21:50:52 +00:00
|
|
|
fValue.fString = new UnicodeString(stringToCopy);
|
|
|
|
}
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Creates a formattable object with a UnicodeString* value.
|
|
|
|
// (adopting symantics)
|
|
|
|
|
|
|
|
Formattable::Formattable(UnicodeString* stringToAdopt)
|
|
|
|
{
|
2010-02-26 02:29:00 +00:00
|
|
|
init();
|
|
|
|
fType = kString;
|
1999-08-16 21:50:52 +00:00
|
|
|
fValue.fString = stringToAdopt;
|
2004-04-27 21:47:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Formattable::Formattable(UObject* objectToAdopt)
|
|
|
|
{
|
2010-02-26 02:29:00 +00:00
|
|
|
init();
|
|
|
|
fType = kObject;
|
2004-04-27 21:47:09 +00:00
|
|
|
fValue.fObject = objectToAdopt;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
Formattable::Formattable(const Formattable* arrayToCopy, int32_t count)
|
2002-07-02 23:58:34 +00:00
|
|
|
: UObject(), fType(kArray)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2010-02-26 02:29:00 +00:00
|
|
|
init();
|
|
|
|
fType = kArray;
|
1999-08-16 21:50:52 +00:00
|
|
|
fValue.fArrayAndCount.fArray = createArrayCopy(arrayToCopy, count);
|
|
|
|
fValue.fArrayAndCount.fCount = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// copy constructor
|
|
|
|
|
2010-02-26 02:29:00 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
Formattable::Formattable(const Formattable &source)
|
2010-02-26 02:29:00 +00:00
|
|
|
: UObject(*this)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2010-02-26 02:29:00 +00:00
|
|
|
init();
|
1999-08-16 21:50:52 +00:00
|
|
|
*this = source;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// assignment operator
|
|
|
|
|
|
|
|
Formattable&
|
|
|
|
Formattable::operator=(const Formattable& source)
|
|
|
|
{
|
|
|
|
if (this != &source)
|
|
|
|
{
|
|
|
|
// Disposes the current formattable value/setting.
|
|
|
|
dispose();
|
|
|
|
|
|
|
|
// Sets the correct data type for this value.
|
|
|
|
fType = source.fType;
|
|
|
|
switch (fType)
|
|
|
|
{
|
|
|
|
case kArray:
|
|
|
|
// Sets each element in the array one by one and records the array count.
|
|
|
|
fValue.fArrayAndCount.fCount = source.fValue.fArrayAndCount.fCount;
|
|
|
|
fValue.fArrayAndCount.fArray = createArrayCopy(source.fValue.fArrayAndCount.fArray,
|
|
|
|
source.fValue.fArrayAndCount.fCount);
|
|
|
|
break;
|
|
|
|
case kString:
|
|
|
|
// Sets the string value.
|
|
|
|
fValue.fString = new UnicodeString(*source.fValue.fString);
|
|
|
|
break;
|
|
|
|
case kDouble:
|
|
|
|
// Sets the double value.
|
|
|
|
fValue.fDouble = source.fValue.fDouble;
|
|
|
|
break;
|
|
|
|
case kLong:
|
2003-11-04 23:10:46 +00:00
|
|
|
case kInt64:
|
1999-08-16 21:50:52 +00:00
|
|
|
// Sets the long value.
|
2003-10-31 22:47:25 +00:00
|
|
|
fValue.fInt64 = source.fValue.fInt64;
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
case kDate:
|
|
|
|
// Sets the Date value.
|
|
|
|
fValue.fDate = source.fValue.fDate;
|
|
|
|
break;
|
2004-04-27 21:47:09 +00:00
|
|
|
case kObject:
|
|
|
|
fValue.fObject = objectClone(source.fValue.fObject);
|
|
|
|
break;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2010-02-26 02:29:00 +00:00
|
|
|
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
if (source.fDecimalNum != NULL) {
|
2012-05-30 00:41:57 +00:00
|
|
|
fDecimalNum = new DigitList(*source.fDecimalNum); // TODO: use internal digit list
|
2010-02-26 02:29:00 +00:00
|
|
|
}
|
|
|
|
if (source.fDecimalStr != NULL) {
|
2010-05-20 21:16:44 +00:00
|
|
|
fDecimalStr = new CharString(*source.fDecimalStr, status);
|
2010-02-26 02:29:00 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete fDecimalStr;
|
|
|
|
fDecimalStr = NULL;
|
|
|
|
}
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool
|
1999-08-16 21:50:52 +00:00
|
|
|
Formattable::operator==(const Formattable& that) const
|
|
|
|
{
|
2004-04-28 04:04:36 +00:00
|
|
|
int32_t i;
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
if (this == &that) return TRUE;
|
|
|
|
|
|
|
|
// Returns FALSE if the data types are different.
|
|
|
|
if (fType != that.fType) return FALSE;
|
|
|
|
|
|
|
|
// Compares the actual data values.
|
2004-04-15 06:19:55 +00:00
|
|
|
UBool equal = TRUE;
|
1999-08-16 21:50:52 +00:00
|
|
|
switch (fType) {
|
|
|
|
case kDate:
|
2004-04-15 06:19:55 +00:00
|
|
|
equal = (fValue.fDate == that.fValue.fDate);
|
|
|
|
break;
|
1999-08-16 21:50:52 +00:00
|
|
|
case kDouble:
|
2004-04-15 06:19:55 +00:00
|
|
|
equal = (fValue.fDouble == that.fValue.fDouble);
|
|
|
|
break;
|
1999-08-16 21:50:52 +00:00
|
|
|
case kLong:
|
2004-04-15 06:19:55 +00:00
|
|
|
case kInt64:
|
|
|
|
equal = (fValue.fInt64 == that.fValue.fInt64);
|
|
|
|
break;
|
1999-08-16 21:50:52 +00:00
|
|
|
case kString:
|
2004-04-15 06:19:55 +00:00
|
|
|
equal = (*(fValue.fString) == *(that.fValue.fString));
|
|
|
|
break;
|
1999-08-16 21:50:52 +00:00
|
|
|
case kArray:
|
2004-04-15 06:19:55 +00:00
|
|
|
if (fValue.fArrayAndCount.fCount != that.fValue.fArrayAndCount.fCount) {
|
|
|
|
equal = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
// Checks each element for equality.
|
2004-04-28 04:04:36 +00:00
|
|
|
for (i=0; i<fValue.fArrayAndCount.fCount; ++i) {
|
2004-04-15 06:19:55 +00:00
|
|
|
if (fValue.fArrayAndCount.fArray[i] != that.fValue.fArrayAndCount.fArray[i]) {
|
|
|
|
equal = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
2004-04-27 21:47:09 +00:00
|
|
|
case kObject:
|
2008-02-23 19:15:18 +00:00
|
|
|
if (fValue.fObject == NULL || that.fValue.fObject == NULL) {
|
|
|
|
equal = FALSE;
|
|
|
|
} else {
|
|
|
|
equal = objectEquals(fValue.fObject, that.fValue.fObject);
|
|
|
|
}
|
2004-04-27 21:47:09 +00:00
|
|
|
break;
|
2004-04-15 06:19:55 +00:00
|
|
|
}
|
|
|
|
|
2010-02-26 02:29:00 +00:00
|
|
|
// TODO: compare digit lists if numeric.
|
2004-04-15 06:19:55 +00:00
|
|
|
return equal;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
Formattable::~Formattable()
|
|
|
|
{
|
|
|
|
dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
void Formattable::dispose()
|
|
|
|
{
|
|
|
|
// Deletes the data value if necessary.
|
|
|
|
switch (fType) {
|
|
|
|
case kString:
|
|
|
|
delete fValue.fString;
|
|
|
|
break;
|
|
|
|
case kArray:
|
|
|
|
delete[] fValue.fArrayAndCount.fArray;
|
|
|
|
break;
|
2004-04-27 21:47:09 +00:00
|
|
|
case kObject:
|
|
|
|
delete fValue.fObject;
|
|
|
|
break;
|
2004-03-27 04:49:02 +00:00
|
|
|
default:
|
2001-03-06 19:44:45 +00:00
|
|
|
break;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2010-02-26 02:29:00 +00:00
|
|
|
|
|
|
|
fType = kLong;
|
|
|
|
fValue.fInt64 = 0;
|
2012-05-30 00:41:57 +00:00
|
|
|
|
2010-02-26 02:29:00 +00:00
|
|
|
delete fDecimalStr;
|
|
|
|
fDecimalStr = NULL;
|
2012-05-30 00:41:57 +00:00
|
|
|
|
|
|
|
FmtStackData *stackData = (FmtStackData*)fStackData;
|
|
|
|
if(fDecimalNum != &(stackData->stackDecimalNum)) {
|
|
|
|
delete fDecimalNum;
|
|
|
|
} else {
|
|
|
|
fDecimalNum->~DigitList(); // destruct, don't deallocate
|
|
|
|
}
|
2010-02-26 02:29:00 +00:00
|
|
|
fDecimalNum = NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2003-11-05 21:59:41 +00:00
|
|
|
Formattable *
|
|
|
|
Formattable::clone() const {
|
|
|
|
return new Formattable(*this);
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Gets the data type of this Formattable object.
|
|
|
|
Formattable::Type
|
|
|
|
Formattable::getType() const
|
|
|
|
{
|
|
|
|
return fType;
|
|
|
|
}
|
2001-03-06 19:44:45 +00:00
|
|
|
|
2004-04-27 21:47:09 +00:00
|
|
|
UBool
|
|
|
|
Formattable::isNumeric() const {
|
|
|
|
switch (fType) {
|
|
|
|
case kDouble:
|
|
|
|
case kLong:
|
|
|
|
case kInt64:
|
|
|
|
return TRUE;
|
|
|
|
default:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-04 23:10:46 +00:00
|
|
|
// -------------------------------------
|
|
|
|
int32_t
|
2004-03-27 04:49:02 +00:00
|
|
|
//Formattable::getLong(UErrorCode* status) const
|
|
|
|
Formattable::getLong(UErrorCode& status) const
|
2003-11-04 23:10:46 +00:00
|
|
|
{
|
2004-03-27 04:49:02 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2003-11-04 23:10:46 +00:00
|
|
|
return 0;
|
2004-03-27 04:49:02 +00:00
|
|
|
}
|
2003-11-04 23:10:46 +00:00
|
|
|
|
|
|
|
switch (fType) {
|
|
|
|
case Formattable::kLong:
|
|
|
|
return (int32_t)fValue.fInt64;
|
2013-09-20 05:00:30 +00:00
|
|
|
case Formattable::kInt64:
|
2003-11-04 23:10:46 +00:00
|
|
|
if (fValue.fInt64 > INT32_MAX) {
|
2004-03-27 04:49:02 +00:00
|
|
|
status = U_INVALID_FORMAT_ERROR;
|
2003-11-04 23:10:46 +00:00
|
|
|
return INT32_MAX;
|
|
|
|
} else if (fValue.fInt64 < INT32_MIN) {
|
2004-03-27 04:49:02 +00:00
|
|
|
status = U_INVALID_FORMAT_ERROR;
|
2003-11-04 23:10:46 +00:00
|
|
|
return INT32_MIN;
|
|
|
|
} else {
|
|
|
|
return (int32_t)fValue.fInt64;
|
|
|
|
}
|
|
|
|
case Formattable::kDouble:
|
|
|
|
if (fValue.fDouble > INT32_MAX) {
|
2004-03-27 04:49:02 +00:00
|
|
|
status = U_INVALID_FORMAT_ERROR;
|
2003-11-04 23:10:46 +00:00
|
|
|
return INT32_MAX;
|
|
|
|
} else if (fValue.fDouble < INT32_MIN) {
|
2004-03-27 04:49:02 +00:00
|
|
|
status = U_INVALID_FORMAT_ERROR;
|
2003-11-04 23:10:46 +00:00
|
|
|
return INT32_MIN;
|
|
|
|
} else {
|
2004-03-27 04:49:02 +00:00
|
|
|
return (int32_t)fValue.fDouble; // loses fraction
|
2003-11-04 23:10:46 +00:00
|
|
|
}
|
2004-04-27 21:47:09 +00:00
|
|
|
case Formattable::kObject:
|
2008-02-23 19:15:18 +00:00
|
|
|
if (fValue.fObject == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
2004-04-27 21:47:09 +00:00
|
|
|
// TODO Later replace this with instanceof call
|
|
|
|
if (instanceOfMeasure(fValue.fObject)) {
|
|
|
|
return ((const Measure*) fValue.fObject)->
|
|
|
|
getNumber().getLong(status);
|
|
|
|
}
|
2003-11-04 23:10:46 +00:00
|
|
|
default:
|
2004-03-27 04:49:02 +00:00
|
|
|
status = U_INVALID_FORMAT_ERROR;
|
2003-11-04 23:10:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
2011-01-04 07:42:32 +00:00
|
|
|
// Maximum int that can be represented exactly in a double. (53 bits)
|
|
|
|
// Larger ints may be rounded to a near-by value as not all are representable.
|
|
|
|
// TODO: move this constant elsewhere, possibly configure it for different
|
|
|
|
// floating point formats, if any non-standard ones are still in use.
|
2011-01-04 19:01:12 +00:00
|
|
|
static const int64_t U_DOUBLE_MAX_EXACT_INT = 9007199254740992LL;
|
2011-01-04 07:42:32 +00:00
|
|
|
|
2003-11-04 23:10:46 +00:00
|
|
|
int64_t
|
2004-03-27 04:49:02 +00:00
|
|
|
Formattable::getInt64(UErrorCode& status) const
|
2003-11-04 23:10:46 +00:00
|
|
|
{
|
2004-03-27 04:49:02 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2003-11-04 23:10:46 +00:00
|
|
|
return 0;
|
2004-03-27 04:49:02 +00:00
|
|
|
}
|
2003-11-04 23:10:46 +00:00
|
|
|
|
|
|
|
switch (fType) {
|
|
|
|
case Formattable::kLong:
|
|
|
|
case Formattable::kInt64:
|
|
|
|
return fValue.fInt64;
|
|
|
|
case Formattable::kDouble:
|
2011-01-04 07:42:32 +00:00
|
|
|
if (fValue.fDouble > (double)U_INT64_MAX) {
|
2004-03-27 04:49:02 +00:00
|
|
|
status = U_INVALID_FORMAT_ERROR;
|
2003-11-05 01:49:45 +00:00
|
|
|
return U_INT64_MAX;
|
2011-01-04 07:42:32 +00:00
|
|
|
} else if (fValue.fDouble < (double)U_INT64_MIN) {
|
2004-03-27 04:49:02 +00:00
|
|
|
status = U_INVALID_FORMAT_ERROR;
|
2003-11-05 01:49:45 +00:00
|
|
|
return U_INT64_MIN;
|
2011-01-04 07:42:32 +00:00
|
|
|
} else if (fabs(fValue.fDouble) > U_DOUBLE_MAX_EXACT_INT && fDecimalNum != NULL) {
|
|
|
|
int64_t val = fDecimalNum->getInt64();
|
|
|
|
if (val != 0) {
|
|
|
|
return val;
|
|
|
|
} else {
|
|
|
|
status = U_INVALID_FORMAT_ERROR;
|
|
|
|
return fValue.fDouble > 0 ? U_INT64_MAX : U_INT64_MIN;
|
|
|
|
}
|
2003-11-04 23:10:46 +00:00
|
|
|
} else {
|
|
|
|
return (int64_t)fValue.fDouble;
|
2011-01-04 07:42:32 +00:00
|
|
|
}
|
2004-04-27 21:47:09 +00:00
|
|
|
case Formattable::kObject:
|
2008-02-23 19:15:18 +00:00
|
|
|
if (fValue.fObject == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
2004-04-27 21:47:09 +00:00
|
|
|
if (instanceOfMeasure(fValue.fObject)) {
|
|
|
|
return ((const Measure*) fValue.fObject)->
|
|
|
|
getNumber().getInt64(status);
|
|
|
|
}
|
2003-11-04 23:10:46 +00:00
|
|
|
default:
|
2004-03-27 04:49:02 +00:00
|
|
|
status = U_INVALID_FORMAT_ERROR;
|
2003-11-04 23:10:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
double
|
2004-03-27 04:49:02 +00:00
|
|
|
Formattable::getDouble(UErrorCode& status) const
|
2003-11-04 23:10:46 +00:00
|
|
|
{
|
2004-03-27 04:49:02 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2003-11-04 23:10:46 +00:00
|
|
|
return 0;
|
2004-03-27 04:49:02 +00:00
|
|
|
}
|
2003-11-04 23:10:46 +00:00
|
|
|
|
|
|
|
switch (fType) {
|
|
|
|
case Formattable::kLong:
|
|
|
|
case Formattable::kInt64: // loses precision
|
|
|
|
return (double)fValue.fInt64;
|
|
|
|
case Formattable::kDouble:
|
|
|
|
return fValue.fDouble;
|
2004-04-27 21:47:09 +00:00
|
|
|
case Formattable::kObject:
|
2008-02-23 19:15:18 +00:00
|
|
|
if (fValue.fObject == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
2004-04-27 21:47:09 +00:00
|
|
|
// TODO Later replace this with instanceof call
|
|
|
|
if (instanceOfMeasure(fValue.fObject)) {
|
|
|
|
return ((const Measure*) fValue.fObject)->
|
|
|
|
getNumber().getDouble(status);
|
|
|
|
}
|
2003-11-04 23:10:46 +00:00
|
|
|
default:
|
2004-03-27 04:49:02 +00:00
|
|
|
status = U_INVALID_FORMAT_ERROR;
|
2003-11-04 23:10:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-27 21:47:09 +00:00
|
|
|
const UObject*
|
|
|
|
Formattable::getObject() const {
|
|
|
|
return (fType == kObject) ? fValue.fObject : NULL;
|
2004-04-15 06:19:55 +00:00
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Sets the value to a double value d.
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
|
|
|
Formattable::setDouble(double d)
|
|
|
|
{
|
|
|
|
dispose();
|
|
|
|
fType = kDouble;
|
|
|
|
fValue.fDouble = d;
|
|
|
|
}
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Sets the value to a long value l.
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
|
|
|
Formattable::setLong(int32_t l)
|
|
|
|
{
|
|
|
|
dispose();
|
|
|
|
fType = kLong;
|
2003-10-31 22:47:25 +00:00
|
|
|
fValue.fInt64 = l;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Sets the value to an int64 value ll.
|
|
|
|
|
|
|
|
void
|
|
|
|
Formattable::setInt64(int64_t ll)
|
|
|
|
{
|
|
|
|
dispose();
|
|
|
|
fType = kInt64;
|
|
|
|
fValue.fInt64 = ll;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Sets the value to a Date instance d.
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
|
|
|
Formattable::setDate(UDate d)
|
|
|
|
{
|
|
|
|
dispose();
|
|
|
|
fType = kDate;
|
|
|
|
fValue.fDate = d;
|
|
|
|
}
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Sets the value to a string value stringToCopy.
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
|
|
|
Formattable::setString(const UnicodeString& stringToCopy)
|
|
|
|
{
|
|
|
|
dispose();
|
|
|
|
fType = kString;
|
|
|
|
fValue.fString = new UnicodeString(stringToCopy);
|
|
|
|
}
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Sets the value to an array of Formattable objects.
|
|
|
|
|
|
|
|
void
|
|
|
|
Formattable::setArray(const Formattable* array, int32_t count)
|
|
|
|
{
|
|
|
|
dispose();
|
|
|
|
fType = kArray;
|
|
|
|
fValue.fArrayAndCount.fArray = createArrayCopy(array, count);
|
|
|
|
fValue.fArrayAndCount.fCount = count;
|
2001-03-06 19:44:45 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Adopts the stringToAdopt value.
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
|
|
|
Formattable::adoptString(UnicodeString* stringToAdopt)
|
|
|
|
{
|
|
|
|
dispose();
|
|
|
|
fType = kString;
|
|
|
|
fValue.fString = stringToAdopt;
|
|
|
|
}
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Adopts the array value and its count.
|
2001-03-06 19:44:45 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
|
|
|
Formattable::adoptArray(Formattable* array, int32_t count)
|
|
|
|
{
|
|
|
|
dispose();
|
|
|
|
fType = kArray;
|
|
|
|
fValue.fArrayAndCount.fArray = array;
|
|
|
|
fValue.fArrayAndCount.fCount = count;
|
2001-03-06 19:44:45 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2004-04-15 06:19:55 +00:00
|
|
|
void
|
2004-04-27 21:47:09 +00:00
|
|
|
Formattable::adoptObject(UObject* objectToAdopt) {
|
|
|
|
dispose();
|
|
|
|
fType = kObject;
|
|
|
|
fValue.fObject = objectToAdopt;
|
2004-04-15 06:19:55 +00:00
|
|
|
}
|
|
|
|
|
2003-11-04 23:10:46 +00:00
|
|
|
// -------------------------------------
|
|
|
|
UnicodeString&
|
2004-03-27 04:49:02 +00:00
|
|
|
Formattable::getString(UnicodeString& result, UErrorCode& status) const
|
2003-11-04 23:10:46 +00:00
|
|
|
{
|
2004-03-27 04:49:02 +00:00
|
|
|
if (fType != kString) {
|
|
|
|
setError(status, U_INVALID_FORMAT_ERROR);
|
2003-11-04 23:10:46 +00:00
|
|
|
result.setToBogus();
|
|
|
|
} else {
|
2008-01-11 19:42:41 +00:00
|
|
|
if (fValue.fString == NULL) {
|
2008-02-23 19:15:18 +00:00
|
|
|
setError(status, U_MEMORY_ALLOCATION_ERROR);
|
2008-01-11 19:42:41 +00:00
|
|
|
} else {
|
2008-02-23 19:15:18 +00:00
|
|
|
result = *fValue.fString;
|
2008-01-11 19:42:41 +00:00
|
|
|
}
|
2003-11-04 23:10:46 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
const UnicodeString&
|
2004-03-27 04:49:02 +00:00
|
|
|
Formattable::getString(UErrorCode& status) const
|
2003-11-04 23:10:46 +00:00
|
|
|
{
|
2004-03-27 04:49:02 +00:00
|
|
|
if (fType != kString) {
|
|
|
|
setError(status, U_INVALID_FORMAT_ERROR);
|
2003-11-04 23:10:46 +00:00
|
|
|
return *getBogus();
|
|
|
|
}
|
2008-01-11 19:42:41 +00:00
|
|
|
if (fValue.fString == NULL) {
|
2008-02-23 19:15:18 +00:00
|
|
|
setError(status, U_MEMORY_ALLOCATION_ERROR);
|
|
|
|
return *getBogus();
|
2008-01-11 19:42:41 +00:00
|
|
|
}
|
2003-11-04 23:10:46 +00:00
|
|
|
return *fValue.fString;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
UnicodeString&
|
2004-03-27 04:49:02 +00:00
|
|
|
Formattable::getString(UErrorCode& status)
|
2003-11-04 23:10:46 +00:00
|
|
|
{
|
2004-03-27 04:49:02 +00:00
|
|
|
if (fType != kString) {
|
|
|
|
setError(status, U_INVALID_FORMAT_ERROR);
|
2003-11-04 23:10:46 +00:00
|
|
|
return *getBogus();
|
|
|
|
}
|
2008-01-11 19:42:41 +00:00
|
|
|
if (fValue.fString == NULL) {
|
|
|
|
setError(status, U_MEMORY_ALLOCATION_ERROR);
|
|
|
|
return *getBogus();
|
|
|
|
}
|
2003-11-04 23:10:46 +00:00
|
|
|
return *fValue.fString;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
const Formattable*
|
2004-03-27 04:49:02 +00:00
|
|
|
Formattable::getArray(int32_t& count, UErrorCode& status) const
|
2003-11-04 23:10:46 +00:00
|
|
|
{
|
2004-03-27 04:49:02 +00:00
|
|
|
if (fType != kArray) {
|
|
|
|
setError(status, U_INVALID_FORMAT_ERROR);
|
2003-11-04 23:10:46 +00:00
|
|
|
count = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
count = fValue.fArrayAndCount.fCount;
|
|
|
|
return fValue.fArrayAndCount.fArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
// Gets the bogus string, ensures mondo bogosity.
|
|
|
|
|
|
|
|
UnicodeString*
|
2003-11-26 22:57:27 +00:00
|
|
|
Formattable::getBogus() const
|
2003-11-04 23:10:46 +00:00
|
|
|
{
|
2003-11-26 22:57:27 +00:00
|
|
|
return (UnicodeString*)&fBogus; /* cast away const :-( */
|
2003-11-04 23:10:46 +00:00
|
|
|
}
|
|
|
|
|
2010-02-26 02:29:00 +00:00
|
|
|
|
|
|
|
// --------------------------------------
|
|
|
|
StringPiece Formattable::getDecimalNumber(UErrorCode &status) {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
if (fDecimalStr != NULL) {
|
2013-06-26 06:31:09 +00:00
|
|
|
return fDecimalStr->toStringPiece();
|
2010-02-26 02:29:00 +00:00
|
|
|
}
|
|
|
|
|
2013-06-26 06:31:09 +00:00
|
|
|
CharString *decimalStr = internalGetCharString(status);
|
|
|
|
if(decimalStr == NULL) {
|
|
|
|
return ""; // getDecimalNumber returns "" for error cases
|
|
|
|
} else {
|
|
|
|
return decimalStr->toStringPiece();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CharString *Formattable::internalGetCharString(UErrorCode &status) {
|
|
|
|
if(fDecimalStr == NULL) {
|
|
|
|
if (fDecimalNum == NULL) {
|
2010-02-26 02:29:00 +00:00
|
|
|
// No decimal number for the formattable yet. Which means the value was
|
|
|
|
// set directly by the user as an int, int64 or double. If the value came
|
|
|
|
// from parsing, or from the user setting a decimal number, fDecimalNum
|
|
|
|
// would already be set.
|
|
|
|
//
|
2013-06-26 06:31:09 +00:00
|
|
|
fDecimalNum = new DigitList; // TODO: use internal digit list
|
2010-02-26 02:29:00 +00:00
|
|
|
if (fDecimalNum == NULL) {
|
2013-06-26 06:31:09 +00:00
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return NULL;
|
2010-02-26 02:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (fType) {
|
|
|
|
case kDouble:
|
2013-06-26 06:31:09 +00:00
|
|
|
fDecimalNum->set(this->getDouble());
|
|
|
|
break;
|
2010-02-26 02:29:00 +00:00
|
|
|
case kLong:
|
2013-06-26 06:31:09 +00:00
|
|
|
fDecimalNum->set(this->getLong());
|
|
|
|
break;
|
2010-02-26 02:29:00 +00:00
|
|
|
case kInt64:
|
2013-06-26 06:31:09 +00:00
|
|
|
fDecimalNum->set(this->getInt64());
|
|
|
|
break;
|
2010-02-26 02:29:00 +00:00
|
|
|
default:
|
2013-06-26 06:31:09 +00:00
|
|
|
// The formattable's value is not a numeric type.
|
|
|
|
status = U_INVALID_STATE_ERROR;
|
|
|
|
return NULL;
|
2010-02-26 02:29:00 +00:00
|
|
|
}
|
2013-06-26 06:31:09 +00:00
|
|
|
}
|
2010-02-26 02:29:00 +00:00
|
|
|
|
2013-06-26 06:31:09 +00:00
|
|
|
fDecimalStr = new CharString;
|
|
|
|
if (fDecimalStr == NULL) {
|
2010-02-26 02:29:00 +00:00
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
2013-06-26 06:31:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
fDecimalNum->getDecimal(*fDecimalStr, status);
|
2010-02-26 02:29:00 +00:00
|
|
|
}
|
2013-06-26 06:31:09 +00:00
|
|
|
return fDecimalStr;
|
2010-02-26 02:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-30 00:41:57 +00:00
|
|
|
DigitList *
|
|
|
|
Formattable::getInternalDigitList() {
|
|
|
|
FmtStackData *stackData = (FmtStackData*)fStackData;
|
|
|
|
if(fDecimalNum != &(stackData->stackDecimalNum)) {
|
|
|
|
delete fDecimalNum;
|
|
|
|
fDecimalNum = new (&(stackData->stackDecimalNum), kOnStack) DigitList();
|
|
|
|
} else {
|
|
|
|
fDecimalNum->clear();
|
|
|
|
}
|
|
|
|
return fDecimalNum;
|
|
|
|
}
|
2010-02-26 02:29:00 +00:00
|
|
|
|
|
|
|
// ---------------------------------------
|
|
|
|
void
|
|
|
|
Formattable::adoptDigitList(DigitList *dl) {
|
2012-05-30 00:41:57 +00:00
|
|
|
if(fDecimalNum==dl) {
|
|
|
|
fDecimalNum = NULL; // don't delete
|
|
|
|
}
|
|
|
|
dispose();
|
|
|
|
|
|
|
|
fDecimalNum = dl;
|
2010-02-26 02:29:00 +00:00
|
|
|
|
2012-05-30 00:41:57 +00:00
|
|
|
if(dl==NULL) { // allow adoptDigitList(NULL) to clear
|
|
|
|
return;
|
|
|
|
}
|
2010-02-26 02:29:00 +00:00
|
|
|
|
|
|
|
// Set the value into the Union of simple type values.
|
|
|
|
// Cannot use the set() functions because they would delete the fDecimalNum value,
|
|
|
|
|
|
|
|
if (fDecimalNum->fitsIntoLong(FALSE)) {
|
|
|
|
fType = kLong;
|
|
|
|
fValue.fInt64 = fDecimalNum->getLong();
|
|
|
|
} else if (fDecimalNum->fitsIntoInt64(FALSE)) {
|
|
|
|
fType = kInt64;
|
|
|
|
fValue.fInt64 = fDecimalNum->getInt64();
|
|
|
|
} else {
|
|
|
|
fType = kDouble;
|
|
|
|
fValue.fDouble = fDecimalNum->getDouble();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------
|
|
|
|
void
|
|
|
|
Formattable::setDecimalNumber(const StringPiece &numberString, UErrorCode &status) {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dispose();
|
|
|
|
|
|
|
|
// Copy the input string and nul-terminate it.
|
|
|
|
// The decNumber library requires nul-terminated input. StringPiece input
|
|
|
|
// is not guaranteed nul-terminated. Too bad.
|
2010-05-20 21:16:44 +00:00
|
|
|
// CharString automatically adds the nul.
|
2012-05-30 00:41:57 +00:00
|
|
|
DigitList *dnum = new DigitList(); // TODO: use getInternalDigitList
|
2010-02-26 02:29:00 +00:00
|
|
|
if (dnum == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2010-05-20 21:16:44 +00:00
|
|
|
dnum->set(CharString(numberString, status).toStringPiece(), status);
|
2010-02-26 02:29:00 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete dnum;
|
|
|
|
return; // String didn't contain a decimal number.
|
|
|
|
}
|
|
|
|
adoptDigitList(dnum);
|
|
|
|
|
|
|
|
// Note that we do not hang on to the caller's input string.
|
|
|
|
// If we are asked for the string, we will regenerate one from fDecimalNum.
|
|
|
|
}
|
|
|
|
|
2000-07-24 17:32:41 +00:00
|
|
|
#if 0
|
1999-08-16 21:50:52 +00:00
|
|
|
//----------------------------------------------------
|
|
|
|
// console I/O
|
|
|
|
//----------------------------------------------------
|
|
|
|
#ifdef _DEBUG
|
|
|
|
|
2000-04-25 21:33:15 +00:00
|
|
|
#include <iostream>
|
|
|
|
using namespace std;
|
2000-04-20 17:16:45 +00:00
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/datefmt.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "unistrm.h"
|
|
|
|
|
2002-06-27 01:19:20 +00:00
|
|
|
class FormattableStreamer /* not : public UObject because all methods are static */ {
|
1999-08-16 21:50:52 +00:00
|
|
|
public:
|
|
|
|
static void streamOut(ostream& stream, const Formattable& obj);
|
2002-06-27 01:19:20 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
FormattableStreamer() {} // private - forbid instantiation
|
1999-08-16 21:50:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// This is for debugging purposes only. This will send a displayable
|
|
|
|
// form of the Formattable object to the output stream.
|
|
|
|
|
|
|
|
void
|
|
|
|
FormattableStreamer::streamOut(ostream& stream, const Formattable& obj)
|
|
|
|
{
|
|
|
|
static DateFormat *defDateFormat = 0;
|
|
|
|
|
|
|
|
UnicodeString buffer;
|
|
|
|
switch(obj.getType()) {
|
|
|
|
case Formattable::kDate :
|
|
|
|
// Creates a DateFormat instance for formatting the
|
|
|
|
// Date instance.
|
|
|
|
if (defDateFormat == 0) {
|
|
|
|
defDateFormat = DateFormat::createInstance();
|
|
|
|
}
|
|
|
|
defDateFormat->format(obj.getDate(), buffer);
|
|
|
|
stream << buffer;
|
|
|
|
break;
|
|
|
|
case Formattable::kDouble :
|
|
|
|
// Output the double as is.
|
|
|
|
stream << obj.getDouble() << 'D';
|
|
|
|
break;
|
|
|
|
case Formattable::kLong :
|
|
|
|
// Output the double as is.
|
|
|
|
stream << obj.getLong() << 'L';
|
|
|
|
break;
|
|
|
|
case Formattable::kString:
|
|
|
|
// Output the double as is. Please see UnicodeString console
|
|
|
|
// I/O routine for more details.
|
|
|
|
stream << '"' << obj.getString(buffer) << '"';
|
|
|
|
break;
|
|
|
|
case Formattable::kArray:
|
|
|
|
int32_t i, count;
|
|
|
|
const Formattable* array;
|
|
|
|
array = obj.getArray(count);
|
|
|
|
stream << '[';
|
|
|
|
// Recursively calling the console I/O routine for each element in the array.
|
|
|
|
for (i=0; i<count; ++i) {
|
|
|
|
FormattableStreamer::streamOut(stream, array[i]);
|
|
|
|
stream << ( (i==(count-1)) ? "" : ", " );
|
|
|
|
}
|
|
|
|
stream << ']';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Not a recognizable Formattable object.
|
|
|
|
stream << "INVALID_Formattable";
|
|
|
|
}
|
|
|
|
stream.flush();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-07-24 17:32:41 +00:00
|
|
|
#endif
|
2001-10-08 23:26:58 +00:00
|
|
|
|
2013-09-20 05:00:30 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
/* ---- UFormattable implementation ---- */
|
2013-06-26 06:31:09 +00:00
|
|
|
|
2013-09-20 05:00:30 +00:00
|
|
|
U_NAMESPACE_USE
|
2013-06-26 06:31:09 +00:00
|
|
|
|
|
|
|
U_DRAFT UFormattable* U_EXPORT2
|
|
|
|
ufmt_open(UErrorCode *status) {
|
|
|
|
if( U_FAILURE(*status) ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
UFormattable *fmt = (new Formattable())->toUFormattable();
|
|
|
|
|
|
|
|
if( fmt == NULL ) {
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
}
|
|
|
|
return fmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_DRAFT void U_EXPORT2
|
|
|
|
ufmt_close(UFormattable *fmt) {
|
|
|
|
Formattable *obj = Formattable::fromUFormattable(fmt);
|
|
|
|
|
|
|
|
delete obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_INTERNAL UFormattableType U_EXPORT2
|
2013-09-20 05:00:30 +00:00
|
|
|
ufmt_getType(const UFormattable *fmt, UErrorCode *status) {
|
2013-06-26 06:31:09 +00:00
|
|
|
if(U_FAILURE(*status)) {
|
2013-09-20 21:17:16 +00:00
|
|
|
return (UFormattableType)UFMT_COUNT;
|
2013-06-26 06:31:09 +00:00
|
|
|
}
|
2013-09-20 05:00:30 +00:00
|
|
|
const Formattable *obj = Formattable::fromUFormattable(fmt);
|
|
|
|
return (UFormattableType)obj->getType();
|
2013-06-26 06:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
U_INTERNAL UBool U_EXPORT2
|
2013-09-20 05:00:30 +00:00
|
|
|
ufmt_isNumeric(const UFormattable *fmt) {
|
|
|
|
const Formattable *obj = Formattable::fromUFormattable(fmt);
|
2013-06-26 06:31:09 +00:00
|
|
|
return obj->isNumeric();
|
|
|
|
}
|
|
|
|
|
|
|
|
U_DRAFT UDate U_EXPORT2
|
2013-09-20 05:00:30 +00:00
|
|
|
ufmt_getDate(const UFormattable *fmt, UErrorCode *status) {
|
|
|
|
const Formattable *obj = Formattable::fromUFormattable(fmt);
|
2013-06-26 06:31:09 +00:00
|
|
|
|
|
|
|
return obj->getDate(*status);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_DRAFT double U_EXPORT2
|
|
|
|
ufmt_getDouble(UFormattable *fmt, UErrorCode *status) {
|
|
|
|
Formattable *obj = Formattable::fromUFormattable(fmt);
|
|
|
|
|
|
|
|
return obj->getDouble(*status);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_DRAFT int32_t U_EXPORT2
|
|
|
|
ufmt_getLong(UFormattable *fmt, UErrorCode *status) {
|
|
|
|
Formattable *obj = Formattable::fromUFormattable(fmt);
|
|
|
|
|
|
|
|
return obj->getLong(*status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
U_DRAFT const void *U_EXPORT2
|
2013-09-20 05:00:30 +00:00
|
|
|
ufmt_getObject(const UFormattable *fmt, UErrorCode *status) {
|
|
|
|
const Formattable *obj = Formattable::fromUFormattable(fmt);
|
2013-06-26 06:31:09 +00:00
|
|
|
|
|
|
|
const void *ret = obj->getObject();
|
|
|
|
if( ret==NULL &&
|
|
|
|
(obj->getType() != Formattable::kObject) &&
|
|
|
|
U_SUCCESS( *status )) {
|
|
|
|
*status = U_INVALID_FORMAT_ERROR;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_DRAFT const UChar* U_EXPORT2
|
|
|
|
ufmt_getUChars(UFormattable *fmt, int32_t *len, UErrorCode *status) {
|
|
|
|
Formattable *obj = Formattable::fromUFormattable(fmt);
|
|
|
|
|
|
|
|
// avoid bogosity by checking the type first.
|
|
|
|
if( obj->getType() != Formattable::kString ) {
|
|
|
|
if( U_SUCCESS(*status) ){
|
|
|
|
*status = U_INVALID_FORMAT_ERROR;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should return a valid string
|
|
|
|
UnicodeString &str = obj->getString(*status);
|
|
|
|
if( U_SUCCESS(*status) && len != NULL ) {
|
|
|
|
*len = str.length();
|
|
|
|
}
|
|
|
|
return str.getTerminatedBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
U_DRAFT int32_t U_EXPORT2
|
2013-09-20 05:00:30 +00:00
|
|
|
ufmt_getArrayLength(const UFormattable* fmt, UErrorCode *status) {
|
|
|
|
const Formattable *obj = Formattable::fromUFormattable(fmt);
|
2013-06-26 06:31:09 +00:00
|
|
|
|
|
|
|
int32_t count;
|
|
|
|
(void)obj->getArray(count, *status);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_DRAFT UFormattable * U_EXPORT2
|
|
|
|
ufmt_getArrayItemByIndex(UFormattable* fmt, int32_t n, UErrorCode *status) {
|
|
|
|
Formattable *obj = Formattable::fromUFormattable(fmt);
|
|
|
|
int32_t count;
|
|
|
|
(void)obj->getArray(count, *status);
|
|
|
|
if(U_FAILURE(*status)) {
|
|
|
|
return NULL;
|
|
|
|
} else if(n<0 || n>=count) {
|
|
|
|
setError(*status, U_INDEX_OUTOFBOUNDS_ERROR);
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
return (*obj)[n].toUFormattable(); // returns non-const Formattable
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_DRAFT const char * U_EXPORT2
|
|
|
|
ufmt_getDecNumChars(UFormattable *fmt, int32_t *len, UErrorCode *status) {
|
|
|
|
if(U_FAILURE(*status)) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
Formattable *obj = Formattable::fromUFormattable(fmt);
|
|
|
|
CharString *charString = obj->internalGetCharString(*status);
|
|
|
|
if(U_FAILURE(*status)) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
if(charString == NULL) {
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return "";
|
|
|
|
} else {
|
|
|
|
if(len!=NULL) {
|
|
|
|
*len = charString->length();
|
|
|
|
}
|
|
|
|
return charString->data();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_DRAFT int64_t U_EXPORT2
|
|
|
|
ufmt_getInt64(UFormattable *fmt, UErrorCode *status) {
|
|
|
|
Formattable *obj = Formattable::fromUFormattable(fmt);
|
|
|
|
return obj->getInt64(*status);
|
|
|
|
}
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//eof
|