lnumeric.c revision 227753
1264790Sbapt/*
2264790Sbapt * Copyright (c) 2000, 2001 Alexey Zelkin <phantom@FreeBSD.org>
3264790Sbapt * All rights reserved.
4264790Sbapt *
5264790Sbapt * Copyright (c) 2011 The FreeBSD Foundation
6264790Sbapt * All rights reserved.
7264790Sbapt * Portions of this software were developed by David Chisnall
8264790Sbapt * under sponsorship from the FreeBSD Foundation.
9264790Sbapt *
10264790Sbapt * Redistribution and use in source and binary forms, with or without
11264790Sbapt * modification, are permitted provided that the following conditions
12264790Sbapt * are met:
13264790Sbapt * 1. Redistributions of source code must retain the above copyright
14264790Sbapt *    notice, this list of conditions and the following disclaimer.
15264790Sbapt * 2. Redistributions in binary form must reproduce the above copyright
16264790Sbapt *    notice, this list of conditions and the following disclaimer in the
17264790Sbapt *    documentation and/or other materials provided with the distribution.
18264790Sbapt *
19264790Sbapt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20264790Sbapt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21264790Sbapt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22264790Sbapt * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23264790Sbapt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24264790Sbapt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25264790Sbapt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26264790Sbapt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27264790Sbapt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28264790Sbapt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29264790Sbapt * SUCH DAMAGE.
30264790Sbapt */
31264790Sbapt
32264790Sbapt#include <sys/cdefs.h>
33264790Sbapt__FBSDID("$FreeBSD: head/lib/libc/locale/lnumeric.c 227753 2011-11-20 14:45:42Z theraven $");
34264790Sbapt
35264790Sbapt#include <limits.h>
36264790Sbapt
37264790Sbapt#include "ldpart.h"
38264790Sbapt#include "lnumeric.h"
39264790Sbapt
40264790Sbaptextern const char *__fix_locale_grouping_str(const char *);
41264790Sbapt
42#define LCNUMERIC_SIZE (sizeof(struct lc_numeric_T) / sizeof(char *))
43
44static char	numempty[] = { CHAR_MAX, '\0' };
45
46static const struct lc_numeric_T _C_numeric_locale = {
47	".",     	/* decimal_point */
48	"",     	/* thousands_sep */
49	numempty	/* grouping */
50};
51
52static void
53destruct_numeric(void *v)
54{
55	struct xlocale_numeric *l = v;
56	if (l->buffer)
57		free(l->buffer);
58	free(l);
59}
60
61struct xlocale_numeric __xlocale_global_numeric;
62
63static int
64numeric_load_locale(struct xlocale_numeric *loc, int *using_locale, int *changed,
65		const char *name)
66{
67	int ret;
68	struct lc_numeric_T *l = &loc->locale;
69
70	ret = __part_load_locale(name, using_locale,
71		&loc->buffer, "LC_NUMERIC",
72		LCNUMERIC_SIZE, LCNUMERIC_SIZE,
73		(const char**)l);
74	if (ret != _LDP_ERROR)
75		*changed= 1;
76	if (ret == _LDP_LOADED) {
77		/* Can't be empty according to C99 */
78		if (*l->decimal_point == '\0')
79			l->decimal_point =
80			    _C_numeric_locale.decimal_point;
81		l->grouping =
82		    __fix_locale_grouping_str(l->grouping);
83	}
84	return (ret);
85}
86
87int
88__numeric_load_locale(const char *name)
89{
90	return numeric_load_locale(&__xlocale_global_numeric,
91			&__xlocale_global_locale.using_numeric_locale,
92			&__xlocale_global_locale.numeric_locale_changed, name);
93}
94void *
95__numeric_load(const char *name, locale_t l)
96{
97	struct xlocale_numeric *new = calloc(sizeof(struct xlocale_numeric), 1);
98	new->header.header.destructor = destruct_numeric;
99	if (numeric_load_locale(new, &l->using_numeric_locale,
100				&l->numeric_locale_changed, name) == _LDP_ERROR)
101	{
102		xlocale_release(new);
103		return NULL;
104	}
105	return new;
106}
107
108struct lc_numeric_T *
109__get_current_numeric_locale(locale_t loc)
110{
111	return (loc->using_numeric_locale
112		? &((struct xlocale_numeric *)loc->components[XLC_NUMERIC])->locale
113		: (struct lc_numeric_T *)&_C_numeric_locale);
114}
115
116#ifdef LOCALE_DEBUG
117void
118numericdebug(void) {
119printf(	"decimal_point = %s\n"
120	"thousands_sep = %s\n"
121	"grouping = %s\n",
122	_numeric_locale.decimal_point,
123	_numeric_locale.thousands_sep,
124	_numeric_locale.grouping
125);
126}
127#endif /* LOCALE_DEBUG */
128