2 ************************************************************************
3 * Copyright (c) 2007-2010, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ************************************************************************
10 #include "unicode/utypes.h"
12 #if !UCONFIG_NO_FORMATTING
13 #include "unicode/calendar.h"
14 #include "unicode/ucal.h"
15 #include "unicode/udat.h"
18 #include "unicode/unistr.h"
20 #define U_FIELDS_SET_MAX 64
23 * This class represents a collection of integer values (fields), each designated by
24 * one of a particular set of enum values. Each integer value (int32_t) is optional and
25 * may or may not be set.
32 * subclass interface - construct the FieldsSet to reference one of the standard
34 * @param whichEnum which enumaration value goes with this set. Will be used to calculate string
35 * values and also enum size.
38 FieldsSet(UDebugEnumType whichEnum
);
41 * subclass interface - construct the FieldsSet without using a standard enum type.
42 * @param fieldCount how many fields this object can hold.
44 FieldsSet(int32_t fieldsCount
);
49 * Compare two sets. In typical test usage, 'this' is the resul of
50 * a tested operation, and 'other' is the predefined expected value.
52 * @param other the set to compare against.
53 * @param status will return U_ILLEGAL_ARGUMENT_ERROR if sets are not the same size
54 * @return a formatted string listing which fields are set in
55 * this, with the comparison made agaainst those fields in other.
57 U_NAMESPACE_QUALIFIER UnicodeString
diffFrom(const FieldsSet
& other
, UErrorCode
&status
) const;
61 * Fill-in fields from a specified string, such as "NAME1=VALUE1,NAME2=VALUE2", etc.
62 * @param str string to parse
63 * @param status status of parse
64 * @return the number of valid parsed fields on success, or a negative number on failure.
66 int32_t parseFrom(const U_NAMESPACE_QUALIFIER UnicodeString
& str
, UErrorCode
& status
) {
67 return parseFrom(str
,NULL
,status
);
71 * Fill-in fields from a specified string, such as "NAME1=VALUE1,NAME2=VALUE2", etc.
72 * @param inheritFrom if a field's value is given as 0-length, such as NAME1 in "NAME1=,NAME2=VALUE2",
73 * the specified FieldsSet's value for NAME1 will be copied into this.
74 * @param str string to parse
75 * @param status status of parse
76 * @return the number of valid parsed fields on success, or a negative number on failure.
78 int32_t parseFrom(const U_NAMESPACE_QUALIFIER UnicodeString
& str
,
79 const FieldsSet
& inheritFrom
,
81 return parseFrom(str
, &inheritFrom
, status
);
85 * Fill-in fields from a specified string, such as "NAME1=VALUE1,NAME2=VALUE2", etc.
86 * @param inheritFrom if a field's value is given as 0-length, such as NAME1 in "NAME1=,NAME2=VALUE2",
87 * the specified FieldsSet's value for NAME1 will be copied into this.
88 * @param str string to parse
89 * @param status status of parse
90 * @return the number of valid parsed fields on success, or a negative number on failure.
92 int32_t parseFrom(const U_NAMESPACE_QUALIFIER UnicodeString
& str
,
93 const FieldsSet
* inheritFrom
,
98 * Callback interface for subclass.
99 * This function is called when parsing a field name, such as "MONTH" in "MONTH=4".
100 * Base implementation is to lookup the enum value using udbg_* utilities, or else as an integer if
101 * enum is not available.
103 * If there is a special directive, the implementer can catch it here and return -1 after special processing completes.
105 * @param inheritFrom the set inheriting from - may be null.
106 * @param name the field name (key side)
107 * @param substr the string in question (value side)
108 * @param status error status - set to error for failure.
109 * @return field number, or negative if field should be skipped.
111 virtual int32_t handleParseName(const FieldsSet
* inheritFrom
,
112 const U_NAMESPACE_QUALIFIER UnicodeString
& name
,
113 const U_NAMESPACE_QUALIFIER UnicodeString
& substr
,
117 * Callback interface for subclass.
118 * Base implementation is to call parseValueDefault(...)
119 * @param inheritFrom the set inheriting from - may be null.
120 * @param field which field is being parsed
121 * @param substr the string in question (value side)
122 * @param status error status - set to error for failure.
123 * @see parseValueDefault
125 virtual void handleParseValue(const FieldsSet
* inheritFrom
,
127 const U_NAMESPACE_QUALIFIER UnicodeString
& substr
,
131 * the default implementation for handleParseValue.
132 * Base implementation is to parse a decimal integer value, or inherit from inheritFrom if the string is 0-length.
133 * Implementations of this function should call set(field,...) on successful parse.
134 * @see handleParseValue
136 void parseValueDefault(const FieldsSet
* inheritFrom
,
138 const U_NAMESPACE_QUALIFIER UnicodeString
& substr
,
143 * convenience implementation for handleParseValue
144 * attempt to load a value from an enum value using udbg_enumByString()
145 * if fails, will call parseValueDefault()
146 * @see handleParseValue
148 void parseValueEnum(UDebugEnumType type
,
149 const FieldsSet
* inheritFrom
,
151 const U_NAMESPACE_QUALIFIER UnicodeString
& substr
,
156 * Not callable - construct a default FieldsSet
162 * construct the object.
165 void construct(UDebugEnumType whichEnum
, int32_t fieldCount
);
171 virtual ~FieldsSet();
174 * Mark all fields as unset
179 * Mark a specific field as unset
180 * @param field the field to unset
182 void clear(int32_t field
);
185 * Set a specific field
186 * @param field the field to set (i.e. enum value)
187 * @param value the field's value
189 void set(int32_t field
, int32_t value
);
191 UBool
isSet(int32_t field
) const;
194 * Return the field's value
195 * @param field which field
196 * @return field's value, or -1 if unset.
198 int32_t get(int32_t field
) const;
201 * Return true if both FieldsSet objects either are based on the same enum, or have the same number of fields.
203 UBool
isSameType(const FieldsSet
& other
) const;
206 * @return the number of fields
208 int32_t fieldCount() const;
211 int32_t fValue
[U_FIELDS_SET_MAX
];
212 UBool fIsSet
[U_FIELDS_SET_MAX
];
215 UDebugEnumType fEnum
;
219 * A subclass of FieldsSet representing the fields in a Calendar
222 class CalendarFieldsSet
: public FieldsSet
{
225 virtual ~CalendarFieldsSet();
227 // void clear(UCalendarDateFields field) { clear((int32_t)field); }
228 // void set(UCalendarDateFields field, int32_t amount) { set ((int32_t)field, amount); }
230 // UBool isSet(UCalendarDateFields field) const { return isSet((int32_t)field); }
231 // int32_t get(UCalendarDateFields field) const { return get((int32_t)field); }
234 * @param matches fillin to hold any fields different. Will have the calendar's value set on them.
235 * @return true if the calendar matches in these fields.
237 UBool
matches(U_NAMESPACE_QUALIFIER Calendar
*cal
,
238 CalendarFieldsSet
&diffSet
,
239 UErrorCode
& status
) const;
242 * For each set field, set the same field on this Calendar.
243 * Doesn't clear the Calendar first.
244 * @param cal Calendar to modify
245 * @param status Contains any errors propagated by the Calendar.
247 void setOnCalendar(U_NAMESPACE_QUALIFIER Calendar
*cal
, UErrorCode
& status
) const;
253 void handleParseValue(const FieldsSet
* inheritFrom
,
255 const U_NAMESPACE_QUALIFIER UnicodeString
& substr
,
260 * This class simply implements a set of date and time styles
261 * such as DATE=SHORT or TIME=SHORT,DATE=LONG, such as would be passed
262 * to DateFormat::createInstance()
265 class DateTimeStyleSet
: public FieldsSet
{
268 virtual ~DateTimeStyleSet();
271 * @return the date style, or UDAT_NONE if not set
273 UDateFormatStyle
getDateStyle() const;
276 * @return the time style, or UDAT_NONE if not set
278 UDateFormatStyle
getTimeStyle() const;
280 void handleParseValue(const FieldsSet
* inheritFrom
,
282 const U_NAMESPACE_QUALIFIER UnicodeString
& substr
,
284 int32_t handleParseName(const FieldsSet
* inheritFrom
,
285 const U_NAMESPACE_QUALIFIER UnicodeString
& name
,
286 const U_NAMESPACE_QUALIFIER UnicodeString
& substr
,
291 #endif /*!UCONFIG_NO_FORMAT*/