]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/locale/lmonetary.c
zfs: merge openzfs/zfs@229b9f4ed
[FreeBSD/FreeBSD.git] / lib / libc / locale / lmonetary.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 <limits.h>
35 #include <stddef.h>
36 #include <stdlib.h>
37
38 #include "ldpart.h"
39 #include "lmonetary.h"
40
41 extern const char * __fix_locale_grouping_str(const char *);
42
43 #define LCMONETARY_SIZE_FULL (sizeof(struct lc_monetary_T) / sizeof(char *))
44 #define LCMONETARY_SIZE_MIN \
45                 (offsetof(struct lc_monetary_T, int_p_cs_precedes) / \
46                     sizeof(char *))
47
48 static char     empty[] = "";
49 static char     numempty[] = { CHAR_MAX, '\0'};
50
51 static const struct lc_monetary_T _C_monetary_locale = {
52         empty,          /* int_curr_symbol */
53         empty,          /* currency_symbol */
54         empty,          /* mon_decimal_point */
55         empty,          /* mon_thousands_sep */
56         numempty,       /* mon_grouping */
57         empty,          /* positive_sign */
58         empty,          /* negative_sign */
59         numempty,       /* int_frac_digits */
60         numempty,       /* frac_digits */
61         numempty,       /* p_cs_precedes */
62         numempty,       /* p_sep_by_space */
63         numempty,       /* n_cs_precedes */
64         numempty,       /* n_sep_by_space */
65         numempty,       /* p_sign_posn */
66         numempty,       /* n_sign_posn */
67         numempty,       /* int_p_cs_precedes */
68         numempty,       /* int_n_cs_precedes */
69         numempty,       /* int_p_sep_by_space */
70         numempty,       /* int_n_sep_by_space */
71         numempty,       /* int_p_sign_posn */
72         numempty        /* int_n_sign_posn */
73 };
74
75 struct xlocale_monetary __xlocale_global_monetary;
76
77 static char
78 cnv(const char *str)
79 {
80         int i = strtol(str, NULL, 10);
81
82         if (i == -1)
83                 i = CHAR_MAX;
84         return ((char)i);
85 }
86
87 static void
88 destruct_monetary(void *v)
89 {
90         struct xlocale_monetary *l = v;
91         if (l->buffer)
92                 free(l->buffer);
93         free(l);
94 }
95
96 static int
97 monetary_load_locale_l(struct xlocale_monetary *loc, int *using_locale,
98     int *changed, const char *name)
99 {
100         int ret;
101         struct lc_monetary_T *l = &loc->locale;
102
103         ret = __part_load_locale(name, using_locale,
104             &loc->buffer, "LC_MONETARY",
105             LCMONETARY_SIZE_FULL, LCMONETARY_SIZE_MIN,
106             (const char **)l);
107         if (ret == _LDP_LOADED) {
108                 l->mon_grouping =
109                      __fix_locale_grouping_str(l->mon_grouping);
110
111 #define M_ASSIGN_CHAR(NAME) (((char *)l->NAME)[0] = \
112                              cnv(l->NAME))
113
114                 M_ASSIGN_CHAR(int_frac_digits);
115                 M_ASSIGN_CHAR(frac_digits);
116                 M_ASSIGN_CHAR(p_cs_precedes);
117                 M_ASSIGN_CHAR(p_sep_by_space);
118                 M_ASSIGN_CHAR(n_cs_precedes);
119                 M_ASSIGN_CHAR(n_sep_by_space);
120                 M_ASSIGN_CHAR(p_sign_posn);
121                 M_ASSIGN_CHAR(n_sign_posn);
122
123                 /*
124                  * The six additional C99 international monetary formatting
125                  * parameters default to the national parameters when
126                  * reading FreeBSD LC_MONETARY data files.
127                  */
128 #define M_ASSIGN_ICHAR(NAME)                                            \
129                 do {                                                    \
130                         if (l->int_##NAME == NULL)      \
131                                 l->int_##NAME =         \
132                                     l->NAME;            \
133                         else                                            \
134                                 M_ASSIGN_CHAR(int_##NAME);              \
135                 } while (0)
136
137                 M_ASSIGN_ICHAR(p_cs_precedes);
138                 M_ASSIGN_ICHAR(n_cs_precedes);
139                 M_ASSIGN_ICHAR(p_sep_by_space);
140                 M_ASSIGN_ICHAR(n_sep_by_space);
141                 M_ASSIGN_ICHAR(p_sign_posn);
142                 M_ASSIGN_ICHAR(n_sign_posn);
143         }
144         if (ret != _LDP_ERROR)
145                 atomic_store_rel_int(changed, 1);
146         return (ret);
147 }
148
149 int
150 __monetary_load_locale(const char *name)
151 {
152         return (monetary_load_locale_l(&__xlocale_global_monetary,
153             &__xlocale_global_locale.using_monetary_locale,
154             &__xlocale_global_locale.monetary_locale_changed, name));
155 }
156
157 void *
158 __monetary_load(const char *name, locale_t l)
159 {
160         struct xlocale_monetary *new = calloc(sizeof(struct xlocale_monetary),
161             1);
162         if (new == NULL)
163                 return (NULL);
164         new->header.header.destructor = destruct_monetary;
165         if (monetary_load_locale_l(new, &l->using_monetary_locale,
166             &l->monetary_locale_changed, name) == _LDP_ERROR) {
167                 xlocale_release(new);
168                 return (NULL);
169         }
170         return (new);
171 }
172
173 struct lc_monetary_T *
174 __get_current_monetary_locale(locale_t loc)
175 {
176         return (loc->using_monetary_locale ?
177             &((struct xlocale_monetary*)loc->components[XLC_MONETARY])->locale :
178             (struct lc_monetary_T *)&_C_monetary_locale);
179 }
180
181 #ifdef LOCALE_DEBUG
182 void
183 monetdebug(void) {
184 printf( "int_curr_symbol = %s\n"
185         "currency_symbol = %s\n"
186         "mon_decimal_point = %s\n"
187         "mon_thousands_sep = %s\n"
188         "mon_grouping = %s\n"
189         "positive_sign = %s\n"
190         "negative_sign = %s\n"
191         "int_frac_digits = %d\n"
192         "frac_digits = %d\n"
193         "p_cs_precedes = %d\n"
194         "p_sep_by_space = %d\n"
195         "n_cs_precedes = %d\n"
196         "n_sep_by_space = %d\n"
197         "p_sign_posn = %d\n"
198         "n_sign_posn = %d\n"
199         "int_p_cs_precedes = %d\n"
200         "int_p_sep_by_space = %d\n"
201         "int_n_cs_precedes = %d\n"
202         "int_n_sep_by_space = %d\n"
203         "int_p_sign_posn = %d\n"
204         "int_n_sign_posn = %d\n",
205         _monetary_locale.int_curr_symbol,
206         _monetary_locale.currency_symbol,
207         _monetary_locale.mon_decimal_point,
208         _monetary_locale.mon_thousands_sep,
209         _monetary_locale.mon_grouping,
210         _monetary_locale.positive_sign,
211         _monetary_locale.negative_sign,
212         _monetary_locale.int_frac_digits[0],
213         _monetary_locale.frac_digits[0],
214         _monetary_locale.p_cs_precedes[0],
215         _monetary_locale.p_sep_by_space[0],
216         _monetary_locale.n_cs_precedes[0],
217         _monetary_locale.n_sep_by_space[0],
218         _monetary_locale.p_sign_posn[0],
219         _monetary_locale.n_sign_posn[0],
220         _monetary_locale.int_p_cs_precedes[0],
221         _monetary_locale.int_p_sep_by_space[0],
222         _monetary_locale.int_n_cs_precedes[0],
223         _monetary_locale.int_n_sep_by_space[0],
224         _monetary_locale.int_p_sign_posn[0],
225         _monetary_locale.int_n_sign_posn[0]
226 );
227 }
228 #endif /* LOCALE_DEBUG */