PowerPC: remove branch prediction from rint implementation
[glibc.git] / locale / programs / ld-numeric.c
blobf8e8f82b53dddb687bcce89472bea84f25b4f66f
1 /* Copyright (C) 1995-2002,2005,2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.org>, 1995.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; version 2 of the License, or
8 (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>. */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 #include <langinfo.h>
23 #include <string.h>
24 #include <sys/uio.h>
26 #include <assert.h>
28 #include "localedef.h"
29 #include "linereader.h"
30 #include "localeinfo.h"
31 #include "locfile.h"
34 /* The real definition of the struct for the LC_NUMERIC locale. */
35 struct locale_numeric_t
37 const char *decimal_point;
38 const char *thousands_sep;
39 char *grouping;
40 size_t grouping_len;
41 uint32_t decimal_point_wc;
42 uint32_t thousands_sep_wc;
46 static void
47 numeric_startup (struct linereader *lr, struct localedef_t *locale,
48 int ignore_content)
50 if (!ignore_content)
52 locale->categories[LC_NUMERIC].numeric =
53 (struct locale_numeric_t *) xcalloc (1,
54 sizeof (struct locale_numeric_t));
57 if (lr != NULL)
59 lr->translate_strings = 1;
60 lr->return_widestr = 0;
65 void
66 numeric_finish (struct localedef_t *locale, const struct charmap_t *charmap)
68 struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric;
69 int nothing = 0;
71 /* Now resolve copying and also handle completely missing definitions. */
72 if (numeric == NULL)
74 /* First see whether we were supposed to copy. If yes, find the
75 actual definition. */
76 if (locale->copy_name[LC_NUMERIC] != NULL)
78 /* Find the copying locale. This has to happen transitively since
79 the locale we are copying from might also copying another one. */
80 struct localedef_t *from = locale;
83 from = find_locale (LC_NUMERIC, from->copy_name[LC_NUMERIC],
84 from->repertoire_name, charmap);
85 while (from->categories[LC_NUMERIC].numeric == NULL
86 && from->copy_name[LC_NUMERIC] != NULL);
88 numeric = locale->categories[LC_NUMERIC].numeric
89 = from->categories[LC_NUMERIC].numeric;
92 /* If there is still no definition issue an warning and create an
93 empty one. */
94 if (numeric == NULL)
96 if (! be_quiet)
97 WITH_CUR_LOCALE (error (0, 0, _("\
98 No definition for %s category found"), "LC_NUMERIC"));
99 numeric_startup (NULL, locale, 0);
100 numeric = locale->categories[LC_NUMERIC].numeric;
101 nothing = 1;
105 /* The decimal point must not be empty. This is not said explicitly
106 in POSIX but ANSI C (ISO/IEC 9899) says in 4.4.2.1 it has to be
107 != "". */
108 if (numeric->decimal_point == NULL)
110 if (! be_quiet && ! nothing)
111 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
112 "LC_NUMERIC", "decimal_point"));
113 numeric->decimal_point = ".";
115 else if (numeric->decimal_point[0] == '\0' && ! be_quiet && ! nothing)
117 WITH_CUR_LOCALE (error (0, 0, _("\
118 %s: value for field `%s' must not be an empty string"),
119 "LC_NUMERIC", "decimal_point"));
121 if (numeric->decimal_point_wc == L'\0')
122 numeric->decimal_point_wc = L'.';
124 if (numeric->grouping_len == 0 && ! be_quiet && ! nothing)
125 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
126 "LC_NUMERIC", "grouping"));
130 void
131 numeric_output (struct localedef_t *locale, const struct charmap_t *charmap,
132 const char *output_path)
134 struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric;
135 struct iovec iov[3 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC)];
136 struct locale_file data;
137 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC)];
138 size_t cnt = 0;
140 data.magic = LIMAGIC (LC_NUMERIC);
141 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC);
142 iov[cnt].iov_base = (void *) &data;
143 iov[cnt].iov_len = sizeof (data);
144 ++cnt;
146 iov[cnt].iov_base = (void *) idx;
147 iov[cnt].iov_len = sizeof (idx);
148 ++cnt;
150 idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
151 iov[cnt].iov_base = (void *) (numeric->decimal_point ?: "");
152 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
153 ++cnt;
155 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
156 iov[cnt].iov_base = (void *) (numeric->thousands_sep ?: "");
157 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
158 ++cnt;
160 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
161 iov[cnt].iov_base = numeric->grouping;
162 iov[cnt].iov_len = numeric->grouping_len;
163 ++cnt;
165 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
167 /* Align following data */
168 iov[cnt].iov_base = (void *) "\0\0";
169 iov[cnt].iov_len = ((idx[cnt - 2] + 3) & ~3) - idx[cnt - 2];
170 idx[cnt - 2] = (idx[cnt - 2] + 3) & ~3;
171 ++cnt;
173 iov[cnt].iov_base = (void *) &numeric->decimal_point_wc;
174 iov[cnt].iov_len = sizeof (uint32_t);
175 ++cnt;
177 idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
178 iov[cnt].iov_base = (void *) &numeric->thousands_sep_wc;
179 iov[cnt].iov_len = sizeof (uint32_t);
180 ++cnt;
182 idx[cnt - 3] = idx[cnt - 4] + iov[cnt - 1].iov_len;
183 iov[cnt].iov_base = (void *) charmap->code_set_name;
184 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
186 assert (cnt + 1 == 3 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC));
188 write_locale_data (output_path, LC_NUMERIC, "LC_NUMERIC",
189 3 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC), iov);
193 /* The parser for the LC_NUMERIC section of the locale definition. */
194 void
195 numeric_read (struct linereader *ldfile, struct localedef_t *result,
196 const struct charmap_t *charmap, const char *repertoire_name,
197 int ignore_content)
199 struct repertoire_t *repertoire = NULL;
200 struct locale_numeric_t *numeric;
201 struct token *now;
202 enum token_t nowtok;
204 /* Get the repertoire we have to use. */
205 if (repertoire_name != NULL)
206 repertoire = repertoire_read (repertoire_name);
208 /* The rest of the line containing `LC_NUMERIC' must be free. */
209 lr_ignore_rest (ldfile, 1);
214 now = lr_token (ldfile, charmap, result, NULL, verbose);
215 nowtok = now->tok;
217 while (nowtok == tok_eol);
219 /* If we see `copy' now we are almost done. */
220 if (nowtok == tok_copy)
222 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_numeric,
223 LC_NUMERIC, "LC_NUMERIC", ignore_content);
224 return;
227 /* Prepare the data structures. */
228 numeric_startup (ldfile, result, ignore_content);
229 numeric = result->categories[LC_NUMERIC].numeric;
231 while (1)
233 /* Of course we don't proceed beyond the end of file. */
234 if (nowtok == tok_eof)
235 break;
237 /* Ingore empty lines. */
238 if (nowtok == tok_eol)
240 now = lr_token (ldfile, charmap, result, NULL, verbose);
241 nowtok = now->tok;
242 continue;
245 switch (nowtok)
247 #define STR_ELEM(cat) \
248 case tok_##cat: \
249 /* Ignore the rest of the line if we don't need the input of \
250 this line. */ \
251 if (ignore_content) \
253 lr_ignore_rest (ldfile, 0); \
254 break; \
257 ldfile->return_widestr = 1; \
258 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
259 if (now->tok != tok_string) \
260 goto err_label; \
261 if (numeric->cat != NULL) \
262 lr_error (ldfile, _("\
263 %s: field `%s' declared more than once"), "LC_NUMERIC", #cat); \
264 else if (!ignore_content && now->val.str.startmb == NULL) \
266 lr_error (ldfile, _("\
267 %s: unknown character in field `%s'"), "LC_NUMERIC", #cat); \
268 numeric->cat = ""; \
269 numeric->cat##_wc = L'\0'; \
271 else if (now->val.str.startwc != NULL && now->val.str.lenwc > 2) \
273 lr_error (ldfile, _("\
274 %s: value for field `%s' must be a single character"), "LC_NUMERIC", #cat); \
276 else if (!ignore_content) \
278 numeric->cat = now->val.str.startmb; \
280 if (now->val.str.startwc != NULL) \
281 numeric->cat##_wc = *now->val.str.startwc; \
283 ldfile->return_widestr = 0; \
284 break
286 STR_ELEM (decimal_point);
287 STR_ELEM (thousands_sep);
289 case tok_grouping:
290 /* Ignore the rest of the line if we don't need the input of
291 this line. */
292 if (ignore_content)
294 lr_ignore_rest (ldfile, 0);
295 break;
298 now = lr_token (ldfile, charmap, result, NULL, verbose);
299 if (now->tok != tok_minus1 && now->tok != tok_number)
300 goto err_label;
301 else
303 size_t act = 0;
304 size_t max = 10;
305 char *grouping = xmalloc (max);
309 if (act + 1 >= max)
311 max *= 2;
312 grouping = xrealloc (grouping, max);
315 if (act > 0 && grouping[act - 1] == '\177')
317 lr_error (ldfile, _("\
318 %s: `-1' must be last entry in `%s' field"), "LC_NUMERIC", "grouping");
319 lr_ignore_rest (ldfile, 0);
320 break;
323 if (now->tok == tok_minus1)
324 grouping[act++] = '\177';
325 else if (now->val.num == 0)
327 /* A value of 0 disables grouping from here on but
328 we must not store a NUL character since this
329 terminates the string. Use something different
330 which must not be used otherwise. */
331 grouping[act++] = '\377';
333 else if (now->val.num > 126)
334 lr_error (ldfile, _("\
335 %s: values for field `%s' must be smaller than 127"),
336 "LC_NUMERIC", "grouping");
337 else
338 grouping[act++] = now->val.num;
340 /* Next must be semicolon. */
341 now = lr_token (ldfile, charmap, result, NULL, verbose);
342 if (now->tok != tok_semicolon)
343 break;
345 now = lr_token (ldfile, charmap, result, NULL, verbose);
347 while (now->tok == tok_minus1 || now->tok == tok_number);
349 if (now->tok != tok_eol)
350 goto err_label;
352 grouping[act++] = '\0';
354 numeric->grouping = xrealloc (grouping, act);
355 numeric->grouping_len = act;
357 break;
359 case tok_end:
360 /* Next we assume `LC_NUMERIC'. */
361 now = lr_token (ldfile, charmap, result, NULL, verbose);
362 if (now->tok == tok_eof)
363 break;
364 if (now->tok == tok_eol)
365 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_NUMERIC");
366 else if (now->tok != tok_lc_numeric)
367 lr_error (ldfile, _("\
368 %1$s: definition does not end with `END %1$s'"), "LC_NUMERIC");
369 lr_ignore_rest (ldfile, now->tok == tok_lc_numeric);
370 return;
372 default:
373 err_label:
374 SYNTAX_ERROR (_("%s: syntax error"), "LC_NUMERIC");
377 /* Prepare for the next round. */
378 now = lr_token (ldfile, charmap, result, NULL, verbose);
379 nowtok = now->tok;
382 /* When we come here we reached the end of the file. */
383 lr_error (ldfile, _("%s: premature end of file"), "LC_NUMERIC");