xref: /freebsd/lib/libc/locale/lmonetary.c (revision 683fe11379470192b45860f10e5574a3dd26d4eb)
190423eceSAlexey Zelkin /*
274f2b975SAlexey Zelkin  * Copyright (c) 2000, 2001 Alexey Zelkin <phantom@FreeBSD.org>
390423eceSAlexey Zelkin  * All rights reserved.
490423eceSAlexey Zelkin  *
590423eceSAlexey Zelkin  * Redistribution and use in source and binary forms, with or without
690423eceSAlexey Zelkin  * modification, are permitted provided that the following conditions
790423eceSAlexey Zelkin  * are met:
890423eceSAlexey Zelkin  * 1. Redistributions of source code must retain the above copyright
990423eceSAlexey Zelkin  *    notice, this list of conditions and the following disclaimer.
1090423eceSAlexey Zelkin  * 2. Redistributions in binary form must reproduce the above copyright
1190423eceSAlexey Zelkin  *    notice, this list of conditions and the following disclaimer in the
1290423eceSAlexey Zelkin  *    documentation and/or other materials provided with the distribution.
1390423eceSAlexey Zelkin  *
1490423eceSAlexey Zelkin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1590423eceSAlexey Zelkin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1690423eceSAlexey Zelkin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1790423eceSAlexey Zelkin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1890423eceSAlexey Zelkin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1990423eceSAlexey Zelkin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2090423eceSAlexey Zelkin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2190423eceSAlexey Zelkin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2290423eceSAlexey Zelkin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2390423eceSAlexey Zelkin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2490423eceSAlexey Zelkin  * SUCH DAMAGE.
2590423eceSAlexey Zelkin  */
2690423eceSAlexey Zelkin 
27333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
28333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
29333fc21eSDavid E. O'Brien 
301bd7723dSAlexey Zelkin #include <limits.h>
31f4da1a75STim J. Robbins #include <stddef.h>
32831e8f61SAndrey A. Chernov #include <stdlib.h>
33683fe113SAlexey Zelkin 
3490423eceSAlexey Zelkin #include "ldpart.h"
35683fe113SAlexey Zelkin #include "lmonetary.h"
3690423eceSAlexey Zelkin 
3790423eceSAlexey Zelkin extern int __mlocale_changed;
381bd7723dSAlexey Zelkin extern const char * __fix_locale_grouping_str(const char *);
3990423eceSAlexey Zelkin 
408a093dadSAndrey A. Chernov #define LCMONETARY_SIZE_FULL (sizeof(struct lc_monetary_T) / sizeof(char *))
418a093dadSAndrey A. Chernov #define LCMONETARY_SIZE_MIN \
428a093dadSAndrey A. Chernov 		(offsetof(struct lc_monetary_T, int_p_cs_precedes) / \
438a093dadSAndrey A. Chernov 		    sizeof(char *))
4490423eceSAlexey Zelkin 
4590423eceSAlexey Zelkin static char	empty[] = "";
461bd7723dSAlexey Zelkin static char	numempty[] = { CHAR_MAX, '\0'};
4790423eceSAlexey Zelkin 
4890423eceSAlexey Zelkin static const struct lc_monetary_T _C_monetary_locale = {
4990423eceSAlexey Zelkin 	empty,		/* int_curr_symbol */
5090423eceSAlexey Zelkin 	empty,		/* currency_symbol */
5154e3bc25SAndrey A. Chernov 	empty,		/* mon_decimal_point */
5290423eceSAlexey Zelkin 	empty,		/* mon_thousands_sep */
5390423eceSAlexey Zelkin 	numempty,	/* mon_grouping */
5490423eceSAlexey Zelkin 	empty,		/* positive_sign */
5590423eceSAlexey Zelkin 	empty,		/* negative_sign */
5690423eceSAlexey Zelkin 	numempty,	/* int_frac_digits */
5790423eceSAlexey Zelkin 	numempty,	/* frac_digits */
5890423eceSAlexey Zelkin 	numempty,	/* p_cs_precedes */
5990423eceSAlexey Zelkin 	numempty,	/* p_sep_by_space */
6090423eceSAlexey Zelkin 	numempty,	/* n_cs_precedes */
6190423eceSAlexey Zelkin 	numempty,	/* n_sep_by_space */
6290423eceSAlexey Zelkin 	numempty,	/* p_sign_posn */
63f4da1a75STim J. Robbins 	numempty,	/* n_sign_posn */
64f4da1a75STim J. Robbins 	numempty,	/* int_p_cs_precedes */
65f4da1a75STim J. Robbins 	numempty,	/* int_n_cs_precedes */
66f4da1a75STim J. Robbins 	numempty,	/* int_p_sep_by_space */
67f4da1a75STim J. Robbins 	numempty,	/* int_n_sep_by_space */
68f4da1a75STim J. Robbins 	numempty,	/* int_p_sign_posn */
69f4da1a75STim J. Robbins 	numempty	/* int_n_sign_posn */
7090423eceSAlexey Zelkin };
7190423eceSAlexey Zelkin 
7290423eceSAlexey Zelkin static struct lc_monetary_T _monetary_locale;
7390423eceSAlexey Zelkin static int	_monetary_using_locale;
7474f2b975SAlexey Zelkin static char	*_monetary_locale_buf;
7590423eceSAlexey Zelkin 
76831e8f61SAndrey A. Chernov static char
77ecc4c620SAndrey A. Chernov cnv(const char *str)
78ecc4c620SAndrey A. Chernov {
79831e8f61SAndrey A. Chernov 	int i = strtol(str, NULL, 10);
80ecc4c620SAndrey A. Chernov 
81831e8f61SAndrey A. Chernov 	if (i == -1)
82831e8f61SAndrey A. Chernov 		i = CHAR_MAX;
83ecc4c620SAndrey A. Chernov 	return ((char)i);
84831e8f61SAndrey A. Chernov }
85831e8f61SAndrey A. Chernov 
8690423eceSAlexey Zelkin int
87ecc4c620SAndrey A. Chernov __monetary_load_locale(const char *name)
88ecc4c620SAndrey A. Chernov {
8990423eceSAlexey Zelkin 	int ret;
90ecc4c620SAndrey A. Chernov 
9190423eceSAlexey Zelkin 	ret = __part_load_locale(name, &_monetary_using_locale,
923dfdc427SJordan K. Hubbard 		&_monetary_locale_buf, "LC_MONETARY",
938a093dadSAndrey A. Chernov 		LCMONETARY_SIZE_FULL, LCMONETARY_SIZE_MIN,
9490423eceSAlexey Zelkin 		(const char **)&_monetary_locale);
9576692b80SAndrey A. Chernov 	if (ret != _LDP_ERROR)
9676692b80SAndrey A. Chernov 		__mlocale_changed = 1;
9776692b80SAndrey A. Chernov 	if (ret == _LDP_LOADED) {
981bd7723dSAlexey Zelkin 		_monetary_locale.mon_grouping =
991bd7723dSAlexey Zelkin 		     __fix_locale_grouping_str(_monetary_locale.mon_grouping);
100831e8f61SAndrey A. Chernov 
101831e8f61SAndrey A. Chernov #define M_ASSIGN_CHAR(NAME) (((char *)_monetary_locale.NAME)[0] = \
102831e8f61SAndrey A. Chernov 			     cnv(_monetary_locale.NAME))
103831e8f61SAndrey A. Chernov 
104831e8f61SAndrey A. Chernov 		M_ASSIGN_CHAR(int_frac_digits);
105831e8f61SAndrey A. Chernov 		M_ASSIGN_CHAR(frac_digits);
106831e8f61SAndrey A. Chernov 		M_ASSIGN_CHAR(p_cs_precedes);
107831e8f61SAndrey A. Chernov 		M_ASSIGN_CHAR(p_sep_by_space);
108831e8f61SAndrey A. Chernov 		M_ASSIGN_CHAR(n_cs_precedes);
109831e8f61SAndrey A. Chernov 		M_ASSIGN_CHAR(n_sep_by_space);
110831e8f61SAndrey A. Chernov 		M_ASSIGN_CHAR(p_sign_posn);
111831e8f61SAndrey A. Chernov 		M_ASSIGN_CHAR(n_sign_posn);
112f4da1a75STim J. Robbins 
113f4da1a75STim J. Robbins 		/*
114f4da1a75STim J. Robbins 		 * The six additional C99 international monetary formatting
115f4da1a75STim J. Robbins 		 * parameters default to the national parameters when
116683fe113SAlexey Zelkin 		 * reading FreeBSD LC_MONETARY data files.
117f4da1a75STim J. Robbins 		 */
118f4da1a75STim J. Robbins #define	M_ASSIGN_ICHAR(NAME)						\
119f4da1a75STim J. Robbins 		do {							\
120f4da1a75STim J. Robbins 			if (_monetary_locale.int_##NAME == NULL)	\
121f4da1a75STim J. Robbins 				_monetary_locale.int_##NAME =		\
122f4da1a75STim J. Robbins 				    _monetary_locale.NAME;		\
123f4da1a75STim J. Robbins 			else						\
124f4da1a75STim J. Robbins 				M_ASSIGN_CHAR(int_##NAME);		\
125f4da1a75STim J. Robbins 		} while (0)
126f4da1a75STim J. Robbins 
127f4da1a75STim J. Robbins 		M_ASSIGN_ICHAR(p_cs_precedes);
128f4da1a75STim J. Robbins 		M_ASSIGN_ICHAR(n_cs_precedes);
129f4da1a75STim J. Robbins 		M_ASSIGN_ICHAR(p_sep_by_space);
130f4da1a75STim J. Robbins 		M_ASSIGN_ICHAR(n_sep_by_space);
131f4da1a75STim J. Robbins 		M_ASSIGN_ICHAR(p_sign_posn);
132f4da1a75STim J. Robbins 		M_ASSIGN_ICHAR(n_sign_posn);
133831e8f61SAndrey A. Chernov 	}
134ecc4c620SAndrey A. Chernov 	return (ret);
13590423eceSAlexey Zelkin }
13690423eceSAlexey Zelkin 
13790423eceSAlexey Zelkin struct lc_monetary_T *
138ecc4c620SAndrey A. Chernov __get_current_monetary_locale(void)
139ecc4c620SAndrey A. Chernov {
14090423eceSAlexey Zelkin 	return (_monetary_using_locale
14190423eceSAlexey Zelkin 		? &_monetary_locale
14290423eceSAlexey Zelkin 		: (struct lc_monetary_T *)&_C_monetary_locale);
14390423eceSAlexey Zelkin }
14490423eceSAlexey Zelkin 
14590423eceSAlexey Zelkin #ifdef LOCALE_DEBUG
14690423eceSAlexey Zelkin void
14790423eceSAlexey Zelkin monetdebug() {
14890423eceSAlexey Zelkin printf(	"int_curr_symbol = %s\n"
14990423eceSAlexey Zelkin 	"currency_symbol = %s\n"
15090423eceSAlexey Zelkin 	"mon_decimal_point = %s\n"
15190423eceSAlexey Zelkin 	"mon_thousands_sep = %s\n"
15290423eceSAlexey Zelkin 	"mon_grouping = %s\n"
15390423eceSAlexey Zelkin 	"positive_sign = %s\n"
15490423eceSAlexey Zelkin 	"negative_sign = %s\n"
155831e8f61SAndrey A. Chernov 	"int_frac_digits = %d\n"
156831e8f61SAndrey A. Chernov 	"frac_digits = %d\n"
157831e8f61SAndrey A. Chernov 	"p_cs_precedes = %d\n"
158831e8f61SAndrey A. Chernov 	"p_sep_by_space = %d\n"
159831e8f61SAndrey A. Chernov 	"n_cs_precedes = %d\n"
160831e8f61SAndrey A. Chernov 	"n_sep_by_space = %d\n"
161831e8f61SAndrey A. Chernov 	"p_sign_posn = %d\n"
162831e8f61SAndrey A. Chernov 	"n_sign_posn = %d\n",
163683fe113SAlexey Zelkin 	"int_p_cs_precedes = %d\n"
164683fe113SAlexey Zelkin 	"int_p_sep_by_space = %d\n"
165683fe113SAlexey Zelkin 	"int_n_cs_precedes = %d\n"
166683fe113SAlexey Zelkin 	"int_n_sep_by_space = %d\n"
167683fe113SAlexey Zelkin 	"int_p_sign_posn = %d\n"
168683fe113SAlexey Zelkin 	"int_n_sign_posn = %d\n",
16990423eceSAlexey Zelkin 	_monetary_locale.int_curr_symbol,
17090423eceSAlexey Zelkin 	_monetary_locale.currency_symbol,
17190423eceSAlexey Zelkin 	_monetary_locale.mon_decimal_point,
17290423eceSAlexey Zelkin 	_monetary_locale.mon_thousands_sep,
17390423eceSAlexey Zelkin 	_monetary_locale.mon_grouping,
17490423eceSAlexey Zelkin 	_monetary_locale.positive_sign,
17590423eceSAlexey Zelkin 	_monetary_locale.negative_sign,
176831e8f61SAndrey A. Chernov 	_monetary_locale.int_frac_digits[0],
177831e8f61SAndrey A. Chernov 	_monetary_locale.frac_digits[0],
178831e8f61SAndrey A. Chernov 	_monetary_locale.p_cs_precedes[0],
179831e8f61SAndrey A. Chernov 	_monetary_locale.p_sep_by_space[0],
180831e8f61SAndrey A. Chernov 	_monetary_locale.n_cs_precedes[0],
181831e8f61SAndrey A. Chernov 	_monetary_locale.n_sep_by_space[0],
182831e8f61SAndrey A. Chernov 	_monetary_locale.p_sign_posn[0],
183683fe113SAlexey Zelkin 	_monetary_locale.n_sign_posn[0],
184683fe113SAlexey Zelkin 	_monetary_locale.int_p_cs_precedes[0],
185683fe113SAlexey Zelkin 	_monetary_locale.int_p_sep_by_space[0],
186683fe113SAlexey Zelkin 	_monetary_locale.int_n_cs_precedes[0],
187683fe113SAlexey Zelkin 	_monetary_locale.int_n_sep_by_space[0],
188683fe113SAlexey Zelkin 	_monetary_locale.int_p_sign_posn[0],
189683fe113SAlexey Zelkin 	_monetary_locale.int_n_sign_posn[0]
19090423eceSAlexey Zelkin );
19190423eceSAlexey Zelkin }
19290423eceSAlexey Zelkin #endif /* LOCALE_DEBUG */
193