]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/smpdtfst.cpp
ICU-531.48.tar.gz
[apple/icu.git] / icuSources / i18n / smpdtfst.cpp
1 /*
2 *******************************************************************************
3 * Copyright (C) 2009-2013, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
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
14 #include "unicode/utypes.h"
15
16 #if !UCONFIG_NO_FORMATTING
17
18 #include "unicode/uniset.h"
19 #include "unicode/udat.h"
20 #include "cmemory.h"
21 #include "uassert.h"
22 #include "ucln_in.h"
23 #include "umutex.h"
24
25
26 #include "smpdtfst.h"
27
28 U_NAMESPACE_BEGIN
29
30 SimpleDateFormatStaticSets *gStaticSets = NULL;
31 UInitOnce gSimpleDateFormatStaticSetsInitOnce = U_INITONCE_INITIALIZER;
32
33 SimpleDateFormatStaticSets::SimpleDateFormatStaticSets(UErrorCode &status)
34 : fDateIgnorables(NULL),
35 fTimeIgnorables(NULL),
36 fOtherIgnorables(NULL)
37 {
38 fDateIgnorables = new UnicodeSet(UNICODE_STRING("[-,./[:whitespace:]]", 20), status);
39 fTimeIgnorables = new UnicodeSet(UNICODE_STRING("[-.:[:whitespace:]]", 19), status);
40 fOtherIgnorables = new UnicodeSet(UNICODE_STRING("[:whitespace:]", 14), status);
41
42 // Check for null pointers
43 if (fDateIgnorables == NULL || fTimeIgnorables == NULL || fOtherIgnorables == NULL) {
44 goto ExitConstrDeleteAll;
45 }
46
47 // Freeze all the sets
48 fDateIgnorables->freeze();
49 fTimeIgnorables->freeze();
50 fOtherIgnorables->freeze();
51
52 return; // If we reached this point, everything is fine so just exit
53
54 ExitConstrDeleteAll: // Remove all sets and return error
55 delete fDateIgnorables; fDateIgnorables = NULL;
56 delete fTimeIgnorables; fTimeIgnorables = NULL;
57 delete fOtherIgnorables; fOtherIgnorables = NULL;
58
59 status = U_MEMORY_ALLOCATION_ERROR;
60 }
61
62
63 SimpleDateFormatStaticSets::~SimpleDateFormatStaticSets() {
64 delete fDateIgnorables; fDateIgnorables = NULL;
65 delete fTimeIgnorables; fTimeIgnorables = NULL;
66 delete fOtherIgnorables; fOtherIgnorables = NULL;
67 }
68
69
70 //------------------------------------------------------------------------------
71 //
72 // smpdtfmt_cleanup Memory cleanup function, free/delete all
73 // cached memory. Called by ICU's u_cleanup() function.
74 //
75 //------------------------------------------------------------------------------
76 UBool
77 SimpleDateFormatStaticSets::cleanup(void)
78 {
79 delete gStaticSets;
80 gStaticSets = NULL;
81 gSimpleDateFormatStaticSetsInitOnce.reset();
82 return TRUE;
83 }
84
85 U_CDECL_BEGIN
86 static UBool U_CALLCONV
87 smpdtfmt_cleanup(void)
88 {
89 return SimpleDateFormatStaticSets::cleanup();
90 }
91
92 static void U_CALLCONV smpdtfmt_initSets(UErrorCode &status) {
93 ucln_i18n_registerCleanup(UCLN_I18N_SMPDTFMT, smpdtfmt_cleanup);
94 U_ASSERT(gStaticSets == NULL);
95 gStaticSets = new SimpleDateFormatStaticSets(status);
96 if (gStaticSets == NULL) {
97 status = U_MEMORY_ALLOCATION_ERROR;
98 return;
99 }
100 }
101
102 U_CDECL_END
103
104 UnicodeSet *SimpleDateFormatStaticSets::getIgnorables(UDateFormatField fieldIndex)
105 {
106 UErrorCode status = U_ZERO_ERROR;
107 umtx_initOnce(gSimpleDateFormatStaticSetsInitOnce, &smpdtfmt_initSets, status);
108 if (U_FAILURE(status)) {
109 return NULL;
110 }
111
112 switch (fieldIndex) {
113 case UDAT_YEAR_FIELD:
114 case UDAT_MONTH_FIELD:
115 case UDAT_DATE_FIELD:
116 case UDAT_STANDALONE_DAY_FIELD:
117 case UDAT_STANDALONE_MONTH_FIELD:
118 return gStaticSets->fDateIgnorables;
119
120 case UDAT_HOUR_OF_DAY1_FIELD:
121 case UDAT_HOUR_OF_DAY0_FIELD:
122 case UDAT_MINUTE_FIELD:
123 case UDAT_SECOND_FIELD:
124 case UDAT_HOUR1_FIELD:
125 case UDAT_HOUR0_FIELD:
126 return gStaticSets->fTimeIgnorables;
127
128 default:
129 return gStaticSets->fOtherIgnorables;
130 }
131 }
132
133 U_NAMESPACE_END
134
135 #endif // #if !UCONFIG_NO_FORMATTING