2 *******************************************************************************
4 * Copyright (C) 1998-2007, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
11 * Modification History:
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 *******************************************************************************
19 #include "unicode/utypes.h"
21 #if !UCONFIG_NO_FORMATTING
29 #include "unicode/ustring.h"
30 #include "unicode/uloc.h"
32 static UBool isFormatsInitialized
= FALSE
;
33 static UNumberFormat
*gPosixNumberFormat
[ULOCALEBUNDLE_NUMBERFORMAT_COUNT
];
36 static UBool U_CALLCONV
locbund_cleanup(void) {
38 for (style
= 0; style
< ULOCALEBUNDLE_NUMBERFORMAT_COUNT
; style
++) {
39 unum_close(gPosixNumberFormat
[style
]);
40 gPosixNumberFormat
[style
] = NULL
;
42 isFormatsInitialized
= FALSE
;
48 static U_INLINE UNumberFormat
* copyInvariantFormatter(ULocaleBundle
*result
, UNumberFormatStyle style
) {
49 if (result
->fNumberFormat
[style
-1] == NULL
) {
50 UErrorCode status
= U_ZERO_ERROR
;
53 UMTX_CHECK(NULL
, gPosixNumberFormat
[style
-1] == NULL
, needsInit
);
55 UNumberFormat
*formatAlias
= unum_open(style
, NULL
, 0, "en_US_POSIX", NULL
, &status
);
57 /* Cache upon first request. */
58 if (U_SUCCESS(status
)) {
60 gPosixNumberFormat
[style
-1] = formatAlias
;
61 ucln_io_registerCleanup(UCLN_IO_LOCBUND
, locbund_cleanup
);
66 /* Copy the needed formatter. */
67 result
->fNumberFormat
[style
-1] = unum_clone(gPosixNumberFormat
[style
-1], &status
);
69 return result
->fNumberFormat
[style
-1];
73 u_locbund_init(ULocaleBundle
*result
, const char *loc
)
81 loc
= uloc_getDefault();
84 uprv_memset(result
, 0, sizeof(ULocaleBundle
));
86 len
= (int32_t)strlen(loc
);
87 result
->fLocale
= (char*) uprv_malloc(len
+ 1);
88 if(result
->fLocale
== 0) {
92 uprv_strcpy(result
->fLocale
, loc
);
94 result
->isInvariantLocale
= uprv_strcmp(result
->fLocale
, "en_US_POSIX") == 0;
100 u_locbund_new(const char *loc)
102 ULocaleBundle *result = (ULocaleBundle*) uprv_malloc(sizeof(ULocaleBundle));
103 return u_locbund_init(result, loc);
107 u_locbund_clone(const ULocaleBundle *bundle)
109 ULocaleBundle *result = (ULocaleBundle*)uprv_malloc(sizeof(ULocaleBundle));
110 UErrorCode status = U_ZERO_ERROR;
116 result->fLocale = (char*) uprv_malloc(strlen(bundle->fLocale) + 1);
117 if(result->fLocale == 0) {
122 strcpy(result->fLocale, bundle->fLocale );
124 for (styleIdx = 0; styleIdx < ULOCALEBUNDLE_NUMBERFORMAT_COUNT; styleIdx++) {
125 status = U_ZERO_ERROR;
126 if (result->fNumberFormat[styleIdx]) {
127 result->fNumberFormat[styleIdx] = unum_clone(bundle->fNumberFormat[styleIdx], &status);
128 if (U_FAILURE(status)) {
129 result->fNumberFormat[styleIdx] = NULL;
133 result->fNumberFormat[styleIdx] = NULL;
136 result->fDateFormat = (bundle->fDateFormat == 0 ? 0 :
137 udat_clone(bundle->fDateFormat, &status));
138 result->fTimeFormat = (bundle->fTimeFormat == 0 ? 0 :
139 udat_clone(bundle->fTimeFormat, &status));
145 u_locbund_close(ULocaleBundle
*bundle
)
149 uprv_free(bundle
->fLocale
);
151 for (styleIdx
= 0; styleIdx
< ULOCALEBUNDLE_NUMBERFORMAT_COUNT
; styleIdx
++) {
152 if (bundle
->fNumberFormat
[styleIdx
]) {
153 unum_close(bundle
->fNumberFormat
[styleIdx
]);
157 uprv_memset(bundle
, 0, sizeof(ULocaleBundle
));
158 /* uprv_free(bundle);*/
162 u_locbund_getNumberFormat(ULocaleBundle
*bundle
, UNumberFormatStyle style
)
164 UNumberFormat
*formatAlias
= NULL
;
165 if (style
> UNUM_IGNORE
) {
166 formatAlias
= bundle
->fNumberFormat
[style
-1];
167 if (formatAlias
== NULL
) {
168 if (bundle
->isInvariantLocale
) {
169 formatAlias
= copyInvariantFormatter(bundle
, style
);
172 UErrorCode status
= U_ZERO_ERROR
;
173 formatAlias
= unum_open(style
, NULL
, 0, bundle
->fLocale
, NULL
, &status
);
174 if (U_FAILURE(status
)) {
175 unum_close(formatAlias
);
179 bundle
->fNumberFormat
[style
-1] = formatAlias
;
187 #endif /* #if !UCONFIG_NO_FORMATTING */