Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | ******************************************************************************** |
michael@0 | 3 | * Copyright (C) 1997-2011, International Business Machines Corporation and others. |
michael@0 | 4 | * All Rights Reserved. |
michael@0 | 5 | ******************************************************************************** |
michael@0 | 6 | * |
michael@0 | 7 | * File FORMAT.H |
michael@0 | 8 | * |
michael@0 | 9 | * Modification History: |
michael@0 | 10 | * |
michael@0 | 11 | * Date Name Description |
michael@0 | 12 | * 02/19/97 aliu Converted from java. |
michael@0 | 13 | * 03/17/97 clhuang Updated per C++ implementation. |
michael@0 | 14 | * 03/27/97 helena Updated to pass the simple test after code review. |
michael@0 | 15 | ******************************************************************************** |
michael@0 | 16 | */ |
michael@0 | 17 | // ***************************************************************************** |
michael@0 | 18 | // This file was generated from the java source file Format.java |
michael@0 | 19 | // ***************************************************************************** |
michael@0 | 20 | |
michael@0 | 21 | #ifndef FORMAT_H |
michael@0 | 22 | #define FORMAT_H |
michael@0 | 23 | |
michael@0 | 24 | |
michael@0 | 25 | #include "unicode/utypes.h" |
michael@0 | 26 | |
michael@0 | 27 | /** |
michael@0 | 28 | * \file |
michael@0 | 29 | * \brief C++ API: Base class for all formats. |
michael@0 | 30 | */ |
michael@0 | 31 | |
michael@0 | 32 | #if !UCONFIG_NO_FORMATTING |
michael@0 | 33 | |
michael@0 | 34 | #include "unicode/unistr.h" |
michael@0 | 35 | #include "unicode/fmtable.h" |
michael@0 | 36 | #include "unicode/fieldpos.h" |
michael@0 | 37 | #include "unicode/fpositer.h" |
michael@0 | 38 | #include "unicode/parsepos.h" |
michael@0 | 39 | #include "unicode/parseerr.h" |
michael@0 | 40 | #include "unicode/locid.h" |
michael@0 | 41 | |
michael@0 | 42 | U_NAMESPACE_BEGIN |
michael@0 | 43 | |
michael@0 | 44 | /** |
michael@0 | 45 | * Base class for all formats. This is an abstract base class which |
michael@0 | 46 | * specifies the protocol for classes which convert other objects or |
michael@0 | 47 | * values, such as numeric values and dates, and their string |
michael@0 | 48 | * representations. In some cases these representations may be |
michael@0 | 49 | * localized or contain localized characters or strings. For example, |
michael@0 | 50 | * a numeric formatter such as DecimalFormat may convert a numeric |
michael@0 | 51 | * value such as 12345 to the string "$12,345". It may also parse |
michael@0 | 52 | * the string back into a numeric value. A date and time formatter |
michael@0 | 53 | * like SimpleDateFormat may represent a specific date, encoded |
michael@0 | 54 | * numerically, as a string such as "Wednesday, February 26, 1997 AD". |
michael@0 | 55 | * <P> |
michael@0 | 56 | * Many of the concrete subclasses of Format employ the notion of |
michael@0 | 57 | * a pattern. A pattern is a string representation of the rules which |
michael@0 | 58 | * govern the interconversion between values and strings. For example, |
michael@0 | 59 | * a DecimalFormat object may be associated with the pattern |
michael@0 | 60 | * "$#,##0.00;($#,##0.00)", which is a common US English format for |
michael@0 | 61 | * currency values, yielding strings such as "$1,234.45" for 1234.45, |
michael@0 | 62 | * and "($987.65)" for 987.6543. The specific syntax of a pattern |
michael@0 | 63 | * is defined by each subclass. |
michael@0 | 64 | * <P> |
michael@0 | 65 | * Even though many subclasses use patterns, the notion of a pattern |
michael@0 | 66 | * is not inherent to Format classes in general, and is not part of |
michael@0 | 67 | * the explicit base class protocol. |
michael@0 | 68 | * <P> |
michael@0 | 69 | * Two complex formatting classes bear mentioning. These are |
michael@0 | 70 | * MessageFormat and ChoiceFormat. ChoiceFormat is a subclass of |
michael@0 | 71 | * NumberFormat which allows the user to format different number ranges |
michael@0 | 72 | * as strings. For instance, 0 may be represented as "no files", 1 as |
michael@0 | 73 | * "one file", and any number greater than 1 as "many files". |
michael@0 | 74 | * MessageFormat is a formatter which utilizes other Format objects to |
michael@0 | 75 | * format a string containing with multiple values. For instance, |
michael@0 | 76 | * A MessageFormat object might produce the string "There are no files |
michael@0 | 77 | * on the disk MyDisk on February 27, 1997." given the arguments 0, |
michael@0 | 78 | * "MyDisk", and the date value of 2/27/97. See the ChoiceFormat |
michael@0 | 79 | * and MessageFormat headers for further information. |
michael@0 | 80 | * <P> |
michael@0 | 81 | * If formatting is unsuccessful, a failing UErrorCode is returned when |
michael@0 | 82 | * the Format cannot format the type of object, otherwise if there is |
michael@0 | 83 | * something illformed about the the Unicode replacement character |
michael@0 | 84 | * 0xFFFD is returned. |
michael@0 | 85 | * <P> |
michael@0 | 86 | * If there is no match when parsing, a parse failure UErrorCode is |
michael@0 | 87 | * retured for methods which take no ParsePosition. For the method |
michael@0 | 88 | * that takes a ParsePosition, the index parameter is left unchanged. |
michael@0 | 89 | * <P> |
michael@0 | 90 | * <em>User subclasses are not supported.</em> While clients may write |
michael@0 | 91 | * subclasses, such code will not necessarily work and will not be |
michael@0 | 92 | * guaranteed to work stably from release to release. |
michael@0 | 93 | */ |
michael@0 | 94 | class U_I18N_API Format : public UObject { |
michael@0 | 95 | public: |
michael@0 | 96 | |
michael@0 | 97 | /** Destructor |
michael@0 | 98 | * @stable ICU 2.4 |
michael@0 | 99 | */ |
michael@0 | 100 | virtual ~Format(); |
michael@0 | 101 | |
michael@0 | 102 | /** |
michael@0 | 103 | * Return true if the given Format objects are semantically equal. |
michael@0 | 104 | * Objects of different subclasses are considered unequal. |
michael@0 | 105 | * @param other the object to be compared with. |
michael@0 | 106 | * @return Return true if the given Format objects are semantically equal. |
michael@0 | 107 | * Objects of different subclasses are considered unequal. |
michael@0 | 108 | * @stable ICU 2.0 |
michael@0 | 109 | */ |
michael@0 | 110 | virtual UBool operator==(const Format& other) const = 0; |
michael@0 | 111 | |
michael@0 | 112 | /** |
michael@0 | 113 | * Return true if the given Format objects are not semantically |
michael@0 | 114 | * equal. |
michael@0 | 115 | * @param other the object to be compared with. |
michael@0 | 116 | * @return Return true if the given Format objects are not semantically. |
michael@0 | 117 | * @stable ICU 2.0 |
michael@0 | 118 | */ |
michael@0 | 119 | UBool operator!=(const Format& other) const { return !operator==(other); } |
michael@0 | 120 | |
michael@0 | 121 | /** |
michael@0 | 122 | * Clone this object polymorphically. The caller is responsible |
michael@0 | 123 | * for deleting the result when done. |
michael@0 | 124 | * @return A copy of the object |
michael@0 | 125 | * @stable ICU 2.0 |
michael@0 | 126 | */ |
michael@0 | 127 | virtual Format* clone() const = 0; |
michael@0 | 128 | |
michael@0 | 129 | /** |
michael@0 | 130 | * Formats an object to produce a string. |
michael@0 | 131 | * |
michael@0 | 132 | * @param obj The object to format. |
michael@0 | 133 | * @param appendTo Output parameter to receive result. |
michael@0 | 134 | * Result is appended to existing contents. |
michael@0 | 135 | * @param status Output parameter filled in with success or failure status. |
michael@0 | 136 | * @return Reference to 'appendTo' parameter. |
michael@0 | 137 | * @stable ICU 2.0 |
michael@0 | 138 | */ |
michael@0 | 139 | UnicodeString& format(const Formattable& obj, |
michael@0 | 140 | UnicodeString& appendTo, |
michael@0 | 141 | UErrorCode& status) const; |
michael@0 | 142 | |
michael@0 | 143 | /** |
michael@0 | 144 | * Format an object to produce a string. This is a pure virtual method which |
michael@0 | 145 | * subclasses must implement. This method allows polymorphic formatting |
michael@0 | 146 | * of Formattable objects. If a subclass of Format receives a Formattable |
michael@0 | 147 | * object type it doesn't handle (e.g., if a numeric Formattable is passed |
michael@0 | 148 | * to a DateFormat object) then it returns a failing UErrorCode. |
michael@0 | 149 | * |
michael@0 | 150 | * @param obj The object to format. |
michael@0 | 151 | * @param appendTo Output parameter to receive result. |
michael@0 | 152 | * Result is appended to existing contents. |
michael@0 | 153 | * @param pos On input: an alignment field, if desired. |
michael@0 | 154 | * On output: the offsets of the alignment field. |
michael@0 | 155 | * @param status Output param filled with success/failure status. |
michael@0 | 156 | * @return Reference to 'appendTo' parameter. |
michael@0 | 157 | * @stable ICU 2.0 |
michael@0 | 158 | */ |
michael@0 | 159 | virtual UnicodeString& format(const Formattable& obj, |
michael@0 | 160 | UnicodeString& appendTo, |
michael@0 | 161 | FieldPosition& pos, |
michael@0 | 162 | UErrorCode& status) const = 0; |
michael@0 | 163 | /** |
michael@0 | 164 | * Format an object to produce a string. Subclasses should override this |
michael@0 | 165 | * method. This method allows polymorphic formatting of Formattable objects. |
michael@0 | 166 | * If a subclass of Format receives a Formattable object type it doesn't |
michael@0 | 167 | * handle (e.g., if a numeric Formattable is passed to a DateFormat object) |
michael@0 | 168 | * then it returns a failing UErrorCode. |
michael@0 | 169 | * |
michael@0 | 170 | * @param obj The object to format. |
michael@0 | 171 | * @param appendTo Output parameter to receive result. |
michael@0 | 172 | * Result is appended to existing contents. |
michael@0 | 173 | * @param posIter On return, can be used to iterate over positions |
michael@0 | 174 | * of fields generated by this format call. |
michael@0 | 175 | * @param status Output param filled with success/failure status. |
michael@0 | 176 | * @return Reference to 'appendTo' parameter. |
michael@0 | 177 | * @stable ICU 4.4 |
michael@0 | 178 | */ |
michael@0 | 179 | virtual UnicodeString& format(const Formattable& obj, |
michael@0 | 180 | UnicodeString& appendTo, |
michael@0 | 181 | FieldPositionIterator* posIter, |
michael@0 | 182 | UErrorCode& status) const; |
michael@0 | 183 | |
michael@0 | 184 | /** |
michael@0 | 185 | * Parse a string to produce an object. This is a pure virtual |
michael@0 | 186 | * method which subclasses must implement. This method allows |
michael@0 | 187 | * polymorphic parsing of strings into Formattable objects. |
michael@0 | 188 | * <P> |
michael@0 | 189 | * Before calling, set parse_pos.index to the offset you want to |
michael@0 | 190 | * start parsing at in the source. After calling, parse_pos.index |
michael@0 | 191 | * is the end of the text you parsed. If error occurs, index is |
michael@0 | 192 | * unchanged. |
michael@0 | 193 | * <P> |
michael@0 | 194 | * When parsing, leading whitespace is discarded (with successful |
michael@0 | 195 | * parse), while trailing whitespace is left as is. |
michael@0 | 196 | * <P> |
michael@0 | 197 | * Example: |
michael@0 | 198 | * <P> |
michael@0 | 199 | * Parsing "_12_xy" (where _ represents a space) for a number, |
michael@0 | 200 | * with index == 0 will result in the number 12, with |
michael@0 | 201 | * parse_pos.index updated to 3 (just before the second space). |
michael@0 | 202 | * Parsing a second time will result in a failing UErrorCode since |
michael@0 | 203 | * "xy" is not a number, and leave index at 3. |
michael@0 | 204 | * <P> |
michael@0 | 205 | * Subclasses will typically supply specific parse methods that |
michael@0 | 206 | * return different types of values. Since methods can't overload |
michael@0 | 207 | * on return types, these will typically be named "parse", while |
michael@0 | 208 | * this polymorphic method will always be called parseObject. Any |
michael@0 | 209 | * parse method that does not take a parse_pos should set status |
michael@0 | 210 | * to an error value when no text in the required format is at the |
michael@0 | 211 | * start position. |
michael@0 | 212 | * |
michael@0 | 213 | * @param source The string to be parsed into an object. |
michael@0 | 214 | * @param result Formattable to be set to the parse result. |
michael@0 | 215 | * If parse fails, return contents are undefined. |
michael@0 | 216 | * @param parse_pos The position to start parsing at. Upon return |
michael@0 | 217 | * this param is set to the position after the |
michael@0 | 218 | * last character successfully parsed. If the |
michael@0 | 219 | * source is not parsed successfully, this param |
michael@0 | 220 | * will remain unchanged. |
michael@0 | 221 | * @stable ICU 2.0 |
michael@0 | 222 | */ |
michael@0 | 223 | virtual void parseObject(const UnicodeString& source, |
michael@0 | 224 | Formattable& result, |
michael@0 | 225 | ParsePosition& parse_pos) const = 0; |
michael@0 | 226 | |
michael@0 | 227 | /** |
michael@0 | 228 | * Parses a string to produce an object. This is a convenience method |
michael@0 | 229 | * which calls the pure virtual parseObject() method, and returns a |
michael@0 | 230 | * failure UErrorCode if the ParsePosition indicates failure. |
michael@0 | 231 | * |
michael@0 | 232 | * @param source The string to be parsed into an object. |
michael@0 | 233 | * @param result Formattable to be set to the parse result. |
michael@0 | 234 | * If parse fails, return contents are undefined. |
michael@0 | 235 | * @param status Output param to be filled with success/failure |
michael@0 | 236 | * result code. |
michael@0 | 237 | * @stable ICU 2.0 |
michael@0 | 238 | */ |
michael@0 | 239 | void parseObject(const UnicodeString& source, |
michael@0 | 240 | Formattable& result, |
michael@0 | 241 | UErrorCode& status) const; |
michael@0 | 242 | |
michael@0 | 243 | /** Get the locale for this format object. You can choose between valid and actual locale. |
michael@0 | 244 | * @param type type of the locale we're looking for (valid or actual) |
michael@0 | 245 | * @param status error code for the operation |
michael@0 | 246 | * @return the locale |
michael@0 | 247 | * @stable ICU 2.8 |
michael@0 | 248 | */ |
michael@0 | 249 | Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const; |
michael@0 | 250 | |
michael@0 | 251 | #ifndef U_HIDE_INTERNAL_API |
michael@0 | 252 | /** Get the locale for this format object. You can choose between valid and actual locale. |
michael@0 | 253 | * @param type type of the locale we're looking for (valid or actual) |
michael@0 | 254 | * @param status error code for the operation |
michael@0 | 255 | * @return the locale |
michael@0 | 256 | * @internal |
michael@0 | 257 | */ |
michael@0 | 258 | const char* getLocaleID(ULocDataLocaleType type, UErrorCode &status) const; |
michael@0 | 259 | #endif /* U_HIDE_INTERNAL_API */ |
michael@0 | 260 | |
michael@0 | 261 | protected: |
michael@0 | 262 | /** @stable ICU 2.8 */ |
michael@0 | 263 | void setLocaleIDs(const char* valid, const char* actual); |
michael@0 | 264 | |
michael@0 | 265 | protected: |
michael@0 | 266 | /** |
michael@0 | 267 | * Default constructor for subclass use only. Does nothing. |
michael@0 | 268 | * @stable ICU 2.0 |
michael@0 | 269 | */ |
michael@0 | 270 | Format(); |
michael@0 | 271 | |
michael@0 | 272 | /** |
michael@0 | 273 | * @stable ICU 2.0 |
michael@0 | 274 | */ |
michael@0 | 275 | Format(const Format&); // Does nothing; for subclasses only |
michael@0 | 276 | |
michael@0 | 277 | /** |
michael@0 | 278 | * @stable ICU 2.0 |
michael@0 | 279 | */ |
michael@0 | 280 | Format& operator=(const Format&); // Does nothing; for subclasses |
michael@0 | 281 | |
michael@0 | 282 | |
michael@0 | 283 | /** |
michael@0 | 284 | * Simple function for initializing a UParseError from a UnicodeString. |
michael@0 | 285 | * |
michael@0 | 286 | * @param pattern The pattern to copy into the parseError |
michael@0 | 287 | * @param pos The position in pattern where the error occured |
michael@0 | 288 | * @param parseError The UParseError object to fill in |
michael@0 | 289 | * @stable ICU 2.4 |
michael@0 | 290 | */ |
michael@0 | 291 | static void syntaxError(const UnicodeString& pattern, |
michael@0 | 292 | int32_t pos, |
michael@0 | 293 | UParseError& parseError); |
michael@0 | 294 | |
michael@0 | 295 | private: |
michael@0 | 296 | char actualLocale[ULOC_FULLNAME_CAPACITY]; |
michael@0 | 297 | char validLocale[ULOC_FULLNAME_CAPACITY]; |
michael@0 | 298 | }; |
michael@0 | 299 | |
michael@0 | 300 | U_NAMESPACE_END |
michael@0 | 301 | |
michael@0 | 302 | #endif /* #if !UCONFIG_NO_FORMATTING */ |
michael@0 | 303 | |
michael@0 | 304 | #endif // _FORMAT |
michael@0 | 305 | //eof |