]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/format.cpp
2 *******************************************************************************
3 * Copyright (C) 1997-2012, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
9 * Modification History:
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 ********************************************************************************
18 // *****************************************************************************
19 // This file was generated from the java source file Format.java
20 // *****************************************************************************
22 #include "utypeinfo.h" // for 'typeid' to work
24 #include "unicode/utypes.h"
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
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...
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
) {
43 /* Format class implementation ---------------------------------------------- */
45 #if !UCONFIG_NO_FORMATTING
47 #include "unicode/format.h"
48 #include "unicode/ures.h"
52 // *****************************************************************************
54 // *****************************************************************************
58 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FieldPosition
)
60 FieldPosition::~FieldPosition() {}
63 FieldPosition::clone() const {
64 return new FieldPosition(*this);
67 // -------------------------------------
68 // default constructor
73 *validLocale
= *actualLocale
= 0;
76 // -------------------------------------
82 // -------------------------------------
85 Format::Format(const Format
&that
)
91 // -------------------------------------
92 // assignment operator
95 Format::operator=(const Format
& that
)
98 uprv_strcpy(validLocale
, that
.validLocale
);
99 uprv_strcpy(actualLocale
, that
.actualLocale
);
104 // -------------------------------------
105 // Formats the obj and append the result in the buffer, toAppendTo.
106 // This calls the actual implementation in the concrete subclasses.
109 Format::format(const Formattable
& obj
,
110 UnicodeString
& toAppendTo
,
111 UErrorCode
& status
) const
113 if (U_FAILURE(status
)) return toAppendTo
;
115 FieldPosition
pos(FieldPosition::DONT_CARE
);
117 return format(obj
, toAppendTo
, pos
, status
);
120 // -------------------------------------
121 // Default implementation sets unsupported error; subclasses should
125 Format::format(const Formattable
& /* unused obj */,
126 UnicodeString
& toAppendTo
,
127 FieldPositionIterator
* /* unused posIter */,
128 UErrorCode
& status
) const
130 if (!U_FAILURE(status
)) {
131 status
= U_UNSUPPORTED_ERROR
;
136 // -------------------------------------
137 // Parses the source string and create the corresponding
138 // result object. Checks the parse position for errors.
141 Format::parseObject(const UnicodeString
& source
,
143 UErrorCode
& status
) const
145 if (U_FAILURE(status
)) return;
147 ParsePosition
parsePosition(0);
148 parseObject(source
, result
, parsePosition
);
149 if (parsePosition
.getIndex() == 0) {
150 status
= U_INVALID_FORMAT_ERROR
;
154 // -------------------------------------
157 Format::operator==(const Format
& that
) const
159 // Subclasses: Call this method and then add more specific checks.
160 return typeid(*this) == typeid(that
);
162 //---------------------------------------
165 * Simple function for initializing a UParseError from a UnicodeString.
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
172 void Format::syntaxError(const UnicodeString
& pattern
,
174 UParseError
& parseError
) {
175 parseError
.offset
= pos
;
176 parseError
.line
=0; // we are not using line number
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*/));
182 pattern
.extract(start
,stop
-start
,parseError
.preContext
,0);
183 //null terminate the buffer
184 parseError
.preContext
[stop
-start
] = 0;
188 stop
= ((pos
+U_PARSE_CONTEXT_LEN
)<=pattern
.length()) ? (pos
+(U_PARSE_CONTEXT_LEN
-1)) :
190 pattern
.extract(start
,stop
-start
,parseError
.postContext
,0);
191 //null terminate the buffer
192 parseError
.postContext
[stop
-start
]= 0;
196 Format::getLocale(ULocDataLocaleType type
, UErrorCode
& status
) const {
197 U_LOCALE_BASED(locBased
, *this);
198 return locBased
.getLocale(type
, status
);
202 Format::getLocaleID(ULocDataLocaleType type
, UErrorCode
& status
) const {
203 U_LOCALE_BASED(locBased
, *this);
204 return locBased
.getLocaleID(type
, status
);
208 Format::setLocaleIDs(const char* valid
, const char* actual
) {
209 U_LOCALE_BASED(locBased
, *this);
210 locBased
.setLocaleIDs(valid
, actual
);
215 #endif /* #if !UCONFIG_NO_FORMATTING */