Module alexpath moved from res.c to alexpath.c. Minor changes in res-test.c
[idzebra-moved-to-github.git] / util / res.c
index 56f01c9..5655a61 100644 (file)
@@ -4,63 +4,44 @@
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: res.c,v $
- * Revision 1.1  1994-08-17 15:34:23  adam
+ * Revision 1.4  1994-08-18 10:02:01  adam
+ * Module alexpath moved from res.c to alexpath.c. Minor changes in res-test.c
+ *
+ * Revision 1.3  1994/08/18  09:43:51  adam
+ * Development of resource manager. Only missing is res_write.
+ *
+ * Revision 1.2  1994/08/18  08:23:26  adam
+ * Res.c now use handles. xmalloc defines xstrdup.
+ *
+ * Revision 1.1  1994/08/17  15:34:23  adam
  * Initial version of resource manager.
  *
  */
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <util.h>
 
-struct res {
-    char *name;
-    char *value;
-    struct res *next;
-};
-
-static struct res *first = NULL, *last = NULL;
-static int res_init = 0;
-
-static FILE *fr;
-
-const char *alex_path (const char *name)
-{
-    static char path[256];
-    char *alex_prefix;
-
-    if (!(alex_prefix = getenv ("ALEXPREFIX")))
-        alex_prefix = "./";
-    
-    if (*alex_prefix && alex_prefix[strlen(alex_prefix)-1] == '/')
-        sprintf (path, "%s%s", alex_prefix, name);
-    else
-        sprintf (path, "%s/%s", alex_prefix, name);
-    return path;
-}
-
-static void reread (void)
+static void reread (Res r)
 {
-    struct res *resp;
+    struct res_entry *resp;
     char *line;
-    char *alex_base;
     char *val_buf;
     int val_size, val_max = 1024;
     char path[256];
     char fr_buf[1024];
+    FILE *fr;
 
-    res_init = 1;
+    r->init = 1;
 
     val_buf = xmalloc (val_max);
 
-    if (!(alex_base = getenv ("ALEXBASE")))
-        alex_base = "base";
-
-    strcpy (path, alex_path(alex_base));
+    strcpy (path, alex_path(r->name));
 
     fr = fopen (path, "r");
     if (!fr)
     {
-        log (LOG_FATAL, "cannot open %s", path);
+        log (LOG_FATAL|LOG_ERRNO, "cannot open %s", path);
         exit (1);
     }
     while (1)
@@ -76,13 +57,13 @@ static void reread (void)
                 no++;
             fr_buf[no] = '\0';
 
-            if (!first)
-                resp = last = first = xmalloc (sizeof(*resp));
+            if (!r->first)
+                resp = r->last = r->first = xmalloc (sizeof(*resp));
             else
             {
                 resp = xmalloc (sizeof(*resp));
-                last->next = resp;
-                last = resp;
+                r->last->next = resp;
+                r->last = resp;
             }
             resp->next = NULL;
             resp->name = xmalloc (no+1);
@@ -106,13 +87,13 @@ static void reread (void)
             if (no < 0)
                 continue;
             fr_buf[no++] = '\0';
-            if (!first)
-                resp = last = first = xmalloc (sizeof(*resp));
+            if (!r->first)
+                resp = r->last = r->first = xmalloc (sizeof(*resp));
             else
             {
                 resp = xmalloc (sizeof(*resp));
-                last->next = resp;
-                last = resp;
+                r->last->next = resp;
+                r->last = resp;
             }
             resp->next = NULL;
             resp->name = xmalloc (no);
@@ -128,6 +109,8 @@ static void reread (void)
                     val_buf[val_size++] = '\0';
                     resp->value = xmalloc (val_size);
                     strcpy (resp->value, val_buf);
+                    log (LOG_DEBUG, "(name=%s,value=%s)",
+                         resp->name, resp->value);
                     break;
                 }
                 else if (fr_buf[no] == '\\' && fr_buf[no+1] == '\n')
@@ -142,7 +125,7 @@ static void reread (void)
                     no = 0;
                 }
                 else
-                    val_buf[val_size] = fr_buf[no++];
+                    val_buf[val_size++] = fr_buf[no++];
             }
         }
     }                
@@ -150,25 +133,91 @@ static void reread (void)
     fclose (fr);
 }
 
+Res res_open (const char *name)
+{
+    Res r = xmalloc (sizeof(*r));
+    r->init = 0;
+    r->name = xstrdup (name);
+    return r;
+}
 
-const char *res_get (const char *name)
+void res_close (Res r)
 {
-    if (!res_init)
-        reread ();
-    return NULL;
+    if (r->init)
+    {
+        struct res_entry *re, *re1;
+        for (re = r->first; re; re=re1)
+        {
+            if (re->name)
+                xfree (re->name);
+            if (re->value)
+                xfree (re->value);
+            re1 = re->next;
+            xfree (re);
+        }
+    }
+    xfree (r);
 }
 
-const char *res_put (const char *name, const char *value)
+const char *res_get (Res r, const char *name)
 {
-    if (!res_init)
-        reread ();
+    struct res_entry *re;
+    if (!r->init)
+        reread (r);
+    
+    for (re = r->first; re; re=re->next)
+        if (re->value && !strcmp (re->name, name))
+            return re->value;
     return NULL;
 }
-    
-int res_write (void)
+
+void res_put (Res r, const char *name, const char *value)
+{
+    struct res_entry *re;
+    if (!r->init)
+        reread (r);
+
+    for (re = r->first; re; re=re->next)
+        if (re->value && !strcmp (re->name, name))
+        {
+            xfree (re->value);
+            re->value = xstrdup (value);
+            return;
+        }
+    if (!r->first)
+        re = r->last = r->first = xmalloc (sizeof(*re));
+    else
+    {
+        re = xmalloc (sizeof(*re));
+        r->last->next = re;
+        r->last = re;
+    }
+    re->next = NULL;
+    re->name = xstrdup (name);
+    re->value = xstrdup (value);
+}
+
+void res_trav (Res r, const char *prefix, 
+               void (*f)(const char *name, const char *value))
+{
+    struct res_entry *re;
+    int l = 0;
+
+    if (prefix)
+        l = strlen(prefix);
+    if (!r->init)
+        reread (r);
+    for (re = r->first; re; re=re->next)
+        if (re->value)
+            if (l==0 || !memcmp (re->name, prefix, l))
+                (*f)(re->name, re->value);
+}
+
+
+int res_write (Res r)
 {
-    if (!res_init)
-        reread ();
+    if (!r->init)
+        reread (r);
     return 0;
 }