lmonetary.c revision 104982
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 104982 2002-10-12 11:31:07Z ache $");
29
30#include <limits.h>
31#include <stddef.h>
32#include <stdlib.h>
33#include "lmonetary.h"
34#include "ldpart.h"
35
36extern int __mlocale_changed;
37extern const char * __fix_locale_grouping_str(const char *);
38
39#define LCMONETARY_SIZE_FULL (sizeof(struct lc_monetary_T) / sizeof(char *))
40#define LCMONETARY_SIZE_MIN \
41		(offsetof(struct lc_monetary_T, int_p_cs_precedes) / \
42		    sizeof(char *))
43
44static char	empty[] = "";
45static char	numempty[] = { CHAR_MAX, '\0'};
46
47static const struct lc_monetary_T _C_monetary_locale = {
48	empty,		/* int_curr_symbol */
49	empty,		/* currency_symbol */
50	empty,		/* mon_decimal_point */
51	empty,		/* mon_thousands_sep */
52	numempty,	/* mon_grouping */
53	empty,		/* positive_sign */
54	empty,		/* negative_sign */
55	numempty,	/* int_frac_digits */
56	numempty,	/* frac_digits */
57	numempty,	/* p_cs_precedes */
58	numempty,	/* p_sep_by_space */
59	numempty,	/* n_cs_precedes */
60	numempty,	/* n_sep_by_space */
61	numempty,	/* p_sign_posn */
62	numempty,	/* n_sign_posn */
63	numempty,	/* int_p_cs_precedes */
64	numempty,	/* int_n_cs_precedes */
65	numempty,	/* int_p_sep_by_space */
66	numempty,	/* int_n_sep_by_space */
67	numempty,	/* int_p_sign_posn */
68	numempty	/* int_n_sign_posn */
69};
70
71static struct lc_monetary_T _monetary_locale;
72static int	_monetary_using_locale;
73static char	*_monetary_locale_buf;
74
75static char
76cnv(const char *str)
77{
78	int i = strtol(str, NULL, 10);
79
80	if (i == -1)
81		i = CHAR_MAX;
82	return ((char)i);
83}
84
85int
86__monetary_load_locale(const char *name)
87{
88	int ret;
89
90	ret = __part_load_locale(name, &_monetary_using_locale,
91		_monetary_locale_buf, "LC_MONETARY",
92		LCMONETARY_SIZE_FULL, LCMONETARY_SIZE_MIN,
93		(const char **)&_monetary_locale);
94	if (ret != _LDP_ERROR)
95		__mlocale_changed = 1;
96	if (ret == _LDP_LOADED) {
97		_monetary_locale.mon_grouping =
98		     __fix_locale_grouping_str(_monetary_locale.mon_grouping);
99
100#define M_ASSIGN_CHAR(NAME) (((char *)_monetary_locale.NAME)[0] = \
101			     cnv(_monetary_locale.NAME))
102
103		M_ASSIGN_CHAR(int_frac_digits);
104		M_ASSIGN_CHAR(frac_digits);
105		M_ASSIGN_CHAR(p_cs_precedes);
106		M_ASSIGN_CHAR(p_sep_by_space);
107		M_ASSIGN_CHAR(n_cs_precedes);
108		M_ASSIGN_CHAR(n_sep_by_space);
109		M_ASSIGN_CHAR(p_sign_posn);
110		M_ASSIGN_CHAR(n_sign_posn);
111
112		/*
113		 * The six additional C99 international monetary formatting
114		 * parameters default to the national parameters when
115		 * reading FreeBSD 4 LC_MONETARY data files.
116		 */
117#define	M_ASSIGN_ICHAR(NAME)						\
118		do {							\
119			if (_monetary_locale.int_##NAME == NULL)	\
120				_monetary_locale.int_##NAME =		\
121				    _monetary_locale.NAME;		\
122			else						\
123				M_ASSIGN_CHAR(int_##NAME);		\
124		} while (0)
125
126		M_ASSIGN_ICHAR(p_cs_precedes);
127		M_ASSIGN_ICHAR(n_cs_precedes);
128		M_ASSIGN_ICHAR(p_sep_by_space);
129		M_ASSIGN_ICHAR(n_sep_by_space);
130		M_ASSIGN_ICHAR(p_sign_posn);
131		M_ASSIGN_ICHAR(n_sign_posn);
132	}
133	return (ret);
134}
135
136struct lc_monetary_T *
137__get_current_monetary_locale(void)
138{
139	return (_monetary_using_locale
140		? &_monetary_locale
141		: (struct lc_monetary_T *)&_C_monetary_locale);
142}
143
144#ifdef LOCALE_DEBUG
145void
146monetdebug() {
147printf(	"int_curr_symbol = %s\n"
148	"currency_symbol = %s\n"
149	"mon_decimal_point = %s\n"
150	"mon_thousands_sep = %s\n"
151	"mon_grouping = %s\n"
152	"positive_sign = %s\n"
153	"negative_sign = %s\n"
154	"int_frac_digits = %d\n"
155	"frac_digits = %d\n"
156	"p_cs_precedes = %d\n"
157	"p_sep_by_space = %d\n"
158	"n_cs_precedes = %d\n"
159	"n_sep_by_space = %d\n"
160	"p_sign_posn = %d\n"
161	"n_sign_posn = %d\n",
162	_monetary_locale.int_curr_symbol,
163	_monetary_locale.currency_symbol,
164	_monetary_locale.mon_decimal_point,
165	_monetary_locale.mon_thousands_sep,
166	_monetary_locale.mon_grouping,
167	_monetary_locale.positive_sign,
168	_monetary_locale.negative_sign,
169	_monetary_locale.int_frac_digits[0],
170	_monetary_locale.frac_digits[0],
171	_monetary_locale.p_cs_precedes[0],
172	_monetary_locale.p_sep_by_space[0],
173	_monetary_locale.n_cs_precedes[0],
174	_monetary_locale.n_sep_by_space[0],
175	_monetary_locale.p_sign_posn[0],
176	_monetary_locale.n_sign_posn[0]
177);
178}
179#endif /* LOCALE_DEBUG */
180