xref: /freebsd/lib/libc/locale/lmonetary.c (revision 814bd1ed438f7dfc5bedcb1f3e772a46fe7026bb)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2000, 2001 Alexey Zelkin <phantom@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Copyright (c) 2011 The FreeBSD Foundation
8  *
9  * Portions of this software were developed by David Chisnall
10  * under sponsorship from the FreeBSD Foundation.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <limits.h>
38 #include <stddef.h>
39 #include <stdlib.h>
40 
41 #include "ldpart.h"
42 #include "lmonetary.h"
43 
44 extern const char * __fix_locale_grouping_str(const char *);
45 
46 #define LCMONETARY_SIZE_FULL (sizeof(struct lc_monetary_T) / sizeof(char *))
47 #define LCMONETARY_SIZE_MIN \
48 		(offsetof(struct lc_monetary_T, int_p_cs_precedes) / \
49 		    sizeof(char *))
50 
51 static char	empty[] = "";
52 static char	numempty[] = { CHAR_MAX, '\0'};
53 
54 static const struct lc_monetary_T _C_monetary_locale = {
55 	empty,		/* int_curr_symbol */
56 	empty,		/* currency_symbol */
57 	empty,		/* mon_decimal_point */
58 	empty,		/* mon_thousands_sep */
59 	numempty,	/* mon_grouping */
60 	empty,		/* positive_sign */
61 	empty,		/* negative_sign */
62 	numempty,	/* int_frac_digits */
63 	numempty,	/* frac_digits */
64 	numempty,	/* p_cs_precedes */
65 	numempty,	/* p_sep_by_space */
66 	numempty,	/* n_cs_precedes */
67 	numempty,	/* n_sep_by_space */
68 	numempty,	/* p_sign_posn */
69 	numempty,	/* n_sign_posn */
70 	numempty,	/* int_p_cs_precedes */
71 	numempty,	/* int_n_cs_precedes */
72 	numempty,	/* int_p_sep_by_space */
73 	numempty,	/* int_n_sep_by_space */
74 	numempty,	/* int_p_sign_posn */
75 	numempty	/* int_n_sign_posn */
76 };
77 
78 struct xlocale_monetary __xlocale_global_monetary;
79 
80 static char
81 cnv(const char *str)
82 {
83 	int i = strtol(str, NULL, 10);
84 
85 	if (i == -1)
86 		i = CHAR_MAX;
87 	return ((char)i);
88 }
89 
90 static void
91 destruct_monetary(void *v)
92 {
93 	struct xlocale_monetary *l = v;
94 	if (l->buffer)
95 		free(l->buffer);
96 	free(l);
97 }
98 
99 static int
100 monetary_load_locale_l(struct xlocale_monetary *loc, int *using_locale,
101     int *changed, const char *name)
102 {
103 	int ret;
104 	struct lc_monetary_T *l = &loc->locale;
105 
106 	ret = __part_load_locale(name, using_locale,
107 	    &loc->buffer, "LC_MONETARY",
108 	    LCMONETARY_SIZE_FULL, LCMONETARY_SIZE_MIN,
109 	    (const char **)l);
110 	if (ret == _LDP_LOADED) {
111 		l->mon_grouping =
112 		     __fix_locale_grouping_str(l->mon_grouping);
113 
114 #define M_ASSIGN_CHAR(NAME) (((char *)l->NAME)[0] = \
115 			     cnv(l->NAME))
116 
117 		M_ASSIGN_CHAR(int_frac_digits);
118 		M_ASSIGN_CHAR(frac_digits);
119 		M_ASSIGN_CHAR(p_cs_precedes);
120 		M_ASSIGN_CHAR(p_sep_by_space);
121 		M_ASSIGN_CHAR(n_cs_precedes);
122 		M_ASSIGN_CHAR(n_sep_by_space);
123 		M_ASSIGN_CHAR(p_sign_posn);
124 		M_ASSIGN_CHAR(n_sign_posn);
125 
126 		/*
127 		 * The six additional C99 international monetary formatting
128 		 * parameters default to the national parameters when
129 		 * reading FreeBSD LC_MONETARY data files.
130 		 */
131 #define	M_ASSIGN_ICHAR(NAME)						\
132 		do {							\
133 			if (l->int_##NAME == NULL)	\
134 				l->int_##NAME =		\
135 				    l->NAME;		\
136 			else						\
137 				M_ASSIGN_CHAR(int_##NAME);		\
138 		} while (0)
139 
140 		M_ASSIGN_ICHAR(p_cs_precedes);
141 		M_ASSIGN_ICHAR(n_cs_precedes);
142 		M_ASSIGN_ICHAR(p_sep_by_space);
143 		M_ASSIGN_ICHAR(n_sep_by_space);
144 		M_ASSIGN_ICHAR(p_sign_posn);
145 		M_ASSIGN_ICHAR(n_sign_posn);
146 	}
147 	if (ret != _LDP_ERROR)
148 		atomic_store_rel_int(changed, 1);
149 	return (ret);
150 }
151 
152 int
153 __monetary_load_locale(const char *name)
154 {
155 	return (monetary_load_locale_l(&__xlocale_global_monetary,
156 	    &__xlocale_global_locale.using_monetary_locale,
157 	    &__xlocale_global_locale.monetary_locale_changed, name));
158 }
159 
160 void *
161 __monetary_load(const char *name, locale_t l)
162 {
163 	struct xlocale_monetary *new = calloc(sizeof(struct xlocale_monetary),
164 	    1);
165 	if (new == NULL)
166 		return (NULL);
167 	new->header.header.destructor = destruct_monetary;
168 	if (monetary_load_locale_l(new, &l->using_monetary_locale,
169 	    &l->monetary_locale_changed, name) == _LDP_ERROR) {
170 		xlocale_release(new);
171 		return (NULL);
172 	}
173 	return (new);
174 }
175 
176 struct lc_monetary_T *
177 __get_current_monetary_locale(locale_t loc)
178 {
179 	return (loc->using_monetary_locale ?
180 	    &((struct xlocale_monetary*)loc->components[XLC_MONETARY])->locale :
181 	    (struct lc_monetary_T *)&_C_monetary_locale);
182 }
183 
184 #ifdef LOCALE_DEBUG
185 void
186 monetdebug(void) {
187 printf(	"int_curr_symbol = %s\n"
188 	"currency_symbol = %s\n"
189 	"mon_decimal_point = %s\n"
190 	"mon_thousands_sep = %s\n"
191 	"mon_grouping = %s\n"
192 	"positive_sign = %s\n"
193 	"negative_sign = %s\n"
194 	"int_frac_digits = %d\n"
195 	"frac_digits = %d\n"
196 	"p_cs_precedes = %d\n"
197 	"p_sep_by_space = %d\n"
198 	"n_cs_precedes = %d\n"
199 	"n_sep_by_space = %d\n"
200 	"p_sign_posn = %d\n"
201 	"n_sign_posn = %d\n"
202 	"int_p_cs_precedes = %d\n"
203 	"int_p_sep_by_space = %d\n"
204 	"int_n_cs_precedes = %d\n"
205 	"int_n_sep_by_space = %d\n"
206 	"int_p_sign_posn = %d\n"
207 	"int_n_sign_posn = %d\n",
208 	_monetary_locale.int_curr_symbol,
209 	_monetary_locale.currency_symbol,
210 	_monetary_locale.mon_decimal_point,
211 	_monetary_locale.mon_thousands_sep,
212 	_monetary_locale.mon_grouping,
213 	_monetary_locale.positive_sign,
214 	_monetary_locale.negative_sign,
215 	_monetary_locale.int_frac_digits[0],
216 	_monetary_locale.frac_digits[0],
217 	_monetary_locale.p_cs_precedes[0],
218 	_monetary_locale.p_sep_by_space[0],
219 	_monetary_locale.n_cs_precedes[0],
220 	_monetary_locale.n_sep_by_space[0],
221 	_monetary_locale.p_sign_posn[0],
222 	_monetary_locale.n_sign_posn[0],
223 	_monetary_locale.int_p_cs_precedes[0],
224 	_monetary_locale.int_p_sep_by_space[0],
225 	_monetary_locale.int_n_cs_precedes[0],
226 	_monetary_locale.int_n_sep_by_space[0],
227 	_monetary_locale.int_p_sign_posn[0],
228 	_monetary_locale.int_n_sign_posn[0]
229 );
230 }
231 #endif /* LOCALE_DEBUG */
232