1 /* Copyright (C) 1995-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published
6 by the Free Software Foundation; version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
28 #include "localedef.h"
29 #include "linereader.h"
30 #include "localeinfo.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
;
41 uint32_t decimal_point_wc
;
42 uint32_t thousands_sep_wc
;
47 numeric_startup (struct linereader
*lr
, struct localedef_t
*locale
,
52 locale
->categories
[LC_NUMERIC
].numeric
=
53 (struct locale_numeric_t
*) xcalloc (1,
54 sizeof (struct locale_numeric_t
));
59 lr
->translate_strings
= 1;
60 lr
->return_widestr
= 0;
66 numeric_finish (struct localedef_t
*locale
, const struct charmap_t
*charmap
)
68 struct locale_numeric_t
*numeric
= locale
->categories
[LC_NUMERIC
].numeric
;
71 /* Now resolve copying and also handle completely missing definitions. */
74 /* First see whether we were supposed to copy. If yes, find the
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
97 No definition for %s category found"), "LC_NUMERIC");
98 numeric_startup (NULL
, locale
, 0);
99 numeric
= locale
->categories
[LC_NUMERIC
].numeric
;
104 /* The decimal point must not be empty. This is not said explicitly
105 in POSIX but ANSI C (ISO/IEC 9899) says in 4.4.2.1 it has to be
107 if (numeric
->decimal_point
== NULL
)
110 record_error (0, 0, _("%s: field `%s' not defined"),
111 "LC_NUMERIC", "decimal_point");
112 numeric
->decimal_point
= ".";
114 else if (numeric
->decimal_point
[0] == '\0' && ! nothing
)
116 record_error (0, 0, _("\
117 %s: value for field `%s' must not be an empty string"),
118 "LC_NUMERIC", "decimal_point");
120 if (numeric
->decimal_point_wc
== L
'\0')
121 numeric
->decimal_point_wc
= L
'.';
123 if (numeric
->grouping_len
== 0 && ! nothing
)
124 record_error (0, 0, _("%s: field `%s' not defined"),
125 "LC_NUMERIC", "grouping");
130 numeric_output (struct localedef_t
*locale
, const struct charmap_t
*charmap
,
131 const char *output_path
)
133 struct locale_numeric_t
*numeric
= locale
->categories
[LC_NUMERIC
].numeric
;
134 struct locale_file file
;
136 init_locale_data (&file
, _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC
));
137 add_locale_string (&file
, numeric
->decimal_point
?: "");
138 add_locale_string (&file
, numeric
->thousands_sep
?: "");
139 add_locale_raw_data (&file
, numeric
->grouping
, numeric
->grouping_len
);
140 add_locale_uint32 (&file
, numeric
->decimal_point_wc
);
141 add_locale_uint32 (&file
, numeric
->thousands_sep_wc
);
142 add_locale_string (&file
, charmap
->code_set_name
);
143 write_locale_data (output_path
, LC_NUMERIC
, "LC_NUMERIC", &file
);
147 /* The parser for the LC_NUMERIC section of the locale definition. */
149 numeric_read (struct linereader
*ldfile
, struct localedef_t
*result
,
150 const struct charmap_t
*charmap
, const char *repertoire_name
,
153 struct repertoire_t
*repertoire
= NULL
;
154 struct locale_numeric_t
*numeric
;
158 /* Get the repertoire we have to use. */
159 if (repertoire_name
!= NULL
)
160 repertoire
= repertoire_read (repertoire_name
);
162 /* The rest of the line containing `LC_NUMERIC' must be free. */
163 lr_ignore_rest (ldfile
, 1);
168 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
171 while (nowtok
== tok_eol
);
173 /* If we see `copy' now we are almost done. */
174 if (nowtok
== tok_copy
)
176 handle_copy (ldfile
, charmap
, repertoire_name
, result
, tok_lc_numeric
,
177 LC_NUMERIC
, "LC_NUMERIC", ignore_content
);
181 /* Prepare the data structures. */
182 numeric_startup (ldfile
, result
, ignore_content
);
183 numeric
= result
->categories
[LC_NUMERIC
].numeric
;
187 /* Of course we don't proceed beyond the end of file. */
188 if (nowtok
== tok_eof
)
191 /* Ingore empty lines. */
192 if (nowtok
== tok_eol
)
194 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
201 #define STR_ELEM(cat) \
203 /* Ignore the rest of the line if we don't need the input of \
205 if (ignore_content) \
207 lr_ignore_rest (ldfile, 0); \
211 ldfile->return_widestr = 1; \
212 now = lr_token (ldfile, charmap, result, repertoire, verbose); \
213 if (now->tok != tok_string) \
215 if (numeric->cat != NULL) \
216 lr_error (ldfile, _("\
217 %s: field `%s' declared more than once"), "LC_NUMERIC", #cat); \
218 else if (!ignore_content && now->val.str.startmb == NULL) \
220 lr_error (ldfile, _("\
221 %s: unknown character in field `%s'"), "LC_NUMERIC", #cat); \
223 numeric->cat##_wc = L'\0'; \
225 else if (now->val.str.startwc != NULL && now->val.str.lenwc > 2) \
227 lr_error (ldfile, _("\
228 %s: value for field `%s' must be a single character"), "LC_NUMERIC", #cat); \
230 else if (!ignore_content) \
232 numeric->cat = now->val.str.startmb; \
234 if (now->val.str.startwc != NULL) \
235 numeric->cat##_wc = *now->val.str.startwc; \
237 ldfile->return_widestr = 0; \
240 STR_ELEM (decimal_point
);
241 STR_ELEM (thousands_sep
);
244 /* Ignore the rest of the line if we don't need the input of
248 lr_ignore_rest (ldfile
, 0);
252 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
253 if (now
->tok
!= tok_minus1
&& now
->tok
!= tok_number
)
259 char *grouping
= xmalloc (max
);
266 grouping
= xrealloc (grouping
, max
);
269 if (act
> 0 && grouping
[act
- 1] == '\177')
271 lr_error (ldfile
, _("\
272 %s: `-1' must be last entry in `%s' field"), "LC_NUMERIC", "grouping");
273 lr_ignore_rest (ldfile
, 0);
277 if (now
->tok
== tok_minus1
)
278 grouping
[act
++] = '\177';
279 else if (now
->val
.num
== 0)
281 /* A value of 0 disables grouping from here on but
282 we must not store a NUL character since this
283 terminates the string. Use something different
284 which must not be used otherwise. */
285 grouping
[act
++] = '\377';
287 else if (now
->val
.num
> 126)
288 lr_error (ldfile
, _("\
289 %s: values for field `%s' must be smaller than 127"),
290 "LC_NUMERIC", "grouping");
292 grouping
[act
++] = now
->val
.num
;
294 /* Next must be semicolon. */
295 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
296 if (now
->tok
!= tok_semicolon
)
299 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
301 while (now
->tok
== tok_minus1
|| now
->tok
== tok_number
);
303 if (now
->tok
!= tok_eol
)
306 /* A single -1 means no grouping. */
307 if (act
== 1 && grouping
[0] == '\177')
309 grouping
[act
++] = '\0';
311 numeric
->grouping
= xrealloc (grouping
, act
);
312 numeric
->grouping_len
= act
;
317 /* Next we assume `LC_NUMERIC'. */
318 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
319 if (now
->tok
== tok_eof
)
321 if (now
->tok
== tok_eol
)
322 lr_error (ldfile
, _("%s: incomplete `END' line"), "LC_NUMERIC");
323 else if (now
->tok
!= tok_lc_numeric
)
324 lr_error (ldfile
, _("\
325 %1$s: definition does not end with `END %1$s'"), "LC_NUMERIC");
326 lr_ignore_rest (ldfile
, now
->tok
== tok_lc_numeric
);
331 SYNTAX_ERROR (_("%s: syntax error"), "LC_NUMERIC");
334 /* Prepare for the next round. */
335 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
339 /* When we come here we reached the end of the file. */
340 lr_error (ldfile
, _("%s: premature end of file"), "LC_NUMERIC");