michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2009-2013, International Business Machines Corporation and * michael@0: * others. All Rights Reserved. * michael@0: ******************************************************************************* michael@0: * michael@0: * This file contains the class SimpleDateFormatStaticSets michael@0: * michael@0: * SimpleDateFormatStaticSets holds the UnicodeSets that are needed for lenient michael@0: * parsing of literal characters in date/time strings. michael@0: ******************************************************************************** michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/uniset.h" michael@0: #include "unicode/udat.h" michael@0: #include "cmemory.h" michael@0: #include "uassert.h" michael@0: #include "ucln_in.h" michael@0: #include "umutex.h" michael@0: michael@0: michael@0: #include "smpdtfst.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: SimpleDateFormatStaticSets *gStaticSets = NULL; michael@0: UInitOnce gSimpleDateFormatStaticSetsInitOnce = U_INITONCE_INITIALIZER; michael@0: michael@0: SimpleDateFormatStaticSets::SimpleDateFormatStaticSets(UErrorCode &status) michael@0: : fDateIgnorables(NULL), michael@0: fTimeIgnorables(NULL), michael@0: fOtherIgnorables(NULL) michael@0: { michael@0: fDateIgnorables = new UnicodeSet(UNICODE_STRING("[-,./[:whitespace:]]", 20), status); michael@0: fTimeIgnorables = new UnicodeSet(UNICODE_STRING("[-.:[:whitespace:]]", 19), status); michael@0: fOtherIgnorables = new UnicodeSet(UNICODE_STRING("[:whitespace:]", 14), status); michael@0: michael@0: // Check for null pointers michael@0: if (fDateIgnorables == NULL || fTimeIgnorables == NULL || fOtherIgnorables == NULL) { michael@0: goto ExitConstrDeleteAll; michael@0: } michael@0: michael@0: // Freeze all the sets michael@0: fDateIgnorables->freeze(); michael@0: fTimeIgnorables->freeze(); michael@0: fOtherIgnorables->freeze(); michael@0: michael@0: return; // If we reached this point, everything is fine so just exit michael@0: michael@0: ExitConstrDeleteAll: // Remove all sets and return error michael@0: delete fDateIgnorables; fDateIgnorables = NULL; michael@0: delete fTimeIgnorables; fTimeIgnorables = NULL; michael@0: delete fOtherIgnorables; fOtherIgnorables = NULL; michael@0: michael@0: status = U_MEMORY_ALLOCATION_ERROR; michael@0: } michael@0: michael@0: michael@0: SimpleDateFormatStaticSets::~SimpleDateFormatStaticSets() { michael@0: delete fDateIgnorables; fDateIgnorables = NULL; michael@0: delete fTimeIgnorables; fTimeIgnorables = NULL; michael@0: delete fOtherIgnorables; fOtherIgnorables = NULL; michael@0: } michael@0: michael@0: michael@0: //------------------------------------------------------------------------------ michael@0: // michael@0: // smpdtfmt_cleanup Memory cleanup function, free/delete all michael@0: // cached memory. Called by ICU's u_cleanup() function. michael@0: // michael@0: //------------------------------------------------------------------------------ michael@0: UBool michael@0: SimpleDateFormatStaticSets::cleanup(void) michael@0: { michael@0: delete gStaticSets; michael@0: gStaticSets = NULL; michael@0: gSimpleDateFormatStaticSetsInitOnce.reset(); michael@0: return TRUE; michael@0: } michael@0: michael@0: U_CDECL_BEGIN michael@0: static UBool U_CALLCONV michael@0: smpdtfmt_cleanup(void) michael@0: { michael@0: return SimpleDateFormatStaticSets::cleanup(); michael@0: } michael@0: michael@0: static void U_CALLCONV smpdtfmt_initSets(UErrorCode &status) { michael@0: ucln_i18n_registerCleanup(UCLN_I18N_SMPDTFMT, smpdtfmt_cleanup); michael@0: U_ASSERT(gStaticSets == NULL); michael@0: gStaticSets = new SimpleDateFormatStaticSets(status); michael@0: if (gStaticSets == NULL) { michael@0: status = U_MEMORY_ALLOCATION_ERROR; michael@0: return; michael@0: } michael@0: } michael@0: michael@0: U_CDECL_END michael@0: michael@0: UnicodeSet *SimpleDateFormatStaticSets::getIgnorables(UDateFormatField fieldIndex) michael@0: { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: umtx_initOnce(gSimpleDateFormatStaticSetsInitOnce, &smpdtfmt_initSets, status); michael@0: if (U_FAILURE(status)) { michael@0: return NULL; michael@0: } michael@0: michael@0: switch (fieldIndex) { michael@0: case UDAT_YEAR_FIELD: michael@0: case UDAT_MONTH_FIELD: michael@0: case UDAT_DATE_FIELD: michael@0: case UDAT_STANDALONE_DAY_FIELD: michael@0: case UDAT_STANDALONE_MONTH_FIELD: michael@0: return gStaticSets->fDateIgnorables; michael@0: michael@0: case UDAT_HOUR_OF_DAY1_FIELD: michael@0: case UDAT_HOUR_OF_DAY0_FIELD: michael@0: case UDAT_MINUTE_FIELD: michael@0: case UDAT_SECOND_FIELD: michael@0: case UDAT_HOUR1_FIELD: michael@0: case UDAT_HOUR0_FIELD: michael@0: return gStaticSets->fTimeIgnorables; michael@0: michael@0: default: michael@0: return gStaticSets->fOtherIgnorables; michael@0: } michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif // #if !UCONFIG_NO_FORMATTING