update from main archive 960919
[platform/upstream/linaro-glibc.git] / locale / programs / ld-time.c
1 /* Copyright (C) 1995, 1996 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper, <drepper@gnu.ai.mit.edu>.
4
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.
9
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.
14
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
17 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.  */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <langinfo.h>
25 #include <string.h>
26
27 /* Undefine following line in production version.  */
28 /* #define NDEBUG 1 */
29 #include <assert.h>
30
31 #include "locales.h"
32 #include "localeinfo.h"
33 #include "stringtrans.h"
34
35
36 void *xmalloc (size_t __n);
37 void *xrealloc (void *__p, size_t __n);
38
39
40 /* The real definition of the struct for the LC_TIME locale.  */
41 struct locale_time_t
42 {
43   const char *abday[7];
44   size_t cur_num_abday;
45   const char *day[7];
46   size_t cur_num_day;
47   const char *abmon[12];
48   size_t cur_num_abmon;
49   const char *mon[12];
50   size_t cur_num_mon;
51   const char *am_pm[2];
52   size_t cur_num_am_pm;
53   const char *d_t_fmt;
54   const char *d_fmt;
55   const char *t_fmt;
56   const char *t_fmt_ampm;
57   const char **era;
58   size_t era_num;
59   const char *era_year;
60   const char *era_d_t_fmt;
61   const char *era_t_fmt;
62   const char *era_d_fmt;
63   const char *alt_digits[100];
64   size_t cur_num_alt_digits;
65 };
66
67
68 void
69 time_startup (struct linereader *lr, struct localedef_t *locale,
70               struct charset_t *charset)
71 {
72   struct locale_time_t *time;
73
74   /* It is important that we always use UCS1 encoding for strings now.  */
75   encoding_method = ENC_UCS1;
76
77   locale->categories[LC_TIME].time = time =
78     (struct locale_time_t *) xmalloc (sizeof (struct locale_time_t));
79
80   memset (time, '\0', sizeof (struct locale_time_t));
81 }
82
83
84 void
85 time_finish (struct localedef_t *locale)
86 {
87   struct locale_time_t *time = locale->categories[LC_TIME].time;
88
89 #define TESTARR_ELEM(cat, max)                                                \
90   if (time->cur_num_##cat == 0)                                               \
91     error (0, 0, _("field `%s' in category `%s' not defined"),                \
92            #cat, "LC_TIME");                                                  \
93   else if (time->cur_num_##cat != max)                                        \
94     error (0, 0, _("field `%s' in category `%s' has not enough values"),      \
95            #cat, "LC_TIME")
96
97   TESTARR_ELEM (abday, 7);
98   TESTARR_ELEM (day, 7);
99   TESTARR_ELEM (abmon, 12);
100   TESTARR_ELEM (mon, 12);
101   TESTARR_ELEM (am_pm, 2);
102
103 #define TEST_ELEM(cat)                                                        \
104   if (time->cat == NULL)                                                      \
105     error (0, 0, _("field `%s' in category `%s' not defined"),                \
106            #cat, "LC_TIME")
107
108   TEST_ELEM (d_t_fmt);
109   TEST_ELEM (d_fmt);
110   TEST_ELEM (t_fmt);
111   TEST_ELEM (t_fmt_ampm);
112 }
113
114
115 void
116 time_output (struct localedef_t *locale, const char *output_path)
117 {
118   struct locale_time_t *time = locale->categories[LC_TIME].time;
119   struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_TIME)
120                   + (time->era_num > 0 ? time->era_num - 1 : 0)
121                   + time->cur_num_alt_digits];
122   struct locale_file data;
123   u_int32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_TIME)];
124   size_t cnt, last_idx, num;
125
126   if ((locale->binary & (1 << LC_TIME)) != 0)
127     {
128       iov[0].iov_base = time;
129       iov[0].iov_len = locale->len[LC_TIME];
130
131       write_locale_data (output_path, "LC_TIME", 1, iov);
132
133       return;
134     }
135
136   data.magic = LIMAGIC (LC_TIME);
137   data.n = _NL_ITEM_INDEX (_NL_NUM_LC_TIME);
138   iov[0].iov_base = (void *) &data;
139   iov[0].iov_len = sizeof (data);
140
141   iov[1].iov_base = (void *) idx;
142   iov[1].iov_len = sizeof (idx);
143
144   idx[0] = iov[0].iov_len + iov[1].iov_len;
145
146   /* The ab'days.  */
147   for (cnt = 0; cnt <= _NL_ITEM_INDEX (ABDAY_7); ++cnt)
148     {
149       iov[2 + cnt].iov_base =
150         (void *) (time->abday[cnt - _NL_ITEM_INDEX (ABDAY_1)] ?: "");
151       iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
152       idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
153     }
154
155   /* The days.  */
156   for (; cnt <= _NL_ITEM_INDEX (DAY_7); ++cnt)
157     {
158       iov[2 + cnt].iov_base =
159         (void *) (time->day[cnt - _NL_ITEM_INDEX (DAY_1)] ?: "");
160       iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
161       idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
162     }
163
164   /* The ab'mons.  */
165   for (; cnt <= _NL_ITEM_INDEX (ABMON_12); ++cnt)
166     {
167       iov[2 + cnt].iov_base =
168         (void *) (time->abmon[cnt - _NL_ITEM_INDEX (ABMON_1)] ?: "");
169       iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
170       idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
171     }
172
173   /* The mons.  */
174   for (; cnt <= _NL_ITEM_INDEX (MON_12); ++cnt)
175     {
176       iov[2 + cnt].iov_base =
177         (void *) (time->mon[cnt - _NL_ITEM_INDEX (MON_1)] ?: "");
178       iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
179       idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
180     }
181
182   /* AM/PM.  */
183   for (; cnt <= _NL_ITEM_INDEX (PM_STR); ++cnt)
184     {
185       iov[2 + cnt].iov_base =
186         (void *) (time->am_pm[cnt - _NL_ITEM_INDEX (AM_STR)] ?: "");
187       iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
188       idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
189     }
190
191   iov[2 + cnt].iov_base = (void *) (time->d_t_fmt ?: "");
192   iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
193   idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
194   ++cnt;
195
196   iov[2 + cnt].iov_base = (void *) (time->d_fmt ?: "");
197   iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
198   idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
199   ++cnt;
200
201   iov[2 + cnt].iov_base = (void *) (time->t_fmt ?: "");
202   iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
203   idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
204   ++cnt;
205
206   iov[2 + cnt].iov_base = (void *) (time->t_fmt_ampm ?: "");
207   iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
208   idx[1 + cnt] = idx[cnt] + iov[2 + cnt].iov_len;
209   last_idx = ++cnt;
210
211   idx[1 + last_idx] = idx[last_idx];
212   for (num = 0; num < time->era_num; ++num, ++cnt)
213     {
214       iov[2 + cnt].iov_base = (void *) time->era[num];
215       iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
216     }
217   ++last_idx;
218
219   iov[2 + cnt].iov_base = (void *) (time->era_year ?: "");
220   iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
221   idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
222   ++cnt;
223   ++last_idx;
224
225   iov[2 + cnt].iov_base = (void *) (time->era_d_fmt ?: "");
226   iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
227   idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
228   ++cnt;
229   ++last_idx;
230
231   idx[1 + last_idx] = idx[last_idx];
232   for (num = 0; num < time->cur_num_alt_digits; ++num, ++cnt)
233     {
234       iov[2 + cnt].iov_base = (void *) (time->alt_digits[num] ?: "");
235       iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
236       idx[1 + last_idx] += iov[2 + cnt].iov_len;
237     }
238   ++last_idx;
239
240   iov[2 + cnt].iov_base = (void *) (time->era_d_t_fmt ?: "");
241   iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
242   idx[1 + last_idx] = idx[last_idx] + iov[2 + cnt].iov_len;
243   ++cnt;
244
245   iov[2 + cnt].iov_base = (void *) (time->era_d_fmt ?: "");
246   iov[2 + cnt].iov_len = strlen (iov[2 + cnt].iov_base) + 1;
247   ++cnt;
248
249   assert (cnt == (_NL_ITEM_INDEX (_NL_NUM_LC_TIME) - 1
250                   + time->cur_num_alt_digits));
251
252   write_locale_data (output_path, "LC_TIME", 2 + cnt, iov);
253 }
254
255
256 void
257 time_add (struct linereader *lr, struct localedef_t *locale,
258           enum token_t tok, struct token *code,
259           struct charset_t *charset)
260 {
261   struct locale_time_t *time = locale->categories[LC_TIME].time;
262
263   switch (tok)
264     {
265 #define STRARR_ELEM(cat, max)                                                 \
266     case tok_##cat:                                                           \
267       if (time->cur_num_##cat >= max)                                         \
268         lr_error (lr, _("\
269 too many values for field `%s' in category `LC_TIME'"),                       \
270                   #cat, "LC_TIME");                                           \
271       else if (code->val.str.start == NULL)                                   \
272         {                                                                     \
273           lr_error (lr, _("unknown character in field `%s' of category `%s'"),\
274                     #cat, "LC_TIME");                                         \
275           time->cat[time->cur_num_##cat++] = "";                              \
276         }                                                                     \
277       else                                                                    \
278         time->cat[time->cur_num_##cat++] = code->val.str.start;               \
279       break
280
281     STRARR_ELEM (abday, 7);
282     STRARR_ELEM (day, 7);
283     STRARR_ELEM (abmon, 12);
284     STRARR_ELEM (mon, 12);
285     STRARR_ELEM (am_pm, 2);
286     STRARR_ELEM (alt_digits, 100);
287
288     case tok_era:
289       if (code->val.str.start == NULL)
290         lr_error (lr, _("unknown character in field `%s' of category `%s'"),
291                   "era", "LC_TIME");
292       else
293         {
294           ++time->era_num;
295           time->era = xrealloc (time->era, time->era_num * sizeof (char *));
296           time->era[time->era_num - 1] = code->val.str.start;
297         }
298       break;
299
300 #define STR_ELEM(cat)                                                         \
301     case tok_##cat:                                                           \
302       if (time->cat != NULL)                                                  \
303         lr_error (lr, _("\
304 field `%s' in category `%s' declared more than once"),                        \
305                   #cat, "LC_TIME");                                           \
306       else if (code->val.str.start == NULL)                                   \
307         {                                                                     \
308           lr_error (lr, _("unknown character in field `%s' of category `%s'"),\
309                     #cat, "LC_TIME");                                         \
310           time->cat = "";                                                     \
311         }                                                                     \
312       else                                                                    \
313         time->cat = code->val.str.start;                                      \
314       break
315
316     STR_ELEM (d_t_fmt);
317     STR_ELEM (d_fmt);
318     STR_ELEM (t_fmt);
319     STR_ELEM (t_fmt_ampm);
320     STR_ELEM (era_year);
321     STR_ELEM (era_d_t_fmt);
322     STR_ELEM (era_d_fmt);
323     STR_ELEM (era_t_fmt);
324
325     default:
326       assert (! "unknown token in category `LC_TIME': should not happen");
327     }
328 }