1/*
2*******************************************************************************
3* Copyright (C) 2007-2012, 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_K             ((UChar)0x004B)
48#define CAP_L             ((UChar)0x004C)
49#define CAP_M             ((UChar)0x004D)
50#define CAP_O             ((UChar)0x004F)
51#define CAP_Q             ((UChar)0x0051)
52#define CAP_S             ((UChar)0x0053)
53#define CAP_T             ((UChar)0x0054)
54#define CAP_U             ((UChar)0x0055)
55#define CAP_V             ((UChar)0x0056)
56#define CAP_W             ((UChar)0x0057)
57#define CAP_Y             ((UChar)0x0059)
58#define CAP_Z             ((UChar)0x005A)
59#define LOWLINE           ((UChar)0x005F)
60#define LOW_A             ((UChar)0x0061)
61#define LOW_C             ((UChar)0x0063)
62#define LOW_D             ((UChar)0x0064)
63#define LOW_E             ((UChar)0x0065)
64#define LOW_F             ((UChar)0x0066)
65#define LOW_G             ((UChar)0x0067)
66#define LOW_H             ((UChar)0x0068)
67#define LOW_I             ((UChar)0x0069)
68#define LOW_J             ((UChar)0x006A)
69#define LOW_K             ((UChar)0x006B)
70#define LOW_L             ((UChar)0x006C)
71#define LOW_M             ((UChar)0x006D)
72#define LOW_N             ((UChar)0x006E)
73#define LOW_O             ((UChar)0x006F)
74#define LOW_P             ((UChar)0x0070)
75#define LOW_Q             ((UChar)0x0071)
76#define LOW_R             ((UChar)0x0072)
77#define LOW_S             ((UChar)0x0073)
78#define LOW_T             ((UChar)0x0074)
79#define LOW_U             ((UChar)0x0075)
80#define LOW_V             ((UChar)0x0076)
81#define LOW_W             ((UChar)0x0077)
82#define LOW_Y             ((UChar)0x0079)
83#define LOW_Z             ((UChar)0x007A)
84#define DT_SHORT          -0x102
85#define DT_LONG           -0x103
86#define DT_NUMERIC         0x100
87#define DT_NARROW         -0x101
88#define DT_DELTA           0x10
89
90U_NAMESPACE_BEGIN
91
92const int32_t UDATPG_FRACTIONAL_MASK = 1<<UDATPG_FRACTIONAL_SECOND_FIELD;
93const int32_t UDATPG_SECOND_AND_FRACTIONAL_MASK = (1<<UDATPG_SECOND_FIELD) | (1<<UDATPG_FRACTIONAL_SECOND_FIELD);
94
95typedef enum dtStrEnum {
96    DT_BASESKELETON,
97    DT_SKELETON,
98    DT_PATTERN
99}dtStrEnum;
100
101typedef struct dtTypeElem {
102    UChar                  patternChar;
103    UDateTimePatternField  field;
104    int16_t                type;
105    int16_t                minLen;
106    int16_t                weight;
107}dtTypeElem;
108
109class PtnSkeleton : public UMemory {
110public:
111    int32_t type[UDATPG_FIELD_COUNT];
112    UnicodeString original[UDATPG_FIELD_COUNT];
113    UnicodeString baseOriginal[UDATPG_FIELD_COUNT];
114
115    PtnSkeleton();
116    PtnSkeleton(const PtnSkeleton& other);
117    UBool equals(const PtnSkeleton& other);
118    UnicodeString getSkeleton();
119    UnicodeString getBaseSkeleton();
120    virtual ~PtnSkeleton();
121};
122
123
124class PtnElem : public UMemory {
125public:
126    UnicodeString basePattern;
127    PtnSkeleton   *skeleton;
128    UnicodeString pattern;
129    UBool         skeletonWasSpecified; // if specified in availableFormats, not derived
130    PtnElem       *next;
131
132    PtnElem(const UnicodeString &basePattern, const UnicodeString &pattern);
133    virtual ~PtnElem();
134
135};
136
137class FormatParser : public UMemory {
138public:
139    UnicodeString items[MAX_DT_TOKEN];
140    int32_t  itemNumber;
141
142    FormatParser();
143    virtual ~FormatParser();
144    void set(const UnicodeString& patternString);
145    UBool isQuoteLiteral(const UnicodeString& s) const;
146    void getQuoteLiteral(UnicodeString& quote, int32_t *itemIndex);
147    int32_t getCanonicalIndex(const UnicodeString& s) { return getCanonicalIndex(s, TRUE); }
148    int32_t getCanonicalIndex(const UnicodeString& s, UBool strict);
149    UBool isPatternSeparator(UnicodeString& field);
150    void setFilter(UErrorCode &status);
151
152private:
153   typedef enum TokenStatus {
154       START,
155       ADD_TOKEN,
156       SYNTAX_ERROR,
157       DONE
158   } ToeknStatus;
159
160   TokenStatus status;
161   virtual TokenStatus setTokens(const UnicodeString& pattern, int32_t startPos, int32_t *len);
162};
163
164class DistanceInfo : public UMemory {
165public:
166    int32_t missingFieldMask;
167    int32_t extraFieldMask;
168
169    DistanceInfo() {}
170    virtual ~DistanceInfo();
171    void clear() { missingFieldMask = extraFieldMask = 0; }
172    void setTo(DistanceInfo& other);
173    void addMissing(int32_t field) { missingFieldMask |= (1<<field); }
174    void addExtra(int32_t field) { extraFieldMask |= (1<<field); }
175};
176
177class DateTimeMatcher: public UMemory {
178public:
179    PtnSkeleton skeleton;
180
181    void getBasePattern(UnicodeString &basePattern);
182    UnicodeString getPattern();
183    void set(const UnicodeString& pattern, FormatParser* fp);
184    void set(const UnicodeString& pattern, FormatParser* fp, PtnSkeleton& skeleton);
185    void copyFrom(const PtnSkeleton& skeleton);
186    void copyFrom();
187    PtnSkeleton* getSkeletonPtr();
188    UBool equals(const DateTimeMatcher* other) const;
189    int32_t getDistance(const DateTimeMatcher& other, int32_t includeMask, DistanceInfo& distanceInfo);
190    DateTimeMatcher();
191    DateTimeMatcher(const DateTimeMatcher& other);
192    virtual ~DateTimeMatcher();
193    int32_t getFieldMask();
194};
195
196class PatternMap : public UMemory {
197public:
198    PtnElem *boot[MAX_PATTERN_ENTRIES];
199    PatternMap();
200    virtual  ~PatternMap();
201    void  add(const UnicodeString& basePattern, const PtnSkeleton& skeleton, const UnicodeString& value, UBool skeletonWasSpecified, UErrorCode& status);
202    const UnicodeString* getPatternFromBasePattern(UnicodeString& basePattern, UBool& skeletonWasSpecified);
203    const UnicodeString* getPatternFromSkeleton(PtnSkeleton& skeleton, const PtnSkeleton** specifiedSkeletonPtr = 0);
204    void copyFrom(const PatternMap& other, UErrorCode& status);
205    PtnElem* getHeader(UChar baseChar);
206    UBool equals(const PatternMap& other);
207private:
208    UBool isDupAllowed;
209    PtnElem*  getDuplicateElem(const UnicodeString &basePattern, const PtnSkeleton& skeleton, PtnElem *baseElem);
210}; // end  PatternMap
211
212class PatternMapIterator : public UMemory {
213public:
214    PatternMapIterator();
215    virtual ~PatternMapIterator();
216    void set(PatternMap& patternMap);
217    PtnSkeleton* getSkeleton();
218    UBool hasNext();
219    DateTimeMatcher& next();
220private:
221    int32_t bootIndex;
222    PtnElem *nodePtr;
223    DateTimeMatcher *matcher;
224    PatternMap *patternMap;
225};
226
227class DTSkeletonEnumeration : public StringEnumeration {
228public:
229    DTSkeletonEnumeration(PatternMap &patternMap, dtStrEnum type, UErrorCode& status);
230    virtual ~DTSkeletonEnumeration();
231    static UClassID U_EXPORT2 getStaticClassID(void);
232    virtual UClassID getDynamicClassID(void) const;
233    virtual const UnicodeString* snext(UErrorCode& status);
234    virtual void reset(UErrorCode& status);
235    virtual int32_t count(UErrorCode& status) const;
236private:
237    int32_t pos;
238    UBool isCanonicalItem(const UnicodeString& item);
239    UVector *fSkeletons;
240};
241
242class DTRedundantEnumeration : public StringEnumeration {
243public:
244    DTRedundantEnumeration();
245    virtual ~DTRedundantEnumeration();
246    static UClassID U_EXPORT2 getStaticClassID(void);
247    virtual UClassID getDynamicClassID(void) const;
248    virtual const UnicodeString* snext(UErrorCode& status);
249    virtual void reset(UErrorCode& status);
250    virtual int32_t count(UErrorCode& status) const;
251    void add(const UnicodeString &pattern, UErrorCode& status);
252private:
253    int32_t pos;
254    UBool isCanonicalItem(const UnicodeString& item);
255    UVector *fPatterns;
256};
257
258U_NAMESPACE_END
259
260#endif
261