OLD | NEW |
(Empty) | |
| 1 /* |
| 2 ******************************************************************************* |
| 3 * Copyright (C) 1997-2010, International Business Machines Corporation and * |
| 4 * others. All Rights Reserved. * |
| 5 ******************************************************************************* |
| 6 * |
| 7 * File FMTABLE.CPP |
| 8 * |
| 9 * Modification History: |
| 10 * |
| 11 * Date Name Description |
| 12 * 03/25/97 clhuang Initial Implementation. |
| 13 ******************************************************************************** |
| 14 */ |
| 15 |
| 16 #include "unicode/utypes.h" |
| 17 |
| 18 #if !UCONFIG_NO_FORMATTING |
| 19 |
| 20 #include "unicode/fmtable.h" |
| 21 #include "unicode/ustring.h" |
| 22 #include "unicode/measure.h" |
| 23 #include "unicode/curramt.h" |
| 24 #include "charstr.h" |
| 25 #include "cmemory.h" |
| 26 #include "cstring.h" |
| 27 #include "decNumber.h" |
| 28 #include "digitlst.h" |
| 29 |
| 30 // ***************************************************************************** |
| 31 // class Formattable |
| 32 // ***************************************************************************** |
| 33 |
| 34 U_NAMESPACE_BEGIN |
| 35 |
| 36 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Formattable) |
| 37 |
| 38 //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. |
| 39 |
| 40 // NOTE: As of 3.0, there are limitations to the UObject API. It does |
| 41 // not (yet) support cloning, operator=, nor operator==. To |
| 42 // work around this, I implement some simple inlines here. Later |
| 43 // these can be modified or removed. [alan] |
| 44 |
| 45 // NOTE: These inlines assume that all fObjects are in fact instances |
| 46 // of the Measure class, which is true as of 3.0. [alan] |
| 47 |
| 48 // Return TRUE if *a == *b. |
| 49 static inline UBool objectEquals(const UObject* a, const UObject* b) { |
| 50 // LATER: return *a == *b; |
| 51 return *((const Measure*) a) == *((const Measure*) b); |
| 52 } |
| 53 |
| 54 // Return a clone of *a. |
| 55 static inline UObject* objectClone(const UObject* a) { |
| 56 // LATER: return a->clone(); |
| 57 return ((const Measure*) a)->clone(); |
| 58 } |
| 59 |
| 60 // Return TRUE if *a is an instance of Measure. |
| 61 static inline UBool instanceOfMeasure(const UObject* a) { |
| 62 return dynamic_cast<const Measure*>(a) != NULL; |
| 63 } |
| 64 |
| 65 /** |
| 66 * Creates a new Formattable array and copies the values from the specified |
| 67 * original. |
| 68 * @param array the original array |
| 69 * @param count the original array count |
| 70 * @return the new Formattable array. |
| 71 */ |
| 72 static Formattable* createArrayCopy(const Formattable* array, int32_t count) { |
| 73 Formattable *result = new Formattable[count]; |
| 74 if (result != NULL) { |
| 75 for (int32_t i=0; i<count; ++i) |
| 76 result[i] = array[i]; // Don't memcpy! |
| 77 } |
| 78 return result; |
| 79 } |
| 80 |
| 81 //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. |
| 82 |
| 83 /** |
| 84 * Set 'ec' to 'err' only if 'ec' is not already set to a failing UErrorCode. |
| 85 */ |
| 86 static void setError(UErrorCode& ec, UErrorCode err) { |
| 87 if (U_SUCCESS(ec)) { |
| 88 ec = err; |
| 89 } |
| 90 } |
| 91 |
| 92 // |
| 93 // Common initialization code, shared by constructors. |
| 94 // Put everything into a known state. |
| 95 // |
| 96 void Formattable::init() { |
| 97 fValue.fInt64 = 0; |
| 98 fType = kLong; |
| 99 fDecimalStr = NULL; |
| 100 fDecimalNum = NULL; |
| 101 fBogus.setToBogus(); |
| 102 } |
| 103 |
| 104 // ------------------------------------- |
| 105 // default constructor. |
| 106 // Creates a formattable object with a long value 0. |
| 107 |
| 108 Formattable::Formattable() { |
| 109 init(); |
| 110 } |
| 111 |
| 112 // ------------------------------------- |
| 113 // Creates a formattable object with a Date instance. |
| 114 |
| 115 Formattable::Formattable(UDate date, ISDATE /*isDate*/) |
| 116 { |
| 117 init(); |
| 118 fType = kDate; |
| 119 fValue.fDate = date; |
| 120 } |
| 121 |
| 122 // ------------------------------------- |
| 123 // Creates a formattable object with a double value. |
| 124 |
| 125 Formattable::Formattable(double value) |
| 126 { |
| 127 init(); |
| 128 fType = kDouble; |
| 129 fValue.fDouble = value; |
| 130 } |
| 131 |
| 132 // ------------------------------------- |
| 133 // Creates a formattable object with an int32_t value. |
| 134 |
| 135 Formattable::Formattable(int32_t value) |
| 136 { |
| 137 init(); |
| 138 fValue.fInt64 = value; |
| 139 } |
| 140 |
| 141 // ------------------------------------- |
| 142 // Creates a formattable object with an int64_t value. |
| 143 |
| 144 Formattable::Formattable(int64_t value) |
| 145 { |
| 146 init(); |
| 147 fType = kInt64; |
| 148 fValue.fInt64 = value; |
| 149 } |
| 150 |
| 151 // ------------------------------------- |
| 152 // Creates a formattable object with a decimal number value from a string. |
| 153 |
| 154 Formattable::Formattable(const StringPiece &number, UErrorCode &status) { |
| 155 init(); |
| 156 setDecimalNumber(number, status); |
| 157 } |
| 158 |
| 159 |
| 160 // ------------------------------------- |
| 161 // Creates a formattable object with a UnicodeString instance. |
| 162 |
| 163 Formattable::Formattable(const UnicodeString& stringToCopy) |
| 164 { |
| 165 init(); |
| 166 fType = kString; |
| 167 fValue.fString = new UnicodeString(stringToCopy); |
| 168 } |
| 169 |
| 170 // ------------------------------------- |
| 171 // Creates a formattable object with a UnicodeString* value. |
| 172 // (adopting symantics) |
| 173 |
| 174 Formattable::Formattable(UnicodeString* stringToAdopt) |
| 175 { |
| 176 init(); |
| 177 fType = kString; |
| 178 fValue.fString = stringToAdopt; |
| 179 } |
| 180 |
| 181 Formattable::Formattable(UObject* objectToAdopt) |
| 182 { |
| 183 init(); |
| 184 fType = kObject; |
| 185 fValue.fObject = objectToAdopt; |
| 186 } |
| 187 |
| 188 // ------------------------------------- |
| 189 |
| 190 Formattable::Formattable(const Formattable* arrayToCopy, int32_t count) |
| 191 : UObject(), fType(kArray) |
| 192 { |
| 193 init(); |
| 194 fType = kArray; |
| 195 fValue.fArrayAndCount.fArray = createArrayCopy(arrayToCopy, count); |
| 196 fValue.fArrayAndCount.fCount = count; |
| 197 } |
| 198 |
| 199 // ------------------------------------- |
| 200 // copy constructor |
| 201 |
| 202 |
| 203 Formattable::Formattable(const Formattable &source) |
| 204 : UObject(*this) |
| 205 { |
| 206 init(); |
| 207 *this = source; |
| 208 } |
| 209 |
| 210 // ------------------------------------- |
| 211 // assignment operator |
| 212 |
| 213 Formattable& |
| 214 Formattable::operator=(const Formattable& source) |
| 215 { |
| 216 if (this != &source) |
| 217 { |
| 218 // Disposes the current formattable value/setting. |
| 219 dispose(); |
| 220 |
| 221 // Sets the correct data type for this value. |
| 222 fType = source.fType; |
| 223 switch (fType) |
| 224 { |
| 225 case kArray: |
| 226 // Sets each element in the array one by one and records the array c
ount. |
| 227 fValue.fArrayAndCount.fCount = source.fValue.fArrayAndCount.fCount; |
| 228 fValue.fArrayAndCount.fArray = createArrayCopy(source.fValue.fArrayA
ndCount.fArray, |
| 229 source.fValue.fArrayA
ndCount.fCount); |
| 230 break; |
| 231 case kString: |
| 232 // Sets the string value. |
| 233 fValue.fString = new UnicodeString(*source.fValue.fString); |
| 234 break; |
| 235 case kDouble: |
| 236 // Sets the double value. |
| 237 fValue.fDouble = source.fValue.fDouble; |
| 238 break; |
| 239 case kLong: |
| 240 case kInt64: |
| 241 // Sets the long value. |
| 242 fValue.fInt64 = source.fValue.fInt64; |
| 243 break; |
| 244 case kDate: |
| 245 // Sets the Date value. |
| 246 fValue.fDate = source.fValue.fDate; |
| 247 break; |
| 248 case kObject: |
| 249 fValue.fObject = objectClone(source.fValue.fObject); |
| 250 break; |
| 251 } |
| 252 |
| 253 UErrorCode status = U_ZERO_ERROR; |
| 254 if (source.fDecimalNum != NULL) { |
| 255 fDecimalNum = new DigitList(*source.fDecimalNum); |
| 256 } |
| 257 if (source.fDecimalStr != NULL) { |
| 258 fDecimalStr = new CharString(*source.fDecimalStr, status); |
| 259 if (U_FAILURE(status)) { |
| 260 delete fDecimalStr; |
| 261 fDecimalStr = NULL; |
| 262 } |
| 263 } |
| 264 } |
| 265 return *this; |
| 266 } |
| 267 |
| 268 // ------------------------------------- |
| 269 |
| 270 UBool |
| 271 Formattable::operator==(const Formattable& that) const |
| 272 { |
| 273 int32_t i; |
| 274 |
| 275 if (this == &that) return TRUE; |
| 276 |
| 277 // Returns FALSE if the data types are different. |
| 278 if (fType != that.fType) return FALSE; |
| 279 |
| 280 // Compares the actual data values. |
| 281 UBool equal = TRUE; |
| 282 switch (fType) { |
| 283 case kDate: |
| 284 equal = (fValue.fDate == that.fValue.fDate); |
| 285 break; |
| 286 case kDouble: |
| 287 equal = (fValue.fDouble == that.fValue.fDouble); |
| 288 break; |
| 289 case kLong: |
| 290 case kInt64: |
| 291 equal = (fValue.fInt64 == that.fValue.fInt64); |
| 292 break; |
| 293 case kString: |
| 294 equal = (*(fValue.fString) == *(that.fValue.fString)); |
| 295 break; |
| 296 case kArray: |
| 297 if (fValue.fArrayAndCount.fCount != that.fValue.fArrayAndCount.fCount) { |
| 298 equal = FALSE; |
| 299 break; |
| 300 } |
| 301 // Checks each element for equality. |
| 302 for (i=0; i<fValue.fArrayAndCount.fCount; ++i) { |
| 303 if (fValue.fArrayAndCount.fArray[i] != that.fValue.fArrayAndCount.fA
rray[i]) { |
| 304 equal = FALSE; |
| 305 break; |
| 306 } |
| 307 } |
| 308 break; |
| 309 case kObject: |
| 310 if (fValue.fObject == NULL || that.fValue.fObject == NULL) { |
| 311 equal = FALSE; |
| 312 } else { |
| 313 equal = objectEquals(fValue.fObject, that.fValue.fObject); |
| 314 } |
| 315 break; |
| 316 } |
| 317 |
| 318 // TODO: compare digit lists if numeric. |
| 319 return equal; |
| 320 } |
| 321 |
| 322 // ------------------------------------- |
| 323 |
| 324 Formattable::~Formattable() |
| 325 { |
| 326 dispose(); |
| 327 } |
| 328 |
| 329 // ------------------------------------- |
| 330 |
| 331 void Formattable::dispose() |
| 332 { |
| 333 // Deletes the data value if necessary. |
| 334 switch (fType) { |
| 335 case kString: |
| 336 delete fValue.fString; |
| 337 break; |
| 338 case kArray: |
| 339 delete[] fValue.fArrayAndCount.fArray; |
| 340 break; |
| 341 case kObject: |
| 342 delete fValue.fObject; |
| 343 break; |
| 344 default: |
| 345 break; |
| 346 } |
| 347 |
| 348 fType = kLong; |
| 349 fValue.fInt64 = 0; |
| 350 delete fDecimalStr; |
| 351 fDecimalStr = NULL; |
| 352 delete fDecimalNum; |
| 353 fDecimalNum = NULL; |
| 354 } |
| 355 |
| 356 Formattable * |
| 357 Formattable::clone() const { |
| 358 return new Formattable(*this); |
| 359 } |
| 360 |
| 361 // ------------------------------------- |
| 362 // Gets the data type of this Formattable object. |
| 363 Formattable::Type |
| 364 Formattable::getType() const |
| 365 { |
| 366 return fType; |
| 367 } |
| 368 |
| 369 UBool |
| 370 Formattable::isNumeric() const { |
| 371 switch (fType) { |
| 372 case kDouble: |
| 373 case kLong: |
| 374 case kInt64: |
| 375 return TRUE; |
| 376 default: |
| 377 return FALSE; |
| 378 } |
| 379 } |
| 380 |
| 381 // ------------------------------------- |
| 382 int32_t |
| 383 //Formattable::getLong(UErrorCode* status) const |
| 384 Formattable::getLong(UErrorCode& status) const |
| 385 { |
| 386 if (U_FAILURE(status)) { |
| 387 return 0; |
| 388 } |
| 389 |
| 390 switch (fType) { |
| 391 case Formattable::kLong: |
| 392 return (int32_t)fValue.fInt64; |
| 393 case Formattable::kInt64: |
| 394 if (fValue.fInt64 > INT32_MAX) { |
| 395 status = U_INVALID_FORMAT_ERROR; |
| 396 return INT32_MAX; |
| 397 } else if (fValue.fInt64 < INT32_MIN) { |
| 398 status = U_INVALID_FORMAT_ERROR; |
| 399 return INT32_MIN; |
| 400 } else { |
| 401 return (int32_t)fValue.fInt64; |
| 402 } |
| 403 case Formattable::kDouble: |
| 404 if (fValue.fDouble > INT32_MAX) { |
| 405 status = U_INVALID_FORMAT_ERROR; |
| 406 return INT32_MAX; |
| 407 } else if (fValue.fDouble < INT32_MIN) { |
| 408 status = U_INVALID_FORMAT_ERROR; |
| 409 return INT32_MIN; |
| 410 } else { |
| 411 return (int32_t)fValue.fDouble; // loses fraction |
| 412 } |
| 413 case Formattable::kObject: |
| 414 if (fValue.fObject == NULL) { |
| 415 status = U_MEMORY_ALLOCATION_ERROR; |
| 416 return 0; |
| 417 } |
| 418 // TODO Later replace this with instanceof call |
| 419 if (instanceOfMeasure(fValue.fObject)) { |
| 420 return ((const Measure*) fValue.fObject)-> |
| 421 getNumber().getLong(status); |
| 422 } |
| 423 default: |
| 424 status = U_INVALID_FORMAT_ERROR; |
| 425 return 0; |
| 426 } |
| 427 } |
| 428 |
| 429 // ------------------------------------- |
| 430 int64_t |
| 431 Formattable::getInt64(UErrorCode& status) const |
| 432 { |
| 433 if (U_FAILURE(status)) { |
| 434 return 0; |
| 435 } |
| 436 |
| 437 switch (fType) { |
| 438 case Formattable::kLong: |
| 439 case Formattable::kInt64: |
| 440 return fValue.fInt64; |
| 441 case Formattable::kDouble: |
| 442 if (fValue.fDouble >= U_INT64_MAX) { |
| 443 status = U_INVALID_FORMAT_ERROR; |
| 444 return U_INT64_MAX; |
| 445 } else if (fValue.fDouble <= U_INT64_MIN) { |
| 446 status = U_INVALID_FORMAT_ERROR; |
| 447 return U_INT64_MIN; |
| 448 } else { |
| 449 return (int64_t)fValue.fDouble; |
| 450 } |
| 451 case Formattable::kObject: |
| 452 if (fValue.fObject == NULL) { |
| 453 status = U_MEMORY_ALLOCATION_ERROR; |
| 454 return 0; |
| 455 } |
| 456 // TODO Later replace this with instanceof call |
| 457 if (instanceOfMeasure(fValue.fObject)) { |
| 458 return ((const Measure*) fValue.fObject)-> |
| 459 getNumber().getInt64(status); |
| 460 } |
| 461 default: |
| 462 status = U_INVALID_FORMAT_ERROR; |
| 463 return 0; |
| 464 } |
| 465 } |
| 466 |
| 467 // ------------------------------------- |
| 468 double |
| 469 Formattable::getDouble(UErrorCode& status) const |
| 470 { |
| 471 if (U_FAILURE(status)) { |
| 472 return 0; |
| 473 } |
| 474 |
| 475 switch (fType) { |
| 476 case Formattable::kLong: |
| 477 case Formattable::kInt64: // loses precision |
| 478 return (double)fValue.fInt64; |
| 479 case Formattable::kDouble: |
| 480 return fValue.fDouble; |
| 481 case Formattable::kObject: |
| 482 if (fValue.fObject == NULL) { |
| 483 status = U_MEMORY_ALLOCATION_ERROR; |
| 484 return 0; |
| 485 } |
| 486 // TODO Later replace this with instanceof call |
| 487 if (instanceOfMeasure(fValue.fObject)) { |
| 488 return ((const Measure*) fValue.fObject)-> |
| 489 getNumber().getDouble(status); |
| 490 } |
| 491 default: |
| 492 status = U_INVALID_FORMAT_ERROR; |
| 493 return 0; |
| 494 } |
| 495 } |
| 496 |
| 497 const UObject* |
| 498 Formattable::getObject() const { |
| 499 return (fType == kObject) ? fValue.fObject : NULL; |
| 500 } |
| 501 |
| 502 // ------------------------------------- |
| 503 // Sets the value to a double value d. |
| 504 |
| 505 void |
| 506 Formattable::setDouble(double d) |
| 507 { |
| 508 dispose(); |
| 509 fType = kDouble; |
| 510 fValue.fDouble = d; |
| 511 } |
| 512 |
| 513 // ------------------------------------- |
| 514 // Sets the value to a long value l. |
| 515 |
| 516 void |
| 517 Formattable::setLong(int32_t l) |
| 518 { |
| 519 dispose(); |
| 520 fType = kLong; |
| 521 fValue.fInt64 = l; |
| 522 } |
| 523 |
| 524 // ------------------------------------- |
| 525 // Sets the value to an int64 value ll. |
| 526 |
| 527 void |
| 528 Formattable::setInt64(int64_t ll) |
| 529 { |
| 530 dispose(); |
| 531 fType = kInt64; |
| 532 fValue.fInt64 = ll; |
| 533 } |
| 534 |
| 535 // ------------------------------------- |
| 536 // Sets the value to a Date instance d. |
| 537 |
| 538 void |
| 539 Formattable::setDate(UDate d) |
| 540 { |
| 541 dispose(); |
| 542 fType = kDate; |
| 543 fValue.fDate = d; |
| 544 } |
| 545 |
| 546 // ------------------------------------- |
| 547 // Sets the value to a string value stringToCopy. |
| 548 |
| 549 void |
| 550 Formattable::setString(const UnicodeString& stringToCopy) |
| 551 { |
| 552 dispose(); |
| 553 fType = kString; |
| 554 fValue.fString = new UnicodeString(stringToCopy); |
| 555 } |
| 556 |
| 557 // ------------------------------------- |
| 558 // Sets the value to an array of Formattable objects. |
| 559 |
| 560 void |
| 561 Formattable::setArray(const Formattable* array, int32_t count) |
| 562 { |
| 563 dispose(); |
| 564 fType = kArray; |
| 565 fValue.fArrayAndCount.fArray = createArrayCopy(array, count); |
| 566 fValue.fArrayAndCount.fCount = count; |
| 567 } |
| 568 |
| 569 // ------------------------------------- |
| 570 // Adopts the stringToAdopt value. |
| 571 |
| 572 void |
| 573 Formattable::adoptString(UnicodeString* stringToAdopt) |
| 574 { |
| 575 dispose(); |
| 576 fType = kString; |
| 577 fValue.fString = stringToAdopt; |
| 578 } |
| 579 |
| 580 // ------------------------------------- |
| 581 // Adopts the array value and its count. |
| 582 |
| 583 void |
| 584 Formattable::adoptArray(Formattable* array, int32_t count) |
| 585 { |
| 586 dispose(); |
| 587 fType = kArray; |
| 588 fValue.fArrayAndCount.fArray = array; |
| 589 fValue.fArrayAndCount.fCount = count; |
| 590 } |
| 591 |
| 592 void |
| 593 Formattable::adoptObject(UObject* objectToAdopt) { |
| 594 dispose(); |
| 595 fType = kObject; |
| 596 fValue.fObject = objectToAdopt; |
| 597 } |
| 598 |
| 599 // ------------------------------------- |
| 600 UnicodeString& |
| 601 Formattable::getString(UnicodeString& result, UErrorCode& status) const |
| 602 { |
| 603 if (fType != kString) { |
| 604 setError(status, U_INVALID_FORMAT_ERROR); |
| 605 result.setToBogus(); |
| 606 } else { |
| 607 if (fValue.fString == NULL) { |
| 608 setError(status, U_MEMORY_ALLOCATION_ERROR); |
| 609 } else { |
| 610 result = *fValue.fString; |
| 611 } |
| 612 } |
| 613 return result; |
| 614 } |
| 615 |
| 616 // ------------------------------------- |
| 617 const UnicodeString& |
| 618 Formattable::getString(UErrorCode& status) const |
| 619 { |
| 620 if (fType != kString) { |
| 621 setError(status, U_INVALID_FORMAT_ERROR); |
| 622 return *getBogus(); |
| 623 } |
| 624 if (fValue.fString == NULL) { |
| 625 setError(status, U_MEMORY_ALLOCATION_ERROR); |
| 626 return *getBogus(); |
| 627 } |
| 628 return *fValue.fString; |
| 629 } |
| 630 |
| 631 // ------------------------------------- |
| 632 UnicodeString& |
| 633 Formattable::getString(UErrorCode& status) |
| 634 { |
| 635 if (fType != kString) { |
| 636 setError(status, U_INVALID_FORMAT_ERROR); |
| 637 return *getBogus(); |
| 638 } |
| 639 if (fValue.fString == NULL) { |
| 640 setError(status, U_MEMORY_ALLOCATION_ERROR); |
| 641 return *getBogus(); |
| 642 } |
| 643 return *fValue.fString; |
| 644 } |
| 645 |
| 646 // ------------------------------------- |
| 647 const Formattable* |
| 648 Formattable::getArray(int32_t& count, UErrorCode& status) const |
| 649 { |
| 650 if (fType != kArray) { |
| 651 setError(status, U_INVALID_FORMAT_ERROR); |
| 652 count = 0; |
| 653 return NULL; |
| 654 } |
| 655 count = fValue.fArrayAndCount.fCount; |
| 656 return fValue.fArrayAndCount.fArray; |
| 657 } |
| 658 |
| 659 // ------------------------------------- |
| 660 // Gets the bogus string, ensures mondo bogosity. |
| 661 |
| 662 UnicodeString* |
| 663 Formattable::getBogus() const |
| 664 { |
| 665 return (UnicodeString*)&fBogus; /* cast away const :-( */ |
| 666 } |
| 667 |
| 668 |
| 669 // -------------------------------------- |
| 670 StringPiece Formattable::getDecimalNumber(UErrorCode &status) { |
| 671 if (U_FAILURE(status)) { |
| 672 return ""; |
| 673 } |
| 674 if (fDecimalStr != NULL) { |
| 675 return fDecimalStr->toStringPiece(); |
| 676 } |
| 677 |
| 678 if (fDecimalNum == NULL) { |
| 679 // No decimal number for the formattable yet. Which means the value was |
| 680 // set directly by the user as an int, int64 or double. If the value ca
me |
| 681 // from parsing, or from the user setting a decimal number, fDecimalNum |
| 682 // would already be set. |
| 683 // |
| 684 fDecimalNum = new DigitList; |
| 685 if (fDecimalNum == NULL) { |
| 686 status = U_MEMORY_ALLOCATION_ERROR; |
| 687 return ""; |
| 688 } |
| 689 |
| 690 switch (fType) { |
| 691 case kDouble: |
| 692 fDecimalNum->set(this->getDouble()); |
| 693 break; |
| 694 case kLong: |
| 695 fDecimalNum->set(this->getLong()); |
| 696 break; |
| 697 case kInt64: |
| 698 fDecimalNum->set(this->getInt64()); |
| 699 break; |
| 700 default: |
| 701 // The formattable's value is not a numeric type. |
| 702 status = U_INVALID_STATE_ERROR; |
| 703 return ""; |
| 704 } |
| 705 } |
| 706 |
| 707 fDecimalStr = new CharString; |
| 708 if (fDecimalStr == NULL) { |
| 709 status = U_MEMORY_ALLOCATION_ERROR; |
| 710 return ""; |
| 711 } |
| 712 fDecimalNum->getDecimal(*fDecimalStr, status); |
| 713 |
| 714 return fDecimalStr->toStringPiece(); |
| 715 } |
| 716 |
| 717 |
| 718 |
| 719 // --------------------------------------- |
| 720 void |
| 721 Formattable::adoptDigitList(DigitList *dl) { |
| 722 dispose(); |
| 723 |
| 724 fDecimalNum = dl; |
| 725 |
| 726 // Set the value into the Union of simple type values. |
| 727 // Cannot use the set() functions because they would delete the fDecimalNum
value, |
| 728 |
| 729 if (fDecimalNum->fitsIntoLong(FALSE)) { |
| 730 fType = kLong; |
| 731 fValue.fInt64 = fDecimalNum->getLong(); |
| 732 } else if (fDecimalNum->fitsIntoInt64(FALSE)) { |
| 733 fType = kInt64; |
| 734 fValue.fInt64 = fDecimalNum->getInt64(); |
| 735 } else { |
| 736 fType = kDouble; |
| 737 fValue.fDouble = fDecimalNum->getDouble(); |
| 738 } |
| 739 } |
| 740 |
| 741 |
| 742 // --------------------------------------- |
| 743 void |
| 744 Formattable::setDecimalNumber(const StringPiece &numberString, UErrorCode &statu
s) { |
| 745 if (U_FAILURE(status)) { |
| 746 return; |
| 747 } |
| 748 dispose(); |
| 749 |
| 750 // Copy the input string and nul-terminate it. |
| 751 // The decNumber library requires nul-terminated input. StringPiece inpu
t |
| 752 // is not guaranteed nul-terminated. Too bad. |
| 753 // CharString automatically adds the nul. |
| 754 DigitList *dnum = new DigitList(); |
| 755 if (dnum == NULL) { |
| 756 status = U_MEMORY_ALLOCATION_ERROR; |
| 757 return; |
| 758 } |
| 759 dnum->set(CharString(numberString, status).toStringPiece(), status); |
| 760 if (U_FAILURE(status)) { |
| 761 delete dnum; |
| 762 return; // String didn't contain a decimal number. |
| 763 } |
| 764 adoptDigitList(dnum); |
| 765 |
| 766 // Note that we do not hang on to the caller's input string. |
| 767 // If we are asked for the string, we will regenerate one from fDecimalNum. |
| 768 } |
| 769 |
| 770 #if 0 |
| 771 //---------------------------------------------------- |
| 772 // console I/O |
| 773 //---------------------------------------------------- |
| 774 #ifdef _DEBUG |
| 775 |
| 776 #if U_IOSTREAM_SOURCE >= 199711 |
| 777 #include <iostream> |
| 778 using namespace std; |
| 779 #elif U_IOSTREAM_SOURCE >= 198506 |
| 780 #include <iostream.h> |
| 781 #endif |
| 782 |
| 783 #include "unicode/datefmt.h" |
| 784 #include "unistrm.h" |
| 785 |
| 786 class FormattableStreamer /* not : public UObject because all methods are static
*/ { |
| 787 public: |
| 788 static void streamOut(ostream& stream, const Formattable& obj); |
| 789 |
| 790 private: |
| 791 FormattableStreamer() {} // private - forbid instantiation |
| 792 }; |
| 793 |
| 794 // This is for debugging purposes only. This will send a displayable |
| 795 // form of the Formattable object to the output stream. |
| 796 |
| 797 void |
| 798 FormattableStreamer::streamOut(ostream& stream, const Formattable& obj) |
| 799 { |
| 800 static DateFormat *defDateFormat = 0; |
| 801 |
| 802 UnicodeString buffer; |
| 803 switch(obj.getType()) { |
| 804 case Formattable::kDate : |
| 805 // Creates a DateFormat instance for formatting the |
| 806 // Date instance. |
| 807 if (defDateFormat == 0) { |
| 808 defDateFormat = DateFormat::createInstance(); |
| 809 } |
| 810 defDateFormat->format(obj.getDate(), buffer); |
| 811 stream << buffer; |
| 812 break; |
| 813 case Formattable::kDouble : |
| 814 // Output the double as is. |
| 815 stream << obj.getDouble() << 'D'; |
| 816 break; |
| 817 case Formattable::kLong : |
| 818 // Output the double as is. |
| 819 stream << obj.getLong() << 'L'; |
| 820 break; |
| 821 case Formattable::kString: |
| 822 // Output the double as is. Please see UnicodeString console |
| 823 // I/O routine for more details. |
| 824 stream << '"' << obj.getString(buffer) << '"'; |
| 825 break; |
| 826 case Formattable::kArray: |
| 827 int32_t i, count; |
| 828 const Formattable* array; |
| 829 array = obj.getArray(count); |
| 830 stream << '['; |
| 831 // Recursively calling the console I/O routine for each element in t
he array. |
| 832 for (i=0; i<count; ++i) { |
| 833 FormattableStreamer::streamOut(stream, array[i]); |
| 834 stream << ( (i==(count-1)) ? "" : ", " ); |
| 835 } |
| 836 stream << ']'; |
| 837 break; |
| 838 default: |
| 839 // Not a recognizable Formattable object. |
| 840 stream << "INVALID_Formattable"; |
| 841 } |
| 842 stream.flush(); |
| 843 } |
| 844 #endif |
| 845 |
| 846 #endif |
| 847 |
| 848 U_NAMESPACE_END |
| 849 |
| 850 #endif /* #if !UCONFIG_NO_FORMATTING */ |
| 851 |
| 852 //eof |
OLD | NEW |