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