-/* $Id: res.c,v 1.57 2007-05-16 08:57:27 adam Exp $
+/* $Id: res.c,v 1.58 2007-05-16 10:57:06 adam Exp $
Copyright (C) 1995-2007
Index Data ApS
#include <unistd.h>
#endif
+#include <yaz/tokenizer.h>
#include <yaz/yaz-util.h>
#include <idzebra/res.h>
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);
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;
}
--- /dev/null
+/* $Id: tstres.c,v 1.1 2007-05-16 10:57:06 adam Exp $
+ Copyright (C) 1995-2007
+ Index Data ApS
+
+This file is part of the Zebra server.
+
+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
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+*/
+
+#include <idzebra/res.h>
+#include <yaz/test.h>
+
+static void tst_res_open(void)
+{
+ Res res = 0;
+
+ res_close(res);
+
+ res = res_open(0, 0);
+ YAZ_CHECK(res);
+ res_close(res);
+}
+
+
+static void tst_res_read_file(void)
+{
+ Res res = res_open(0, 0);
+
+ YAZ_CHECK(res);
+ if (res)
+ {
+ int r = res_read_file(res, "notfound");
+ YAZ_CHECK_EQ(r, ZEBRA_FAIL);
+ }
+ if (res)
+ {
+ const char *v;
+ int r = res_read_file(res, "tstres.cfg");
+ YAZ_CHECK_EQ(r, ZEBRA_OK);
+
+ v = res_get_def(res, "register", "none");
+ YAZ_CHECK(!strcmp(v, "a:b"));
+
+ v = res_get_def(res, "name", "none");
+ YAZ_CHECK(!strcmp(v, "c d"));
+
+ v = res_get_def(res, "here", "none");
+ YAZ_CHECK(!strcmp(v, "_"));
+
+ v = res_get_def(res, "namex", "none");
+ YAZ_CHECK(!strcmp(v, "none"));
+ }
+ res_close(res);
+}
+
+int main (int argc, char **argv)
+{
+ YAZ_CHECK_INIT(argc, argv);
+ tst_res_open();
+ tst_res_read_file();
+ YAZ_CHECK_TERM;
+}
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+