home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / i18n / fmtable.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-08-16  |  9.5 KB  |  367 lines

  1. /*
  2. ********************************************************************************
  3. *                                                                              *
  4. * COPYRIGHT:                                                                   *
  5. *   (C) Copyright Taligent, Inc.,  1997                                        *
  6. *   (C) Copyright International Business Machines Corporation,  1997-1998      *
  7. *   Licensed Material - Program-Property of IBM - All Rights Reserved.         *
  8. *   US Government Users Restricted Rights - Use, duplication, or disclosure    *
  9. *   restricted by GSA ADP Schedule Contract with IBM Corp.                     *
  10. *                                                                              *
  11. ********************************************************************************
  12. *
  13. * File FMTABLE.CPP
  14. *
  15. * Modification History:
  16. *
  17. *   Date        Name        Description
  18. *   03/25/97    clhuang     Initial Implementation.
  19. ********************************************************************************
  20. */
  21. #ifndef _FMTABLE
  22. #include "fmtable.h"
  23. #endif
  24.  
  25. #ifndef _CMEMORY
  26. #include "cmemory.h"
  27. #endif
  28.  
  29. // *****************************************************************************
  30. // class Formattable
  31. // *****************************************************************************
  32.  
  33. // -------------------------------------
  34. // default constructor.
  35. // Creates a formattable object with a long value 0.
  36.  
  37. Formattable::Formattable()
  38.     :   fType(kLong)
  39. {
  40.     fValue.fLong = 0;
  41. }
  42.  
  43. // -------------------------------------
  44. // Creates a formattable object with a Date instance.
  45.  
  46. Formattable::Formattable(UDate date, ISDATE isDate)
  47.     :   fType(kDate)
  48. {
  49.     fValue.fDate = date;
  50. }
  51.  
  52. // -------------------------------------
  53. // Creates a formattable object with a double value.
  54.  
  55. Formattable::Formattable(double value)
  56.     :   fType(kDouble)
  57. {
  58.     fValue.fDouble = value;
  59. }
  60.  
  61. // -------------------------------------
  62. // Creates a formattable object with a long value.
  63.  
  64. Formattable::Formattable(int32_t value)
  65.     :   fType(kLong)
  66. {
  67.     fValue.fLong = value;
  68. }
  69.  
  70. // -------------------------------------
  71. // Creates a formattable object with a char* string.
  72.  
  73. Formattable::Formattable(const char* stringToCopy)
  74.     :   fType(kString)
  75. {
  76.     fValue.fString = new UnicodeString(stringToCopy);
  77. }
  78.  
  79. // -------------------------------------
  80. // Creates a formattable object with a UnicodeString instance.
  81.  
  82. Formattable::Formattable(const UnicodeString& stringToCopy)
  83.     :   fType(kString)
  84. {
  85.     fValue.fString = new UnicodeString(stringToCopy);
  86. }
  87.  
  88. // -------------------------------------
  89. // Creates a formattable object with a UnicodeString* value.
  90. // (adopting symantics)
  91.  
  92. Formattable::Formattable(UnicodeString* stringToAdopt)
  93.     :   fType(kString)
  94. {
  95.     fValue.fString = stringToAdopt;
  96. }
  97.  
  98. // -------------------------------------
  99.  
  100. Formattable::Formattable(const Formattable* arrayToCopy, int32_t count)
  101.     :   fType(kArray)
  102. {
  103.     fValue.fArrayAndCount.fArray = createArrayCopy(arrayToCopy, count);
  104.     fValue.fArrayAndCount.fCount = count;
  105. }
  106.  
  107. // -------------------------------------
  108. // copy constructor
  109.  
  110. Formattable::Formattable(const Formattable &source)
  111. :   fType(kLong)
  112. {
  113.     *this = source;
  114. }
  115.  
  116. // -------------------------------------
  117. // assignment operator
  118.  
  119. Formattable&
  120. Formattable::operator=(const Formattable& source)
  121. {
  122.     if (this != &source)
  123.     {
  124.         // Disposes the current formattable value/setting.
  125.         dispose();
  126.  
  127.         // Sets the correct data type for this value.
  128.         fType = source.fType;
  129.         switch (fType)
  130.         {
  131.         case kArray:
  132.             // Sets each element in the array one by one and records the array count.
  133.             fValue.fArrayAndCount.fCount = source.fValue.fArrayAndCount.fCount;
  134.             fValue.fArrayAndCount.fArray = createArrayCopy(source.fValue.fArrayAndCount.fArray,
  135.                                                            source.fValue.fArrayAndCount.fCount);
  136.             break;
  137.         case kString:
  138.             // Sets the string value.
  139.             fValue.fString = new UnicodeString(*source.fValue.fString);
  140.             break;
  141.         case kDouble:
  142.             // Sets the double value.
  143.             fValue.fDouble = source.fValue.fDouble;
  144.             break;
  145.         case kLong:
  146.             // Sets the long value.
  147.             fValue.fLong = source.fValue.fLong;
  148.             break;
  149.         case kDate:
  150.             // Sets the Date value.
  151.             fValue.fDate = source.fValue.fDate;
  152.             break;
  153.         }
  154.     }
  155.     return *this;
  156. }
  157.  
  158. // -------------------------------------
  159.  
  160. bool_t
  161. Formattable::operator==(const Formattable& that) const
  162. {
  163.     // Checks class ID.
  164.     if (this == &that) return TRUE;
  165.  
  166.     // Returns FALSE if the data types are different.
  167.     if (fType != that.fType) return FALSE;
  168.  
  169.     // Compares the actual data values.
  170.     switch (fType) {
  171.     case kDate:
  172.         return fValue.fDate == that.fValue.fDate;
  173.     case kDouble:
  174.         return fValue.fDouble == that.fValue.fDouble;
  175.     case kLong:
  176.         return fValue.fLong == that.fValue.fLong;
  177.     case kString:
  178.         return *(fValue.fString) == *(that.fValue.fString);
  179.     case kArray:
  180.         if (fValue.fArrayAndCount.fCount != that.fValue.fArrayAndCount.fCount)
  181.             return FALSE;
  182.         // Checks each element for equality.
  183.         for (int32_t i=0; i<fValue.fArrayAndCount.fCount; ++i)
  184.             if (fValue.fArrayAndCount.fArray[i] != that.fValue.fArrayAndCount.fArray[i])
  185.                 return FALSE;
  186.         break;
  187.     }
  188.     return TRUE;
  189. }
  190.  
  191. // -------------------------------------
  192.  
  193. Formattable::~Formattable()
  194. {
  195.     dispose();
  196. }
  197.  
  198. // -------------------------------------
  199.  
  200. void Formattable::dispose()
  201. {
  202.     // Deletes the data value if necessary.
  203.     switch (fType) {
  204.     case kString:
  205.         delete fValue.fString;
  206.         break;
  207.     case kArray:
  208.         delete[] fValue.fArrayAndCount.fArray;
  209.         break;
  210.     }
  211. }
  212.  
  213. // -------------------------------------
  214. // Gets the data type of this Formattable object. 
  215. Formattable::Type
  216. Formattable::getType() const
  217. {
  218.     return fType;
  219. }
  220.  
  221. // -------------------------------------
  222. // Sets the value to a double value d.
  223.  
  224. void
  225. Formattable::setDouble(double d)
  226. {
  227.     dispose();
  228.     fType = kDouble;
  229.     fValue.fDouble = d;
  230. }
  231.  
  232. // -------------------------------------
  233. // Sets the value to a long value l.
  234.  
  235. void
  236. Formattable::setLong(int32_t l)
  237. {
  238.     dispose();
  239.     fType = kLong;
  240.     fValue.fLong = l;
  241. }
  242.  
  243. // -------------------------------------
  244. // Sets the value to a Date instance d.
  245.  
  246. void
  247. Formattable::setDate(UDate d)
  248. {
  249.     dispose();
  250.     fType = kDate;
  251.     fValue.fDate = d;
  252. }
  253.  
  254. // -------------------------------------
  255. // Sets the value to a string value stringToCopy.
  256.  
  257. void
  258. Formattable::setString(const UnicodeString& stringToCopy)
  259. {
  260.     dispose();
  261.     fType = kString;
  262.     fValue.fString = new UnicodeString(stringToCopy);
  263. }
  264.  
  265. // -------------------------------------
  266. // Sets the value to an array of Formattable objects.
  267.  
  268. void
  269. Formattable::setArray(const Formattable* array, int32_t count)
  270. {
  271.     dispose();
  272.     fType = kArray;
  273.     fValue.fArrayAndCount.fArray = createArrayCopy(array, count);
  274.     fValue.fArrayAndCount.fCount = count;
  275.  
  276. // -------------------------------------
  277. // Adopts the stringToAdopt value.
  278.  
  279. void
  280. Formattable::adoptString(UnicodeString* stringToAdopt)
  281. {
  282.     dispose();
  283.     fType = kString;
  284.     fValue.fString = stringToAdopt;
  285. }
  286.  
  287. // -------------------------------------
  288. // Adopts the array value and its count.
  289.  
  290. void
  291. Formattable::adoptArray(Formattable* array, int32_t count)
  292. {
  293.     dispose();
  294.     fType = kArray;
  295.     fValue.fArrayAndCount.fArray = array;
  296.     fValue.fArrayAndCount.fCount = count;
  297.  
  298. //----------------------------------------------------
  299. // console I/O
  300. //----------------------------------------------------
  301. #ifdef _DEBUG
  302.  
  303. #include "datefmt.h"
  304. #include "unistrm.h"
  305.  
  306. class FormattableStreamer
  307. {
  308. public:
  309.     static void streamOut(ostream& stream, const Formattable& obj);
  310. };
  311.  
  312. // This is for debugging purposes only.  This will send a displayable
  313. // form of the Formattable object to the output stream.
  314.  
  315. void
  316. FormattableStreamer::streamOut(ostream& stream, const Formattable& obj)
  317. {
  318.     static DateFormat *defDateFormat = 0;
  319.  
  320.     UnicodeString buffer;
  321.     switch(obj.getType()) {
  322.         case Formattable::kDate : 
  323.             // Creates a DateFormat instance for formatting the
  324.             // Date instance.
  325.             if (defDateFormat == 0) {
  326.                 defDateFormat = DateFormat::createInstance();
  327.             }
  328.             defDateFormat->format(obj.getDate(), buffer);
  329.             stream << buffer;
  330.             break;
  331.         case Formattable::kDouble :
  332.             // Output the double as is.
  333.             stream << obj.getDouble() << 'D';
  334.             break;
  335.         case Formattable::kLong :
  336.             // Output the double as is.
  337.             stream << obj.getLong() << 'L';
  338.             break;
  339.         case Formattable::kString:
  340.             // Output the double as is.  Please see UnicodeString console
  341.             // I/O routine for more details.
  342.             stream << '"' << obj.getString(buffer) << '"';
  343.             break;
  344.         case Formattable::kArray:
  345.             int32_t i, count;
  346.             const Formattable* array;
  347.             array = obj.getArray(count);
  348.             stream << '[';
  349.             // Recursively calling the console I/O routine for each element in the array.
  350.             for (i=0; i<count; ++i) {
  351.                 FormattableStreamer::streamOut(stream, array[i]);
  352.                 stream << ( (i==(count-1)) ? "" : ", " );
  353.             }
  354.             stream << ']';
  355.             break;
  356.         default:
  357.             // Not a recognizable Formattable object.
  358.             stream << "INVALID_Formattable";
  359.     }
  360.     stream.flush();
  361. }
  362. #endif
  363.  
  364. //eof
  365.