intl/icu/source/i18n/smpdtfst.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/i18n/smpdtfst.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,135 @@
     1.4 +/*
     1.5 +*******************************************************************************
     1.6 +* Copyright (C) 2009-2013, International Business Machines Corporation and    *
     1.7 +* others. All Rights Reserved.                                                *
     1.8 +*******************************************************************************
     1.9 +*
    1.10 +* This file contains the class SimpleDateFormatStaticSets
    1.11 +*
    1.12 +* SimpleDateFormatStaticSets holds the UnicodeSets that are needed for lenient
    1.13 +* parsing of literal characters in date/time strings.
    1.14 +********************************************************************************
    1.15 +*/
    1.16 +
    1.17 +#include "unicode/utypes.h"
    1.18 +
    1.19 +#if !UCONFIG_NO_FORMATTING
    1.20 +
    1.21 +#include "unicode/uniset.h"
    1.22 +#include "unicode/udat.h"
    1.23 +#include "cmemory.h"
    1.24 +#include "uassert.h"
    1.25 +#include "ucln_in.h"
    1.26 +#include "umutex.h"
    1.27 +
    1.28 +
    1.29 +#include "smpdtfst.h"
    1.30 +
    1.31 +U_NAMESPACE_BEGIN
    1.32 +
    1.33 +SimpleDateFormatStaticSets *gStaticSets = NULL;
    1.34 +UInitOnce gSimpleDateFormatStaticSetsInitOnce = U_INITONCE_INITIALIZER;
    1.35 +
    1.36 +SimpleDateFormatStaticSets::SimpleDateFormatStaticSets(UErrorCode &status)
    1.37 +: fDateIgnorables(NULL),
    1.38 +  fTimeIgnorables(NULL),
    1.39 +  fOtherIgnorables(NULL)
    1.40 +{
    1.41 +    fDateIgnorables  = new UnicodeSet(UNICODE_STRING("[-,./[:whitespace:]]", 20), status);
    1.42 +    fTimeIgnorables  = new UnicodeSet(UNICODE_STRING("[-.:[:whitespace:]]", 19),  status);
    1.43 +    fOtherIgnorables = new UnicodeSet(UNICODE_STRING("[:whitespace:]", 14),       status);
    1.44 +
    1.45 +    // Check for null pointers
    1.46 +    if (fDateIgnorables == NULL || fTimeIgnorables == NULL || fOtherIgnorables == NULL) {
    1.47 +        goto ExitConstrDeleteAll;
    1.48 +    }
    1.49 +
    1.50 +    // Freeze all the sets
    1.51 +    fDateIgnorables->freeze();
    1.52 +    fTimeIgnorables->freeze();
    1.53 +    fOtherIgnorables->freeze();
    1.54 +
    1.55 +    return; // If we reached this point, everything is fine so just exit
    1.56 +
    1.57 +ExitConstrDeleteAll: // Remove all sets and return error
    1.58 +    delete fDateIgnorables;  fDateIgnorables = NULL;
    1.59 +    delete fTimeIgnorables;  fTimeIgnorables = NULL;
    1.60 +    delete fOtherIgnorables; fOtherIgnorables = NULL;
    1.61 +
    1.62 +    status = U_MEMORY_ALLOCATION_ERROR;
    1.63 +}
    1.64 +
    1.65 +
    1.66 +SimpleDateFormatStaticSets::~SimpleDateFormatStaticSets() {
    1.67 +    delete fDateIgnorables;  fDateIgnorables = NULL;
    1.68 +    delete fTimeIgnorables;  fTimeIgnorables = NULL;
    1.69 +    delete fOtherIgnorables; fOtherIgnorables = NULL;
    1.70 +}
    1.71 +
    1.72 +
    1.73 +//------------------------------------------------------------------------------
    1.74 +//
    1.75 +//   smpdtfmt_cleanup     Memory cleanup function, free/delete all
    1.76 +//                      cached memory.  Called by ICU's u_cleanup() function.
    1.77 +//
    1.78 +//------------------------------------------------------------------------------
    1.79 +UBool
    1.80 +SimpleDateFormatStaticSets::cleanup(void)
    1.81 +{
    1.82 +    delete gStaticSets;
    1.83 +    gStaticSets = NULL;
    1.84 +    gSimpleDateFormatStaticSetsInitOnce.reset();
    1.85 +    return TRUE;
    1.86 +}
    1.87 +
    1.88 +U_CDECL_BEGIN
    1.89 +static UBool U_CALLCONV
    1.90 +smpdtfmt_cleanup(void)
    1.91 +{
    1.92 +    return SimpleDateFormatStaticSets::cleanup();
    1.93 +}
    1.94 +
    1.95 +static void U_CALLCONV smpdtfmt_initSets(UErrorCode &status) {
    1.96 +    ucln_i18n_registerCleanup(UCLN_I18N_SMPDTFMT, smpdtfmt_cleanup);
    1.97 +    U_ASSERT(gStaticSets == NULL);
    1.98 +    gStaticSets = new SimpleDateFormatStaticSets(status);
    1.99 +    if (gStaticSets == NULL) {
   1.100 +        status = U_MEMORY_ALLOCATION_ERROR;
   1.101 +        return;
   1.102 +    }
   1.103 +}
   1.104 +
   1.105 +U_CDECL_END
   1.106 +
   1.107 +UnicodeSet *SimpleDateFormatStaticSets::getIgnorables(UDateFormatField fieldIndex)
   1.108 +{
   1.109 +    UErrorCode status = U_ZERO_ERROR;
   1.110 +    umtx_initOnce(gSimpleDateFormatStaticSetsInitOnce, &smpdtfmt_initSets, status);
   1.111 +    if (U_FAILURE(status)) {
   1.112 +        return NULL;
   1.113 +    }
   1.114 +    
   1.115 +    switch (fieldIndex) {
   1.116 +        case UDAT_YEAR_FIELD:
   1.117 +        case UDAT_MONTH_FIELD:
   1.118 +        case UDAT_DATE_FIELD:
   1.119 +        case UDAT_STANDALONE_DAY_FIELD:
   1.120 +        case UDAT_STANDALONE_MONTH_FIELD:
   1.121 +            return gStaticSets->fDateIgnorables;
   1.122 +            
   1.123 +        case UDAT_HOUR_OF_DAY1_FIELD:
   1.124 +        case UDAT_HOUR_OF_DAY0_FIELD:
   1.125 +        case UDAT_MINUTE_FIELD:
   1.126 +        case UDAT_SECOND_FIELD:
   1.127 +        case UDAT_HOUR1_FIELD:
   1.128 +        case UDAT_HOUR0_FIELD:
   1.129 +            return gStaticSets->fTimeIgnorables;
   1.130 +            
   1.131 +        default:
   1.132 +            return gStaticSets->fOtherIgnorables;
   1.133 +    }
   1.134 +}
   1.135 +
   1.136 +U_NAMESPACE_END
   1.137 +
   1.138 +#endif // #if !UCONFIG_NO_FORMATTING

mercurial