]> git.saurik.com Git - apple/libc.git/blob - locale/FreeBSD/lmonetary.c.patch
Libc-498.tar.gz
[apple/libc.git] / locale / FreeBSD / lmonetary.c.patch
1 --- lmonetary.c.orig 2004-11-25 11:38:17.000000000 -0800
2 +++ lmonetary.c 2005-03-16 23:05:30.000000000 -0800
3 @@ -27,14 +27,16 @@
4 #include <sys/cdefs.h>
5 __FBSDID("$FreeBSD: src/lib/libc/locale/lmonetary.c,v 1.19 2003/06/26 10:46:16 phantom Exp $");
6
7 +#include "xlocale_private.h"
8 +
9 #include <limits.h>
10 #include <stddef.h>
11 #include <stdlib.h>
12 +#include <string.h>
13
14 #include "ldpart.h"
15 #include "lmonetary.h"
16
17 -extern int __mlocale_changed;
18 extern const char * __fix_locale_grouping_str(const char *);
19
20 #define LCMONETARY_SIZE_FULL (sizeof(struct lc_monetary_T) / sizeof(char *))
21 @@ -50,7 +52,7 @@
22 empty, /* currency_symbol */
23 empty, /* mon_decimal_point */
24 empty, /* mon_thousands_sep */
25 - numempty, /* mon_grouping */
26 + empty, /* mon_grouping [C99 7.11.2.1]*/
27 empty, /* positive_sign */
28 empty, /* negative_sign */
29 numempty, /* int_frac_digits */
30 @@ -69,10 +71,6 @@
31 numempty /* int_n_sign_posn */
32 };
33
34 -static struct lc_monetary_T _monetary_locale;
35 -static int _monetary_using_locale;
36 -static char *_monetary_locale_buf;
37 -
38 static char
39 cnv(const char *str)
40 {
41 @@ -83,23 +81,57 @@
42 return ((char)i);
43 }
44
45 -int
46 -__monetary_load_locale(const char *name)
47 +__private_extern__ int
48 +__monetary_load_locale(const char *name, locale_t loc)
49 {
50 int ret;
51 + struct __xlocale_st_monetary *xp;
52 + static struct __xlocale_st_monetary *cache = NULL;
53 +
54 + /* 'name' must be already checked. */
55 + if (strcmp(name, "C") == 0 || strcmp(name, "POSIX") == 0) {
56 + if (!loc->_monetary_using_locale)
57 + return (_LDP_CACHE);
58 + loc->_monetary_using_locale = 0;
59 + XL_RELEASE(loc->__lc_monetary);
60 + loc->__lc_monetary = NULL;
61 + loc->__mlocale_changed = 1;
62 + return (_LDP_CACHE);
63 + }
64 +
65 + if (loc->_monetary_using_locale && strcmp(name, loc->__lc_monetary->_monetary_locale_buf) == 0)
66 + return (_LDP_CACHE);
67 + /*
68 + * If the locale name is the same as our cache, use the cache.
69 + */
70 + if (cache && cache->_monetary_locale_buf && strcmp(name, cache->_monetary_locale_buf) == 0) {
71 + loc->_monetary_using_locale = 1;
72 + XL_RELEASE(loc->__lc_monetary);
73 + loc->__lc_monetary = cache;
74 + XL_RETAIN(loc->__lc_monetary);
75 + loc->__mlocale_changed = 1;
76 + return (_LDP_CACHE);
77 + }
78 + if ((xp = (struct __xlocale_st_monetary *)malloc(sizeof(*xp))) == NULL)
79 + return _LDP_ERROR;
80 + xp->__refcount = 1;
81 + xp->__free_extra = (__free_extra_t)__ldpart_free_extra;
82 + xp->_monetary_locale_buf = NULL;
83
84 - ret = __part_load_locale(name, &_monetary_using_locale,
85 - &_monetary_locale_buf, "LC_MONETARY",
86 + ret = __part_load_locale(name, &loc->_monetary_using_locale,
87 + &xp->_monetary_locale_buf, "LC_MONETARY",
88 LCMONETARY_SIZE_FULL, LCMONETARY_SIZE_MIN,
89 - (const char **)&_monetary_locale);
90 + (const char **)&xp->_monetary_locale);
91 if (ret != _LDP_ERROR)
92 - __mlocale_changed = 1;
93 + loc->__mlocale_changed = 1;
94 + else
95 + free(xp);
96 if (ret == _LDP_LOADED) {
97 - _monetary_locale.mon_grouping =
98 - __fix_locale_grouping_str(_monetary_locale.mon_grouping);
99 + xp->_monetary_locale.mon_grouping =
100 + __fix_locale_grouping_str(xp->_monetary_locale.mon_grouping);
101
102 -#define M_ASSIGN_CHAR(NAME) (((char *)_monetary_locale.NAME)[0] = \
103 - cnv(_monetary_locale.NAME))
104 +#define M_ASSIGN_CHAR(NAME) (((char *)xp->_monetary_locale.NAME)[0] = \
105 + cnv(xp->_monetary_locale.NAME))
106
107 M_ASSIGN_CHAR(int_frac_digits);
108 M_ASSIGN_CHAR(frac_digits);
109 @@ -117,9 +149,9 @@
110 */
111 #define M_ASSIGN_ICHAR(NAME) \
112 do { \
113 - if (_monetary_locale.int_##NAME == NULL) \
114 - _monetary_locale.int_##NAME = \
115 - _monetary_locale.NAME; \
116 + if (xp->_monetary_locale.int_##NAME == NULL) \
117 + xp->_monetary_locale.int_##NAME = \
118 + xp->_monetary_locale.NAME; \
119 else \
120 M_ASSIGN_CHAR(int_##NAME); \
121 } while (0)
122 @@ -130,21 +162,27 @@
123 M_ASSIGN_ICHAR(n_sep_by_space);
124 M_ASSIGN_ICHAR(p_sign_posn);
125 M_ASSIGN_ICHAR(n_sign_posn);
126 + XL_RELEASE(loc->__lc_monetary);
127 + loc->__lc_monetary = xp;
128 + XL_RELEASE(cache);
129 + cache = xp;
130 + XL_RETAIN(cache);
131 }
132 return (ret);
133 }
134
135 -struct lc_monetary_T *
136 -__get_current_monetary_locale(void)
137 +__private_extern__ struct lc_monetary_T *
138 +__get_current_monetary_locale(locale_t loc)
139 {
140 - return (_monetary_using_locale
141 - ? &_monetary_locale
142 + return (loc->_monetary_using_locale
143 + ? &loc->__lc_monetary->_monetary_locale
144 : (struct lc_monetary_T *)&_C_monetary_locale);
145 }
146
147 #ifdef LOCALE_DEBUG
148 void
149 monetdebug() {
150 +locale_t loc = __current_locale();
151 printf( "int_curr_symbol = %s\n"
152 "currency_symbol = %s\n"
153 "mon_decimal_point = %s\n"
154 @@ -166,27 +204,27 @@
155 "int_n_sep_by_space = %d\n"
156 "int_p_sign_posn = %d\n"
157 "int_n_sign_posn = %d\n",
158 - _monetary_locale.int_curr_symbol,
159 - _monetary_locale.currency_symbol,
160 - _monetary_locale.mon_decimal_point,
161 - _monetary_locale.mon_thousands_sep,
162 - _monetary_locale.mon_grouping,
163 - _monetary_locale.positive_sign,
164 - _monetary_locale.negative_sign,
165 - _monetary_locale.int_frac_digits[0],
166 - _monetary_locale.frac_digits[0],
167 - _monetary_locale.p_cs_precedes[0],
168 - _monetary_locale.p_sep_by_space[0],
169 - _monetary_locale.n_cs_precedes[0],
170 - _monetary_locale.n_sep_by_space[0],
171 - _monetary_locale.p_sign_posn[0],
172 - _monetary_locale.n_sign_posn[0],
173 - _monetary_locale.int_p_cs_precedes[0],
174 - _monetary_locale.int_p_sep_by_space[0],
175 - _monetary_locale.int_n_cs_precedes[0],
176 - _monetary_locale.int_n_sep_by_space[0],
177 - _monetary_locale.int_p_sign_posn[0],
178 - _monetary_locale.int_n_sign_posn[0]
179 + loc->__lc_monetary->_monetary_locale.int_curr_symbol,
180 + loc->__lc_monetary->_monetary_locale.currency_symbol,
181 + loc->__lc_monetary->_monetary_locale.mon_decimal_point,
182 + loc->__lc_monetary->_monetary_locale.mon_thousands_sep,
183 + loc->__lc_monetary->_monetary_locale.mon_grouping,
184 + loc->__lc_monetary->_monetary_locale.positive_sign,
185 + loc->__lc_monetary->_monetary_locale.negative_sign,
186 + loc->__lc_monetary->_monetary_locale.int_frac_digits[0],
187 + loc->__lc_monetary->_monetary_locale.frac_digits[0],
188 + loc->__lc_monetary->_monetary_locale.p_cs_precedes[0],
189 + loc->__lc_monetary->_monetary_locale.p_sep_by_space[0],
190 + loc->__lc_monetary->_monetary_locale.n_cs_precedes[0],
191 + loc->__lc_monetary->_monetary_locale.n_sep_by_space[0],
192 + loc->__lc_monetary->_monetary_locale.p_sign_posn[0],
193 + loc->__lc_monetary->_monetary_locale.n_sign_posn[0],
194 + loc->__lc_monetary->_monetary_locale.int_p_cs_precedes[0],
195 + loc->__lc_monetary->_monetary_locale.int_p_sep_by_space[0],
196 + loc->__lc_monetary->_monetary_locale.int_n_cs_precedes[0],
197 + loc->__lc_monetary->_monetary_locale.int_n_sep_by_space[0],
198 + loc->__lc_monetary->_monetary_locale.int_p_sign_posn[0],
199 + loc->__lc_monetary->_monetary_locale.int_n_sign_posn[0]
200 );
201 }
202 #endif /* LOCALE_DEBUG */