X-Git-Url: http://git.indexdata.com/?p=idzebra-moved-to-github.git;a=blobdiff_plain;f=util%2Fres.c;h=efb6473806a4dc26c503fee9d13c204e9566f855;hp=5df5efdf1b051f3cad1ddb82382259231d75fd50;hb=96e4c5479e111511f5df3531b6648931251b9e5d;hpb=d64c82cd05145e97c4edef0434c383a29c95c8fe diff --git a/util/res.c b/util/res.c index 5df5efd..efb6473 100644 --- a/util/res.c +++ b/util/res.c @@ -1,8 +1,5 @@ -/* $Id: res.c,v 1.57 2007-05-16 08:57:27 adam Exp $ - Copyright (C) 1995-2007 - Index Data ApS - -This file is part of the Zebra server. +/* This file is part of the Zebra server. + Copyright (C) 1994-2011 Index Data Zebra is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free @@ -20,6 +17,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +#if HAVE_CONFIG_H +#include +#endif #include #include #include @@ -31,6 +31,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #endif +#include #include #include @@ -144,12 +145,8 @@ static char *xstrdup_env(const char *src) ZEBRA_RES res_read_file(Res r, const char *fname) { - struct res_entry *resp; - char *line; - char *val_buf; - int val_size, val_max = 256; - char fr_buf[1024]; FILE *fr; + int errors = 0; assert(r); @@ -157,81 +154,80 @@ ZEBRA_RES res_read_file(Res r, const char *fname) if (!fr) { yaz_log(YLOG_WARN|YLOG_ERRNO, "Cannot open `%s'", fname); - return ZEBRA_FAIL; + errors++; } - val_buf = (char*) xmalloc(val_max); - while (1) + else { - line = fgets(fr_buf, sizeof(fr_buf)-1, fr); - if (!line) - break; - if (*line != '#') + char fr_buf[1024]; + char *line; + int lineno = 1; + WRBUF wrbuf_val = wrbuf_alloc(); + yaz_tok_cfg_t yt = yaz_tok_cfg_create(); + + while ((line = fgets(fr_buf, sizeof(fr_buf)-1, fr))) { - int no = 0; - while (1) + yaz_tok_parse_t tp = yaz_tok_parse_buf(yt, line); + int t = yaz_tok_move(tp); + + if (t == YAZ_TOK_STRING) { - if (fr_buf[no] == 0 || fr_buf[no] == '\n' ) + size_t sz; + struct res_entry *resp; + const char *cp = yaz_tok_parse_string(tp); + const char *cp1 = strchr(cp, ':'); + + if (!cp1) { - no = 0; + yaz_log(YLOG_FATAL, "%s:%d missing colon after '%s'", + fname, lineno, cp); + errors++; break; } - if (strchr(": \t", fr_buf[no])) - break; - no++; - } - if (!no) - continue; - fr_buf[no++] = '\0'; - resp = add_entry(r); - resp->name = (char*) xmalloc(no); - strcpy(resp->name, fr_buf); - - while (strchr(" \t", fr_buf[no])) - no++; - val_size = 0; - while (1) - { - if (fr_buf[no] == '\0' || strchr("\n\r\f", fr_buf[no])) + resp = add_entry(r); + sz = cp1 - cp; + resp->name = xmalloc(sz + 1); + memcpy(resp->name, cp, sz); + resp->name[sz] = '\0'; + + wrbuf_rewind(wrbuf_val); + + if (cp1[1]) { - while (val_size > 0 && - (val_buf[val_size-1] == ' ' || - val_buf[val_size-1] == '\t')) - val_size--; - val_buf[val_size] = '\0'; - resp->value = xstrdup_env(val_buf); - yaz_log(YLOG_DEBUG, "(name=%s,value=%s)", - resp->name, resp->value); - break; + /* name:value */ + wrbuf_puts(wrbuf_val, cp1+1); } - else if (fr_buf[no] == '\\' && strchr("\n\r\f", fr_buf[no+1])) + else { - line = fgets(fr_buf, sizeof(fr_buf)-1, fr); - if (!line) + /* name: value */ + t = yaz_tok_move(tp); + + if (t != YAZ_TOK_STRING) { - val_buf[val_size] = '\0'; - resp->value = xstrdup_env(val_buf); + resp->value = xstrdup(""); + yaz_log(YLOG_FATAL, "%s:%d missing value after '%s'", + fname, lineno, resp->name); + errors++; break; } - no = 0; + wrbuf_puts(wrbuf_val, yaz_tok_parse_string(tp)); } - else + while ((t=yaz_tok_move(tp)) == YAZ_TOK_STRING) { - val_buf[val_size++] = fr_buf[no++]; - if (val_size+1 >= val_max) - { - char *nb; - - nb = (char*) xmalloc(val_max+=1024); - memcpy(nb, val_buf, val_size); - xfree(val_buf); - val_buf = nb; - } + wrbuf_putc(wrbuf_val, ' '); + wrbuf_puts(wrbuf_val, yaz_tok_parse_string(tp)); } + resp->value = xstrdup_env(wrbuf_cstr(wrbuf_val)); + /* printf("name=%s value=%s\n", resp->name, resp->value); */ } - } - } - xfree(val_buf); - fclose(fr); + lineno++; + yaz_tok_parse_destroy(tp); + } + fclose(fr); + yaz_tok_cfg_destroy(yt); + wrbuf_destroy(wrbuf_val); + } + if (errors) + return ZEBRA_FAIL; return ZEBRA_OK; } @@ -548,6 +544,7 @@ int res_check(Res r_i, Res r_v) /* * Local variables: * c-basic-offset: 4 + * c-file-style: "Stroustrup" * indent-tabs-mode: nil * End: * vim: shiftwidth=4 tabstop=8 expandtab