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