Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / intl / icu / source / io / locbund.cpp
blobddf81679d11ceebd468bfd577411f29f7bc4350a
1 /*
2 *******************************************************************************
4 * Copyright (C) 1998-2013, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
9 * File locbund.cpp
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
23 #include "locbund.h"
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"
33 static UNumberFormat *gPosixNumberFormat[ULOCALEBUNDLE_NUMBERFORMAT_COUNT];
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;
42 return TRUE;
44 U_CDECL_END
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);
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);
65 return result->fNumberFormat[style-1];
68 U_CAPI ULocaleBundle *
69 u_locbund_init(ULocaleBundle *result, const char *loc)
71 int32_t len;
73 if(result == 0)
74 return 0;
76 if (loc == NULL) {
77 loc = uloc_getDefault();
80 uprv_memset(result, 0, sizeof(ULocaleBundle));
82 len = (int32_t)strlen(loc);
83 result->fLocale = (char*) uprv_malloc(len + 1);
84 if(result->fLocale == 0) {
85 return 0;
88 uprv_strcpy(result->fLocale, loc);
90 result->isInvariantLocale = uprv_strcmp(result->fLocale, "en_US_POSIX") == 0;
92 return result;
95 /*U_CAPI ULocaleBundle *
96 u_locbund_new(const char *loc)
98 ULocaleBundle *result = (ULocaleBundle*) uprv_malloc(sizeof(ULocaleBundle));
99 return u_locbund_init(result, loc);
102 U_CAPI ULocaleBundle *
103 u_locbund_clone(const ULocaleBundle *bundle)
105 ULocaleBundle *result = (ULocaleBundle*)uprv_malloc(sizeof(ULocaleBundle));
106 UErrorCode status = U_ZERO_ERROR;
107 int32_t styleIdx;
109 if(result == 0)
110 return 0;
112 result->fLocale = (char*) uprv_malloc(strlen(bundle->fLocale) + 1);
113 if(result->fLocale == 0) {
114 uprv_free(result);
115 return 0;
118 strcpy(result->fLocale, bundle->fLocale );
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;
128 else {
129 result->fNumberFormat[styleIdx] = NULL;
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));
137 return result;
140 U_CAPI void
141 u_locbund_close(ULocaleBundle *bundle)
143 int32_t styleIdx;
145 uprv_free(bundle->fLocale);
147 for (styleIdx = 0; styleIdx < ULOCALEBUNDLE_NUMBERFORMAT_COUNT; styleIdx++) {
148 if (bundle->fNumberFormat[styleIdx]) {
149 unum_close(bundle->fNumberFormat[styleIdx]);
153 uprv_memset(bundle, 0, sizeof(ULocaleBundle));
154 /* uprv_free(bundle);*/
157 U_CAPI UNumberFormat *
158 u_locbund_getNumberFormat(ULocaleBundle *bundle, UNumberFormatStyle style)
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);
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;
174 else {
175 bundle->fNumberFormat[style-1] = formatAlias;
180 return formatAlias;
183 #endif /* #if !UCONFIG_NO_FORMATTING */