xref: /freebsd/lib/libc/locale/lmonetary.c (revision 54e3bc256ec564b859aad94d6372b8c532cc9686)
190423eceSAlexey Zelkin /*
290423eceSAlexey Zelkin  * Copyright (c) 2000, 2001 Alexey Zelkin
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  * $FreeBSD$
2790423eceSAlexey Zelkin  */
2890423eceSAlexey Zelkin 
291bd7723dSAlexey Zelkin #include <limits.h>
3090423eceSAlexey Zelkin #include "lmonetary.h"
3190423eceSAlexey Zelkin #include "ldpart.h"
3290423eceSAlexey Zelkin 
3390423eceSAlexey Zelkin extern int __mlocale_changed;
341bd7723dSAlexey Zelkin extern const char * __fix_locale_grouping_str(const char *);
3590423eceSAlexey Zelkin 
3690423eceSAlexey Zelkin #define LCMONETARY_SIZE (sizeof(struct lc_monetary_T) / sizeof(char *))
3790423eceSAlexey Zelkin 
3890423eceSAlexey Zelkin static char	empty[] = "";
391bd7723dSAlexey Zelkin static char     numempty[] = { CHAR_MAX, '\0'};
4090423eceSAlexey Zelkin 
4190423eceSAlexey Zelkin static const struct lc_monetary_T _C_monetary_locale = {
4290423eceSAlexey Zelkin 	empty ,		/* int_curr_symbol */
4390423eceSAlexey Zelkin 	empty ,		/* currency_symbol */
4454e3bc25SAndrey A. Chernov 	empty ,		/* mon_decimal_point */
4590423eceSAlexey Zelkin 	empty ,		/* mon_thousands_sep */
4690423eceSAlexey Zelkin 	numempty ,	/* mon_grouping */
4790423eceSAlexey Zelkin 	empty ,		/* positive_sign */
4890423eceSAlexey Zelkin 	empty ,		/* negative_sign */
4990423eceSAlexey Zelkin 	numempty ,	/* int_frac_digits */
5090423eceSAlexey Zelkin 	numempty ,	/* frac_digits */
5190423eceSAlexey Zelkin 	numempty ,	/* p_cs_precedes */
5290423eceSAlexey Zelkin 	numempty ,	/* p_sep_by_space */
5390423eceSAlexey Zelkin 	numempty ,	/* n_cs_precedes */
5490423eceSAlexey Zelkin 	numempty ,	/* n_sep_by_space */
5590423eceSAlexey Zelkin 	numempty ,	/* p_sign_posn */
5690423eceSAlexey Zelkin 	numempty	/* n_sign_posn */
5790423eceSAlexey Zelkin };
5890423eceSAlexey Zelkin 
5990423eceSAlexey Zelkin static struct lc_monetary_T _monetary_locale;
6090423eceSAlexey Zelkin static int	_monetary_using_locale;
6190423eceSAlexey Zelkin static char *	monetary_locale_buf;
6290423eceSAlexey Zelkin 
6390423eceSAlexey Zelkin int
6490423eceSAlexey Zelkin __monetary_load_locale(const char *name) {
6590423eceSAlexey Zelkin 
6690423eceSAlexey Zelkin 	int ret;
671bd7723dSAlexey Zelkin 	__mlocale_changed = 1;
6890423eceSAlexey Zelkin 	ret = __part_load_locale(name, &_monetary_using_locale,
6939d2c772SAlexey Zelkin 		monetary_locale_buf, "LC_MONETARY",
7039d2c772SAlexey Zelkin 		LCMONETARY_SIZE, LCMONETARY_SIZE,
7190423eceSAlexey Zelkin 		(const char **)&_monetary_locale);
72b161fb00SAlexey Zelkin 	if (ret == 0 && _monetary_using_locale)
731bd7723dSAlexey Zelkin 		_monetary_locale.mon_grouping =
741bd7723dSAlexey Zelkin 		     __fix_locale_grouping_str(_monetary_locale.mon_grouping);
7590423eceSAlexey Zelkin 	return ret;
7690423eceSAlexey Zelkin }
7790423eceSAlexey Zelkin 
7890423eceSAlexey Zelkin struct lc_monetary_T *
7990423eceSAlexey Zelkin __get_current_monetary_locale(void) {
8090423eceSAlexey Zelkin 
8190423eceSAlexey Zelkin 	return (_monetary_using_locale
8290423eceSAlexey Zelkin 		? &_monetary_locale
8390423eceSAlexey Zelkin 		: (struct lc_monetary_T *)&_C_monetary_locale);
8490423eceSAlexey Zelkin }
8590423eceSAlexey Zelkin 
8690423eceSAlexey Zelkin #ifdef LOCALE_DEBUG
8790423eceSAlexey Zelkin void
8890423eceSAlexey Zelkin monetdebug() {
8990423eceSAlexey Zelkin printf(	"int_curr_symbol = %s\n"
9090423eceSAlexey Zelkin 	"currency_symbol = %s\n"
9190423eceSAlexey Zelkin 	"mon_decimal_point = %s\n"
9290423eceSAlexey Zelkin 	"mon_thousands_sep = %s\n"
9390423eceSAlexey Zelkin 	"mon_grouping = %s\n"
9490423eceSAlexey Zelkin 	"positive_sign = %s\n"
9590423eceSAlexey Zelkin 	"negative_sign = %s\n"
9690423eceSAlexey Zelkin 	"int_frac_digits = %s\n"
9790423eceSAlexey Zelkin 	"frac_digits = %s\n"
9890423eceSAlexey Zelkin 	"p_cs_precedes = %s\n"
9990423eceSAlexey Zelkin 	"p_sep_by_space = %s\n"
10090423eceSAlexey Zelkin 	"n_cs_precedes = %s\n"
10190423eceSAlexey Zelkin 	"n_sep_by_space = %s\n"
10290423eceSAlexey Zelkin 	"p_sign_posn = %s\n"
10390423eceSAlexey Zelkin 	"n_sign_posn = %s\n",
10490423eceSAlexey Zelkin 	_monetary_locale.int_curr_symbol,
10590423eceSAlexey Zelkin 	_monetary_locale.currency_symbol,
10690423eceSAlexey Zelkin 	_monetary_locale.mon_decimal_point,
10790423eceSAlexey Zelkin 	_monetary_locale.mon_thousands_sep,
10890423eceSAlexey Zelkin 	_monetary_locale.mon_grouping,
10990423eceSAlexey Zelkin 	_monetary_locale.positive_sign,
11090423eceSAlexey Zelkin 	_monetary_locale.negative_sign,
11190423eceSAlexey Zelkin 	_monetary_locale.int_frac_digits,
11290423eceSAlexey Zelkin 	_monetary_locale.frac_digits,
11390423eceSAlexey Zelkin 	_monetary_locale.p_cs_precedes,
11490423eceSAlexey Zelkin 	_monetary_locale.p_sep_by_space,
11590423eceSAlexey Zelkin 	_monetary_locale.n_cs_precedes,
11690423eceSAlexey Zelkin 	_monetary_locale.n_sep_by_space,
11790423eceSAlexey Zelkin 	_monetary_locale.p_sign_posn,
11890423eceSAlexey Zelkin 	_monetary_locale.n_sign_posn
11990423eceSAlexey Zelkin );
12090423eceSAlexey Zelkin }
12190423eceSAlexey Zelkin #endif /* LOCALE_DEBUG */
122