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