• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 *******************************************************************************
3 * Copyright (C) 1997-2010, 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 "unicode/utypeinfo.h"  // for 'typeid' to work
23 
24 #include "unicode/utypes.h"
25 
26 /*
27  * Dummy code:
28  * If all modules in the I18N library are switched off, then there are no
29  * library exports and MSVC 6 writes a .dll but not a .lib file.
30  * Unless we export _something_ in that case...
31  */
32 #if UCONFIG_NO_COLLATION && UCONFIG_NO_FORMATTING && UCONFIG_NO_TRANSLITERATION
33 U_CAPI int32_t U_EXPORT2
uprv_icuin_lib_dummy(int32_t i)34 uprv_icuin_lib_dummy(int32_t i) {
35     return -i;
36 }
37 #endif
38 
39 /* Format class implementation ---------------------------------------------- */
40 
41 #if !UCONFIG_NO_FORMATTING
42 
43 #include "unicode/format.h"
44 #include "unicode/ures.h"
45 #include "cstring.h"
46 #include "locbased.h"
47 
48 // *****************************************************************************
49 // class Format
50 // *****************************************************************************
51 
52 U_NAMESPACE_BEGIN
53 
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FieldPosition)54 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FieldPosition)
55 
56 FieldPosition::~FieldPosition() {}
57 
58 FieldPosition *
clone() const59 FieldPosition::clone() const {
60     return new FieldPosition(*this);
61 }
62 
63 // -------------------------------------
64 // default constructor
65 
Format()66 Format::Format()
67     : UObject()
68 {
69     *validLocale = *actualLocale = 0;
70 }
71 
72 // -------------------------------------
73 
~Format()74 Format::~Format()
75 {
76 }
77 
78 // -------------------------------------
79 // copy constructor
80 
Format(const Format & that)81 Format::Format(const Format &that)
82     : UObject(that)
83 {
84     *this = that;
85 }
86 
87 // -------------------------------------
88 // assignment operator
89 
90 Format&
operator =(const Format & that)91 Format::operator=(const Format& that)
92 {
93     if (this != &that) {
94         uprv_strcpy(validLocale, that.validLocale);
95         uprv_strcpy(actualLocale, that.actualLocale);
96     }
97     return *this;
98 }
99 
100 // -------------------------------------
101 // Formats the obj and append the result in the buffer, toAppendTo.
102 // This calls the actual implementation in the concrete subclasses.
103 
104 UnicodeString&
format(const Formattable & obj,UnicodeString & toAppendTo,UErrorCode & status) const105 Format::format(const Formattable& obj,
106                UnicodeString& toAppendTo,
107                UErrorCode& status) const
108 {
109     if (U_FAILURE(status)) return toAppendTo;
110 
111     FieldPosition pos(FieldPosition::DONT_CARE);
112 
113     return format(obj, toAppendTo, pos, status);
114 }
115 
116 // -------------------------------------
117 // Default implementation sets unsupported error; subclasses should
118 // override.
119 
120 UnicodeString&
format(const Formattable &,UnicodeString & toAppendTo,FieldPositionIterator *,UErrorCode & status) const121 Format::format(const Formattable& /* unused obj */,
122                UnicodeString& toAppendTo,
123                FieldPositionIterator* /* unused posIter */,
124                UErrorCode& status) const
125 {
126     if (!U_FAILURE(status)) {
127       status = U_UNSUPPORTED_ERROR;
128     }
129     return toAppendTo;
130 }
131 
132 // -------------------------------------
133 // Parses the source string and create the corresponding
134 // result object.  Checks the parse position for errors.
135 
136 void
parseObject(const UnicodeString & source,Formattable & result,UErrorCode & status) const137 Format::parseObject(const UnicodeString& source,
138                     Formattable& result,
139                     UErrorCode& status) const
140 {
141     if (U_FAILURE(status)) return;
142 
143     ParsePosition parsePosition(0);
144     parseObject(source, result, parsePosition);
145     if (parsePosition.getIndex() == 0) {
146         status = U_INVALID_FORMAT_ERROR;
147     }
148 }
149 
150 // -------------------------------------
151 
152 UBool
operator ==(const Format & that) const153 Format::operator==(const Format& that) const
154 {
155     // Subclasses: Call this method and then add more specific checks.
156     return typeid(*this) == typeid(that);
157 }
158 //---------------------------------------
159 
160 /**
161  * Simple function for initializing a UParseError from a UnicodeString.
162  *
163  * @param pattern The pattern to copy into the parseError
164  * @param pos The position in pattern where the error occured
165  * @param parseError The UParseError object to fill in
166  * @draft ICU 2.4
167  */
syntaxError(const UnicodeString & pattern,int32_t pos,UParseError & parseError)168 void Format::syntaxError(const UnicodeString& pattern,
169                          int32_t pos,
170                          UParseError& parseError) {
171     parseError.offset = pos;
172     parseError.line=0;  // we are not using line number
173 
174     // for pre-context
175     int32_t start = (pos < U_PARSE_CONTEXT_LEN)? 0 : (pos - (U_PARSE_CONTEXT_LEN-1
176                                                              /* subtract 1 so that we have room for null*/));
177     int32_t stop  = pos;
178     pattern.extract(start,stop-start,parseError.preContext,0);
179     //null terminate the buffer
180     parseError.preContext[stop-start] = 0;
181 
182     //for post-context
183     start = pos+1;
184     stop  = ((pos+U_PARSE_CONTEXT_LEN)<=pattern.length()) ? (pos+(U_PARSE_CONTEXT_LEN-1)) :
185         pattern.length();
186     pattern.extract(start,stop-start,parseError.postContext,0);
187     //null terminate the buffer
188     parseError.postContext[stop-start]= 0;
189 }
190 
191 Locale
getLocale(ULocDataLocaleType type,UErrorCode & status) const192 Format::getLocale(ULocDataLocaleType type, UErrorCode& status) const {
193     U_LOCALE_BASED(locBased, *this);
194     return locBased.getLocale(type, status);
195 }
196 
197 const char *
getLocaleID(ULocDataLocaleType type,UErrorCode & status) const198 Format::getLocaleID(ULocDataLocaleType type, UErrorCode& status) const {
199     U_LOCALE_BASED(locBased, *this);
200     return locBased.getLocaleID(type, status);
201 }
202 
203 void
setLocaleIDs(const char * valid,const char * actual)204 Format::setLocaleIDs(const char* valid, const char* actual) {
205     U_LOCALE_BASED(locBased, *this);
206     locBased.setLocaleIDs(valid, actual);
207 }
208 
209 U_NAMESPACE_END
210 
211 #endif /* #if !UCONFIG_NO_FORMATTING */
212 
213 //eof
214