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