xref: /titanic_50/usr/src/lib/libc/port/locale/lmonetary.c (revision 6ea3c0609e50782557505b88bb391b786bca32c9)
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 /*
28  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
29  * Use is subject to license terms.
30  */
31 
32 #include "lint.h"
33 #include <limits.h>
34 #include <stddef.h>
35 #include <stdlib.h>
36 #include "ldpart.h"
37 #include "lmonetary.h"
38 
39 extern int __mlocale_changed;
40 extern const char *__fix_locale_grouping_str(const char *);
41 
42 #define	LCMONETARY_SIZE_FULL (sizeof (struct lc_monetary_T) / sizeof (char *))
43 #define	LCMONETARY_SIZE_MIN \
44 	(offsetof(struct lc_monetary_T, int_p_cs_precedes) / sizeof (char *))
45 
46 static char	empty[] = "";
47 static char	numempty[] = { CHAR_MAX, '\0' };
48 
49 static const struct lc_monetary_T _C_monetary_locale = {
50 	empty,		/* int_curr_symbol */
51 	empty,		/* currency_symbol */
52 	empty,		/* mon_decimal_point */
53 	empty,		/* mon_thousands_sep */
54 	numempty,	/* mon_grouping */
55 	empty,		/* positive_sign */
56 	empty,		/* negative_sign */
57 	numempty,	/* int_frac_digits */
58 	numempty,	/* frac_digits */
59 	numempty,	/* p_cs_precedes */
60 	numempty,	/* p_sep_by_space */
61 	numempty,	/* n_cs_precedes */
62 	numempty,	/* n_sep_by_space */
63 	numempty,	/* p_sign_posn */
64 	numempty,	/* n_sign_posn */
65 	numempty,	/* int_p_cs_precedes */
66 	numempty,	/* int_n_cs_precedes */
67 	numempty,	/* int_p_sep_by_space */
68 	numempty,	/* int_n_sep_by_space */
69 	numempty,	/* int_p_sign_posn */
70 	numempty	/* int_n_sign_posn */
71 };
72 
73 static struct lc_monetary_T _monetary_locale;
74 static int	_monetary_using_locale;
75 static char	*_monetary_locale_buf;
76 
77 static char
78 cnv(const char *str)
79 {
80 	int i = strtol(str, NULL, 10);
81 
82 	if (i == -1)
83 		i = CHAR_MAX;
84 	return ((char)i);
85 }
86 
87 int
88 __monetary_load_locale(const char *name)
89 {
90 	int ret;
91 
92 	ret = __part_load_locale(name, &_monetary_using_locale,
93 	    &_monetary_locale_buf, "LC_MONETARY",
94 	    LCMONETARY_SIZE_FULL, LCMONETARY_SIZE_MIN,
95 	    (const char **)&_monetary_locale);
96 	if (ret != _LDP_ERROR)
97 		__mlocale_changed = 1;
98 	if (ret == _LDP_LOADED) {
99 		_monetary_locale.mon_grouping =
100 		    __fix_locale_grouping_str(_monetary_locale.mon_grouping);
101 
102 #define	M_ASSIGN_CHAR(NAME) \
103 		(((char *)_monetary_locale.NAME)[0] = \
104 			cnv(_monetary_locale.NAME))
105 
106 		M_ASSIGN_CHAR(int_frac_digits);
107 		M_ASSIGN_CHAR(frac_digits);
108 		M_ASSIGN_CHAR(p_cs_precedes);
109 		M_ASSIGN_CHAR(p_sep_by_space);
110 		M_ASSIGN_CHAR(n_cs_precedes);
111 		M_ASSIGN_CHAR(n_sep_by_space);
112 		M_ASSIGN_CHAR(p_sign_posn);
113 		M_ASSIGN_CHAR(n_sign_posn);
114 
115 		/*
116 		 * The six additional C99 international monetary formatting
117 		 * parameters default to the national parameters when
118 		 * reading FreeBSD LC_MONETARY data files.
119 		 */
120 #define	M_ASSIGN_ICHAR(NAME)					\
121 		if (_monetary_locale.int_##NAME == NULL)	\
122 			_monetary_locale.int_##NAME =		\
123 			    _monetary_locale.NAME;		\
124 		else						\
125 			M_ASSIGN_CHAR(int_##NAME);
126 
127 		M_ASSIGN_ICHAR(p_cs_precedes);
128 		M_ASSIGN_ICHAR(n_cs_precedes);
129 		M_ASSIGN_ICHAR(p_sep_by_space);
130 		M_ASSIGN_ICHAR(n_sep_by_space);
131 		M_ASSIGN_ICHAR(p_sign_posn);
132 		M_ASSIGN_ICHAR(n_sign_posn);
133 	}
134 	return (ret);
135 }
136 
137 struct lc_monetary_T *
138 __get_current_monetary_locale(void)
139 {
140 	return (_monetary_using_locale ? &_monetary_locale :
141 	    (struct lc_monetary_T *)&_C_monetary_locale);
142 }
143