]> git.saurik.com Git - apple/icu.git/blame - icuSources/io/locbund.cpp
ICU-66108.tar.gz
[apple/icu.git] / icuSources / io / locbund.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
374ca955
A
3/*
4*******************************************************************************
5*
b331163b 6* Copyright (C) 1998-2014, International Business Machines
374ca955
A
7* Corporation and others. All Rights Reserved.
8*
9*******************************************************************************
10*
4388f060 11* File locbund.cpp
374ca955
A
12*
13* Modification History:
14*
15* Date Name Description
16* 11/18/98 stephen Creation.
73c04bcf 17* 12/10/1999 bobbyr(at)optiosoftware.com Fix for memory leak + string allocation bugs
374ca955
A
18*******************************************************************************
19*/
20
374ca955
A
21#include "unicode/utypes.h"
22
b331163b 23#if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_CONVERSION
374ca955
A
24
25#include "locbund.h"
26
27#include "cmemory.h"
73c04bcf
A
28#include "cstring.h"
29#include "ucln_io.h"
57a6839d 30#include "mutex.h"
73c04bcf 31#include "umutex.h"
374ca955
A
32#include "unicode/ustring.h"
33#include "unicode/uloc.h"
34
73c04bcf
A
35static UNumberFormat *gPosixNumberFormat[ULOCALEBUNDLE_NUMBERFORMAT_COUNT];
36
37U_CDECL_BEGIN
38static UBool U_CALLCONV locbund_cleanup(void) {
39 int32_t style;
40 for (style = 0; style < ULOCALEBUNDLE_NUMBERFORMAT_COUNT; style++) {
41 unum_close(gPosixNumberFormat[style]);
42 gPosixNumberFormat[style] = NULL;
43 }
73c04bcf
A
44 return TRUE;
45}
46U_CDECL_END
47
4388f060 48static inline UNumberFormat * copyInvariantFormatter(ULocaleBundle *result, UNumberFormatStyle style) {
57a6839d 49 U_NAMESPACE_USE
340931cb
A
50 static UMutex gLock;
51 Mutex lock(&gLock);
73c04bcf 52 if (result->fNumberFormat[style-1] == NULL) {
57a6839d
A
53 if (gPosixNumberFormat[style-1] == NULL) {
54 UErrorCode status = U_ZERO_ERROR;
73c04bcf 55 UNumberFormat *formatAlias = unum_open(style, NULL, 0, "en_US_POSIX", NULL, &status);
73c04bcf 56 if (U_SUCCESS(status)) {
73c04bcf
A
57 gPosixNumberFormat[style-1] = formatAlias;
58 ucln_io_registerCleanup(UCLN_IO_LOCBUND, locbund_cleanup);
73c04bcf
A
59 }
60 }
73c04bcf 61 /* Copy the needed formatter. */
57a6839d
A
62 if (gPosixNumberFormat[style-1] != NULL) {
63 UErrorCode status = U_ZERO_ERROR;
64 result->fNumberFormat[style-1] = unum_clone(gPosixNumberFormat[style-1], &status);
65 }
73c04bcf
A
66 }
67 return result->fNumberFormat[style-1];
68}
69
4388f060 70U_CAPI ULocaleBundle *
374ca955
A
71u_locbund_init(ULocaleBundle *result, const char *loc)
72{
73 int32_t len;
74
75 if(result == 0)
76 return 0;
77
78 if (loc == NULL) {
79 loc = uloc_getDefault();
80 }
73c04bcf 81
374ca955
A
82 uprv_memset(result, 0, sizeof(ULocaleBundle));
83
84 len = (int32_t)strlen(loc);
85 result->fLocale = (char*) uprv_malloc(len + 1);
86 if(result->fLocale == 0) {
87 return 0;
88 }
73c04bcf
A
89
90 uprv_strcpy(result->fLocale, loc);
91
92 result->isInvariantLocale = uprv_strcmp(result->fLocale, "en_US_POSIX") == 0;
93
374ca955
A
94 return result;
95}
96
4388f060 97/*U_CAPI ULocaleBundle *
374ca955
A
98u_locbund_new(const char *loc)
99{
100 ULocaleBundle *result = (ULocaleBundle*) uprv_malloc(sizeof(ULocaleBundle));
101 return u_locbund_init(result, loc);
102}
103
4388f060 104U_CAPI ULocaleBundle *
374ca955
A
105u_locbund_clone(const ULocaleBundle *bundle)
106{
107 ULocaleBundle *result = (ULocaleBundle*)uprv_malloc(sizeof(ULocaleBundle));
108 UErrorCode status = U_ZERO_ERROR;
109 int32_t styleIdx;
110
111 if(result == 0)
112 return 0;
113
114 result->fLocale = (char*) uprv_malloc(strlen(bundle->fLocale) + 1);
115 if(result->fLocale == 0) {
116 uprv_free(result);
117 return 0;
118 }
119
120 strcpy(result->fLocale, bundle->fLocale );
121
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;
128 }
129 }
130 else {
131 result->fNumberFormat[styleIdx] = NULL;
132 }
133 }
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));
138
139 return result;
140}*/
141
4388f060 142U_CAPI void
374ca955
A
143u_locbund_close(ULocaleBundle *bundle)
144{
145 int32_t styleIdx;
146
147 uprv_free(bundle->fLocale);
148
149 for (styleIdx = 0; styleIdx < ULOCALEBUNDLE_NUMBERFORMAT_COUNT; styleIdx++) {
150 if (bundle->fNumberFormat[styleIdx]) {
151 unum_close(bundle->fNumberFormat[styleIdx]);
152 }
153 }
154
155 uprv_memset(bundle, 0, sizeof(ULocaleBundle));
156/* uprv_free(bundle);*/
157}
158
4388f060 159U_CAPI UNumberFormat *
374ca955
A
160u_locbund_getNumberFormat(ULocaleBundle *bundle, UNumberFormatStyle style)
161{
162 UNumberFormat *formatAlias = NULL;
46f4442e 163 if (style > UNUM_IGNORE) {
374ca955
A
164 formatAlias = bundle->fNumberFormat[style-1];
165 if (formatAlias == NULL) {
73c04bcf
A
166 if (bundle->isInvariantLocale) {
167 formatAlias = copyInvariantFormatter(bundle, style);
374ca955
A
168 }
169 else {
73c04bcf
A
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);
174 formatAlias = NULL;
175 }
176 else {
177 bundle->fNumberFormat[style-1] = formatAlias;
178 }
374ca955
A
179 }
180 }
181 }
182 return formatAlias;
183}
184
185#endif /* #if !UCONFIG_NO_FORMATTING */