home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / include / fieldpos.h < prev    next >
C/C++ Source or Header  |  1999-11-12  |  7KB  |  229 lines

  1. /*
  2. ********************************************************************************
  3. *                                                                              *
  4. * COPYRIGHT:                                                                   *
  5. *   (C) Copyright Taligent, Inc.,  1997                                        *
  6. *   (C) Copyright International Business Machines Corporation,  1997-1999      *
  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 FIELDPOS.H
  14. *
  15. * Modification History:
  16. *
  17. *   Date        Name        Description
  18. *   02/25/97    aliu        Converted from java.
  19. *   03/17/97    clhuang     Updated per Format implementation.
  20. *    07/17/98    stephen        Added default/copy ctors, and operators =, ==, !=
  21. ********************************************************************************
  22. */
  23. // *****************************************************************************
  24. // This file was generated from the java source file FieldPosition.java
  25. // *****************************************************************************
  26.  
  27. #ifndef FIELDPOS_H
  28. #define FIELDPOS_H
  29.  
  30. #include "utypes.h"
  31.  
  32. /**
  33.  * <code>FieldPosition</code> is a simple class used by <code>Format</code>
  34.  * and its subclasses to identify fields in formatted output. Fields are
  35.  * identified by constants, whose names typically end with <code>_FIELD</code>,
  36.  * defined in the various subclasses of <code>Format</code>. See
  37.  * <code>ERA_FIELD</code> and its friends in <code>DateFormat</code> for
  38.  * an example.
  39.  *
  40.  * <p>
  41.  * <code>FieldPosition</code> keeps track of the position of the
  42.  * field within the formatted output with two indices: the index
  43.  * of the first character of the field and the index of the last
  44.  * character of the field.
  45.  *
  46.  * <p>
  47.  * One version of the <code>format</code> method in the various
  48.  * <code>Format</code> classes requires a <code>FieldPosition</code>
  49.  * object as an argument. You use this <code>format</code> method
  50.  * to perform partial formatting or to get information about the
  51.  * formatted output (such as the position of a field).
  52.  *
  53.  * <p>
  54.  * Below is an example of using <code>FieldPosition</code> to aid
  55.  * alignment of an array of formatted floating-point numbers on
  56.  * their decimal points:
  57.  * <pre>
  58.  * .      double doubleNum[] = {123456789.0, -12345678.9, 1234567.89, -123456.789,
  59.  * .                 12345.6789, -1234.56789, 123.456789, -12.3456789, 1.23456789};
  60.  * .      int dNumSize = (int)(sizeof(doubleNum)/sizeof(double));
  61.  * .      
  62.  * .      UErrorCode status = U_ZERO_ERROR;
  63.  * .      DecimalFormat* fmt = (DecimalFormat*) NumberFormat::createInstance(status);
  64.  * .      fmt->setDecimalSeparatorAlwaysShown(true);
  65.  * .      
  66.  * .      const int tempLen = 20;
  67.  * .      char temp[tempLen];
  68.  * .      
  69.  * .      for (int i=0; i<dNumSize; i++) {
  70.  * .          FieldPosition pos(NumberFormat::INTEGER_FIELD);
  71.  * .          UnicodeString buf;
  72.  * .          char fmtText[tempLen];
  73.  * .          ToCharString(fmt->format(doubleNum[i], buf, pos), fmtText);
  74.  * .          for (int j=0; j<tempLen; j++) temp[j] = ' '; // clear with spaces
  75.  * .          temp[__min(tempLen, tempLen-pos.getEndIndex())] = '\0';
  76.  * .          cout << temp << fmtText   << endl;
  77.  * .      }
  78.  * .      delete fmt;
  79.  * </pre>
  80.  * <p>
  81.  * The code will generate the following output:
  82.  * <pre>
  83.  * .          123,456,789.000
  84.  * .          -12,345,678.900
  85.  * .            1,234,567.880
  86.  * .             -123,456.789
  87.  * .               12,345.678
  88.  * .               -1,234.567
  89.  * .                  123.456
  90.  * .                  -12.345
  91.  * .                    1.234
  92.  * </pre>
  93. */
  94. class U_I18N_API FieldPosition {
  95. public:
  96.     /**
  97.      * DONT_CARE may be specified as the field to indicate that the
  98.      * caller doesn't need to specify a field.  Do not subclass.
  99.      */
  100.     enum { DONT_CARE = -1 };
  101.  
  102.     /**
  103.      * Creates a FieldPosition object with a non-specified field.
  104.      */
  105.     FieldPosition() 
  106.         : fField(DONT_CARE), fBeginIndex(0), fEndIndex(0) {}
  107.  
  108.     /**
  109.      * Creates a FieldPosition object for the given field.  Fields are
  110.      * identified by constants, whose names typically end with _FIELD,
  111.      * in the various subclasses of Format.
  112.      *
  113.      * @see NumberFormat#INTEGER_FIELD
  114.      * @see NumberFormat#FRACTION_FIELD
  115.      * @see DateFormat#YEAR_FIELD
  116.      * @see DateFormat#MONTH_FIELD
  117.      */
  118.     FieldPosition(int32_t field) 
  119.         : fField(field), fBeginIndex(0), fEndIndex(0) {}
  120.  
  121.     /**
  122.      * Copy constructor
  123.      * @param copy the object to be copied from.
  124.      */
  125.     FieldPosition(const FieldPosition& copy) 
  126.         : fField(copy.fField), fBeginIndex(copy.fBeginIndex), fEndIndex(copy.fEndIndex) {}
  127.  
  128.     /**
  129.      * Destructor
  130.      */
  131.     ~FieldPosition() {}
  132.  
  133.     /**
  134.      * Assignment operator
  135.      */
  136.     FieldPosition&      operator=(const FieldPosition& copy);
  137.  
  138.     /** 
  139.      * Equality operator.
  140.      * @return TRUE if the two field positions are equal, FALSE otherwise.
  141.      */
  142.     bool_t              operator==(const FieldPosition& that) const;
  143.  
  144.     /** 
  145.      * Equality operator.
  146.      * @return TRUE if the two field positions are not equal, FALSE otherwise.
  147.      */
  148.     bool_t              operator!=(const FieldPosition& that) const;
  149.  
  150.     /**
  151.      * Retrieve the field identifier.
  152.      */
  153.     int32_t getField(void) const { return fField; }
  154.  
  155.     /**
  156.      * Retrieve the index of the first character in the requested field.
  157.      */
  158.     int32_t getBeginIndex(void) const { return fBeginIndex; }
  159.  
  160.     /**
  161.      * Retrieve the index of the character following the last character in the
  162.      * requested field.
  163.      */
  164.     int32_t getEndIndex(void) const { return fEndIndex; }
  165.  
  166.     /**
  167.      * Set the field.
  168.      */
  169.     void setField(int32_t f) { fField = f; }
  170.  
  171.     /**
  172.      * Set the begin index.  For use by subclasses of Format.
  173.      */
  174.     void setBeginIndex(int32_t bi) { fBeginIndex = bi; }
  175.  
  176.     /**
  177.      * Set the end index.  For use by subclasses of Format.
  178.      */
  179.     void setEndIndex(int32_t ei) { fEndIndex = ei; }
  180.     
  181. private:
  182.     /**
  183.      * Input: Desired field to determine start and end offsets for.
  184.      * The meaning depends on the subclass of Format.
  185.      */
  186.     int32_t fField;
  187.  
  188.     /**
  189.      * Output: End offset of field in text.
  190.      * If the field does not occur in the text, 0 is returned.
  191.      */
  192.     int32_t fEndIndex;
  193.  
  194.     /**
  195.      * Output: Start offset of field in text.
  196.      * If the field does not occur in the text, 0 is returned.
  197.      */
  198.     int32_t fBeginIndex;
  199. };
  200.  
  201. inline FieldPosition&
  202. FieldPosition::operator=(const FieldPosition& copy)
  203. {
  204.     fField         = copy.fField;
  205.     fEndIndex     = copy.fEndIndex;
  206.     fBeginIndex = copy.fBeginIndex;
  207.     return *this;
  208. }
  209.  
  210. inline bool_t
  211. FieldPosition::operator==(const FieldPosition& copy) const
  212. {
  213.     if(    fField         != copy.fField || 
  214.         fEndIndex     != copy.fEndIndex ||
  215.         fBeginIndex != copy.fBeginIndex) 
  216.         return FALSE;
  217.     else
  218.         return TRUE;
  219. }
  220.  
  221. inline bool_t
  222. FieldPosition::operator!=(const FieldPosition& copy) const
  223. {
  224.     return !operator==(copy);
  225. }
  226.  
  227. #endif // _FIELDPOS
  228. //eof
  229.