]> git.ipfire.org Git - thirdparty/glibc.git/blob - locale/programs/ld-numeric.c
Update.
[thirdparty/glibc.git] / locale / programs / ld-numeric.c
1 /* Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.org>, 1995.
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 not,
17 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 #include <sys/uio.h>
27
28 #include <assert.h>
29
30 #include "linereader.h"
31 #include "localedef.h"
32 #include "localeinfo.h"
33 #include "locfile.h"
34
35
36 /* The real definition of the struct for the LC_NUMERIC locale. */
37 struct locale_numeric_t
38 {
39 const char *decimal_point;
40 const char *thousands_sep;
41 char *grouping;
42 size_t grouping_len;
43 };
44
45
46 static void
47 numeric_startup (struct linereader *lr, struct localedef_t *locale,
48 int ignore_content)
49 {
50 if (!ignore_content)
51 {
52 struct locale_numeric_t *numeric;
53
54 locale->categories[LC_NUMERIC].numeric = numeric =
55 (struct locale_numeric_t *) xcalloc (1, sizeof (*numeric));
56
57 numeric->grouping = NULL;
58 numeric->grouping_len = 0;
59 }
60
61 lr->translate_strings = 1;
62 lr->return_widestr = 0;
63 }
64
65
66 void
67 numeric_finish (struct localedef_t *locale, struct charmap_t *charmap)
68 {
69 struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric;
70
71 #define TEST_ELEM(cat) \
72 if (numeric->cat == NULL && !be_quiet) \
73 error (0, 0, _("%s: field `%s' not defined"), "LC_NUMERIC", #cat)
74
75 TEST_ELEM (decimal_point);
76 TEST_ELEM (thousands_sep);
77
78 /* The decimal point must not be empty. This is not said explicitly
79 in POSIX but ANSI C (ISO/IEC 9899) says in 4.4.2.1 it has to be
80 != "". */
81 if (numeric->decimal_point[0] == '\0' && !be_quiet)
82 {
83 error (0, 0, _("\
84 %s: value for field `%s' must not be the empty string"),
85 "LC_NUMERIC", "decimal_point");
86 }
87
88 if (numeric->grouping_len == 0 && !be_quiet)
89 error (0, 0, _("%s: field `%s' not defined"), "LC_NUMERIC", "grouping");
90 }
91
92
93 void
94 numeric_output (struct localedef_t *locale, struct charmap_t *charmap,
95 const char *output_path)
96 {
97 struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric;
98 struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC)];
99 struct locale_file data;
100 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC)];
101 size_t cnt = 0;
102
103 data.magic = LIMAGIC (LC_NUMERIC);
104 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC);
105 iov[cnt].iov_base = (void *) &data;
106 iov[cnt].iov_len = sizeof (data);
107 ++cnt;
108
109 iov[cnt].iov_base = (void *) idx;
110 iov[cnt].iov_len = sizeof (idx);
111 ++cnt;
112
113 idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
114 iov[cnt].iov_base = (void *) (numeric->decimal_point ?: "");
115 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
116 ++cnt;
117
118 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
119 iov[cnt].iov_base = (void *) (numeric->thousands_sep ?: "");
120 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
121 ++cnt;
122
123 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
124 iov[cnt].iov_base = numeric->grouping;
125 iov[cnt].iov_len = numeric->grouping_len;
126
127 assert (cnt + 1 == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC));
128
129 write_locale_data (output_path, "LC_NUMERIC",
130 2 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC), iov);
131 }
132
133
134 /* The parser for the LC_NUMERIC section of the locale definition. */
135 void
136 numeric_read (struct linereader *ldfile, struct localedef_t *result,
137 struct charmap_t *charmap, const char *repertoire_name,
138 int ignore_content)
139 {
140 struct repertoire_t *repertoire = NULL;
141 struct locale_numeric_t *numeric;
142 struct token *now;
143 enum token_t nowtok;
144
145 /* Get the repertoire we have to use. */
146 if (repertoire_name != NULL)
147 repertoire = repertoire_read (repertoire_name);
148
149 /* The rest of the line containing `LC_NUMERIC' must be free. */
150 lr_ignore_rest (ldfile, 1);
151
152
153 do
154 {
155 now = lr_token (ldfile, charmap, NULL);
156 nowtok = now->tok;
157 }
158 while (nowtok == tok_eol);
159
160 /* If we see `copy' now we are almost done. */
161 if (nowtok == tok_copy)
162 {
163 handle_copy (ldfile, charmap, repertoire, tok_lc_numeric, LC_NUMERIC,
164 "LC_NUMERIC", ignore_content);
165 return;
166 }
167
168 /* Prepare the data structures. */
169 numeric_startup (ldfile, result, ignore_content);
170 numeric = result->categories[LC_NUMERIC].numeric;
171
172 while (1)
173 {
174 /* Of course we don't proceed beyond the end of file. */
175 if (nowtok == tok_eof)
176 break;
177
178 /* Ingore empty lines. */
179 if (nowtok == tok_eol)
180 {
181 now = lr_token (ldfile, charmap, NULL);
182 nowtok = now->tok;
183 continue;
184 }
185
186 switch (nowtok)
187 {
188 #define STR_ELEM(cat) \
189 case tok_##cat: \
190 now = lr_token (ldfile, charmap, NULL); \
191 if (now->tok != tok_string) \
192 goto err_label; \
193 if (numeric->cat != NULL) \
194 lr_error (ldfile, _("\
195 %s: field `%s' declared more than once"), "LC_NUMERIC", #cat); \
196 else if (!ignore_content && now->val.str.startmb == NULL) \
197 { \
198 lr_error (ldfile, _("\
199 %s: unknown character in field `%s'"), "LC_NUMERIC", #cat); \
200 numeric->cat = ""; \
201 } \
202 else if (!ignore_content) \
203 numeric->cat = now->val.str.startmb; \
204 break
205
206 STR_ELEM (decimal_point);
207 STR_ELEM (thousands_sep);
208
209 case tok_grouping:
210 now = lr_token (ldfile, charmap, NULL);
211 if (now->tok != tok_minus1 && now->tok != tok_number)
212 goto err_label;
213 else
214 {
215 size_t act = 0;
216 size_t max = 10;
217 char *grouping = ignore_content ? NULL : xmalloc (max);
218
219 do
220 {
221 if (act + 1 >= max)
222 {
223 max *= 2;
224 grouping = xrealloc (grouping, max);
225 }
226
227 if (act > 0 && grouping[act - 1] == '\177')
228 {
229 lr_error (ldfile, _("\
230 %s: `-1' must be last entry in `%s' field"), "LC_NUMERIC", "grouping");
231 lr_ignore_rest (ldfile, 0);
232 break;
233 }
234
235 if (now->tok == tok_minus1)
236 {
237 if (!ignore_content)
238 grouping[act++] = '\177';
239 }
240 else if (now->val.num == 0)
241 {
242 /* A value of 0 disables grouping from here on but
243 we must not store a NUL character since this
244 terminates the string. Use something different
245 which must not be used otherwise. */
246 if (!ignore_content)
247 grouping[act++] = '\377';
248 }
249 else if (now->val.num > 126)
250 lr_error (ldfile, _("\
251 %s: values for field `%s' must be smaller than 127"),
252 "LC_NUMERIC", "grouping");
253 else if (!ignore_content)
254 grouping[act++] = now->val.num;
255
256 /* Next must be semicolon. */
257 now = lr_token (ldfile, charmap, NULL);
258 if (now->tok != tok_semicolon)
259 break;
260
261 now = lr_token (ldfile, charmap, NULL);
262 }
263 while (now->tok == tok_minus1 || now->tok == tok_number);
264
265 if (now->tok != tok_eol)
266 goto err_label;
267
268 if (!ignore_content)
269 {
270 grouping[act++] = '\0';
271
272 numeric->grouping = xrealloc (grouping, act);
273 numeric->grouping_len = act;
274 }
275 }
276 break;
277
278 case tok_end:
279 /* Next we assume `LC_NUMERIC'. */
280 now = lr_token (ldfile, charmap, NULL);
281 if (now->tok == tok_eof)
282 break;
283 if (now->tok == tok_eol)
284 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_NUMERIC");
285 else if (now->tok != tok_lc_numeric)
286 lr_error (ldfile, _("\
287 %1$s: definition does not end with `END %1$s'"), "LC_NUMERIC");
288 lr_ignore_rest (ldfile, now->tok == tok_lc_numeric);
289 return;
290
291 default:
292 err_label:
293 SYNTAX_ERROR (_("%s: syntax error"), "LC_NUMERIC");
294 }
295
296 /* Prepare for the next round. */
297 now = lr_token (ldfile, charmap, NULL);
298 nowtok = now->tok;
299 }
300
301 /* When we come here we reached the end of the file. */
302 lr_error (ldfile, _("%s: premature end of file"), "LC_NUMERIC");
303 }