]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/locale/lnumeric.c
MFV 66082b6c88b9: libbsdxml (expat) 2.4.9
[FreeBSD/FreeBSD.git] / lib / libc / locale / lnumeric.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2000, 2001 Alexey Zelkin <phantom@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Copyright (c) 2011 The FreeBSD Foundation
8  *
9  * Portions of this software were developed by David Chisnall
10  * under sponsorship from the FreeBSD Foundation.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <limits.h>
38
39 #include "ldpart.h"
40 #include "lnumeric.h"
41
42 extern const char *__fix_locale_grouping_str(const char *);
43
44 #define LCNUMERIC_SIZE (sizeof(struct lc_numeric_T) / sizeof(char *))
45
46 static char     numempty[] = { CHAR_MAX, '\0' };
47
48 static const struct lc_numeric_T _C_numeric_locale = {
49         ".",            /* decimal_point */
50         "",             /* thousands_sep */
51         numempty        /* grouping */
52 };
53
54 static void
55 destruct_numeric(void *v)
56 {
57         struct xlocale_numeric *l = v;
58         if (l->buffer)
59                 free(l->buffer);
60         free(l);
61 }
62
63 struct xlocale_numeric __xlocale_global_numeric;
64
65 static int
66 numeric_load_locale(struct xlocale_numeric *loc, int *using_locale,
67     int *changed, const char *name)
68 {
69         int ret;
70         struct lc_numeric_T *l = &loc->locale;
71
72         ret = __part_load_locale(name, using_locale,
73             &loc->buffer, "LC_NUMERIC",
74             LCNUMERIC_SIZE, LCNUMERIC_SIZE,
75             (const char**)l);
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         if (ret != _LDP_ERROR)
85                 atomic_store_rel_int(changed, 1);
86         return (ret);
87 }
88
89 int
90 __numeric_load_locale(const char *name)
91 {
92         return (numeric_load_locale(&__xlocale_global_numeric,
93             &__xlocale_global_locale.using_numeric_locale,
94             &__xlocale_global_locale.numeric_locale_changed, name));
95 }
96
97 void *
98 __numeric_load(const char *name, locale_t l)
99 {
100         struct xlocale_numeric *new = calloc(sizeof(struct xlocale_numeric),
101             1);
102         if (new == NULL)
103                 return (NULL);
104         new->header.header.destructor = destruct_numeric;
105         if (numeric_load_locale(new, &l->using_numeric_locale,
106             &l->numeric_locale_changed, name) == _LDP_ERROR) {
107                 xlocale_release(new);
108                 return (NULL);
109         }
110         return (new);
111 }
112
113 struct lc_numeric_T *
114 __get_current_numeric_locale(locale_t loc)
115 {
116         return (loc->using_numeric_locale ?
117             &((struct xlocale_numeric *)loc->components[XLC_NUMERIC])->locale :
118             (struct lc_numeric_T *)&_C_numeric_locale);
119 }
120
121 #ifdef LOCALE_DEBUG
122 void
123 numericdebug(void) {
124 printf( "decimal_point = %s\n"
125         "thousands_sep = %s\n"
126         "grouping = %s\n",
127         _numeric_locale.decimal_point,
128         _numeric_locale.thousands_sep,
129         _numeric_locale.grouping
130 );
131 }
132 #endif /* LOCALE_DEBUG */