]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/smpdtfst.cpp
ICU-57166.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / smpdtfst.cpp
CommitLineData
4388f060
A
1/*
2*******************************************************************************
57a6839d 3* Copyright (C) 2009-2013, International Business Machines Corporation and *
4388f060
A
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*/
46f4442e
A
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"
57a6839d 21#include "uassert.h"
46f4442e
A
22#include "ucln_in.h"
23#include "umutex.h"
24
25
26#include "smpdtfst.h"
27
28U_NAMESPACE_BEGIN
29
57a6839d
A
30SimpleDateFormatStaticSets *gStaticSets = NULL;
31UInitOnce gSimpleDateFormatStaticSetsInitOnce = U_INITONCE_INITIALIZER;
46f4442e 32
57a6839d 33SimpleDateFormatStaticSets::SimpleDateFormatStaticSets(UErrorCode &status)
46f4442e
A
34: fDateIgnorables(NULL),
35 fTimeIgnorables(NULL),
36 fOtherIgnorables(NULL)
37{
57a6839d
A
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);
4388f060 41
46f4442e
A
42 // Check for null pointers
43 if (fDateIgnorables == NULL || fTimeIgnorables == NULL || fOtherIgnorables == NULL) {
44 goto ExitConstrDeleteAll;
45 }
4388f060 46
46f4442e
A
47 // Freeze all the sets
48 fDateIgnorables->freeze();
49 fTimeIgnorables->freeze();
50 fOtherIgnorables->freeze();
4388f060 51
46f4442e 52 return; // If we reached this point, everything is fine so just exit
4388f060 53
46f4442e
A
54ExitConstrDeleteAll: // Remove all sets and return error
55 delete fDateIgnorables; fDateIgnorables = NULL;
56 delete fTimeIgnorables; fTimeIgnorables = NULL;
57 delete fOtherIgnorables; fOtherIgnorables = NULL;
4388f060 58
57a6839d 59 status = U_MEMORY_ALLOCATION_ERROR;
46f4442e
A
60}
61
62
63SimpleDateFormatStaticSets::~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//------------------------------------------------------------------------------
76UBool
77SimpleDateFormatStaticSets::cleanup(void)
78{
57a6839d
A
79 delete gStaticSets;
80 gStaticSets = NULL;
81 gSimpleDateFormatStaticSetsInitOnce.reset();
46f4442e
A
82 return TRUE;
83}
84
85U_CDECL_BEGIN
86static UBool U_CALLCONV
87smpdtfmt_cleanup(void)
88{
89 return SimpleDateFormatStaticSets::cleanup();
90}
46f4442e 91
57a6839d
A
92static 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;
46f4442e
A
99 }
100}
101
57a6839d
A
102U_CDECL_END
103
46f4442e
A
104UnicodeSet *SimpleDateFormatStaticSets::getIgnorables(UDateFormatField fieldIndex)
105{
57a6839d
A
106 UErrorCode status = U_ZERO_ERROR;
107 umtx_initOnce(gSimpleDateFormatStaticSetsInitOnce, &smpdtfmt_initSets, status);
108 if (U_FAILURE(status)) {
109 return NULL;
110 }
46f4442e
A
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
46f4442e
A
133U_NAMESPACE_END
134
135#endif // #if !UCONFIG_NO_FORMATTING