• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 *******************************************************************************
3 *
4 *   Copyright (C) 1998-2007, International Business Machines
5 *   Corporation and others.  All Rights Reserved.
6 *
7 *******************************************************************************
8 *
9 * File locbund.c
10 *
11 * Modification History:
12 *
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 *******************************************************************************
17 */
18 
19 #include "unicode/utypes.h"
20 
21 #if !UCONFIG_NO_FORMATTING
22 
23 #include "locbund.h"
24 
25 #include "cmemory.h"
26 #include "cstring.h"
27 #include "ucln_io.h"
28 #include "umutex.h"
29 #include "unicode/ustring.h"
30 #include "unicode/uloc.h"
31 
32 static UBool isFormatsInitialized = FALSE;
33 static UNumberFormat *gPosixNumberFormat[ULOCALEBUNDLE_NUMBERFORMAT_COUNT];
34 
35 U_CDECL_BEGIN
locbund_cleanup(void)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     }
42     isFormatsInitialized = FALSE;
43     return TRUE;
44 }
45 U_CDECL_END
46 
47 
copyInvariantFormatter(ULocaleBundle * result,UNumberFormatStyle style)48 static U_INLINE UNumberFormat * copyInvariantFormatter(ULocaleBundle *result, UNumberFormatStyle style) {
49     if (result->fNumberFormat[style-1] == NULL) {
50         UErrorCode status = U_ZERO_ERROR;
51         UBool needsInit;
52 
53         UMTX_CHECK(NULL, gPosixNumberFormat[style-1] == NULL, needsInit);
54         if (needsInit) {
55             UNumberFormat *formatAlias = unum_open(style, NULL, 0, "en_US_POSIX", NULL, &status);
56 
57             /* Cache upon first request. */
58             if (U_SUCCESS(status)) {
59                 umtx_lock(NULL);
60                 gPosixNumberFormat[style-1] = formatAlias;
61                 ucln_io_registerCleanup(UCLN_IO_LOCBUND, locbund_cleanup);
62                 umtx_unlock(NULL);
63             }
64         }
65 
66         /* Copy the needed formatter. */
67         result->fNumberFormat[style-1] = unum_clone(gPosixNumberFormat[style-1], &status);
68     }
69     return result->fNumberFormat[style-1];
70 }
71 
72 ULocaleBundle*
u_locbund_init(ULocaleBundle * result,const char * loc)73 u_locbund_init(ULocaleBundle *result, const char *loc)
74 {
75     int32_t len;
76 
77     if(result == 0)
78         return 0;
79 
80     if (loc == NULL) {
81         loc = uloc_getDefault();
82     }
83 
84     uprv_memset(result, 0, sizeof(ULocaleBundle));
85 
86     len = (int32_t)strlen(loc);
87     result->fLocale = (char*) uprv_malloc(len + 1);
88     if(result->fLocale == 0) {
89         return 0;
90     }
91 
92     uprv_strcpy(result->fLocale, loc);
93 
94     result->isInvariantLocale = uprv_strcmp(result->fLocale, "en_US_POSIX") == 0;
95 
96     return result;
97 }
98 
99 /*ULocaleBundle*
100 u_locbund_new(const char *loc)
101 {
102     ULocaleBundle *result = (ULocaleBundle*) uprv_malloc(sizeof(ULocaleBundle));
103     return u_locbund_init(result, loc);
104 }
105 
106 ULocaleBundle*
107 u_locbund_clone(const ULocaleBundle *bundle)
108 {
109     ULocaleBundle *result = (ULocaleBundle*)uprv_malloc(sizeof(ULocaleBundle));
110     UErrorCode status = U_ZERO_ERROR;
111     int32_t styleIdx;
112 
113     if(result == 0)
114         return 0;
115 
116     result->fLocale = (char*) uprv_malloc(strlen(bundle->fLocale) + 1);
117     if(result->fLocale == 0) {
118         uprv_free(result);
119         return 0;
120     }
121 
122     strcpy(result->fLocale, bundle->fLocale );
123 
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;
130             }
131         }
132         else {
133             result->fNumberFormat[styleIdx] = NULL;
134         }
135     }
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));
140 
141     return result;
142 }*/
143 
144 void
u_locbund_close(ULocaleBundle * bundle)145 u_locbund_close(ULocaleBundle *bundle)
146 {
147     int32_t styleIdx;
148 
149     uprv_free(bundle->fLocale);
150 
151     for (styleIdx = 0; styleIdx < ULOCALEBUNDLE_NUMBERFORMAT_COUNT; styleIdx++) {
152         if (bundle->fNumberFormat[styleIdx]) {
153             unum_close(bundle->fNumberFormat[styleIdx]);
154         }
155     }
156 
157     uprv_memset(bundle, 0, sizeof(ULocaleBundle));
158 /*    uprv_free(bundle);*/
159 }
160 
161 UNumberFormat*
u_locbund_getNumberFormat(ULocaleBundle * bundle,UNumberFormatStyle style)162 u_locbund_getNumberFormat(ULocaleBundle *bundle, UNumberFormatStyle style)
163 {
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);
170             }
171             else {
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);
176                     formatAlias = NULL;
177                 }
178                 else {
179                     bundle->fNumberFormat[style-1] = formatAlias;
180                 }
181             }
182         }
183     }
184     return formatAlias;
185 }
186 
187 #endif /* #if !UCONFIG_NO_FORMATTING */
188