1 /* Copyright (C) 1995-1999, 2000, 2001, 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.org>, 1995.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
30 #include "localedef.h"
31 #include "linereader.h"
32 #include "localeinfo.h"
36 /* The real definition of the struct for the LC_NUMERIC locale. */
37 struct locale_numeric_t
39 const char *decimal_point
;
40 const char *thousands_sep
;
43 uint32_t decimal_point_wc
;
44 uint32_t thousands_sep_wc
;
49 numeric_startup (struct linereader
*lr
, struct localedef_t
*locale
,
54 locale
->categories
[LC_NUMERIC
].numeric
=
55 (struct locale_numeric_t
*) xcalloc (1,
56 sizeof (struct locale_numeric_t
));
61 lr
->translate_strings
= 1;
62 lr
->return_widestr
= 0;
68 numeric_finish (struct localedef_t
*locale
, const struct charmap_t
*charmap
)
70 struct locale_numeric_t
*numeric
= locale
->categories
[LC_NUMERIC
].numeric
;
73 /* Now resolve copying and also handle completely missing definitions. */
76 /* First see whether we were supposed to copy. If yes, find the
78 if (locale
->copy_name
[LC_NUMERIC
] != NULL
)
80 /* Find the copying locale. This has to happen transitively since
81 the locale we are copying from might also copying another one. */
82 struct localedef_t
*from
= locale
;
85 from
= find_locale (LC_NUMERIC
, from
->copy_name
[LC_NUMERIC
],
86 from
->repertoire_name
, charmap
);
87 while (from
->categories
[LC_NUMERIC
].numeric
== NULL
88 && from
->copy_name
[LC_NUMERIC
] != NULL
);
90 numeric
= locale
->categories
[LC_NUMERIC
].numeric
91 = from
->categories
[LC_NUMERIC
].numeric
;
94 /* If there is still no definition issue an warning and create an
99 WITH_CUR_LOCALE (error (0, 0, _("\
100 No definition for %s category found"), "LC_NUMERIC"));
101 numeric_startup (NULL
, locale
, 0);
102 numeric
= locale
->categories
[LC_NUMERIC
].numeric
;
107 /* The decimal point must not be empty. This is not said explicitly
108 in POSIX but ANSI C (ISO/IEC 9899) says in 4.4.2.1 it has to be
110 if (numeric
->decimal_point
== NULL
)
112 if (! be_quiet
&& ! nothing
)
113 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
114 "LC_NUMERIC", "decimal_point"));
115 numeric
->decimal_point
= ".";
117 else if (numeric
->decimal_point
[0] == '\0' && ! be_quiet
&& ! nothing
)
119 WITH_CUR_LOCALE (error (0, 0, _("\
120 %s: value for field `%s' must not be the empty string"),
121 "LC_NUMERIC", "decimal_point"));
123 if (numeric
->decimal_point_wc
== L
'\0')
124 numeric
->decimal_point_wc
= L
'.';
126 if (numeric
->grouping_len
== 0 && ! be_quiet
&& ! nothing
)
127 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
128 "LC_NUMERIC", "grouping"));
133 numeric_output (struct localedef_t
*locale
, const struct charmap_t
*charmap
,
134 const char *output_path
)
136 struct locale_numeric_t
*numeric
= locale
->categories
[LC_NUMERIC
].numeric
;
137 struct iovec iov
[3 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC
)];
138 struct locale_file data
;
139 uint32_t idx
[_NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC
)];
142 data
.magic
= LIMAGIC (LC_NUMERIC
);
143 data
.n
= _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC
);
144 iov
[cnt
].iov_base
= (void *) &data
;
145 iov
[cnt
].iov_len
= sizeof (data
);
148 iov
[cnt
].iov_base
= (void *) idx
;
149 iov
[cnt
].iov_len
= sizeof (idx
);
152 idx
[cnt
- 2] = iov
[0].iov_len
+ iov
[1].iov_len
;
153 iov
[cnt
].iov_base
= (void *) (numeric
->decimal_point
?: "");
154 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
157 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
158 iov
[cnt
].iov_base
= (void *) (numeric
->thousands_sep
?: "");
159 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
162 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
163 iov
[cnt
].iov_base
= numeric
->grouping
;
164 iov
[cnt
].iov_len
= numeric
->grouping_len
;
167 idx
[cnt
- 2] = idx
[cnt
- 3] + iov
[cnt
- 1].iov_len
;
169 /* Align following data */
170 iov
[cnt
].iov_base
= (void *) "\0\0";
171 iov
[cnt
].iov_len
= ((idx
[cnt
- 2] + 3) & ~3) - idx
[cnt
- 2];
172 idx
[cnt
- 2] = (idx
[cnt
- 2] + 3) & ~3;
175 iov
[cnt
].iov_base
= (void *) &numeric
->decimal_point_wc
;
176 iov
[cnt
].iov_len
= sizeof (uint32_t);
179 idx
[cnt
- 3] = idx
[cnt
- 4] + iov
[cnt
- 1].iov_len
;
180 iov
[cnt
].iov_base
= (void *) &numeric
->thousands_sep_wc
;
181 iov
[cnt
].iov_len
= sizeof (uint32_t);
184 idx
[cnt
- 3] = idx
[cnt
- 4] + iov
[cnt
- 1].iov_len
;
185 iov
[cnt
].iov_base
= (void *) charmap
->code_set_name
;
186 iov
[cnt
].iov_len
= strlen (iov
[cnt
].iov_base
) + 1;
188 assert (cnt
+ 1 == 3 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC
));
190 write_locale_data (output_path
, LC_NUMERIC
, "LC_NUMERIC",
191 3 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC
), iov
);
195 /* The parser for the LC_NUMERIC section of the locale definition. */
197 numeric_read (struct linereader
*ldfile
, struct localedef_t
*result
,
198 const struct charmap_t
*charmap
, const char *repertoire_name
,
201 struct repertoire_t
*repertoire
= NULL
;
202 struct locale_numeric_t
*numeric
;
206 /* Get the repertoire we have to use. */
207 if (repertoire_name
!= NULL
)
208 repertoire
= repertoire_read (repertoire_name
);
210 /* The rest of the line containing `LC_NUMERIC' must be free. */
211 lr_ignore_rest (ldfile
, 1);
216 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
219 while (nowtok
== tok_eol
);
221 /* If we see `copy' now we are almost done. */
222 if (nowtok
== tok_copy
)
224 handle_copy (ldfile
, charmap
, repertoire_name
, result
, tok_lc_numeric
,
225 LC_NUMERIC
, "LC_NUMERIC", ignore_content
);
229 /* Prepare the data structures. */
230 numeric_startup (ldfile
, result
, ignore_content
);
231 numeric
= result
->categories
[LC_NUMERIC
].numeric
;
235 /* Of course we don't proceed beyond the end of file. */
236 if (nowtok
== tok_eof
)
239 /* Ingore empty lines. */
240 if (nowtok
== tok_eol
)
242 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
249 #define STR_ELEM(cat) \
251 /* Ignore the rest of the line if we don't need the input of \
253 if (ignore_content) \
255 lr_ignore_rest (ldfile, 0); \
259 ldfile->return_widestr = 1; \
260 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
261 if (now->tok != tok_string) \
263 if (numeric->cat != NULL) \
264 lr_error (ldfile, _("\
265 %s: field `%s' declared more than once"), "LC_NUMERIC", #cat); \
266 else if (!ignore_content && now->val.str.startmb == NULL) \
268 lr_error (ldfile, _("\
269 %s: unknown character in field `%s'"), "LC_NUMERIC", #cat); \
271 numeric->cat##_wc = L'\0'; \
273 else if (now->val.str.startwc != NULL && now->val.str.lenwc > 2) \
275 lr_error (ldfile, _("\
276 %s: value for field `%s' must be a single character"), "LC_NUMERIC", #cat); \
278 else if (!ignore_content) \
280 numeric->cat = now->val.str.startmb; \
282 if (now->val.str.startwc != NULL) \
283 numeric->cat##_wc = *now->val.str.startwc; \
285 ldfile->return_widestr = 0; \
288 STR_ELEM (decimal_point
);
289 STR_ELEM (thousands_sep
);
292 /* Ignore the rest of the line if we don't need the input of
296 lr_ignore_rest (ldfile
, 0);
300 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
301 if (now
->tok
!= tok_minus1
&& now
->tok
!= tok_number
)
307 char *grouping
= ignore_content
? NULL
: xmalloc (max
);
314 grouping
= xrealloc (grouping
, max
);
317 if (act
> 0 && grouping
[act
- 1] == '\177')
319 lr_error (ldfile
, _("\
320 %s: `-1' must be last entry in `%s' field"), "LC_NUMERIC", "grouping");
321 lr_ignore_rest (ldfile
, 0);
325 if (now
->tok
== tok_minus1
)
328 grouping
[act
++] = '\177';
330 else if (now
->val
.num
== 0)
332 /* A value of 0 disables grouping from here on but
333 we must not store a NUL character since this
334 terminates the string. Use something different
335 which must not be used otherwise. */
337 grouping
[act
++] = '\377';
339 else if (now
->val
.num
> 126)
340 lr_error (ldfile
, _("\
341 %s: values for field `%s' must be smaller than 127"),
342 "LC_NUMERIC", "grouping");
343 else if (!ignore_content
)
344 grouping
[act
++] = now
->val
.num
;
346 /* Next must be semicolon. */
347 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
348 if (now
->tok
!= tok_semicolon
)
351 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
353 while (now
->tok
== tok_minus1
|| now
->tok
== tok_number
);
355 if (now
->tok
!= tok_eol
)
360 grouping
[act
++] = '\0';
362 numeric
->grouping
= xrealloc (grouping
, act
);
363 numeric
->grouping_len
= act
;
369 /* Next we assume `LC_NUMERIC'. */
370 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
371 if (now
->tok
== tok_eof
)
373 if (now
->tok
== tok_eol
)
374 lr_error (ldfile
, _("%s: incomplete `END' line"), "LC_NUMERIC");
375 else if (now
->tok
!= tok_lc_numeric
)
376 lr_error (ldfile
, _("\
377 %1$s: definition does not end with `END %1$s'"), "LC_NUMERIC");
378 lr_ignore_rest (ldfile
, now
->tok
== tok_lc_numeric
);
383 SYNTAX_ERROR (_("%s: syntax error"), "LC_NUMERIC");
386 /* Prepare for the next round. */
387 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
391 /* When we come here we reached the end of the file. */
392 lr_error (ldfile
, _("%s: premature end of file"), "LC_NUMERIC");