]>
git.ipfire.org Git - thirdparty/glibc.git/blob - 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.
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.
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.
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. */
30 #include "linereader.h"
31 #include "localedef.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
;
47 numeric_startup (struct linereader
*lr
, struct localedef_t
*locale
,
52 struct locale_numeric_t
*numeric
;
54 locale
->categories
[LC_NUMERIC
].numeric
= numeric
=
55 (struct locale_numeric_t
*) xcalloc (1, sizeof (*numeric
));
57 numeric
->grouping
= NULL
;
58 numeric
->grouping_len
= 0;
61 lr
->translate_strings
= 1;
62 lr
->return_widestr
= 0;
67 numeric_finish (struct localedef_t
*locale
, struct charmap_t
*charmap
)
69 struct locale_numeric_t
*numeric
= locale
->categories
[LC_NUMERIC
].numeric
;
71 #define TEST_ELEM(cat) \
72 if (numeric->cat == NULL && !be_quiet) \
73 error (0, 0, _("%s: field `%s' not defined"), "LC_NUMERIC", #cat)
75 TEST_ELEM (decimal_point
);
76 TEST_ELEM (thousands_sep
);
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
81 if (numeric
->decimal_point
[0] == '\0' && !be_quiet
)
84 %s: value for field `%s' must not be the empty string"),
85 "LC_NUMERIC", "decimal_point");
88 if (numeric
->grouping_len
== 0 && !be_quiet
)
89 error (0, 0, _("%s: field `%s' not defined"), "LC_NUMERIC", "grouping");
94 numeric_output (struct localedef_t
*locale
, struct charmap_t
*charmap
,
95 const char *output_path
)
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
)];
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
);
109 iov
[cnt
].iov_base
= (void *) idx
;
110 iov
[cnt
].iov_len
= sizeof (idx
);
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;
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;
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
;
127 assert (cnt
+ 1 == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC
));
129 write_locale_data (output_path
, "LC_NUMERIC",
130 2 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC
), iov
);
134 /* The parser for the LC_NUMERIC section of the locale definition. */
136 numeric_read (struct linereader
*ldfile
, struct localedef_t
*result
,
137 struct charmap_t
*charmap
, const char *repertoire_name
,
140 struct repertoire_t
*repertoire
= NULL
;
141 struct locale_numeric_t
*numeric
;
145 /* Get the repertoire we have to use. */
146 if (repertoire_name
!= NULL
)
147 repertoire
= repertoire_read (repertoire_name
);
149 /* The rest of the line containing `LC_NUMERIC' must be free. */
150 lr_ignore_rest (ldfile
, 1);
155 now
= lr_token (ldfile
, charmap
, NULL
);
158 while (nowtok
== tok_eol
);
160 /* If we see `copy' now we are almost done. */
161 if (nowtok
== tok_copy
)
163 handle_copy (ldfile
, charmap
, repertoire
, tok_lc_numeric
, LC_NUMERIC
,
164 "LC_NUMERIC", ignore_content
);
168 /* Prepare the data structures. */
169 numeric_startup (ldfile
, result
, ignore_content
);
170 numeric
= result
->categories
[LC_NUMERIC
].numeric
;
174 /* Of course we don't proceed beyond the end of file. */
175 if (nowtok
== tok_eof
)
178 /* Ingore empty lines. */
179 if (nowtok
== tok_eol
)
181 now
= lr_token (ldfile
, charmap
, NULL
);
188 #define STR_ELEM(cat) \
190 now = lr_token (ldfile, charmap, NULL); \
191 if (now->tok != tok_string) \
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) \
198 lr_error (ldfile, _("\
199 %s: unknown character in field `%s'"), "LC_NUMERIC", #cat); \
202 else if (!ignore_content) \
203 numeric->cat = now->val.str.startmb; \
206 STR_ELEM (decimal_point
);
207 STR_ELEM (thousands_sep
);
210 now
= lr_token (ldfile
, charmap
, NULL
);
211 if (now
->tok
!= tok_minus1
&& now
->tok
!= tok_number
)
217 char *grouping
= ignore_content
? NULL
: xmalloc (max
);
224 grouping
= xrealloc (grouping
, max
);
227 if (act
> 0 && grouping
[act
- 1] == '\177')
229 lr_error (ldfile
, _("\
230 %s: `-1' must be last entry in `%s' field"), "LC_NUMERIC", "grouping");
231 lr_ignore_rest (ldfile
, 0);
235 if (now
->tok
== tok_minus1
)
238 grouping
[act
++] = '\177';
240 else if (now
->val
.num
== 0)
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. */
247 grouping
[act
++] = '\377';
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
;
256 /* Next must be semicolon. */
257 now
= lr_token (ldfile
, charmap
, NULL
);
258 if (now
->tok
!= tok_semicolon
)
261 now
= lr_token (ldfile
, charmap
, NULL
);
263 while (now
->tok
== tok_minus1
|| now
->tok
== tok_number
);
265 if (now
->tok
!= tok_eol
)
270 grouping
[act
++] = '\0';
272 numeric
->grouping
= xrealloc (grouping
, act
);
273 numeric
->grouping_len
= act
;
279 /* Next we assume `LC_NUMERIC'. */
280 now
= lr_token (ldfile
, charmap
, NULL
);
281 if (now
->tok
== tok_eof
)
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
);
293 SYNTAX_ERROR (_("%s: syntax error"), "LC_NUMERIC");
296 /* Prepare for the next round. */
297 now
= lr_token (ldfile
, charmap
, NULL
);
301 /* When we come here we reached the end of the file. */
302 lr_error (ldfile
, _("%s: premature end of file"), "LC_NUMERIC");