intl/icu/source/io/locbund.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/io/locbund.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,183 @@
     1.4 +/*
     1.5 +*******************************************************************************
     1.6 +*
     1.7 +*   Copyright (C) 1998-2013, International Business Machines
     1.8 +*   Corporation and others.  All Rights Reserved.
     1.9 +*
    1.10 +*******************************************************************************
    1.11 +*
    1.12 +* File locbund.cpp
    1.13 +*
    1.14 +* Modification History:
    1.15 +*
    1.16 +*   Date        Name        Description
    1.17 +*   11/18/98    stephen        Creation.
    1.18 +*   12/10/1999  bobbyr(at)optiosoftware.com       Fix for memory leak + string allocation bugs
    1.19 +*******************************************************************************
    1.20 +*/
    1.21 +
    1.22 +#include "unicode/utypes.h"
    1.23 +
    1.24 +#if !UCONFIG_NO_FORMATTING
    1.25 +
    1.26 +#include "locbund.h"
    1.27 +
    1.28 +#include "cmemory.h"
    1.29 +#include "cstring.h"
    1.30 +#include "ucln_io.h"
    1.31 +#include "mutex.h"
    1.32 +#include "umutex.h"
    1.33 +#include "unicode/ustring.h"
    1.34 +#include "unicode/uloc.h"
    1.35 +
    1.36 +static UNumberFormat *gPosixNumberFormat[ULOCALEBUNDLE_NUMBERFORMAT_COUNT];
    1.37 +
    1.38 +U_CDECL_BEGIN
    1.39 +static UBool U_CALLCONV locbund_cleanup(void) {
    1.40 +    int32_t style;
    1.41 +    for (style = 0; style < ULOCALEBUNDLE_NUMBERFORMAT_COUNT; style++) {
    1.42 +        unum_close(gPosixNumberFormat[style]);
    1.43 +        gPosixNumberFormat[style] = NULL;
    1.44 +    }
    1.45 +    return TRUE;
    1.46 +}
    1.47 +U_CDECL_END
    1.48 +
    1.49 +static UMutex gLock = U_MUTEX_INITIALIZER;
    1.50 +static inline UNumberFormat * copyInvariantFormatter(ULocaleBundle *result, UNumberFormatStyle style) {
    1.51 +    U_NAMESPACE_USE
    1.52 +    Mutex lock(&gLock);
    1.53 +    if (result->fNumberFormat[style-1] == NULL) {
    1.54 +        if (gPosixNumberFormat[style-1] == NULL) {
    1.55 +            UErrorCode status = U_ZERO_ERROR;
    1.56 +            UNumberFormat *formatAlias = unum_open(style, NULL, 0, "en_US_POSIX", NULL, &status);
    1.57 +            if (U_SUCCESS(status)) {
    1.58 +                gPosixNumberFormat[style-1] = formatAlias;
    1.59 +                ucln_io_registerCleanup(UCLN_IO_LOCBUND, locbund_cleanup);
    1.60 +            }
    1.61 +        }
    1.62 +        /* Copy the needed formatter. */
    1.63 +        if (gPosixNumberFormat[style-1] != NULL) {
    1.64 +            UErrorCode status = U_ZERO_ERROR;
    1.65 +            result->fNumberFormat[style-1] = unum_clone(gPosixNumberFormat[style-1], &status);
    1.66 +        }
    1.67 +    }
    1.68 +    return result->fNumberFormat[style-1];
    1.69 +}
    1.70 +
    1.71 +U_CAPI ULocaleBundle *
    1.72 +u_locbund_init(ULocaleBundle *result, const char *loc)
    1.73 +{
    1.74 +    int32_t len;
    1.75 +
    1.76 +    if(result == 0)
    1.77 +        return 0;
    1.78 +
    1.79 +    if (loc == NULL) {
    1.80 +        loc = uloc_getDefault();
    1.81 +    }
    1.82 +
    1.83 +    uprv_memset(result, 0, sizeof(ULocaleBundle));
    1.84 +
    1.85 +    len = (int32_t)strlen(loc);
    1.86 +    result->fLocale = (char*) uprv_malloc(len + 1);
    1.87 +    if(result->fLocale == 0) {
    1.88 +        return 0;
    1.89 +    }
    1.90 +
    1.91 +    uprv_strcpy(result->fLocale, loc);
    1.92 +
    1.93 +    result->isInvariantLocale = uprv_strcmp(result->fLocale, "en_US_POSIX") == 0;
    1.94 +
    1.95 +    return result;
    1.96 +}
    1.97 +
    1.98 +/*U_CAPI ULocaleBundle *
    1.99 +u_locbund_new(const char *loc)
   1.100 +{
   1.101 +    ULocaleBundle *result = (ULocaleBundle*) uprv_malloc(sizeof(ULocaleBundle));
   1.102 +    return u_locbund_init(result, loc);
   1.103 +}
   1.104 +
   1.105 +U_CAPI ULocaleBundle *
   1.106 +u_locbund_clone(const ULocaleBundle *bundle)
   1.107 +{
   1.108 +    ULocaleBundle *result = (ULocaleBundle*)uprv_malloc(sizeof(ULocaleBundle));
   1.109 +    UErrorCode status = U_ZERO_ERROR;
   1.110 +    int32_t styleIdx;
   1.111 +    
   1.112 +    if(result == 0)
   1.113 +        return 0;
   1.114 +    
   1.115 +    result->fLocale = (char*) uprv_malloc(strlen(bundle->fLocale) + 1);
   1.116 +    if(result->fLocale == 0) {
   1.117 +        uprv_free(result);
   1.118 +        return 0;
   1.119 +    }
   1.120 +    
   1.121 +    strcpy(result->fLocale, bundle->fLocale );
   1.122 +    
   1.123 +    for (styleIdx = 0; styleIdx < ULOCALEBUNDLE_NUMBERFORMAT_COUNT; styleIdx++) {
   1.124 +        status = U_ZERO_ERROR;
   1.125 +        if (result->fNumberFormat[styleIdx]) {
   1.126 +            result->fNumberFormat[styleIdx] = unum_clone(bundle->fNumberFormat[styleIdx], &status);
   1.127 +            if (U_FAILURE(status)) {
   1.128 +                result->fNumberFormat[styleIdx] = NULL;
   1.129 +            }
   1.130 +        }
   1.131 +        else {
   1.132 +            result->fNumberFormat[styleIdx] = NULL;
   1.133 +        }
   1.134 +    }
   1.135 +    result->fDateFormat         = (bundle->fDateFormat == 0 ? 0 :
   1.136 +        udat_clone(bundle->fDateFormat, &status));
   1.137 +    result->fTimeFormat         = (bundle->fTimeFormat == 0 ? 0 :
   1.138 +        udat_clone(bundle->fTimeFormat, &status));
   1.139 +    
   1.140 +    return result;
   1.141 +}*/
   1.142 +
   1.143 +U_CAPI void
   1.144 +u_locbund_close(ULocaleBundle *bundle)
   1.145 +{
   1.146 +    int32_t styleIdx;
   1.147 +
   1.148 +    uprv_free(bundle->fLocale);
   1.149 +    
   1.150 +    for (styleIdx = 0; styleIdx < ULOCALEBUNDLE_NUMBERFORMAT_COUNT; styleIdx++) {
   1.151 +        if (bundle->fNumberFormat[styleIdx]) {
   1.152 +            unum_close(bundle->fNumberFormat[styleIdx]);
   1.153 +        }
   1.154 +    }
   1.155 +    
   1.156 +    uprv_memset(bundle, 0, sizeof(ULocaleBundle));
   1.157 +/*    uprv_free(bundle);*/
   1.158 +}
   1.159 +
   1.160 +U_CAPI UNumberFormat *
   1.161 +u_locbund_getNumberFormat(ULocaleBundle *bundle, UNumberFormatStyle style)
   1.162 +{
   1.163 +    UNumberFormat *formatAlias = NULL;
   1.164 +    if (style > UNUM_IGNORE) {
   1.165 +        formatAlias = bundle->fNumberFormat[style-1];
   1.166 +        if (formatAlias == NULL) {
   1.167 +            if (bundle->isInvariantLocale) {
   1.168 +                formatAlias = copyInvariantFormatter(bundle, style);
   1.169 +            }
   1.170 +            else {
   1.171 +                UErrorCode status = U_ZERO_ERROR;
   1.172 +                formatAlias = unum_open(style, NULL, 0, bundle->fLocale, NULL, &status);
   1.173 +                if (U_FAILURE(status)) {
   1.174 +                    unum_close(formatAlias);
   1.175 +                    formatAlias = NULL;
   1.176 +                }
   1.177 +                else {
   1.178 +                    bundle->fNumberFormat[style-1] = formatAlias;
   1.179 +                }
   1.180 +            }
   1.181 +        }
   1.182 +    }
   1.183 +    return formatAlias;
   1.184 +}
   1.185 +
   1.186 +#endif /* #if !UCONFIG_NO_FORMATTING */

mercurial