lmonetary.c revision 101498
11553Srgrimes/*
21553Srgrimes * Copyright (c) 2000, 2001 Alexey Zelkin <phantom@FreeBSD.org>
31553Srgrimes * All rights reserved.
41553Srgrimes *
51553Srgrimes * Redistribution and use in source and binary forms, with or without
61553Srgrimes * modification, are permitted provided that the following conditions
71553Srgrimes * are met:
81553Srgrimes * 1. Redistributions of source code must retain the above copyright
91553Srgrimes *    notice, this list of conditions and the following disclaimer.
101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111553Srgrimes *    notice, this list of conditions and the following disclaimer in the
121553Srgrimes *    documentation and/or other materials provided with the distribution.
131553Srgrimes *
141553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
181553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241553Srgrimes * SUCH DAMAGE.
251553Srgrimes */
261553Srgrimes
271553Srgrimes#include <sys/cdefs.h>
281553Srgrimes__FBSDID("$FreeBSD: head/lib/libc/locale/lmonetary.c 101498 2002-08-08 05:51:54Z ache $");
291553Srgrimes
301553Srgrimes#include <limits.h>
311553Srgrimes#include <stdlib.h>
321553Srgrimes#include "lmonetary.h"
33118288Sgad#include "ldpart.h"
34112701Seivind
351553Srgrimesextern int __mlocale_changed;
361553Srgrimesextern const char * __fix_locale_grouping_str(const char *);
371553Srgrimes
381553Srgrimes#define LCMONETARY_SIZE (sizeof(struct lc_monetary_T) / sizeof(char *))
391553Srgrimes
401553Srgrimesstatic char	empty[] = "";
411553Srgrimesstatic char	numempty[] = { CHAR_MAX, '\0'};
421553Srgrimes
431553Srgrimesstatic const struct lc_monetary_T _C_monetary_locale = {
441553Srgrimes	empty,		/* int_curr_symbol */
451553Srgrimes	empty,		/* currency_symbol */
461553Srgrimes	empty,		/* mon_decimal_point */
47112701Seivind	empty,		/* mon_thousands_sep */
481553Srgrimes	numempty,	/* mon_grouping */
491553Srgrimes	empty,		/* positive_sign */
501553Srgrimes	empty,		/* negative_sign */
511553Srgrimes	numempty,	/* int_frac_digits */
521553Srgrimes	numempty,	/* frac_digits */
5330407Sjoerg	numempty,	/* p_cs_precedes */
541553Srgrimes	numempty,	/* p_sep_by_space */
551553Srgrimes	numempty,	/* n_cs_precedes */
561553Srgrimes	numempty,	/* n_sep_by_space */
571553Srgrimes	numempty,	/* p_sign_posn */
581553Srgrimes	numempty	/* n_sign_posn */
591553Srgrimes};
601553Srgrimes
611553Srgrimesstatic struct lc_monetary_T _monetary_locale;
621553Srgrimesstatic int	_monetary_using_locale;
631553Srgrimesstatic char	*_monetary_locale_buf;
641553Srgrimes
651553Srgrimesstatic char
661553Srgrimescnv(const char *str)
671553Srgrimes{
681553Srgrimes	int i = strtol(str, NULL, 10);
691553Srgrimes
701553Srgrimes	if (i == -1)
711553Srgrimes		i = CHAR_MAX;
721553Srgrimes	return ((char)i);
731553Srgrimes}
741553Srgrimes
751553Srgrimesint
761553Srgrimes__monetary_load_locale(const char *name)
771553Srgrimes{
781553Srgrimes	int ret;
791553Srgrimes
801553Srgrimes	ret = __part_load_locale(name, &_monetary_using_locale,
811553Srgrimes		_monetary_locale_buf, "LC_MONETARY",
821553Srgrimes		LCMONETARY_SIZE, LCMONETARY_SIZE,
83		(const char **)&_monetary_locale);
84	if (ret != _LDP_ERROR)
85		__mlocale_changed = 1;
86	if (ret == _LDP_LOADED) {
87		_monetary_locale.mon_grouping =
88		     __fix_locale_grouping_str(_monetary_locale.mon_grouping);
89
90#define M_ASSIGN_CHAR(NAME) (((char *)_monetary_locale.NAME)[0] = \
91			     cnv(_monetary_locale.NAME))
92
93		M_ASSIGN_CHAR(int_frac_digits);
94		M_ASSIGN_CHAR(frac_digits);
95		M_ASSIGN_CHAR(p_cs_precedes);
96		M_ASSIGN_CHAR(p_sep_by_space);
97		M_ASSIGN_CHAR(n_cs_precedes);
98		M_ASSIGN_CHAR(n_sep_by_space);
99		M_ASSIGN_CHAR(p_sign_posn);
100		M_ASSIGN_CHAR(n_sign_posn);
101	}
102	return (ret);
103}
104
105struct lc_monetary_T *
106__get_current_monetary_locale(void)
107{
108	return (_monetary_using_locale
109		? &_monetary_locale
110		: (struct lc_monetary_T *)&_C_monetary_locale);
111}
112
113#ifdef LOCALE_DEBUG
114void
115monetdebug() {
116printf(	"int_curr_symbol = %s\n"
117	"currency_symbol = %s\n"
118	"mon_decimal_point = %s\n"
119	"mon_thousands_sep = %s\n"
120	"mon_grouping = %s\n"
121	"positive_sign = %s\n"
122	"negative_sign = %s\n"
123	"int_frac_digits = %d\n"
124	"frac_digits = %d\n"
125	"p_cs_precedes = %d\n"
126	"p_sep_by_space = %d\n"
127	"n_cs_precedes = %d\n"
128	"n_sep_by_space = %d\n"
129	"p_sign_posn = %d\n"
130	"n_sign_posn = %d\n",
131	_monetary_locale.int_curr_symbol,
132	_monetary_locale.currency_symbol,
133	_monetary_locale.mon_decimal_point,
134	_monetary_locale.mon_thousands_sep,
135	_monetary_locale.mon_grouping,
136	_monetary_locale.positive_sign,
137	_monetary_locale.negative_sign,
138	_monetary_locale.int_frac_digits[0],
139	_monetary_locale.frac_digits[0],
140	_monetary_locale.p_cs_precedes[0],
141	_monetary_locale.p_sep_by_space[0],
142	_monetary_locale.n_cs_precedes[0],
143	_monetary_locale.n_sep_by_space[0],
144	_monetary_locale.p_sign_posn[0],
145	_monetary_locale.n_sign_posn[0]
146);
147}
148#endif /* LOCALE_DEBUG */
149