localeconv.c revision 72284
1/*
2 * Copyright (c) 2001 Alexey Zelkin
3 * Copyright (c) 1991, 1993
4 *	The Regents of the University of California.  All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#if defined(LIBC_SCCS) && !defined(lint)
29#if 0
30static char sccsid[] = "@(#)localeconv.c	8.1 (Berkeley) 6/4/93";
31#endif
32static char rcsid[] = "$FreeBSD: head/lib/libc/locale/localeconv.c 72284 2001-02-10 04:32:55Z ache $";
33#endif /* LIBC_SCCS and not lint */
34
35#include <locale.h>
36#include <stdlib.h>
37#include <limits.h>
38#include "lmonetary.h"
39#include "lnumeric.h"
40
41/*
42 * The localeconv() function constructs a struct lconv from the current
43 * monetary and numeric locales.
44 *
45 * Because localeconv() may be called many times (especially by library
46 * routines like printf() & strtod()), the approprate members of the
47 * lconv structure are computed only when the monetary or numeric
48 * locale has been changed.
49 */
50int __mlocale_changed = 1;
51int __nlocale_changed = 1;
52
53/* XXX: FIXME! */
54/* Numbers separated by ";" must be parsed into byte array. */
55static char nogrouping[] = { CHAR_MAX, '\0' };
56
57static char
58cnv(char *str) {
59	int i = strtol(str, NULL, 10);
60	if (i == -1)
61		i = CHAR_MAX;
62	return (char)i;
63}
64
65/*
66 * Return the current locale conversion.
67 */
68struct lconv *
69localeconv()
70{
71    static struct lconv ret;
72
73    if (__mlocale_changed) {
74	/* LC_MONETARY part */
75        struct lc_monetary_T * mptr;
76
77#define M_ASSIGN_STR(NAME) (ret.NAME = (char*)mptr->NAME)
78#define M_ASSIGN_CHAR(NAME) (ret.NAME = cnv((char*)mptr->NAME))
79
80	mptr = __get_current_monetary_locale();
81	M_ASSIGN_STR(int_curr_symbol);
82	M_ASSIGN_STR(currency_symbol);
83	M_ASSIGN_STR(mon_decimal_point);
84	M_ASSIGN_STR(mon_thousands_sep);
85	/* XXX: FIXME! */
86	/* Numbers separated by ";" must be parsed into byte array. */
87	ret.mon_grouping = nogrouping;
88	M_ASSIGN_STR(positive_sign);
89	M_ASSIGN_STR(negative_sign);
90	M_ASSIGN_CHAR(int_frac_digits);
91	M_ASSIGN_CHAR(frac_digits);
92	M_ASSIGN_CHAR(p_cs_precedes);
93	M_ASSIGN_CHAR(p_sep_by_space);
94	M_ASSIGN_CHAR(n_cs_precedes);
95	M_ASSIGN_CHAR(n_sep_by_space);
96	M_ASSIGN_CHAR(p_sign_posn);
97	M_ASSIGN_CHAR(n_sign_posn);
98	__mlocale_changed = 0;
99    }
100
101    if (__nlocale_changed) {
102	/* LC_NUMERIC part */
103        struct lc_numeric_T * nptr;
104
105#define N_ASSIGN_STR(NAME) (ret.NAME = (char*)nptr->NAME)
106
107	nptr = __get_current_numeric_locale();
108	N_ASSIGN_STR(decimal_point);
109	N_ASSIGN_STR(thousands_sep);
110	/* XXX: FIXME! */
111	/* Numbers separated by ";" must be parsed into byte array. */
112	ret.grouping = nogrouping;
113	__nlocale_changed = 0;
114    }
115
116    return (&ret);
117}
118