• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SimpleDateFormatStaticSets(UErrorCode & status)35 SimpleDateFormatStaticSets::SimpleDateFormatStaticSets(UErrorCode &status)
36 : fDateIgnorables(NULL),
37   fTimeIgnorables(NULL),
38   fOtherIgnorables(NULL)
39 {
40     fDateIgnorables  = new UnicodeSet(UNICODE_STRING("[-,./[:whitespace:]]", 20), status);
41     fTimeIgnorables  = new UnicodeSet(UNICODE_STRING("[-.:[:whitespace:]]", 19),  status);
42     fOtherIgnorables = new UnicodeSet(UNICODE_STRING("[:whitespace:]", 14),       status);
43 
44     // Check for null pointers
45     if (fDateIgnorables == NULL || fTimeIgnorables == NULL || fOtherIgnorables == NULL) {
46         goto ExitConstrDeleteAll;
47     }
48 
49     // Freeze all the sets
50     fDateIgnorables->freeze();
51     fTimeIgnorables->freeze();
52     fOtherIgnorables->freeze();
53 
54     return; // If we reached this point, everything is fine so just exit
55 
56 ExitConstrDeleteAll: // Remove all sets and return error
57     delete fDateIgnorables;  fDateIgnorables = NULL;
58     delete fTimeIgnorables;  fTimeIgnorables = NULL;
59     delete fOtherIgnorables; fOtherIgnorables = NULL;
60 
61     status = U_MEMORY_ALLOCATION_ERROR;
62 }
63 
64 
~SimpleDateFormatStaticSets()65 SimpleDateFormatStaticSets::~SimpleDateFormatStaticSets() {
66     delete fDateIgnorables;  fDateIgnorables = NULL;
67     delete fTimeIgnorables;  fTimeIgnorables = NULL;
68     delete fOtherIgnorables; fOtherIgnorables = NULL;
69 }
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
cleanup(void)79 SimpleDateFormatStaticSets::cleanup(void)
80 {
81     delete gStaticSets;
82     gStaticSets = NULL;
83     gSimpleDateFormatStaticSetsInitOnce.reset();
84     return TRUE;
85 }
86 
87 U_CDECL_BEGIN
88 static UBool U_CALLCONV
smpdtfmt_cleanup(void)89 smpdtfmt_cleanup(void)
90 {
91     return SimpleDateFormatStaticSets::cleanup();
92 }
93 
smpdtfmt_initSets(UErrorCode & status)94 static void U_CALLCONV smpdtfmt_initSets(UErrorCode &status) {
95     ucln_i18n_registerCleanup(UCLN_I18N_SMPDTFMT, smpdtfmt_cleanup);
96     U_ASSERT(gStaticSets == NULL);
97     gStaticSets = new SimpleDateFormatStaticSets(status);
98     if (gStaticSets == NULL) {
99         status = U_MEMORY_ALLOCATION_ERROR;
100         return;
101     }
102 }
103 
104 U_CDECL_END
105 
getIgnorables(UDateFormatField fieldIndex)106 UnicodeSet *SimpleDateFormatStaticSets::getIgnorables(UDateFormatField fieldIndex)
107 {
108     UErrorCode status = U_ZERO_ERROR;
109     umtx_initOnce(gSimpleDateFormatStaticSetsInitOnce, &smpdtfmt_initSets, status);
110     if (U_FAILURE(status)) {
111         return NULL;
112     }
113 
114     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             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             return gStaticSets->fTimeIgnorables;
129 
130         default:
131             return gStaticSets->fOtherIgnorables;
132     }
133 }
134 
135 U_NAMESPACE_END
136 
137 #endif // #if !UCONFIG_NO_FORMATTING
138