1 /* 2 ************************************************************************ 3 * Copyright (c) 2007-2008, International Business Machines 4 * Corporation and others. All Rights Reserved. 5 ************************************************************************ 6 */ 7 #ifndef FLDSET_H_ 8 #define FLDSET_H_ 9 10 #include "unicode/utypes.h" 11 12 #if !UCONFIG_NO_FORMATTING 13 #include "unicode/calendar.h" 14 #include "unicode/ucal.h" 15 #include "unicode/udat.h" 16 #include "unicode/udbgutil.h" 17 #include "unicode/dbgutil.h" 18 #include "unicode/unistr.h" 19 20 #define U_FIELDS_SET_MAX 64 21 22 /** 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. 26 * 27 * @internal ICU 3.8 28 */ 29 class FieldsSet { 30 protected: 31 /** 32 * subclass interface - construct the FieldsSet to reference one of the standard 33 * enumerations. 34 * @param whichEnum which enumaration value goes with this set. Will be used to calculate string 35 * values and also enum size. 36 * @see UDebugEnumType 37 */ 38 FieldsSet(UDebugEnumType whichEnum); 39 40 /** 41 * subclass interface - construct the FieldsSet without using a standard enum type. 42 * @param fieldCount how many fields this object can hold. 43 */ 44 FieldsSet(int32_t fieldsCount); 45 46 public: 47 48 /** 49 * Compare two sets. In typical test usage, 'this' is the resul of 50 * a tested operation, and 'other' is the predefined expected value. 51 * 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. 56 */ 57 UnicodeString diffFrom(const FieldsSet& other, UErrorCode &status) const; 58 59 public: 60 /** 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. 65 */ 66 int32_t parseFrom(const UnicodeString& str, UErrorCode& status) { return parseFrom(str,NULL,status); } 67 68 /** 69 * Fill-in fields from a specified string, such as "NAME1=VALUE1,NAME2=VALUE2", etc. 70 * @param inheritFrom if a field's value is given as 0-length, such as NAME1 in "NAME1=,NAME2=VALUE2", 71 * the specified FieldsSet's value for NAME1 will be copied into this. 72 * @param str string to parse 73 * @param status status of parse 74 * @return the number of valid parsed fields on success, or a negative number on failure. 75 */ 76 int32_t parseFrom(const UnicodeString& str, const FieldsSet& inheritFrom, UErrorCode& status) { return parseFrom(str, &inheritFrom, status); } 77 78 /** 79 * Fill-in fields from a specified string, such as "NAME1=VALUE1,NAME2=VALUE2", etc. 80 * @param inheritFrom if a field's value is given as 0-length, such as NAME1 in "NAME1=,NAME2=VALUE2", 81 * the specified FieldsSet's value for NAME1 will be copied into this. 82 * @param str string to parse 83 * @param status status of parse 84 * @return the number of valid parsed fields on success, or a negative number on failure. 85 */ 86 int32_t parseFrom(const UnicodeString& str, const 87 FieldsSet* inheritFrom, UErrorCode& status); 88 89 protected: 90 /** 91 * Callback interface for subclass. 92 * This function is called when parsing a field name, such as "MONTH" in "MONTH=4". 93 * Base implementation is to lookup the enum value using udbg_* utilities, or else as an integer if 94 * enum is not available. 95 * 96 * If there is a special directive, the implementer can catch it here and return -1 after special processing completes. 97 * 98 * @param inheritFrom the set inheriting from - may be null. 99 * @param name the field name (key side) 100 * @param substr the string in question (value side) 101 * @param status error status - set to error for failure. 102 * @return field number, or negative if field should be skipped. 103 */ 104 virtual int32_t handleParseName(const FieldsSet* inheritFrom, const UnicodeString& name, const UnicodeString& substr, UErrorCode& status); 105 106 /** 107 * Callback interface for subclass. 108 * Base implementation is to call parseValueDefault(...) 109 * @param inheritFrom the set inheriting from - may be null. 110 * @param field which field is being parsed 111 * @param substr the string in question (value side) 112 * @param status error status - set to error for failure. 113 * @see parseValueDefault 114 */ 115 virtual void handleParseValue(const FieldsSet* inheritFrom, int32_t field, const UnicodeString& substr, UErrorCode& status); 116 117 /** 118 * the default implementation for handleParseValue. 119 * Base implementation is to parse a decimal integer value, or inherit from inheritFrom if the string is 0-length. 120 * Implementations of this function should call set(field,...) on successful parse. 121 * @see handleParseValue 122 */ 123 void parseValueDefault(const FieldsSet* inheritFrom, int32_t field, const UnicodeString& substr, UErrorCode& status); 124 125 126 /** 127 * convenience implementation for handleParseValue 128 * attempt to load a value from an enum value using udbg_enumByString() 129 * if fails, will call parseValueDefault() 130 * @see handleParseValue 131 */ 132 void parseValueEnum(UDebugEnumType type, const FieldsSet* inheritFrom, int32_t field, const UnicodeString& substr, UErrorCode& status); 133 134 private: 135 /** 136 * Not callable - construct a default FieldsSet 137 * @internal 138 */ 139 FieldsSet(); 140 141 /** 142 * construct the object. 143 * @internal 144 */ 145 void construct(UDebugEnumType whichEnum, int32_t fieldCount); 146 147 public: 148 /** 149 * destructor 150 */ 151 virtual ~FieldsSet(); 152 153 /** 154 * Mark all fields as unset 155 */ 156 void clear(); 157 158 /** 159 * Mark a specific field as unset 160 * @param field the field to unset 161 */ 162 void clear(int32_t field); 163 164 /** 165 * Set a specific field 166 * @param field the field to set (i.e. enum value) 167 * @param value the field's value 168 */ 169 void set(int32_t field, int32_t value); 170 171 UBool isSet(int32_t field) const; 172 173 /** 174 * Return the field's value 175 * @param field which field 176 * @return field's value, or -1 if unset. 177 */ 178 int32_t get(int32_t field) const; 179 180 /** 181 * Return true if both FieldsSet objects either are based on the same enum, or have the same number of fields. 182 */ 183 UBool isSameType(const FieldsSet& other) const; 184 185 /** 186 * @return the number of fields 187 */ 188 int32_t fieldCount() const; 189 190 protected: 191 int32_t fValue[U_FIELDS_SET_MAX]; 192 UBool fIsSet[U_FIELDS_SET_MAX]; 193 protected: 194 int32_t fFieldCount; 195 UDebugEnumType fEnum; 196 }; 197 198 /** 199 * A subclass of FieldsSet representing the fields in a Calendar 200 * @see Calendar 201 */ 202 class CalendarFieldsSet : public FieldsSet { 203 public: 204 CalendarFieldsSet(); 205 virtual ~CalendarFieldsSet(); 206 207 // void clear(UCalendarDateFields field) { clear((int32_t)field); } 208 // void set(UCalendarDateFields field, int32_t amount) { set ((int32_t)field, amount); } 209 210 // UBool isSet(UCalendarDateFields field) const { return isSet((int32_t)field); } 211 // int32_t get(UCalendarDateFields field) const { return get((int32_t)field); } 212 213 /** 214 * @param matches fillin to hold any fields different. Will have the calendar's value set on them. 215 * @return true if the calendar matches in these fields. 216 */ 217 UBool matches(Calendar *cal, CalendarFieldsSet &diffSet, 218 UErrorCode& status) const; 219 220 /** 221 * For each set field, set the same field on this Calendar. 222 * Doesn't clear the Calendar first. 223 * @param cal Calendar to modify 224 * @param status Contains any errors propagated by the Calendar. 225 */ 226 void setOnCalendar(Calendar *cal, UErrorCode& status) const; 227 228 protected: 229 /** 230 * subclass override 231 */ 232 void handleParseValue(const FieldsSet* inheritFrom, int32_t field, const UnicodeString& substr, UErrorCode& status); 233 }; 234 235 /** 236 * This class simply implements a set of date and time styles 237 * such as DATE=SHORT or TIME=SHORT,DATE=LONG, such as would be passed 238 * to DateFormat::createInstance() 239 * @see DateFormat 240 */ 241 class DateTimeStyleSet : public FieldsSet { 242 public: 243 DateTimeStyleSet(); 244 virtual ~DateTimeStyleSet(); 245 246 /** 247 * @return the date style, or UDAT_NONE if not set 248 */ 249 UDateFormatStyle getDateStyle() const; 250 251 /** 252 * @return the time style, or UDAT_NONE if not set 253 */ 254 UDateFormatStyle getTimeStyle() const; 255 protected: 256 void handleParseValue(const FieldsSet* inheritFrom, int32_t field, const UnicodeString& substr, UErrorCode& status); 257 int32_t handleParseName(const FieldsSet* inheritFrom, const UnicodeString& name, const UnicodeString& substr, UErrorCode& status); 258 }; 259 260 261 #endif /*!UCONFIG_NO_FORMAT*/ 262 #endif /*FLDSET_H_*/ 263