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