2 *******************************************************************************
4 * Copyright (C) 1998-2012, 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 UNumberFormat
*gPosixNumberFormat
[ULOCALEBUNDLE_NUMBERFORMAT_COUNT
];
35 static UBool U_CALLCONV
locbund_cleanup(void) {
37 for (style
= 0; style
< ULOCALEBUNDLE_NUMBERFORMAT_COUNT
; style
++) {
38 unum_close(gPosixNumberFormat
[style
]);
39 gPosixNumberFormat
[style
] = NULL
;
46 static inline UNumberFormat
* copyInvariantFormatter(ULocaleBundle
*result
, UNumberFormatStyle style
) {
47 if (result
->fNumberFormat
[style
-1] == NULL
) {
48 UErrorCode status
= U_ZERO_ERROR
;
51 UMTX_CHECK(NULL
, gPosixNumberFormat
[style
-1] == NULL
, needsInit
);
53 UNumberFormat
*formatAlias
= unum_open(style
, NULL
, 0, "en_US_POSIX", NULL
, &status
);
55 /* Cache upon first request. */
56 if (U_SUCCESS(status
)) {
58 gPosixNumberFormat
[style
-1] = formatAlias
;
59 ucln_io_registerCleanup(UCLN_IO_LOCBUND
, locbund_cleanup
);
64 /* Copy the needed formatter. */
65 result
->fNumberFormat
[style
-1] = unum_clone(gPosixNumberFormat
[style
-1], &status
);
67 return result
->fNumberFormat
[style
-1];
70 U_CAPI ULocaleBundle
*
71 u_locbund_init(ULocaleBundle
*result
, const char *loc
)
79 loc
= uloc_getDefault();
82 uprv_memset(result
, 0, sizeof(ULocaleBundle
));
84 len
= (int32_t)strlen(loc
);
85 result
->fLocale
= (char*) uprv_malloc(len
+ 1);
86 if(result
->fLocale
== 0) {
90 uprv_strcpy(result
->fLocale
, loc
);
92 result
->isInvariantLocale
= uprv_strcmp(result
->fLocale
, "en_US_POSIX") == 0;
97 /*U_CAPI ULocaleBundle *
98 u_locbund_new(const char *loc)
100 ULocaleBundle *result = (ULocaleBundle*) uprv_malloc(sizeof(ULocaleBundle));
101 return u_locbund_init(result, loc);
104 U_CAPI ULocaleBundle *
105 u_locbund_clone(const ULocaleBundle *bundle)
107 ULocaleBundle *result = (ULocaleBundle*)uprv_malloc(sizeof(ULocaleBundle));
108 UErrorCode status = U_ZERO_ERROR;
114 result->fLocale = (char*) uprv_malloc(strlen(bundle->fLocale) + 1);
115 if(result->fLocale == 0) {
120 strcpy(result->fLocale, bundle->fLocale );
122 for (styleIdx = 0; styleIdx < ULOCALEBUNDLE_NUMBERFORMAT_COUNT; styleIdx++) {
123 status = U_ZERO_ERROR;
124 if (result->fNumberFormat[styleIdx]) {
125 result->fNumberFormat[styleIdx] = unum_clone(bundle->fNumberFormat[styleIdx], &status);
126 if (U_FAILURE(status)) {
127 result->fNumberFormat[styleIdx] = NULL;
131 result->fNumberFormat[styleIdx] = NULL;
134 result->fDateFormat = (bundle->fDateFormat == 0 ? 0 :
135 udat_clone(bundle->fDateFormat, &status));
136 result->fTimeFormat = (bundle->fTimeFormat == 0 ? 0 :
137 udat_clone(bundle->fTimeFormat, &status));
143 u_locbund_close(ULocaleBundle
*bundle
)
147 uprv_free(bundle
->fLocale
);
149 for (styleIdx
= 0; styleIdx
< ULOCALEBUNDLE_NUMBERFORMAT_COUNT
; styleIdx
++) {
150 if (bundle
->fNumberFormat
[styleIdx
]) {
151 unum_close(bundle
->fNumberFormat
[styleIdx
]);
155 uprv_memset(bundle
, 0, sizeof(ULocaleBundle
));
156 /* uprv_free(bundle);*/
159 U_CAPI UNumberFormat
*
160 u_locbund_getNumberFormat(ULocaleBundle
*bundle
, UNumberFormatStyle style
)
162 UNumberFormat
*formatAlias
= NULL
;
163 if (style
> UNUM_IGNORE
) {
164 formatAlias
= bundle
->fNumberFormat
[style
-1];
165 if (formatAlias
== NULL
) {
166 if (bundle
->isInvariantLocale
) {
167 formatAlias
= copyInvariantFormatter(bundle
, style
);
170 UErrorCode status
= U_ZERO_ERROR
;
171 formatAlias
= unum_open(style
, NULL
, 0, bundle
->fLocale
, NULL
, &status
);
172 if (U_FAILURE(status
)) {
173 unum_close(formatAlias
);
177 bundle
->fNumberFormat
[style
-1] = formatAlias
;
185 #endif /* #if !UCONFIG_NO_FORMATTING */