]> git.ipfire.org Git - thirdparty/glibc.git/blob - locale/programs/ld-numeric.c
0b5fe2afe5ef263e615a8a8a49c707f6fc22e021
[thirdparty/glibc.git] / locale / programs / ld-numeric.c
1 /* Copyright (C) 1995, 1996 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper, <drepper@gnu.ai.mit.edu>.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <langinfo.h>
25 #include <string.h>
26
27 /* Undefine following line in production version. */
28 /* #define NDEBUG 1 */
29 #include <assert.h>
30
31 #include "locales.h"
32 #include "localeinfo.h"
33 #include "stringtrans.h"
34
35 void *xmalloc (size_t __n);
36 void *xrealloc (void *__ptr, size_t __n);
37
38
39 /* The real definition of the struct for the LC_NUMERIC locale. */
40 struct locale_numeric_t
41 {
42 const char *decimal_point;
43 const char *thousands_sep;
44 char *grouping;
45 size_t grouping_max;
46 size_t grouping_act;
47 };
48
49
50 void
51 numeric_startup (struct linereader *lr, struct localedef_t *locale,
52 struct charset_t *charset)
53 {
54 struct locale_numeric_t *numeric;
55
56 /* It is important that we always use UCS1 encoding for strings now. */
57 encoding_method = ENC_UCS1;
58
59 locale->categories[LC_NUMERIC].numeric = numeric =
60 (struct locale_numeric_t *) xmalloc (sizeof (struct locale_numeric_t));
61
62 memset (numeric, '\0', sizeof (struct locale_numeric_t));
63
64 numeric->grouping_max = 80;
65 numeric->grouping = (char *) xmalloc (numeric->grouping_max);
66 numeric->grouping_act = 0;
67 }
68
69
70 void
71 numeric_finish (struct localedef_t *locale)
72 {
73 struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric;
74
75 #define TEST_ELEM(cat) \
76 if (numeric->cat == NULL) \
77 error (0, 0, _("field `%s' in category `%s' not defined"), \
78 #cat, "LC_NUMERIC")
79
80 TEST_ELEM (decimal_point);
81 TEST_ELEM (thousands_sep);
82
83 /* The decimal point must not be empty. This is not said explicitly
84 in POSIX but ANSI C (ISO/IEC 9899) says in 4.4.2.1 it has to be
85 != "". */
86 if (numeric->decimal_point[0] == '\0')
87 {
88 error (0, 0, _("\
89 value for field `%s' in category `%s' must not be the empty string"),
90 "decimal_point", "LC_NUMERIC");
91 }
92
93 if (numeric->grouping_act == 0)
94 error (0, 0, _("field `%s' in category `%s' not defined"),
95 "grouping", "LC_NUMERIC");
96 }
97
98
99 void
100 numeric_output (struct localedef_t *locale, const char *output_path)
101 {
102 struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric;
103 struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC)];
104 struct locale_file data;
105 u32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC)];
106 size_t cnt = 0;
107
108 if ((locale->binary & (1 << LC_NUMERIC)) != 0)
109 {
110 iov[0].iov_base = numeric;
111 iov[0].iov_len = locale->len[LC_NUMERIC];
112
113 write_locale_data (output_path, "LC_NUMERIC", 1, iov);
114
115 return;
116 }
117
118 data.magic = LIMAGIC (LC_NUMERIC);
119 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC);
120 iov[cnt].iov_base = (void *) &data;
121 iov[cnt].iov_len = sizeof (data);
122 ++cnt;
123
124 iov[cnt].iov_base = (void *) idx;
125 iov[cnt].iov_len = sizeof (idx);
126 ++cnt;
127
128 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
129 iov[cnt].iov_base = (void *) (numeric->decimal_point ?: "");
130 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
131 ++cnt;
132
133 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
134 iov[cnt].iov_base = (void *) (numeric->thousands_sep ?: "");
135 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
136 ++cnt;
137
138 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
139 iov[cnt].iov_base = alloca (numeric->grouping_act + 1);
140 iov[cnt].iov_len = numeric->grouping_act + 1;
141 memcpy (iov[cnt].iov_base, numeric->grouping, numeric->grouping_act);
142 ((char *) iov[cnt].iov_base)[numeric->grouping_act] = '\0';
143
144 assert (cnt + 1 == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC));
145
146 write_locale_data (output_path, "LC_NUMERIC",
147 2 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC), iov);
148 }
149
150
151 void
152 numeric_add (struct linereader *lr, struct localedef_t *locale,
153 enum token_t tok, struct token *code,
154 struct charset_t *charset)
155 {
156 struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric;
157
158 switch (tok)
159 {
160 #define STR_ELEM(cat) \
161 case tok_##cat: \
162 if (numeric->cat != NULL) \
163 lr_error (lr, _("\
164 field `%s' in category `%s' declared more than once"), \
165 #cat, "LC_NUMERIC"); \
166 else if (code->val.str.start == NULL) \
167 { \
168 lr_error (lr, _("unknown character in field `%s' of category `%s'"),\
169 #cat, "LC_NUMERIC"); \
170 numeric->cat = ""; \
171 } \
172 else \
173 numeric->cat = code->val.str.start; \
174 break
175
176 STR_ELEM (decimal_point);
177 STR_ELEM (thousands_sep);
178
179 case tok_grouping:
180 if (numeric->grouping_act == numeric->grouping_max)
181 {
182 numeric->grouping_max *= 2;
183 numeric->grouping = (char *) xrealloc (numeric->grouping,
184 numeric->grouping_max);
185 }
186 if (numeric->grouping_act > 0
187 && (numeric->grouping[numeric->grouping_act - 1] == '\177'))
188 {
189 lr_error (lr, _("\
190 `-1' must be last entry in `%s' field in `%s' category"),
191 "grouping", "LC_NUMERIC");
192 --numeric->grouping_act;
193 }
194
195 if (code->tok == tok_minus1)
196 numeric->grouping[numeric->grouping_act++] = '\177';
197 else if (code->val.num > 126)
198 lr_error (lr, _("\
199 values for field `%s' in category `%s' must be smaller than 127"),
200 "grouping", "LC_NUMERIC");
201 else
202 numeric->grouping[numeric->grouping_act++] = code->val.num;
203 break;
204
205 default:
206 assert (! "unknown token in category `LC_NUMERIC': should not happen");
207 }
208 }