]>
Commit | Line | Data |
---|---|---|
c84142e8 | 1 | /* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. |
2c6fe0bd | 2 | This file is part of the GNU C Library. |
c84142e8 | 3 | Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. |
19bc17a9 | 4 | |
2c6fe0bd UD |
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. | |
19bc17a9 | 9 | |
2c6fe0bd UD |
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. | |
19bc17a9 | 14 | |
2c6fe0bd UD |
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. */ | |
19bc17a9 RM |
19 | |
20 | #ifdef HAVE_CONFIG_H | |
21 | # include <config.h> | |
22 | #endif | |
23 | ||
a1470b6f | 24 | #include <alloca.h> |
19bc17a9 RM |
25 | #include <langinfo.h> |
26 | #include <string.h> | |
27 | ||
28 | /* Undefine following line in production version. */ | |
29 | /* #define NDEBUG 1 */ | |
30 | #include <assert.h> | |
31 | ||
32 | #include "locales.h" | |
33 | #include "localeinfo.h" | |
34 | #include "stringtrans.h" | |
35 | ||
36 | void *xmalloc (size_t __n); | |
37 | void *xrealloc (void *__ptr, size_t __n); | |
38 | ||
39 | ||
40 | /* The real definition of the struct for the LC_NUMERIC locale. */ | |
41 | struct locale_numeric_t | |
42 | { | |
43 | const char *decimal_point; | |
44 | const char *thousands_sep; | |
45 | char *grouping; | |
46 | size_t grouping_max; | |
47 | size_t grouping_act; | |
48 | }; | |
49 | ||
50 | ||
51 | void | |
52 | numeric_startup (struct linereader *lr, struct localedef_t *locale, | |
53 | struct charset_t *charset) | |
54 | { | |
55 | struct locale_numeric_t *numeric; | |
56 | ||
57 | /* It is important that we always use UCS1 encoding for strings now. */ | |
58 | encoding_method = ENC_UCS1; | |
59 | ||
60 | locale->categories[LC_NUMERIC].numeric = numeric = | |
61 | (struct locale_numeric_t *) xmalloc (sizeof (struct locale_numeric_t)); | |
62 | ||
63 | memset (numeric, '\0', sizeof (struct locale_numeric_t)); | |
64 | ||
65 | numeric->grouping_max = 80; | |
66 | numeric->grouping = (char *) xmalloc (numeric->grouping_max); | |
67 | numeric->grouping_act = 0; | |
68 | } | |
69 | ||
70 | ||
71 | void | |
72 | numeric_finish (struct localedef_t *locale) | |
73 | { | |
74 | struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric; | |
75 | ||
76 | #define TEST_ELEM(cat) \ | |
c84142e8 | 77 | if (numeric->cat == NULL && !be_quiet) \ |
19bc17a9 RM |
78 | error (0, 0, _("field `%s' in category `%s' not defined"), \ |
79 | #cat, "LC_NUMERIC") | |
80 | ||
81 | TEST_ELEM (decimal_point); | |
82 | TEST_ELEM (thousands_sep); | |
83 | ||
84 | /* The decimal point must not be empty. This is not said explicitly | |
85 | in POSIX but ANSI C (ISO/IEC 9899) says in 4.4.2.1 it has to be | |
86 | != "". */ | |
c84142e8 | 87 | if (numeric->decimal_point[0] == '\0' && !be_quiet) |
19bc17a9 RM |
88 | { |
89 | error (0, 0, _("\ | |
90 | value for field `%s' in category `%s' must not be the empty string"), | |
91 | "decimal_point", "LC_NUMERIC"); | |
92 | } | |
93 | ||
c84142e8 | 94 | if (numeric->grouping_act == 0 && !be_quiet) |
19bc17a9 RM |
95 | error (0, 0, _("field `%s' in category `%s' not defined"), |
96 | "grouping", "LC_NUMERIC"); | |
97 | } | |
98 | ||
99 | ||
100 | void | |
101 | numeric_output (struct localedef_t *locale, const char *output_path) | |
102 | { | |
103 | struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric; | |
104 | struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC)]; | |
105 | struct locale_file data; | |
7a12c6bb | 106 | u_int32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC)]; |
19bc17a9 RM |
107 | size_t cnt = 0; |
108 | ||
109 | if ((locale->binary & (1 << LC_NUMERIC)) != 0) | |
110 | { | |
111 | iov[0].iov_base = numeric; | |
112 | iov[0].iov_len = locale->len[LC_NUMERIC]; | |
113 | ||
114 | write_locale_data (output_path, "LC_NUMERIC", 1, iov); | |
115 | ||
116 | return; | |
117 | } | |
118 | ||
119 | data.magic = LIMAGIC (LC_NUMERIC); | |
120 | data.n = _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC); | |
121 | iov[cnt].iov_base = (void *) &data; | |
122 | iov[cnt].iov_len = sizeof (data); | |
123 | ++cnt; | |
124 | ||
125 | iov[cnt].iov_base = (void *) idx; | |
126 | iov[cnt].iov_len = sizeof (idx); | |
127 | ++cnt; | |
128 | ||
2c6fe0bd | 129 | idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len; |
19bc17a9 RM |
130 | iov[cnt].iov_base = (void *) (numeric->decimal_point ?: ""); |
131 | iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1; | |
132 | ++cnt; | |
133 | ||
134 | idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len; | |
135 | iov[cnt].iov_base = (void *) (numeric->thousands_sep ?: ""); | |
136 | iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1; | |
137 | ++cnt; | |
138 | ||
139 | idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len; | |
140 | iov[cnt].iov_base = alloca (numeric->grouping_act + 1); | |
141 | iov[cnt].iov_len = numeric->grouping_act + 1; | |
142 | memcpy (iov[cnt].iov_base, numeric->grouping, numeric->grouping_act); | |
143 | ((char *) iov[cnt].iov_base)[numeric->grouping_act] = '\0'; | |
144 | ||
145 | assert (cnt + 1 == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC)); | |
146 | ||
147 | write_locale_data (output_path, "LC_NUMERIC", | |
148 | 2 + _NL_ITEM_INDEX (_NL_NUM_LC_NUMERIC), iov); | |
149 | } | |
150 | ||
151 | ||
152 | void | |
153 | numeric_add (struct linereader *lr, struct localedef_t *locale, | |
154 | enum token_t tok, struct token *code, | |
155 | struct charset_t *charset) | |
156 | { | |
157 | struct locale_numeric_t *numeric = locale->categories[LC_NUMERIC].numeric; | |
158 | ||
159 | switch (tok) | |
160 | { | |
161 | #define STR_ELEM(cat) \ | |
162 | case tok_##cat: \ | |
163 | if (numeric->cat != NULL) \ | |
164 | lr_error (lr, _("\ | |
165 | field `%s' in category `%s' declared more than once"), \ | |
166 | #cat, "LC_NUMERIC"); \ | |
167 | else if (code->val.str.start == NULL) \ | |
168 | { \ | |
169 | lr_error (lr, _("unknown character in field `%s' of category `%s'"),\ | |
170 | #cat, "LC_NUMERIC"); \ | |
171 | numeric->cat = ""; \ | |
172 | } \ | |
173 | else \ | |
174 | numeric->cat = code->val.str.start; \ | |
175 | break | |
176 | ||
177 | STR_ELEM (decimal_point); | |
178 | STR_ELEM (thousands_sep); | |
179 | ||
180 | case tok_grouping: | |
181 | if (numeric->grouping_act == numeric->grouping_max) | |
182 | { | |
183 | numeric->grouping_max *= 2; | |
184 | numeric->grouping = (char *) xrealloc (numeric->grouping, | |
185 | numeric->grouping_max); | |
186 | } | |
187 | if (numeric->grouping_act > 0 | |
188 | && (numeric->grouping[numeric->grouping_act - 1] == '\177')) | |
189 | { | |
190 | lr_error (lr, _("\ | |
191 | `-1' must be last entry in `%s' field in `%s' category"), | |
192 | "grouping", "LC_NUMERIC"); | |
193 | --numeric->grouping_act; | |
194 | } | |
195 | ||
196 | if (code->tok == tok_minus1) | |
197 | numeric->grouping[numeric->grouping_act++] = '\177'; | |
198 | else if (code->val.num > 126) | |
199 | lr_error (lr, _("\ | |
200 | values for field `%s' in category `%s' must be smaller than 127"), | |
201 | "grouping", "LC_NUMERIC"); | |
202 | else | |
203 | numeric->grouping[numeric->grouping_act++] = code->val.num; | |
204 | break; | |
205 | ||
206 | default: | |
207 | assert (! "unknown token in category `LC_NUMERIC': should not happen"); | |
208 | } | |
209 | } |