11573Srgrimes/* $NetBSD: citrus_lc_time.c,v 1.5 2010/06/13 04:14:57 tnozaki Exp $ */
214272Spst
31573Srgrimes/*-
41573Srgrimes * Copyright (c)2008 Citrus Project,
51573Srgrimes * All rights reserved.
61573Srgrimes *
71573Srgrimes * Redistribution and use in source and binary forms, with or without
81573Srgrimes * modification, are permitted provided that the following conditions
91573Srgrimes * are met:
101573Srgrimes * 1. Redistributions of source code must retain the above copyright
111573Srgrimes *    notice, this list of conditions and the following disclaimer.
121573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
131573Srgrimes *    notice, this list of conditions and the following disclaimer in the
141573Srgrimes *    documentation and/or other materials provided with the distribution.
151573Srgrimes *
161573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
171573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
181573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
191573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
201573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
211573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
221573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
231573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
241573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261573Srgrimes * SUCH DAMAGE.
271573Srgrimes */
281573Srgrimes
291573Srgrimes#include <sys/cdefs.h>
301573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
3114272Spst__RCSID("$NetBSD: citrus_lc_time.c,v 1.5 2010/06/13 04:14:57 tnozaki Exp $");
321573Srgrimes#endif /* LIBC_SCCS and not lint */
3392986Sobrien
3492986Sobrien#include "namespace.h"
351573Srgrimes#include "reentrant.h"
3671579Sdeischen#include <sys/types.h>
371573Srgrimes#include <sys/localedef.h>
381573Srgrimes#include <sys/queue.h>
391573Srgrimes#include <assert.h>
401573Srgrimes#include <errno.h>
411573Srgrimes#include <langinfo.h>
421573Srgrimes#include <limits.h>
431573Srgrimes#define __SETLOCALE_SOURCE__
441573Srgrimes#include <locale.h>
4571579Sdeischen#include <stddef.h>
461573Srgrimes#include <stdio.h>
471573Srgrimes#include <stdlib.h>
481573Srgrimes#include <string.h>
491573Srgrimes
501573Srgrimes#include "setlocale_local.h"
511573Srgrimes
521573Srgrimes#include "citrus_namespace.h"
531573Srgrimes#include "citrus_types.h"
541573Srgrimes#include "citrus_bcs.h"
551573Srgrimes#include "citrus_region.h"
561573Srgrimes#include "citrus_lookup.h"
571573Srgrimes#include "citrus_aliasname_local.h"
581573Srgrimes#include "citrus_module.h"
591573Srgrimes#include "citrus_mmap.h"
60189291Sdelphij#include "citrus_hash.h"
611573Srgrimes#include "citrus_db.h"
621573Srgrimes#include "citrus_db_hash.h"
631573Srgrimes#include "citrus_memstream.h"
641573Srgrimes#include "runetype_local.h"
651573Srgrimes
661573Srgrimes/*
671573Srgrimes * macro required by all template headers
681573Srgrimes */
691573Srgrimes#define _PREFIX(name)		__CONCAT(_citrus_LC_TIME_, name)
701573Srgrimes
711573Srgrimes#include "nb_lc_time_misc.h"
721573Srgrimes#include "citrus_lc_template_decl.h"
731573Srgrimes
741573Srgrimesstatic __inline void
751573Srgrimes_citrus_LC_TIME_uninit(_TimeLocale *data)
761573Srgrimes{
771573Srgrimes	size_t i, j;
7814272Spst
791573Srgrimes	_DIAGASSERT(data != NULL);
801573Srgrimes
81111010Snectar	for (i = ABDAY_IDX(ABDAY_1), j = ABDAY_IDX(ABDAY_7);  i <= j; ++i)
8214272Spst		free(__UNCONST(data->abday[i]));
831573Srgrimes	for (i = DAY_IDX(DAY_1),     j = DAY_IDX(DAY_7);      i <= j; ++i)
841573Srgrimes		free(__UNCONST(data->day[i]));
85189327Sdelphij	for (i = ABMON_IDX(ABMON_1), j = ABMON_IDX(ABMON_12); i <= j; ++i)
8656698Sjasone		free(__UNCONST(data->abmon[i]));
871573Srgrimes	for (i = MON_IDX(MON_1),     j = MON_IDX(MON_12);     i <= j; ++i)
88189327Sdelphij		free(__UNCONST(data->mon[i]));
89111010Snectar	for (i = AM_PM_IDX(AM_STR),  j = AM_PM_IDX(PM_STR);   i <= j; ++i)
901573Srgrimes		free(__UNCONST(data->am_pm[i]));
911573Srgrimes	free(__UNCONST(data->d_t_fmt));
921573Srgrimes	free(__UNCONST(data->d_fmt));
931573Srgrimes	free(__UNCONST(data->t_fmt));
941573Srgrimes	free(__UNCONST(data->t_fmt_ampm));
951573Srgrimes}
961573Srgrimes
971573Srgrimes#include "citrus_lc_time.h"
981573Srgrimes
991573Srgrimesstruct _citrus_LC_TIME_key {
1001573Srgrimes	const char *name;
1011573Srgrimes	size_t offset;
1021573Srgrimes};
1031573Srgrimes
1041573Srgrimes#define OFFSET(field) offsetof(_TimeLocale, field)
1051573Srgrimesstatic const struct _citrus_LC_TIME_key keys[] = {
1061573Srgrimes  { _CITRUS_LC_TIME_SYM_ABDAY_1,  OFFSET(abday[ABDAY_IDX(ABDAY_1)] ) },
107189291Sdelphij  { _CITRUS_LC_TIME_SYM_ABDAY_2,  OFFSET(abday[ABDAY_IDX(ABDAY_2)] ) },
1081573Srgrimes  { _CITRUS_LC_TIME_SYM_ABDAY_3,  OFFSET(abday[ABDAY_IDX(ABDAY_3)] ) },
1091573Srgrimes  { _CITRUS_LC_TIME_SYM_ABDAY_4,  OFFSET(abday[ABDAY_IDX(ABDAY_4)] ) },
1101573Srgrimes  { _CITRUS_LC_TIME_SYM_ABDAY_5,  OFFSET(abday[ABDAY_IDX(ABDAY_5)] ) },
1111573Srgrimes  { _CITRUS_LC_TIME_SYM_ABDAY_6,  OFFSET(abday[ABDAY_IDX(ABDAY_6)] ) },
1121573Srgrimes  { _CITRUS_LC_TIME_SYM_ABDAY_7,  OFFSET(abday[ABDAY_IDX(ABDAY_7)] ) },
1131573Srgrimes  { _CITRUS_LC_TIME_SYM_DAY_1,    OFFSET(day[DAY_IDX(DAY_1)]       ) },
1141573Srgrimes  { _CITRUS_LC_TIME_SYM_DAY_2,    OFFSET(day[DAY_IDX(DAY_2)]       ) },
1151573Srgrimes  { _CITRUS_LC_TIME_SYM_DAY_3,    OFFSET(day[DAY_IDX(DAY_3)]       ) },
1161573Srgrimes  { _CITRUS_LC_TIME_SYM_DAY_4,    OFFSET(day[DAY_IDX(DAY_4)]       ) },
1171573Srgrimes  { _CITRUS_LC_TIME_SYM_DAY_5,    OFFSET(day[DAY_IDX(DAY_5)]       ) },
1181573Srgrimes  { _CITRUS_LC_TIME_SYM_DAY_6,    OFFSET(day[DAY_IDX(DAY_6)]       ) },
1191573Srgrimes  { _CITRUS_LC_TIME_SYM_DAY_7,    OFFSET(day[DAY_IDX(DAY_7)]       ) },
1201573Srgrimes  { _CITRUS_LC_TIME_SYM_ABMON_1,  OFFSET(abmon[ABMON_IDX(ABMON_1)] ) },
1211573Srgrimes  { _CITRUS_LC_TIME_SYM_ABMON_2,  OFFSET(abmon[ABMON_IDX(ABMON_2)] ) },
1221573Srgrimes  { _CITRUS_LC_TIME_SYM_ABMON_3,  OFFSET(abmon[ABMON_IDX(ABMON_3)] ) },
1231573Srgrimes  { _CITRUS_LC_TIME_SYM_ABMON_4,  OFFSET(abmon[ABMON_IDX(ABMON_4)] ) },
1241573Srgrimes  { _CITRUS_LC_TIME_SYM_ABMON_5,  OFFSET(abmon[ABMON_IDX(ABMON_5)] ) },
1251573Srgrimes  { _CITRUS_LC_TIME_SYM_ABMON_6,  OFFSET(abmon[ABMON_IDX(ABMON_6)] ) },
1261573Srgrimes  { _CITRUS_LC_TIME_SYM_ABMON_7,  OFFSET(abmon[ABMON_IDX(ABMON_7)] ) },
12714272Spst  { _CITRUS_LC_TIME_SYM_ABMON_8,  OFFSET(abmon[ABMON_IDX(ABMON_8)] ) },
1281573Srgrimes  { _CITRUS_LC_TIME_SYM_ABMON_9,  OFFSET(abmon[ABMON_IDX(ABMON_9)] ) },
1291573Srgrimes  { _CITRUS_LC_TIME_SYM_ABMON_10, OFFSET(abmon[ABMON_IDX(ABMON_10)]) },
1301573Srgrimes  { _CITRUS_LC_TIME_SYM_ABMON_11, OFFSET(abmon[ABMON_IDX(ABMON_11)]) },
13114272Spst  { _CITRUS_LC_TIME_SYM_ABMON_12, OFFSET(abmon[ABMON_IDX(ABMON_12)]) },
1321573Srgrimes  { _CITRUS_LC_TIME_SYM_MON_1,    OFFSET(mon[MON_IDX(MON_1)]       ) },
1331573Srgrimes  { _CITRUS_LC_TIME_SYM_MON_2,    OFFSET(mon[MON_IDX(MON_2)]       ) },
1341573Srgrimes  { _CITRUS_LC_TIME_SYM_MON_3,    OFFSET(mon[MON_IDX(MON_3)]       ) },
1351573Srgrimes  { _CITRUS_LC_TIME_SYM_MON_4,    OFFSET(mon[MON_IDX(MON_4)]       ) },
1361573Srgrimes  { _CITRUS_LC_TIME_SYM_MON_5,    OFFSET(mon[MON_IDX(MON_5)]       ) },
1371573Srgrimes  { _CITRUS_LC_TIME_SYM_MON_6,    OFFSET(mon[MON_IDX(MON_6)]       ) },
13814272Spst  { _CITRUS_LC_TIME_SYM_MON_7,    OFFSET(mon[MON_IDX(MON_7)]       ) },
13914272Spst  { _CITRUS_LC_TIME_SYM_MON_8,    OFFSET(mon[MON_IDX(MON_8)]       ) },
1401573Srgrimes  { _CITRUS_LC_TIME_SYM_MON_9,    OFFSET(mon[MON_IDX(MON_9)]       ) },
1411573Srgrimes  { _CITRUS_LC_TIME_SYM_MON_10,   OFFSET(mon[MON_IDX(MON_10)]      ) },
1421573Srgrimes  { _CITRUS_LC_TIME_SYM_MON_11,   OFFSET(mon[MON_IDX(MON_11)]      ) },
1431573Srgrimes  { _CITRUS_LC_TIME_SYM_MON_12,   OFFSET(mon[MON_IDX(MON_12)]      ) },
14414272Spst  { _CITRUS_LC_TIME_SYM_AM_STR,   OFFSET(am_pm[AM_PM_IDX(AM_STR)]  ) },
14514272Spst  { _CITRUS_LC_TIME_SYM_PM_STR,   OFFSET(am_pm[AM_PM_IDX(PM_STR)]  ) },
14614272Spst  { _CITRUS_LC_TIME_SYM_D_T_FMT,  OFFSET(d_t_fmt                   ) },
14714272Spst  { _CITRUS_LC_TIME_SYM_D_FMT,    OFFSET(d_fmt                     ) },
14814272Spst  { _CITRUS_LC_TIME_SYM_T_FMT,    OFFSET(t_fmt                     ) },
14914272Spst  { _CITRUS_LC_TIME_SYM_T_FMT_AMPM, OFFSET(t_fmt_ampm                ) },
15014272Spst  { NULL, 0 }
15114272Spst};
15256698Sjasone
153190484Sdelphijstatic __inline int
15414272Spst_citrus_LC_TIME_init_normal(_TimeLocale * __restrict data,
15514272Spst    struct _citrus_db * __restrict db)
15614272Spst{
15714272Spst        const struct _citrus_LC_TIME_key *key;
158189327Sdelphij	char **p;
15914272Spst	const char *s;
16014272Spst
16114272Spst	_DIAGASSERT(data != NULL);
16214272Spst	_DIAGASSERT(db != NULL);
16314272Spst
16414272Spst	memset(data, 0, sizeof(*data));
165190484Sdelphij	for (key = &keys[0]; key->name != NULL; ++key) {
16614272Spst		if (_db_lookupstr_by_s(db, key->name, &s, NULL))
16714272Spst			goto fatal;
16814272Spst		p = (char **)(void *)
16914272Spst		    (((char *)(void *)data) + key->offset);
17014272Spst		*p = strdup(s);
17114272Spst		if (*p == NULL)
17214272Spst			goto fatal;
17314272Spst	}
1741573Srgrimes	return 0;
1751573Srgrimes
1761573Srgrimesfatal:
1771573Srgrimes	_citrus_LC_TIME_uninit(data);
1781573Srgrimes	return EFTYPE;
1791573Srgrimes}
18014272Spst
1811573Srgrimesstatic __inline int
1821573Srgrimes_citrus_LC_TIME_init_fallback(_TimeLocale * __restrict data,
183    struct _memstream * __restrict ms)
184{
185        const struct _citrus_LC_TIME_key *key;
186	char **p;
187	const char *s;
188	size_t n;
189
190	memset(data, 0, sizeof(*data));
191	for (key = &keys[0]; key->name != NULL; ++key) {
192		if ((s = _memstream_getln(ms, &n)) == NULL)
193			goto fatal;
194		p = (char **)(void *)
195		    (((char *)(void *)data) + key->offset);
196		*p = strndup(s, n - 1);
197		if (*p == NULL)
198			goto fatal;
199	}
200	return 0;
201
202fatal:
203	_citrus_LC_TIME_uninit(data);
204	return EFTYPE;
205}
206
207/*
208 * macro required by citrus_lc_template.h
209 */
210#define _CATEGORY_DB		"LC_TIME"
211#define _CATEGORY_MAGIC		_CITRUS_LC_TIME_MAGIC_1
212
213#include "citrus_lc_template.h"
214