1219019Sgabor/* $FreeBSD: releng/10.2/lib/libc/iconv/citrus_lookup_factory.c 219019 2011-02-25 00:04:39Z gabor $ */ 2219019Sgabor/* $NetBSD: citrus_lookup_factory.c,v 1.4 2003/10/27 00:12:42 lukem Exp $ */ 3219019Sgabor 4219019Sgabor/*- 5219019Sgabor * Copyright (c)2003 Citrus Project, 6219019Sgabor * All rights reserved. 7219019Sgabor * 8219019Sgabor * Redistribution and use in source and binary forms, with or without 9219019Sgabor * modification, are permitted provided that the following conditions 10219019Sgabor * are met: 11219019Sgabor * 1. Redistributions of source code must retain the above copyright 12219019Sgabor * notice, this list of conditions and the following disclaimer. 13219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright 14219019Sgabor * notice, this list of conditions and the following disclaimer in the 15219019Sgabor * documentation and/or other materials provided with the distribution. 16219019Sgabor * 17219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18219019Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19219019Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20219019Sgabor * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21219019Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22219019Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23219019Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24219019Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25219019Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26219019Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27219019Sgabor * SUCH DAMAGE. 28219019Sgabor */ 29219019Sgabor 30219019Sgabor#include <sys/cdefs.h> 31219019Sgabor 32219019Sgabor#include <assert.h> 33219019Sgabor#include <ctype.h> 34219019Sgabor#include <errno.h> 35219019Sgabor#include <limits.h> 36219019Sgabor#include <stdio.h> 37219019Sgabor#include <stdlib.h> 38219019Sgabor#include <string.h> 39219019Sgabor 40219019Sgabor#include "citrus_namespace.h" 41219019Sgabor#include "citrus_region.h" 42219019Sgabor#include "citrus_bcs.h" 43219019Sgabor#include "citrus_db_factory.h" 44219019Sgabor#include "citrus_db_hash.h" 45219019Sgabor#include "citrus_lookup_factory.h" 46219019Sgabor#include "citrus_lookup_file.h" 47219019Sgabor 48219019Sgabor#define T_COMM '#' 49219019Sgaborstatic int 50219019Sgaborconvert_line(struct _citrus_db_factory *df, const char *line, size_t len) 51219019Sgabor{ 52219019Sgabor const char *p; 53219019Sgabor char data[LINE_MAX], key[LINE_MAX]; 54219019Sgabor 55219019Sgabor /* cut off trailing comment */ 56219019Sgabor p = memchr(line, T_COMM, len); 57219019Sgabor if (p) 58219019Sgabor len = p - line; 59219019Sgabor 60219019Sgabor /* key */ 61219019Sgabor line = _bcs_skip_ws_len(line, &len); 62219019Sgabor if (len == 0) 63219019Sgabor return (0); 64219019Sgabor p = _bcs_skip_nonws_len(line, &len); 65219019Sgabor if (p == line) 66219019Sgabor return (0); 67219019Sgabor snprintf(key, sizeof(key), "%.*s", (int)(p-line), line); 68219019Sgabor _bcs_convert_to_lower(key); 69219019Sgabor 70219019Sgabor /* data */ 71219019Sgabor line = _bcs_skip_ws_len(p, &len); 72219019Sgabor _bcs_trunc_rws_len(line, &len); 73219019Sgabor snprintf(data, sizeof(data), "%.*s", (int)len, line); 74219019Sgabor 75219019Sgabor return (_db_factory_addstr_by_s(df, key, data)); 76219019Sgabor} 77219019Sgabor 78219019Sgaborstatic int 79219019Sgabordump_db(struct _citrus_db_factory *df, struct _region *r) 80219019Sgabor{ 81219019Sgabor void *ptr; 82219019Sgabor size_t size; 83219019Sgabor 84219019Sgabor size = _db_factory_calc_size(df); 85219019Sgabor ptr = malloc(size); 86219019Sgabor if (ptr == NULL) 87219019Sgabor return (errno); 88219019Sgabor _region_init(r, ptr, size); 89219019Sgabor 90219019Sgabor return (_db_factory_serialize(df, _CITRUS_LOOKUP_MAGIC, r)); 91219019Sgabor} 92219019Sgabor 93219019Sgaborint 94219019Sgabor_citrus_lookup_factory_convert(FILE *out, FILE *in) 95219019Sgabor{ 96219019Sgabor struct _citrus_db_factory *df; 97219019Sgabor struct _region r; 98219019Sgabor char *line; 99219019Sgabor size_t size; 100219019Sgabor int ret; 101219019Sgabor 102219019Sgabor ret = _db_factory_create(&df, &_db_hash_std, NULL); 103219019Sgabor if (ret) 104219019Sgabor return (ret); 105219019Sgabor 106219019Sgabor while ((line = fgetln(in, &size)) != NULL) 107219019Sgabor if ((ret = convert_line(df, line, size))) { 108219019Sgabor _db_factory_free(df); 109219019Sgabor return (ret); 110219019Sgabor } 111219019Sgabor 112219019Sgabor ret = dump_db(df, &r); 113219019Sgabor _db_factory_free(df); 114219019Sgabor if (ret) 115219019Sgabor return (ret); 116219019Sgabor 117219019Sgabor if (fwrite(_region_head(&r), _region_size(&r), 1, out) != 1) 118219019Sgabor return (errno); 119219019Sgabor 120219019Sgabor return (0); 121219019Sgabor} 122