]>
Commit | Line | Data |
---|---|---|
46f4442e A |
1 | // |
2 | // smpdtfst.h | |
3 | // | |
4 | // Copyright (C) 2009, International Business Machines Corporation and others. | |
5 | // All Rights Reserved. | |
6 | // | |
7 | // This file contains the class SimpleDateFormatStaticSets | |
8 | // | |
9 | // SimpleDateFormatStaticSets holds the UnicodeSets that are needed for lenient | |
10 | // parsing of literal characters in date/time strings. | |
11 | // | |
12 | ||
13 | #include "unicode/utypes.h" | |
14 | ||
15 | #if !UCONFIG_NO_FORMATTING | |
16 | ||
17 | #include "unicode/uniset.h" | |
18 | #include "unicode/udat.h" | |
19 | #include "cmemory.h" | |
20 | #include "ucln_in.h" | |
21 | #include "umutex.h" | |
22 | ||
23 | ||
24 | #include "smpdtfst.h" | |
25 | ||
26 | U_NAMESPACE_BEGIN | |
27 | ||
28 | SimpleDateFormatStaticSets *SimpleDateFormatStaticSets::gStaticSets = NULL; | |
29 | ||
30 | SimpleDateFormatStaticSets::SimpleDateFormatStaticSets(UErrorCode *status) | |
31 | : fDateIgnorables(NULL), | |
32 | fTimeIgnorables(NULL), | |
33 | fOtherIgnorables(NULL) | |
34 | { | |
35 | fDateIgnorables = new UnicodeSet("[-,./[:whitespace:]]", *status); | |
36 | fTimeIgnorables = new UnicodeSet("[-.:[:whitespace:]]", *status); | |
37 | fOtherIgnorables = new UnicodeSet("[:whitespace:]", *status); | |
38 | ||
39 | // Check for null pointers | |
40 | if (fDateIgnorables == NULL || fTimeIgnorables == NULL || fOtherIgnorables == NULL) { | |
41 | goto ExitConstrDeleteAll; | |
42 | } | |
43 | ||
44 | // Freeze all the sets | |
45 | fDateIgnorables->freeze(); | |
46 | fTimeIgnorables->freeze(); | |
47 | fOtherIgnorables->freeze(); | |
48 | ||
49 | return; // If we reached this point, everything is fine so just exit | |
50 | ||
51 | ExitConstrDeleteAll: // Remove all sets and return error | |
52 | delete fDateIgnorables; fDateIgnorables = NULL; | |
53 | delete fTimeIgnorables; fTimeIgnorables = NULL; | |
54 | delete fOtherIgnorables; fOtherIgnorables = NULL; | |
55 | ||
56 | *status = U_MEMORY_ALLOCATION_ERROR; | |
57 | } | |
58 | ||
59 | ||
60 | SimpleDateFormatStaticSets::~SimpleDateFormatStaticSets() { | |
61 | delete fDateIgnorables; fDateIgnorables = NULL; | |
62 | delete fTimeIgnorables; fTimeIgnorables = NULL; | |
63 | delete fOtherIgnorables; fOtherIgnorables = NULL; | |
64 | } | |
65 | ||
66 | ||
67 | //------------------------------------------------------------------------------ | |
68 | // | |
69 | // smpdtfmt_cleanup Memory cleanup function, free/delete all | |
70 | // cached memory. Called by ICU's u_cleanup() function. | |
71 | // | |
72 | //------------------------------------------------------------------------------ | |
73 | UBool | |
74 | SimpleDateFormatStaticSets::cleanup(void) | |
75 | { | |
76 | delete SimpleDateFormatStaticSets::gStaticSets; | |
77 | SimpleDateFormatStaticSets::gStaticSets = NULL; | |
78 | ||
79 | return TRUE; | |
80 | } | |
81 | ||
82 | U_CDECL_BEGIN | |
83 | static UBool U_CALLCONV | |
84 | smpdtfmt_cleanup(void) | |
85 | { | |
86 | return SimpleDateFormatStaticSets::cleanup(); | |
87 | } | |
88 | U_CDECL_END | |
89 | ||
90 | void SimpleDateFormatStaticSets::initSets(UErrorCode *status) | |
91 | { | |
92 | SimpleDateFormatStaticSets *p; | |
93 | ||
94 | UMTX_CHECK(NULL, gStaticSets, p); | |
95 | if (p == NULL) { | |
96 | p = new SimpleDateFormatStaticSets(status); | |
97 | ||
98 | if (p == NULL) { | |
99 | *status = U_MEMORY_ALLOCATION_ERROR; | |
100 | return; | |
101 | } | |
102 | ||
103 | if (U_FAILURE(*status)) { | |
104 | delete p; | |
105 | return; | |
106 | } | |
107 | ||
108 | umtx_lock(NULL); | |
109 | if (gStaticSets == NULL) { | |
110 | gStaticSets = p; | |
111 | p = NULL; | |
112 | } | |
113 | ||
114 | umtx_unlock(NULL); | |
115 | if (p != NULL) { | |
116 | delete p; | |
117 | } | |
118 | ||
119 | ucln_i18n_registerCleanup(UCLN_I18N_SMPDTFMT, smpdtfmt_cleanup); | |
120 | } | |
121 | } | |
122 | ||
123 | UnicodeSet *SimpleDateFormatStaticSets::getIgnorables(UDateFormatField fieldIndex) | |
124 | { | |
125 | UErrorCode status = U_ZERO_ERROR; | |
126 | ||
127 | initSets(&status); | |
128 | ||
129 | if (U_FAILURE(status)) { | |
130 | return NULL; | |
131 | } | |
132 | ||
133 | switch (fieldIndex) { | |
134 | case UDAT_YEAR_FIELD: | |
135 | case UDAT_MONTH_FIELD: | |
136 | case UDAT_DATE_FIELD: | |
137 | case UDAT_STANDALONE_DAY_FIELD: | |
138 | case UDAT_STANDALONE_MONTH_FIELD: | |
139 | return gStaticSets->fDateIgnorables; | |
140 | ||
141 | case UDAT_HOUR_OF_DAY1_FIELD: | |
142 | case UDAT_HOUR_OF_DAY0_FIELD: | |
143 | case UDAT_MINUTE_FIELD: | |
144 | case UDAT_SECOND_FIELD: | |
145 | case UDAT_HOUR1_FIELD: | |
146 | case UDAT_HOUR0_FIELD: | |
147 | return gStaticSets->fTimeIgnorables; | |
148 | ||
149 | default: | |
150 | return gStaticSets->fOtherIgnorables; | |
151 | } | |
152 | } | |
153 | ||
154 | ||
155 | U_NAMESPACE_END | |
156 | ||
157 | #endif // #if !UCONFIG_NO_FORMATTING |