Fixed bug #1049: zebra.cfg lines with leading space are ignored. Res
authorAdam Dickmeiss <adam@indexdata.dk>
Wed, 16 May 2007 10:57:05 +0000 (10:57 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Wed, 16 May 2007 10:57:05 +0000 (10:57 +0000)
system now uses YAZ' tokenizer. Added test for this (tstres.c).

NEWS
util/Makefile.am
util/res.c
util/tstres.c [new file with mode: 0644]
util/tstres.cfg [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index b35fc91..573dcfd 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,5 @@
+Fixed bug #1049: zebra.cfg lines with leading space are ignored.
+
 Fixed bug #1128: sortmax not honored.
 
 Fixed bug #1121: Crash for some searches with customized string.chr.
index 8ed3458..dc83e27 100644 (file)
@@ -1,14 +1,14 @@
-## $Id: Makefile.am,v 1.31 2007-01-05 10:45:13 adam Exp $
+## $Id: Makefile.am,v 1.32 2007-05-16 10:57:06 adam Exp $
 
 noinst_LTLIBRARIES = libidzebra-util.la
 
-check_PROGRAMS = tstcharmap tstflock tstlockscope tstpass
+check_PROGRAMS = tstcharmap tstflock tstlockscope tstpass tstres
 
 TESTS = $(check_PROGRAMS)
 
 bin_SCRIPTS = idzebra-config-2.0
 
-EXTRA_DIST = zebrasrv.rh tstcharmap.chr tstpass.txt
+EXTRA_DIST = zebrasrv.rh tstcharmap.chr tstpass.txt tstres.cfg
 
 DISTCLEANFILES = idzebra-config-2.0
 
@@ -27,6 +27,8 @@ tstflock_SOURCES = tstflock.c
 
 tstlockscope_SOURCES = tstlockscope.c
 
+tstres_SOURCES = tstres.c
+
 clean-local:
        -rm -rf *.LCK 
        -rm -rf *.log 
index 5df5efd..50ef275 100644 (file)
@@ -1,4 +1,4 @@
-/* $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
 
@@ -31,6 +31,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <unistd.h>
 #endif
 
+#include <yaz/tokenizer.h>
 #include <yaz/yaz-util.h>
 #include <idzebra/res.h>
 
@@ -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;
 }
 
diff --git a/util/tstres.c b/util/tstres.c
new file mode 100644 (file)
index 0000000..f27b829
--- /dev/null
@@ -0,0 +1,83 @@
+/* $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
+ */
+
diff --git a/util/tstres.cfg b/util/tstres.cfg
new file mode 100644 (file)
index 0000000..21c3a55
--- /dev/null
@@ -0,0 +1,4 @@
+# comment
+register:a:b#
+name: c  d
+    here: _  #