home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / i18n / dcfmtsym.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  9.0 KB  |  244 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 DCFMTSYM.CPP
  14. *
  15. * Modification History:
  16. *
  17. *   Date        Name        Description
  18. *   02/19/97    aliu        Converted from java.
  19. *   03/18/97    clhuang     Implemented with C++ APIs.
  20. *   03/27/97    helena      Updated to pass the simple test after code review.
  21. *   08/26/97    aliu        Added currency/intl currency symbol support.
  22. *    07/20/98    stephen        Slightly modified initialization of monetarySeparator
  23. ********************************************************************************
  24. */
  25.  
  26. #include "dcfmtsym.h"
  27. #include "resbund.h"
  28. #include "decimfmt.h"
  29.  
  30. // *****************************************************************************
  31. // class DecimalFormatSymbols
  32. // *****************************************************************************
  33.  
  34. const int32_t DecimalFormatSymbols::fgNumberElementsLength = 11;
  35. const int32_t DecimalFormatSymbols::fgCurrencyElementsLength = 3;
  36.  
  37. /**
  38.  * These are the tags we expect to see in normal resource bundle files associated
  39.  * with a locale.
  40.  */
  41. const UnicodeString DecimalFormatSymbols::fgNumberElements("NumberElements");
  42. const UnicodeString DecimalFormatSymbols::fgCurrencyElements("CurrencyElements");
  43.  
  44. // Because the C-compiler doesn't parse \u escape sequences, we encode the
  45. // \u last resort strings as UChar arrays.
  46. const UChar DecimalFormatSymbols::fgLastResortPerMill[]      = { 0x2030, 0 };
  47. const UChar DecimalFormatSymbols::fgLastResortInfinity[]     = { 0x221E, 0 };
  48. const UChar DecimalFormatSymbols::fgLastResortNaN[]          = { 0xFFFD, 0 };
  49. const UChar DecimalFormatSymbols::fgLastResortCurrency[]     = { 0x00A4, 0 };
  50. const UChar DecimalFormatSymbols::fgLastResortIntlCurrency[] = { 0x00A4, 0x00A4, 0 };
  51.  
  52. const UnicodeString DecimalFormatSymbols::fgLastResortNumberElements[] =
  53. {
  54.     ".",    // decimal separator
  55.     "",     // group (thousands) separator
  56.     ";",    // pattern separator
  57.     "%",    // percent sign
  58.     "0",    // native 0 digit
  59.     "#",    // pattern digit
  60.     "-",    // minus sign
  61.     "E",    // exponential
  62.     DecimalFormatSymbols::fgLastResortPerMill,  // per mill
  63.     DecimalFormatSymbols::fgLastResortInfinity, // infinite
  64.     DecimalFormatSymbols::fgLastResortNaN       // NaN
  65. };
  66.  
  67. const UnicodeString DecimalFormatSymbols::fgLastResortCurrencyElements[] =
  68. {
  69.     DecimalFormatSymbols::fgLastResortCurrency,
  70.     DecimalFormatSymbols::fgLastResortIntlCurrency,
  71.     "."     // monetary decimal separator
  72. };
  73.  
  74. // -------------------------------------
  75. // Initializes this with the decimal format symbols in the default locale.
  76.  
  77. DecimalFormatSymbols::DecimalFormatSymbols(UErrorCode& status)
  78. {
  79.     initialize(Locale::getDefault(), status, TRUE);
  80. }
  81.  
  82. // -------------------------------------
  83. // Initializes this with the decimal format symbols in the desired locale.
  84.  
  85. DecimalFormatSymbols::DecimalFormatSymbols(const Locale& locale, UErrorCode& status)
  86. {
  87.     initialize(locale, status);
  88. }
  89.  
  90. // -------------------------------------
  91.  
  92. DecimalFormatSymbols::~DecimalFormatSymbols()
  93. {
  94. }
  95.  
  96. // -------------------------------------
  97. // copy constructor
  98.  
  99. DecimalFormatSymbols::DecimalFormatSymbols(const DecimalFormatSymbols &source)
  100.     :   fZeroDigit(source.fZeroDigit),
  101.         fGroupingSeparator(source.fGroupingSeparator),
  102.         fDecimalSeparator(source.fDecimalSeparator),
  103.         fPercent(source.fPercent),
  104.         fPerMill(source.fPerMill),
  105.         fDigit(source.fDigit),
  106.         fPlusSign(source.fPlusSign),
  107.         fMinusSign(source.fMinusSign),
  108.         fExponential(source.fExponential),
  109.         fPatternSeparator(source.fPatternSeparator),
  110.         fInfinity(source.fInfinity),
  111.         fNaN(source.fNaN),
  112.         fCurrencySymbol(source.fCurrencySymbol),
  113.         fIntlCurrencySymbol(source.fIntlCurrencySymbol),
  114.         fMonetarySeparator(source.fMonetarySeparator),
  115.         fPadEscape(source.fPadEscape)
  116. {
  117. }
  118.  
  119. // -------------------------------------
  120. // assignment operator
  121.  
  122. DecimalFormatSymbols&
  123. DecimalFormatSymbols::operator=(const DecimalFormatSymbols& rhs)
  124. {
  125.     if (this != &rhs)
  126.     {
  127.         fZeroDigit = rhs.fZeroDigit;
  128.         fGroupingSeparator = rhs.fGroupingSeparator;
  129.         fDecimalSeparator = rhs.fDecimalSeparator;
  130.         fPercent = rhs.fPercent;
  131.         fPerMill = rhs.fPerMill;
  132.         fDigit = rhs.fDigit;
  133.         fPlusSign = rhs.fPlusSign;
  134.         fMinusSign = rhs.fMinusSign;
  135.         fExponential = rhs.fExponential;
  136.         fPatternSeparator = rhs.fPatternSeparator;
  137.         fInfinity = rhs.fInfinity;
  138.         fNaN = rhs.fNaN;
  139.         fCurrencySymbol = rhs.fCurrencySymbol;
  140.         fIntlCurrencySymbol = rhs.fIntlCurrencySymbol;
  141.         fMonetarySeparator = rhs.fMonetarySeparator;
  142.         fPadEscape = rhs.fPadEscape;
  143.     }
  144.     return *this;
  145. }
  146.  
  147. // -------------------------------------
  148.  
  149. bool_t
  150. DecimalFormatSymbols::operator==(const DecimalFormatSymbols& that) const
  151. {
  152.     if (this == &that) return TRUE;
  153.  
  154.     return (fZeroDigit == that.fZeroDigit &&
  155.         fGroupingSeparator == that.fGroupingSeparator &&
  156.         fDecimalSeparator == that.fDecimalSeparator &&
  157.         fPercent == that.fPercent &&
  158.         fPerMill == that.fPerMill &&
  159.         fDigit == that.fDigit &&
  160.         fPlusSign == that.fPlusSign &&
  161.         fMinusSign == that.fMinusSign &&
  162.         fExponential == that.fExponential &&
  163.         fPatternSeparator == that.fPatternSeparator &&
  164.         fInfinity == that.fInfinity &&
  165.         fNaN == that.fNaN &&
  166.         fCurrencySymbol == that.fCurrencySymbol &&
  167.         fIntlCurrencySymbol == that.fIntlCurrencySymbol &&
  168.         fMonetarySeparator == that.fMonetarySeparator &&
  169.         fPadEscape == that.fPadEscape
  170.         );
  171. }
  172.  
  173. // -------------------------------------
  174.  
  175. void
  176. DecimalFormatSymbols::initialize(const Locale& locale, UErrorCode& status,
  177.                                  bool_t useLastResortData)
  178. {
  179.     if (U_FAILURE(status)) return;
  180.  
  181.     ResourceBundle resource(Locale::getDataDirectory(), locale, status);
  182.     if (U_FAILURE(status))
  183.     {
  184.         // Initializes with last resort data if necessary.
  185.         if (useLastResortData)
  186.         {
  187.             status = U_USING_FALLBACK_ERROR;
  188.             initialize(fgLastResortNumberElements, fgLastResortCurrencyElements);
  189.         }
  190.         return;
  191.     }
  192.  
  193.     int32_t numberElementsLength=0;
  194.     // Gets the number element array.
  195.     const UnicodeString* numberElements = resource.getStringArray(fgNumberElements, numberElementsLength, status);
  196.     int32_t currencyElementsLength=0;
  197.     // Gets the currency element array.
  198.     const UnicodeString* currencyElements = resource.getStringArray(fgCurrencyElements, currencyElementsLength, status);
  199.     if (U_FAILURE(status)) return;
  200.  
  201.     // If the array size is too small, something is wrong with the resource
  202.     // bundle, returns the failure error code.
  203.     if (numberElementsLength < fgNumberElementsLength ||
  204.         currencyElementsLength < fgCurrencyElementsLength) {
  205.         status = U_INVALID_FORMAT_ERROR;
  206.         return;
  207.     }
  208.  
  209.     initialize(numberElements, currencyElements);
  210. }
  211.  
  212. // Initializes the DecimalFormatSymbol instance with the data obtained
  213. // from ResourceBundle in the desired locale.
  214.  
  215. void
  216. DecimalFormatSymbols::initialize(const UnicodeString* numberElements, const UnicodeString* currencyElements)
  217. {
  218.     fDecimalSeparator  = numberElements[0][(UTextOffset)0];
  219.     fGroupingSeparator = numberElements[1][(UTextOffset)0];
  220.     fPatternSeparator  = numberElements[2][(UTextOffset)0];
  221.     fPercent           = numberElements[3][(UTextOffset)0];
  222.     fZeroDigit         = numberElements[4][(UTextOffset)0];
  223.     fDigit             = numberElements[5][(UTextOffset)0];
  224.     fPlusSign          = 0x002B; // '+' Hard coded for now; get from resource later
  225.     fMinusSign         = numberElements[6][(UTextOffset)0];
  226.     fExponential       = numberElements[7][(UTextOffset)0];
  227.     fPerMill           = numberElements[8][(UTextOffset)0];
  228.     fInfinity          = numberElements[9];
  229.     fNaN               = numberElements[10];
  230.     fCurrencySymbol     = currencyElements[0];
  231.     fIntlCurrencySymbol = currencyElements[1];
  232.     fPadEscape         = 0x002A; // '*' Hard coded for now; get from resource later
  233.  
  234.     // if the resource data specified the empty string as the monetary decimal
  235.     // separator, that means we should just use the regular separator as the
  236.     // monetary separator
  237.     if(currencyElements[2].size() == 0)
  238.         fMonetarySeparator = fDecimalSeparator;
  239.     else
  240.         fMonetarySeparator = currencyElements[2][(UTextOffset)0];
  241. }
  242.  
  243. //eof
  244.