]>
Commit | Line | Data |
---|---|---|
374ca955 A |
1 | /* |
2 | ******************************************************************************* | |
3 | * | |
57a6839d | 4 | * Copyright (C) 1998-2013, International Business Machines |
374ca955 A |
5 | * Corporation and others. All Rights Reserved. |
6 | * | |
7 | ******************************************************************************* | |
8 | * | |
4388f060 | 9 | * File locbund.cpp |
374ca955 A |
10 | * |
11 | * Modification History: | |
12 | * | |
13 | * Date Name Description | |
14 | * 11/18/98 stephen Creation. | |
73c04bcf | 15 | * 12/10/1999 bobbyr(at)optiosoftware.com Fix for memory leak + string allocation bugs |
374ca955 A |
16 | ******************************************************************************* |
17 | */ | |
18 | ||
374ca955 A |
19 | #include "unicode/utypes.h" |
20 | ||
21 | #if !UCONFIG_NO_FORMATTING | |
22 | ||
23 | #include "locbund.h" | |
24 | ||
25 | #include "cmemory.h" | |
73c04bcf A |
26 | #include "cstring.h" |
27 | #include "ucln_io.h" | |
57a6839d | 28 | #include "mutex.h" |
73c04bcf | 29 | #include "umutex.h" |
374ca955 A |
30 | #include "unicode/ustring.h" |
31 | #include "unicode/uloc.h" | |
32 | ||
73c04bcf A |
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 | } | |
73c04bcf A |
42 | return TRUE; |
43 | } | |
44 | U_CDECL_END | |
45 | ||
57a6839d | 46 | static UMutex gLock = U_MUTEX_INITIALIZER; |
4388f060 | 47 | static inline UNumberFormat * copyInvariantFormatter(ULocaleBundle *result, UNumberFormatStyle style) { |
57a6839d A |
48 | U_NAMESPACE_USE |
49 | Mutex lock(&gLock); | |
73c04bcf | 50 | if (result->fNumberFormat[style-1] == NULL) { |
57a6839d A |
51 | if (gPosixNumberFormat[style-1] == NULL) { |
52 | UErrorCode status = U_ZERO_ERROR; | |
73c04bcf | 53 | UNumberFormat *formatAlias = unum_open(style, NULL, 0, "en_US_POSIX", NULL, &status); |
73c04bcf | 54 | if (U_SUCCESS(status)) { |
73c04bcf A |
55 | gPosixNumberFormat[style-1] = formatAlias; |
56 | ucln_io_registerCleanup(UCLN_IO_LOCBUND, locbund_cleanup); | |
73c04bcf A |
57 | } |
58 | } | |
73c04bcf | 59 | /* Copy the needed formatter. */ |
57a6839d A |
60 | if (gPosixNumberFormat[style-1] != NULL) { |
61 | UErrorCode status = U_ZERO_ERROR; | |
62 | result->fNumberFormat[style-1] = unum_clone(gPosixNumberFormat[style-1], &status); | |
63 | } | |
73c04bcf A |
64 | } |
65 | return result->fNumberFormat[style-1]; | |
66 | } | |
67 | ||
4388f060 | 68 | U_CAPI ULocaleBundle * |
374ca955 A |
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 | } | |
73c04bcf | 79 | |
374ca955 A |
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 | } | |
73c04bcf A |
87 | |
88 | uprv_strcpy(result->fLocale, loc); | |
89 | ||
90 | result->isInvariantLocale = uprv_strcmp(result->fLocale, "en_US_POSIX") == 0; | |
91 | ||
374ca955 A |
92 | return result; |
93 | } | |
94 | ||
4388f060 | 95 | /*U_CAPI ULocaleBundle * |
374ca955 A |
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 | ||
4388f060 | 102 | U_CAPI ULocaleBundle * |
374ca955 A |
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 | ||
4388f060 | 140 | U_CAPI void |
374ca955 A |
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 | ||
4388f060 | 157 | U_CAPI UNumberFormat * |
374ca955 A |
158 | u_locbund_getNumberFormat(ULocaleBundle *bundle, UNumberFormatStyle style) |
159 | { | |
160 | UNumberFormat *formatAlias = NULL; | |
46f4442e | 161 | if (style > UNUM_IGNORE) { |
374ca955 A |
162 | formatAlias = bundle->fNumberFormat[style-1]; |
163 | if (formatAlias == NULL) { | |
73c04bcf A |
164 | if (bundle->isInvariantLocale) { |
165 | formatAlias = copyInvariantFormatter(bundle, style); | |
374ca955 A |
166 | } |
167 | else { | |
73c04bcf A |
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 | } | |
374ca955 A |
177 | } |
178 | } | |
179 | } | |
180 | return formatAlias; | |
181 | } | |
182 | ||
183 | #endif /* #if !UCONFIG_NO_FORMATTING */ |