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