lnumeric.c revision 87658
1239310Sdim/*
2239310Sdim * Copyright (c) 2000, 2001 Alexey Zelkin <phantom@FreeBSD.org>
3239310Sdim * All rights reserved.
4239310Sdim *
5239310Sdim * Redistribution and use in source and binary forms, with or without
6239310Sdim * modification, are permitted provided that the following conditions
7239310Sdim * are met:
8239310Sdim * 1. Redistributions of source code must retain the above copyright
9239310Sdim *    notice, this list of conditions and the following disclaimer.
10239310Sdim * 2. Redistributions in binary form must reproduce the above copyright
11239310Sdim *    notice, this list of conditions and the following disclaimer in the
12239310Sdim *    documentation and/or other materials provided with the distribution.
13239310Sdim *
14239310Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15239310Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16239310Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17239310Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18239310Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19239310Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20249423Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21249423Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22239310Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23263508Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24239310Sdim * SUCH DAMAGE.
25239310Sdim *
26239310Sdim * $FreeBSD: head/lib/libc/locale/lnumeric.c 87658 2001-12-11 15:55:42Z phantom $
27239310Sdim */
28239310Sdim
29249423Sdim#include <limits.h>
30239310Sdim#include "lnumeric.h"
31239310Sdim#include "ldpart.h"
32239310Sdim
33239310Sdimextern int __nlocale_changed;
34239310Sdimextern const char *__fix_locale_grouping_str(const char *);
35239310Sdim
36239310Sdim#define LCNUMERIC_SIZE (sizeof(struct lc_numeric_T) / sizeof(char *))
37239310Sdim
38239310Sdimstatic char	numempty[] = { CHAR_MAX, '\0' };
39239310Sdim
40263508Sdimstatic const struct lc_numeric_T _C_numeric_locale = {
41263508Sdim	".",     	/* decimal_point */
42263508Sdim	"",     	/* thousands_sep */
43263508Sdim	numempty	/* grouping */
44263508Sdim};
45263508Sdim
46263508Sdimstatic struct lc_numeric_T _numeric_locale;
47263508Sdimstatic int	_numeric_using_locale;
48263508Sdimstatic char	*_numeric_locale_buf;
49263508Sdim
50263508Sdimint
51249423Sdim__numeric_load_locale(const char *name) {
52263508Sdim
53263508Sdim	int ret;
54263508Sdim
55263508Sdim	__nlocale_changed = 1;
56263508Sdim	ret = __part_load_locale(name, &_numeric_using_locale,
57263508Sdim		_numeric_locale_buf, "LC_NUMERIC",
58239310Sdim		LCNUMERIC_SIZE, LCNUMERIC_SIZE,
59239310Sdim		(const char **)&_numeric_locale);
60239310Sdim	if (ret == 0 && _numeric_using_locale)
61239310Sdim		_numeric_locale.grouping =
62239310Sdim			__fix_locale_grouping_str(_numeric_locale.grouping);
63249423Sdim	return ret;
64249423Sdim}
65249423Sdim
66249423Sdimstruct lc_numeric_T *
67249423Sdim__get_current_numeric_locale(void) {
68249423Sdim
69249423Sdim	return (_numeric_using_locale
70249423Sdim		? &_numeric_locale
71249423Sdim		: (struct lc_numeric_T *)&_C_numeric_locale);
72249423Sdim}
73239310Sdim
74#ifdef LOCALE_DEBUG
75void
76numericdebug(void) {
77printf(	"decimal_point = %s\n"
78	"thousands_sep = %s\n"
79	"grouping = %s\n",
80	_numeric_locale.decimal_point,
81	_numeric_locale.thousands_sep,
82	_numeric_locale.grouping
83);
84}
85#endif /* LOCALE_DEBUG */
86