]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/format.cpp
ICU-531.31.tar.gz
[apple/icu.git] / icuSources / i18n / format.cpp
CommitLineData
b75a7d8f
A
1/*
2*******************************************************************************
51004dcb 3* Copyright (C) 1997-2012, International Business Machines Corporation and *
b75a7d8f
A
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// *****************************************************************************
729e4ab9 21
51004dcb 22#include "utypeinfo.h" // for 'typeid' to work
729e4ab9 23
b75a7d8f
A
24#include "unicode/utypes.h"
25
4388f060
A
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
b75a7d8f
A
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
37U_CAPI int32_t U_EXPORT2
38uprv_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"
374ca955
A
48#include "unicode/ures.h"
49#include "cstring.h"
50#include "locbased.h"
b75a7d8f
A
51
52// *****************************************************************************
53// class Format
54// *****************************************************************************
55
56U_NAMESPACE_BEGIN
57
374ca955
A
58UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FieldPosition)
59
60FieldPosition::~FieldPosition() {}
61
62FieldPosition *
63FieldPosition::clone() const {
64 return new FieldPosition(*this);
65}
b75a7d8f
A
66
67// -------------------------------------
68// default constructor
69
70Format::Format()
71 : UObject()
72{
374ca955 73 *validLocale = *actualLocale = 0;
b75a7d8f
A
74}
75
76// -------------------------------------
77
78Format::~Format()
79{
80}
81
82// -------------------------------------
83// copy constructor
84
85Format::Format(const Format &that)
86 : UObject(that)
87{
374ca955 88 *this = that;
b75a7d8f
A
89}
90
91// -------------------------------------
92// assignment operator
93
94Format&
374ca955 95Format::operator=(const Format& that)
b75a7d8f 96{
729e4ab9
A
97 if (this != &that) {
98 uprv_strcpy(validLocale, that.validLocale);
99 uprv_strcpy(actualLocale, that.actualLocale);
100 }
b75a7d8f
A
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.
729e4ab9 107
b75a7d8f 108UnicodeString&
729e4ab9
A
109Format::format(const Formattable& obj,
110 UnicodeString& toAppendTo,
b75a7d8f
A
111 UErrorCode& status) const
112{
113 if (U_FAILURE(status)) return toAppendTo;
114
374ca955 115 FieldPosition pos(FieldPosition::DONT_CARE);
b75a7d8f
A
116
117 return format(obj, toAppendTo, pos, status);
118}
729e4ab9 119
b75a7d8f 120// -------------------------------------
729e4ab9
A
121// Default implementation sets unsupported error; subclasses should
122// override.
123
124UnicodeString&
125Format::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
b75a7d8f 138// result object. Checks the parse position for errors.
729e4ab9 139
b75a7d8f 140void
729e4ab9
A
141Format::parseObject(const UnicodeString& source,
142 Formattable& result,
b75a7d8f
A
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}
729e4ab9 153
b75a7d8f
A
154// -------------------------------------
155
156UBool
374ca955 157Format::operator==(const Format& that) const
b75a7d8f 158{
374ca955 159 // Subclasses: Call this method and then add more specific checks.
729e4ab9 160 return typeid(*this) == typeid(that);
b75a7d8f
A
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 */
172void 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
729e4ab9 177
b75a7d8f 178 // for pre-context
46f4442e 179 int32_t start = (pos < U_PARSE_CONTEXT_LEN)? 0 : (pos - (U_PARSE_CONTEXT_LEN-1
b75a7d8f
A
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;
729e4ab9 185
b75a7d8f
A
186 //for post-context
187 start = pos+1;
729e4ab9 188 stop = ((pos+U_PARSE_CONTEXT_LEN)<=pattern.length()) ? (pos+(U_PARSE_CONTEXT_LEN-1)) :
b75a7d8f
A
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
729e4ab9 195Locale
374ca955
A
196Format::getLocale(ULocDataLocaleType type, UErrorCode& status) const {
197 U_LOCALE_BASED(locBased, *this);
198 return locBased.getLocale(type, status);
199}
200
201const char *
202Format::getLocaleID(ULocDataLocaleType type, UErrorCode& status) const {
203 U_LOCALE_BASED(locBased, *this);
204 return locBased.getLocaleID(type, status);
205}
206
207void
208Format::setLocaleIDs(const char* valid, const char* actual) {
209 U_LOCALE_BASED(locBased, *this);
210 locBased.setLocaleIDs(valid, actual);
211}
212
b75a7d8f
A
213U_NAMESPACE_END
214
215#endif /* #if !UCONFIG_NO_FORMATTING */
216
217//eof