Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | ******************************************************************************* |
michael@0 | 3 | * |
michael@0 | 4 | * Copyright (C) 1998-2013, International Business Machines |
michael@0 | 5 | * Corporation and others. All Rights Reserved. |
michael@0 | 6 | * |
michael@0 | 7 | ******************************************************************************* |
michael@0 | 8 | * |
michael@0 | 9 | * File locbund.cpp |
michael@0 | 10 | * |
michael@0 | 11 | * Modification History: |
michael@0 | 12 | * |
michael@0 | 13 | * Date Name Description |
michael@0 | 14 | * 11/18/98 stephen Creation. |
michael@0 | 15 | * 12/10/1999 bobbyr(at)optiosoftware.com Fix for memory leak + string allocation bugs |
michael@0 | 16 | ******************************************************************************* |
michael@0 | 17 | */ |
michael@0 | 18 | |
michael@0 | 19 | #include "unicode/utypes.h" |
michael@0 | 20 | |
michael@0 | 21 | #if !UCONFIG_NO_FORMATTING |
michael@0 | 22 | |
michael@0 | 23 | #include "locbund.h" |
michael@0 | 24 | |
michael@0 | 25 | #include "cmemory.h" |
michael@0 | 26 | #include "cstring.h" |
michael@0 | 27 | #include "ucln_io.h" |
michael@0 | 28 | #include "mutex.h" |
michael@0 | 29 | #include "umutex.h" |
michael@0 | 30 | #include "unicode/ustring.h" |
michael@0 | 31 | #include "unicode/uloc.h" |
michael@0 | 32 | |
michael@0 | 33 | static UNumberFormat *gPosixNumberFormat[ULOCALEBUNDLE_NUMBERFORMAT_COUNT]; |
michael@0 | 34 | |
michael@0 | 35 | U_CDECL_BEGIN |
michael@0 | 36 | static UBool U_CALLCONV locbund_cleanup(void) { |
michael@0 | 37 | int32_t style; |
michael@0 | 38 | for (style = 0; style < ULOCALEBUNDLE_NUMBERFORMAT_COUNT; style++) { |
michael@0 | 39 | unum_close(gPosixNumberFormat[style]); |
michael@0 | 40 | gPosixNumberFormat[style] = NULL; |
michael@0 | 41 | } |
michael@0 | 42 | return TRUE; |
michael@0 | 43 | } |
michael@0 | 44 | U_CDECL_END |
michael@0 | 45 | |
michael@0 | 46 | static UMutex gLock = U_MUTEX_INITIALIZER; |
michael@0 | 47 | static inline UNumberFormat * copyInvariantFormatter(ULocaleBundle *result, UNumberFormatStyle style) { |
michael@0 | 48 | U_NAMESPACE_USE |
michael@0 | 49 | Mutex lock(&gLock); |
michael@0 | 50 | if (result->fNumberFormat[style-1] == NULL) { |
michael@0 | 51 | if (gPosixNumberFormat[style-1] == NULL) { |
michael@0 | 52 | UErrorCode status = U_ZERO_ERROR; |
michael@0 | 53 | UNumberFormat *formatAlias = unum_open(style, NULL, 0, "en_US_POSIX", NULL, &status); |
michael@0 | 54 | if (U_SUCCESS(status)) { |
michael@0 | 55 | gPosixNumberFormat[style-1] = formatAlias; |
michael@0 | 56 | ucln_io_registerCleanup(UCLN_IO_LOCBUND, locbund_cleanup); |
michael@0 | 57 | } |
michael@0 | 58 | } |
michael@0 | 59 | /* Copy the needed formatter. */ |
michael@0 | 60 | if (gPosixNumberFormat[style-1] != NULL) { |
michael@0 | 61 | UErrorCode status = U_ZERO_ERROR; |
michael@0 | 62 | result->fNumberFormat[style-1] = unum_clone(gPosixNumberFormat[style-1], &status); |
michael@0 | 63 | } |
michael@0 | 64 | } |
michael@0 | 65 | return result->fNumberFormat[style-1]; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | U_CAPI ULocaleBundle * |
michael@0 | 69 | u_locbund_init(ULocaleBundle *result, const char *loc) |
michael@0 | 70 | { |
michael@0 | 71 | int32_t len; |
michael@0 | 72 | |
michael@0 | 73 | if(result == 0) |
michael@0 | 74 | return 0; |
michael@0 | 75 | |
michael@0 | 76 | if (loc == NULL) { |
michael@0 | 77 | loc = uloc_getDefault(); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | uprv_memset(result, 0, sizeof(ULocaleBundle)); |
michael@0 | 81 | |
michael@0 | 82 | len = (int32_t)strlen(loc); |
michael@0 | 83 | result->fLocale = (char*) uprv_malloc(len + 1); |
michael@0 | 84 | if(result->fLocale == 0) { |
michael@0 | 85 | return 0; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | uprv_strcpy(result->fLocale, loc); |
michael@0 | 89 | |
michael@0 | 90 | result->isInvariantLocale = uprv_strcmp(result->fLocale, "en_US_POSIX") == 0; |
michael@0 | 91 | |
michael@0 | 92 | return result; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | /*U_CAPI ULocaleBundle * |
michael@0 | 96 | u_locbund_new(const char *loc) |
michael@0 | 97 | { |
michael@0 | 98 | ULocaleBundle *result = (ULocaleBundle*) uprv_malloc(sizeof(ULocaleBundle)); |
michael@0 | 99 | return u_locbund_init(result, loc); |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | U_CAPI ULocaleBundle * |
michael@0 | 103 | u_locbund_clone(const ULocaleBundle *bundle) |
michael@0 | 104 | { |
michael@0 | 105 | ULocaleBundle *result = (ULocaleBundle*)uprv_malloc(sizeof(ULocaleBundle)); |
michael@0 | 106 | UErrorCode status = U_ZERO_ERROR; |
michael@0 | 107 | int32_t styleIdx; |
michael@0 | 108 | |
michael@0 | 109 | if(result == 0) |
michael@0 | 110 | return 0; |
michael@0 | 111 | |
michael@0 | 112 | result->fLocale = (char*) uprv_malloc(strlen(bundle->fLocale) + 1); |
michael@0 | 113 | if(result->fLocale == 0) { |
michael@0 | 114 | uprv_free(result); |
michael@0 | 115 | return 0; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | strcpy(result->fLocale, bundle->fLocale ); |
michael@0 | 119 | |
michael@0 | 120 | for (styleIdx = 0; styleIdx < ULOCALEBUNDLE_NUMBERFORMAT_COUNT; styleIdx++) { |
michael@0 | 121 | status = U_ZERO_ERROR; |
michael@0 | 122 | if (result->fNumberFormat[styleIdx]) { |
michael@0 | 123 | result->fNumberFormat[styleIdx] = unum_clone(bundle->fNumberFormat[styleIdx], &status); |
michael@0 | 124 | if (U_FAILURE(status)) { |
michael@0 | 125 | result->fNumberFormat[styleIdx] = NULL; |
michael@0 | 126 | } |
michael@0 | 127 | } |
michael@0 | 128 | else { |
michael@0 | 129 | result->fNumberFormat[styleIdx] = NULL; |
michael@0 | 130 | } |
michael@0 | 131 | } |
michael@0 | 132 | result->fDateFormat = (bundle->fDateFormat == 0 ? 0 : |
michael@0 | 133 | udat_clone(bundle->fDateFormat, &status)); |
michael@0 | 134 | result->fTimeFormat = (bundle->fTimeFormat == 0 ? 0 : |
michael@0 | 135 | udat_clone(bundle->fTimeFormat, &status)); |
michael@0 | 136 | |
michael@0 | 137 | return result; |
michael@0 | 138 | }*/ |
michael@0 | 139 | |
michael@0 | 140 | U_CAPI void |
michael@0 | 141 | u_locbund_close(ULocaleBundle *bundle) |
michael@0 | 142 | { |
michael@0 | 143 | int32_t styleIdx; |
michael@0 | 144 | |
michael@0 | 145 | uprv_free(bundle->fLocale); |
michael@0 | 146 | |
michael@0 | 147 | for (styleIdx = 0; styleIdx < ULOCALEBUNDLE_NUMBERFORMAT_COUNT; styleIdx++) { |
michael@0 | 148 | if (bundle->fNumberFormat[styleIdx]) { |
michael@0 | 149 | unum_close(bundle->fNumberFormat[styleIdx]); |
michael@0 | 150 | } |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | uprv_memset(bundle, 0, sizeof(ULocaleBundle)); |
michael@0 | 154 | /* uprv_free(bundle);*/ |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | U_CAPI UNumberFormat * |
michael@0 | 158 | u_locbund_getNumberFormat(ULocaleBundle *bundle, UNumberFormatStyle style) |
michael@0 | 159 | { |
michael@0 | 160 | UNumberFormat *formatAlias = NULL; |
michael@0 | 161 | if (style > UNUM_IGNORE) { |
michael@0 | 162 | formatAlias = bundle->fNumberFormat[style-1]; |
michael@0 | 163 | if (formatAlias == NULL) { |
michael@0 | 164 | if (bundle->isInvariantLocale) { |
michael@0 | 165 | formatAlias = copyInvariantFormatter(bundle, style); |
michael@0 | 166 | } |
michael@0 | 167 | else { |
michael@0 | 168 | UErrorCode status = U_ZERO_ERROR; |
michael@0 | 169 | formatAlias = unum_open(style, NULL, 0, bundle->fLocale, NULL, &status); |
michael@0 | 170 | if (U_FAILURE(status)) { |
michael@0 | 171 | unum_close(formatAlias); |
michael@0 | 172 | formatAlias = NULL; |
michael@0 | 173 | } |
michael@0 | 174 | else { |
michael@0 | 175 | bundle->fNumberFormat[style-1] = formatAlias; |
michael@0 | 176 | } |
michael@0 | 177 | } |
michael@0 | 178 | } |
michael@0 | 179 | } |
michael@0 | 180 | return formatAlias; |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | #endif /* #if !UCONFIG_NO_FORMATTING */ |