|
1 /* |
|
2 ******************************************************************************* |
|
3 * Copyright (C) 1997-2012, International Business Machines Corporation and * |
|
4 * others. All Rights Reserved. * |
|
5 ******************************************************************************* |
|
6 * |
|
7 * File FORMAT.CPP |
|
8 * |
|
9 * Modification History: |
|
10 * |
|
11 * Date Name Description |
|
12 * 02/19/97 aliu Converted from java. |
|
13 * 03/17/97 clhuang Implemented with new APIs. |
|
14 * 03/27/97 helena Updated to pass the simple test after code review. |
|
15 * 07/20/98 stephen Added explicit init values for Field/ParsePosition |
|
16 ******************************************************************************** |
|
17 */ |
|
18 // ***************************************************************************** |
|
19 // This file was generated from the java source file Format.java |
|
20 // ***************************************************************************** |
|
21 |
|
22 #include "utypeinfo.h" // for 'typeid' to work |
|
23 |
|
24 #include "unicode/utypes.h" |
|
25 |
|
26 #ifndef U_I18N_IMPLEMENTATION |
|
27 #error U_I18N_IMPLEMENTATION not set - must be set for all ICU source files in i18n/ - see http://userguide.icu-project.org/howtouseicu |
|
28 #endif |
|
29 |
|
30 /* |
|
31 * Dummy code: |
|
32 * If all modules in the I18N library are switched off, then there are no |
|
33 * library exports and MSVC 6 writes a .dll but not a .lib file. |
|
34 * Unless we export _something_ in that case... |
|
35 */ |
|
36 #if UCONFIG_NO_COLLATION && UCONFIG_NO_FORMATTING && UCONFIG_NO_TRANSLITERATION |
|
37 U_CAPI int32_t U_EXPORT2 |
|
38 uprv_icuin_lib_dummy(int32_t i) { |
|
39 return -i; |
|
40 } |
|
41 #endif |
|
42 |
|
43 /* Format class implementation ---------------------------------------------- */ |
|
44 |
|
45 #if !UCONFIG_NO_FORMATTING |
|
46 |
|
47 #include "unicode/format.h" |
|
48 #include "unicode/ures.h" |
|
49 #include "cstring.h" |
|
50 #include "locbased.h" |
|
51 |
|
52 // ***************************************************************************** |
|
53 // class Format |
|
54 // ***************************************************************************** |
|
55 |
|
56 U_NAMESPACE_BEGIN |
|
57 |
|
58 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FieldPosition) |
|
59 |
|
60 FieldPosition::~FieldPosition() {} |
|
61 |
|
62 FieldPosition * |
|
63 FieldPosition::clone() const { |
|
64 return new FieldPosition(*this); |
|
65 } |
|
66 |
|
67 // ------------------------------------- |
|
68 // default constructor |
|
69 |
|
70 Format::Format() |
|
71 : UObject() |
|
72 { |
|
73 *validLocale = *actualLocale = 0; |
|
74 } |
|
75 |
|
76 // ------------------------------------- |
|
77 |
|
78 Format::~Format() |
|
79 { |
|
80 } |
|
81 |
|
82 // ------------------------------------- |
|
83 // copy constructor |
|
84 |
|
85 Format::Format(const Format &that) |
|
86 : UObject(that) |
|
87 { |
|
88 *this = that; |
|
89 } |
|
90 |
|
91 // ------------------------------------- |
|
92 // assignment operator |
|
93 |
|
94 Format& |
|
95 Format::operator=(const Format& that) |
|
96 { |
|
97 if (this != &that) { |
|
98 uprv_strcpy(validLocale, that.validLocale); |
|
99 uprv_strcpy(actualLocale, that.actualLocale); |
|
100 } |
|
101 return *this; |
|
102 } |
|
103 |
|
104 // ------------------------------------- |
|
105 // Formats the obj and append the result in the buffer, toAppendTo. |
|
106 // This calls the actual implementation in the concrete subclasses. |
|
107 |
|
108 UnicodeString& |
|
109 Format::format(const Formattable& obj, |
|
110 UnicodeString& toAppendTo, |
|
111 UErrorCode& status) const |
|
112 { |
|
113 if (U_FAILURE(status)) return toAppendTo; |
|
114 |
|
115 FieldPosition pos(FieldPosition::DONT_CARE); |
|
116 |
|
117 return format(obj, toAppendTo, pos, status); |
|
118 } |
|
119 |
|
120 // ------------------------------------- |
|
121 // Default implementation sets unsupported error; subclasses should |
|
122 // override. |
|
123 |
|
124 UnicodeString& |
|
125 Format::format(const Formattable& /* unused obj */, |
|
126 UnicodeString& toAppendTo, |
|
127 FieldPositionIterator* /* unused posIter */, |
|
128 UErrorCode& status) const |
|
129 { |
|
130 if (!U_FAILURE(status)) { |
|
131 status = U_UNSUPPORTED_ERROR; |
|
132 } |
|
133 return toAppendTo; |
|
134 } |
|
135 |
|
136 // ------------------------------------- |
|
137 // Parses the source string and create the corresponding |
|
138 // result object. Checks the parse position for errors. |
|
139 |
|
140 void |
|
141 Format::parseObject(const UnicodeString& source, |
|
142 Formattable& result, |
|
143 UErrorCode& status) const |
|
144 { |
|
145 if (U_FAILURE(status)) return; |
|
146 |
|
147 ParsePosition parsePosition(0); |
|
148 parseObject(source, result, parsePosition); |
|
149 if (parsePosition.getIndex() == 0) { |
|
150 status = U_INVALID_FORMAT_ERROR; |
|
151 } |
|
152 } |
|
153 |
|
154 // ------------------------------------- |
|
155 |
|
156 UBool |
|
157 Format::operator==(const Format& that) const |
|
158 { |
|
159 // Subclasses: Call this method and then add more specific checks. |
|
160 return typeid(*this) == typeid(that); |
|
161 } |
|
162 //--------------------------------------- |
|
163 |
|
164 /** |
|
165 * Simple function for initializing a UParseError from a UnicodeString. |
|
166 * |
|
167 * @param pattern The pattern to copy into the parseError |
|
168 * @param pos The position in pattern where the error occured |
|
169 * @param parseError The UParseError object to fill in |
|
170 * @draft ICU 2.4 |
|
171 */ |
|
172 void Format::syntaxError(const UnicodeString& pattern, |
|
173 int32_t pos, |
|
174 UParseError& parseError) { |
|
175 parseError.offset = pos; |
|
176 parseError.line=0; // we are not using line number |
|
177 |
|
178 // for pre-context |
|
179 int32_t start = (pos < U_PARSE_CONTEXT_LEN)? 0 : (pos - (U_PARSE_CONTEXT_LEN-1 |
|
180 /* subtract 1 so that we have room for null*/)); |
|
181 int32_t stop = pos; |
|
182 pattern.extract(start,stop-start,parseError.preContext,0); |
|
183 //null terminate the buffer |
|
184 parseError.preContext[stop-start] = 0; |
|
185 |
|
186 //for post-context |
|
187 start = pos+1; |
|
188 stop = ((pos+U_PARSE_CONTEXT_LEN)<=pattern.length()) ? (pos+(U_PARSE_CONTEXT_LEN-1)) : |
|
189 pattern.length(); |
|
190 pattern.extract(start,stop-start,parseError.postContext,0); |
|
191 //null terminate the buffer |
|
192 parseError.postContext[stop-start]= 0; |
|
193 } |
|
194 |
|
195 Locale |
|
196 Format::getLocale(ULocDataLocaleType type, UErrorCode& status) const { |
|
197 U_LOCALE_BASED(locBased, *this); |
|
198 return locBased.getLocale(type, status); |
|
199 } |
|
200 |
|
201 const char * |
|
202 Format::getLocaleID(ULocDataLocaleType type, UErrorCode& status) const { |
|
203 U_LOCALE_BASED(locBased, *this); |
|
204 return locBased.getLocaleID(type, status); |
|
205 } |
|
206 |
|
207 void |
|
208 Format::setLocaleIDs(const char* valid, const char* actual) { |
|
209 U_LOCALE_BASED(locBased, *this); |
|
210 locBased.setLocaleIDs(valid, actual); |
|
211 } |
|
212 |
|
213 U_NAMESPACE_END |
|
214 |
|
215 #endif /* #if !UCONFIG_NO_FORMATTING */ |
|
216 |
|
217 //eof |