1 /* 2 ******************************************************************************* 3 * Copyright (C) 2007, International Business Machines Corporation and 4 * others. All Rights Reserved. * 5 ******************************************************************************* 6 * 7 * File DTPTNGEN.H 8 * 9 ******************************************************************************* 10 */ 11 12 #include "uvector.h" 13 14 #ifndef __DTPTNGEN_IMPL_H__ 15 #define __DTPTNGEN_IMPL_H__ 16 17 // TODO(claireho): Split off Builder class. 18 // TODO(claireho): If splitting off Builder class: As subclass or independent? 19 20 #define MAX_PATTERN_ENTRIES 52 21 #define MAX_CLDR_FIELD_LEN 60 22 #define MAX_DT_TOKEN 50 23 #define MAX_RESOURCE_FIELD 12 24 #define MAX_AVAILABLE_FORMATS 12 25 #define NONE 0 26 #define EXTRA_FIELD 0x10000 27 #define MISSING_FIELD 0x1000 28 #define MAX_STRING_ENUMERATION 200 29 #define SINGLE_QUOTE ((UChar)0x0027) 30 #define FORWARDSLASH ((UChar)0x002F) 31 #define BACKSLASH ((UChar)0x005C) 32 #define SPACE ((UChar)0x0020) 33 #define QUOTATION_MARK ((UChar)0x0022) 34 #define ASTERISK ((UChar)0x002A) 35 #define PLUSSITN ((UChar)0x002B) 36 #define COMMA ((UChar)0x002C) 37 #define HYPHEN ((UChar)0x002D) 38 #define DOT ((UChar)0x002E) 39 #define COLON ((UChar)0x003A) 40 #define CAP_A ((UChar)0x0041) 41 #define CAP_C ((UChar)0x0043) 42 #define CAP_D ((UChar)0x0044) 43 #define CAP_E ((UChar)0x0045) 44 #define CAP_F ((UChar)0x0046) 45 #define CAP_G ((UChar)0x0047) 46 #define CAP_H ((UChar)0x0048) 47 #define CAP_L ((UChar)0x004C) 48 #define CAP_M ((UChar)0x004D) 49 #define CAP_O ((UChar)0x004F) 50 #define CAP_Q ((UChar)0x0051) 51 #define CAP_S ((UChar)0x0053) 52 #define CAP_T ((UChar)0x0054) 53 #define CAP_V ((UChar)0x0056) 54 #define CAP_W ((UChar)0x0057) 55 #define CAP_Y ((UChar)0x0059) 56 #define CAP_Z ((UChar)0x005A) 57 #define LOWLINE ((UChar)0x005F) 58 #define LOW_A ((UChar)0x0061) 59 #define LOW_C ((UChar)0x0063) 60 #define LOW_D ((UChar)0x0064) 61 #define LOW_E ((UChar)0x0065) 62 #define LOW_F ((UChar)0x0066) 63 #define LOW_G ((UChar)0x0067) 64 #define LOW_H ((UChar)0x0068) 65 #define LOW_I ((UChar)0x0069) 66 #define LOW_K ((UChar)0x006B) 67 #define LOW_L ((UChar)0x006C) 68 #define LOW_M ((UChar)0x006D) 69 #define LOW_N ((UChar)0x006E) 70 #define LOW_O ((UChar)0x006F) 71 #define LOW_P ((UChar)0x0070) 72 #define LOW_Q ((UChar)0x0071) 73 #define LOW_R ((UChar)0x0072) 74 #define LOW_S ((UChar)0x0073) 75 #define LOW_T ((UChar)0x0074) 76 #define LOW_U ((UChar)0x0075) 77 #define LOW_V ((UChar)0x0076) 78 #define LOW_W ((UChar)0x0077) 79 #define LOW_Y ((UChar)0x0079) 80 #define LOW_Z ((UChar)0x007A) 81 #define DT_SHORT -0x101 82 #define DT_LONG -0x102 83 #define DT_NUMERIC 0x100 84 #define DT_NARROW -0x100 85 #define DT_DELTA 0x10 86 87 U_NAMESPACE_BEGIN 88 89 const int32_t UDATPG_FRACTIONAL_MASK = 1<<UDATPG_FRACTIONAL_SECOND_FIELD; 90 const int32_t UDATPG_SECOND_AND_FRACTIONAL_MASK = (1<<UDATPG_SECOND_FIELD) | (1<<UDATPG_FRACTIONAL_SECOND_FIELD); 91 92 typedef enum dtStrEnum { 93 DT_BASESKELETON, 94 DT_SKELETON, 95 DT_PATTERN 96 }dtStrEnum; 97 98 typedef struct dtTypeElem { 99 UChar patternChar; 100 UDateTimePatternField field; 101 int32_t type; 102 int32_t minLen; 103 int32_t weight; 104 }dtTypeElem; 105 106 class PtnSkeleton : public UMemory { 107 public: 108 int32_t type[UDATPG_FIELD_COUNT]; 109 UnicodeString original[UDATPG_FIELD_COUNT]; 110 UnicodeString baseOriginal[UDATPG_FIELD_COUNT]; 111 112 PtnSkeleton(); 113 PtnSkeleton(const PtnSkeleton& other); 114 UBool equals(const PtnSkeleton& other); 115 UnicodeString getSkeleton(); 116 UnicodeString getBaseSkeleton(); 117 virtual ~PtnSkeleton(); 118 }; 119 120 121 class PtnElem : public UMemory { 122 public: 123 UnicodeString basePattern; 124 PtnSkeleton *skeleton; 125 UnicodeString pattern; 126 PtnElem *next; 127 128 PtnElem(const UnicodeString &basePattern, const UnicodeString &pattern); 129 virtual ~PtnElem(); 130 131 }; 132 133 class FormatParser : public UMemory { 134 public: 135 UnicodeString items[MAX_DT_TOKEN]; 136 int32_t itemNumber; 137 138 FormatParser(); 139 virtual ~FormatParser(); 140 void set(const UnicodeString& patternString); 141 UBool isQuoteLiteral(const UnicodeString& s) const; 142 void getQuoteLiteral(UnicodeString& quote, int32_t *itemIndex); 143 int32_t getCanonicalIndex(const UnicodeString& s); 144 UBool isPatternSeparator(UnicodeString& field); 145 void setFilter(UErrorCode &status); 146 147 private: 148 typedef enum TokenStatus { 149 START, 150 ADD_TOKEN, 151 SYNTAX_ERROR, 152 DONE 153 } ToeknStatus; 154 155 TokenStatus status; 156 virtual TokenStatus setTokens(const UnicodeString& pattern, int32_t startPos, int32_t *len); 157 }; 158 159 class DistanceInfo : public UMemory { 160 public: 161 int32_t missingFieldMask; 162 int32_t extraFieldMask; 163 DistanceInfo()164 DistanceInfo() {}; ~DistanceInfo()165 virtual ~DistanceInfo() {}; clear()166 void clear() { missingFieldMask = extraFieldMask = 0; }; 167 void setTo(DistanceInfo& other); addMissing(int32_t field)168 void addMissing(int32_t field) { missingFieldMask |= (1<<field); }; addExtra(int32_t field)169 void addExtra(int32_t field) { extraFieldMask |= (1<<field); }; 170 }; 171 172 class DateTimeMatcher: public UMemory { 173 public: 174 PtnSkeleton skeleton; 175 176 void getBasePattern(UnicodeString &basePattern); 177 UnicodeString getPattern(); 178 void set(const UnicodeString& pattern, FormatParser* fp); 179 void set(const UnicodeString& pattern, FormatParser* fp, PtnSkeleton& skeleton); 180 void copyFrom(const PtnSkeleton& skeleton); 181 void copyFrom(); 182 PtnSkeleton* getSkeletonPtr(); 183 UBool equals(const DateTimeMatcher* other) const; 184 int32_t getDistance(const DateTimeMatcher& other, int32_t includeMask, DistanceInfo& distanceInfo); 185 DateTimeMatcher(); 186 DateTimeMatcher(const DateTimeMatcher& other); ~DateTimeMatcher()187 virtual ~DateTimeMatcher() {}; 188 int32_t getFieldMask(); 189 }; 190 191 class PatternMap : public UMemory { 192 public: 193 PtnElem *boot[MAX_PATTERN_ENTRIES]; 194 PatternMap(); 195 virtual ~PatternMap(); 196 void add(const UnicodeString& basePattern, const PtnSkeleton& skeleton, const UnicodeString& value, UErrorCode& status); 197 UErrorCode status; 198 const UnicodeString* getPatternFromBasePattern(UnicodeString& basePattern); 199 const UnicodeString* getPatternFromSkeleton(PtnSkeleton& skeleton); 200 void copyFrom(const PatternMap& other, UErrorCode& status); 201 PtnElem* getHeader(UChar baseChar); 202 UBool equals(const PatternMap& other); 203 private: 204 UBool isDupAllowed; 205 PtnElem* getDuplicateElem(const UnicodeString &basePattern, const PtnSkeleton& skeleton, PtnElem *baseElem); 206 }; // end PatternMap 207 208 class PatternMapIterator : public UMemory { 209 public: 210 PatternMapIterator(); 211 virtual ~PatternMapIterator(); 212 void set(PatternMap& patternMap); 213 PtnSkeleton* getSkeleton(); 214 UBool hasNext(); 215 DateTimeMatcher& next(); 216 private: 217 int32_t bootIndex; 218 PtnElem *nodePtr; 219 DateTimeMatcher *matcher; 220 PatternMap *patternMap; 221 }; 222 223 class U_I18N_API DTSkeletonEnumeration : public StringEnumeration { 224 public: 225 DTSkeletonEnumeration(PatternMap &patternMap, dtStrEnum type, UErrorCode& status); 226 virtual ~DTSkeletonEnumeration(); 227 static UClassID U_EXPORT2 getStaticClassID(void); 228 virtual UClassID getDynamicClassID(void) const; 229 virtual const UnicodeString* snext(UErrorCode& status); 230 virtual void reset(UErrorCode& status); 231 virtual int32_t count(UErrorCode& status) const; 232 private: 233 int32_t pos; 234 UBool isCanonicalItem(const UnicodeString& item); 235 UVector *fSkeletons; 236 }; 237 238 class U_I18N_API DTRedundantEnumeration : public StringEnumeration { 239 public: 240 DTRedundantEnumeration(); 241 virtual ~DTRedundantEnumeration(); 242 static UClassID U_EXPORT2 getStaticClassID(void); 243 virtual UClassID getDynamicClassID(void) const; 244 virtual const UnicodeString* snext(UErrorCode& status); 245 virtual void reset(UErrorCode& status); 246 virtual int32_t count(UErrorCode& status) const; 247 void add(const UnicodeString &pattern, UErrorCode& status); 248 private: 249 int32_t pos; 250 UBool isCanonicalItem(const UnicodeString& item); 251 UVector *fPatterns; 252 }; 253 254 U_NAMESPACE_END 255 256 #endif 257