Home | History | Annotate | Line # | Download | only in citrus
citrus_pivot_factory.c revision 1.1
      1 /*	$NetBSD: citrus_pivot_factory.c,v 1.1 2003/06/25 09:51:39 tshiozak Exp $	*/
      2 
      3 /*-
      4  * Copyright (c)2003 Citrus Project,
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 #if defined(LIBC_SCCS) && !defined(lint)
     31 __RCSID("$NetBSD: citrus_pivot_factory.c,v 1.1 2003/06/25 09:51:39 tshiozak Exp $");
     32 #endif /* LIBC_SCCS and not lint */
     33 
     34 #include <assert.h>
     35 #include <stdio.h>
     36 #include <stdlib.h>
     37 #include <string.h>
     38 #include <errno.h>
     39 #include <ctype.h>
     40 #include <limits.h>
     41 #include <sys/queue.h>
     42 
     43 #include "citrus_namespace.h"
     44 #include "citrus_region.h"
     45 #include "citrus_bcs.h"
     46 #include "citrus_db_factory.h"
     47 #include "citrus_db_hash.h"
     48 #include "citrus_pivot_file.h"
     49 #include "citrus_pivot_factory.h"
     50 
     51 struct src_entry {
     52 	char *se_name;
     53 	struct _citrus_db_factory *se_df;
     54 	SIMPLEQ_ENTRY(src_entry) se_entry;
     55 };
     56 SIMPLEQ_HEAD(src_head, src_entry);
     57 
     58 static int
     59 find_src(struct src_head *sh, struct src_entry **rse, const char *name)
     60 {
     61 	int ret;
     62 	struct src_entry *se;
     63 
     64 	SIMPLEQ_FOREACH(se, sh, se_entry) {
     65 		if (_bcs_strcasecmp(se->se_name, name) == 0) {
     66 			*rse = se;
     67 			return 0;
     68 		}
     69 	}
     70 	se = malloc(sizeof(*se));
     71 	if (se == NULL)
     72 		return errno;
     73 	se->se_name = strdup(name);
     74 	if (se->se_name == NULL) {
     75 		ret = errno;
     76 		free(se);
     77 		return ret;
     78 	}
     79 	ret = _db_factory_create(&se->se_df, &_db_hash_std, NULL);
     80 	if (ret) {
     81 		free(se->se_name);
     82 		free(se);
     83 		return ret;
     84 	}
     85 	SIMPLEQ_INSERT_TAIL(sh, se, se_entry);
     86 	*rse = se;
     87 
     88 	return 0;
     89 }
     90 
     91 static void
     92 free_src(struct src_head *sh)
     93 {
     94 	struct src_entry *se;
     95 
     96 	while((se = SIMPLEQ_FIRST(sh)) != NULL) {
     97 		SIMPLEQ_REMOVE_HEAD(sh, se_entry);
     98 		_db_factory_free(se->se_df);
     99 		free(se->se_name);
    100 		free(se);
    101 	}
    102 }
    103 
    104 
    105 #define T_COMM '#'
    106 static int
    107 convert_line(struct src_head *sh, const char *line, size_t len)
    108 {
    109 	int ret;
    110 	struct src_entry *se;
    111 	const char *p;
    112 	char key1[LINE_MAX], key2[LINE_MAX], data[LINE_MAX];
    113 	u_int32_t val;
    114 
    115 	/* cut off trailing comment */
    116 	p = memchr(line, T_COMM, len);
    117 	if (p)
    118 		len = p - line;
    119 
    120 	/* key1 */
    121 	line = _bcs_skip_ws_len(line, &len);
    122 	if (len == 0)
    123 		return 0;
    124 	p = _bcs_skip_nonws_len(line, &len);
    125 	if (p==line)
    126 		return 0;
    127 	snprintf(key1, sizeof(key1), "%.*s", (int)(p-line), line);
    128 
    129 	/* key2 */
    130 	line = _bcs_skip_ws_len(p, &len);
    131 	if (len == 0)
    132 		return 0;
    133 	p = _bcs_skip_nonws_len(line, &len);
    134 	if (p==line)
    135 		return 0;
    136 	snprintf(key2, sizeof(key2), "%.*s", (int)(p-line), line);
    137 
    138 	/* data */
    139 	line = _bcs_skip_ws_len(p, &len);
    140 	_bcs_trunc_rws_len(line, &len);
    141 	snprintf(data, sizeof(data), "%.*s", (int)len, line);
    142 	/* LINTED: discard const */
    143 	val = strtoul(data, (char **)&p, 0);
    144 	if (*p != '\0')
    145 		return EFTYPE;
    146 
    147 	/* insert to DB */
    148 	ret = find_src(sh, &se, key1);
    149 	if (ret)
    150 		return ret;
    151 
    152 	return _db_factory_add32_by_s(se->se_df, key2, val);
    153 }
    154 
    155 static int
    156 dump_db(struct src_head *sh, struct _region *r)
    157 {
    158 	int ret;
    159 	struct _db_factory *df;
    160 	struct src_entry *se;
    161 	size_t size;
    162 	void *ptr;
    163 	struct _region subr;
    164 
    165 	ret = _db_factory_create(&df, &_db_hash_std, NULL);
    166 	if (ret)
    167 		return ret;
    168 
    169 	SIMPLEQ_FOREACH(se, sh, se_entry) {
    170 		size = _db_factory_calc_size(se->se_df);
    171 		ptr = malloc(size);
    172 		if (ptr == NULL)
    173 			goto quit;
    174 		_region_init(&subr, ptr, size);
    175 		ret = _db_factory_serialize(se->se_df, _CITRUS_PIVOT_SUB_MAGIC,
    176 					    &subr);
    177 		if (ret)
    178 			goto quit;
    179 		ret = _db_factory_add_by_s(df, se->se_name, &subr, 1);
    180 		if (ret)
    181 			goto quit;
    182 	}
    183 
    184 	size = _db_factory_calc_size(df);
    185 	ptr = malloc(size);
    186 	if (ptr == NULL)
    187 		goto quit;
    188 	_region_init(r, ptr, size);
    189 
    190 	ret = _db_factory_serialize(df, _CITRUS_PIVOT_MAGIC, r);
    191 	ptr = NULL;
    192 
    193 quit:
    194 	free(ptr);
    195 	_db_factory_free(df);
    196 	return ret;
    197 }
    198 
    199 int
    200 _citrus_pivot_factory_convert(FILE *out, FILE *in)
    201 {
    202 	struct src_head sh;
    203 	struct _region r;
    204 	char *line;
    205 	size_t size;
    206 	int ret;
    207 
    208 	SIMPLEQ_INIT(&sh);
    209 
    210 	while ((line = fgetln(in, &size)) != NULL)
    211 		if ((ret = convert_line(&sh, line, size))) {
    212 			free_src(&sh);
    213 			return ret;
    214 		}
    215 
    216 	ret = dump_db(&sh, &r);
    217 	free_src(&sh);
    218 	if (ret)
    219 		return ret;
    220 
    221 	if (fwrite(_region_head(&r), _region_size(&r), 1, out) != 1)
    222 		return errno;
    223 
    224 	return 0;
    225 }
    226