Initial version of resource manager.
authorAdam Dickmeiss <adam@indexdata.dk>
Wed, 17 Aug 1994 15:34:14 +0000 (15:34 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Wed, 17 Aug 1994 15:34:14 +0000 (15:34 +0000)
include/res.h [new file with mode: 0644]
util/Makefile
util/res.c [new file with mode: 0644]

diff --git a/include/res.h b/include/res.h
new file mode 100644 (file)
index 0000000..82d892f
--- /dev/null
@@ -0,0 +1,18 @@
+/*
+ * Copyright (C) 1994, Index Data I/S 
+ * All rights reserved.
+ * Sebastian Hammer, Adam Dickmeiss
+ *
+ * $Log: res.h,v $
+ * Revision 1.1  1994-08-17 15:34:14  adam
+ * Initial version of resource manager.
+ *
+ */
+
+#ifndef RES_H
+#define RES_H
+
+const char *res_get (const char *name);
+const char *res_put (const char *name, const char *value);
+int res_write (void);
+#endif
index 45d00da..b9b37ab 100644 (file)
@@ -1,7 +1,7 @@
 # Copyright (C) 1994, Index Data I/S 
 # All rights reserved.
 # Sebastian Hammer, Adam Dickmeiss
-# $Id: Makefile,v 1.3 1994-08-17 13:32:49 adam Exp $
+# $Id: Makefile,v 1.4 1994-08-17 15:34:22 adam Exp $
 
 SHELL=/bin/sh
 INCLUDE=-I../include
@@ -9,7 +9,7 @@ TPROG=opt-test
 CFLAGS=-g -Wall
 DEFS=$(INCLUDE)
 LIB=../lib/util.a
-PO = options.o xmalloc.o log.o
+PO = options.o xmalloc.o log.o res.o
 CPP=cc -E
 
 all: $(LIB)
diff --git a/util/res.c b/util/res.c
new file mode 100644 (file)
index 0000000..56f01c9
--- /dev/null
@@ -0,0 +1,176 @@
+/*
+ * Copyright (C) 1994, Index Data I/S 
+ * All rights reserved.
+ * Sebastian Hammer, Adam Dickmeiss
+ *
+ * $Log: res.c,v $
+ * Revision 1.1  1994-08-17 15:34:23  adam
+ * Initial version of resource manager.
+ *
+ */
+#include <stdio.h>
+#include <stdlib.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)
+{
+    struct res *resp;
+    char *line;
+    char *alex_base;
+    char *val_buf;
+    int val_size, val_max = 1024;
+    char path[256];
+    char fr_buf[1024];
+
+    res_init = 1;
+
+    val_buf = xmalloc (val_max);
+
+    if (!(alex_base = getenv ("ALEXBASE")))
+        alex_base = "base";
+
+    strcpy (path, alex_path(alex_base));
+
+    fr = fopen (path, "r");
+    if (!fr)
+    {
+        log (LOG_FATAL, "cannot open %s", path);
+        exit (1);
+    }
+    while (1)
+    {
+        line = fgets (fr_buf, 1023, fr);
+        if (!line)
+            break;
+        if (*line == '#')
+        {
+            int no = 0;
+
+            while (fr_buf[no] && fr_buf[no] != '\n')
+                no++;
+            fr_buf[no] = '\0';
+
+            if (!first)
+                resp = last = first = xmalloc (sizeof(*resp));
+            else
+            {
+                resp = xmalloc (sizeof(*resp));
+                last->next = resp;
+                last = resp;
+            }
+            resp->next = NULL;
+            resp->name = xmalloc (no+1);
+            resp->value = NULL;
+            strcpy (resp->name, fr_buf);
+        }
+        else
+        {
+            int no = 0;
+            while (1)
+            {
+                if (fr_buf[no] == 0 || fr_buf[no] == '\n' )
+                {
+                    no = -1;
+                    break;
+                }
+                if (fr_buf[no] == ':')
+                    break;
+                no++;
+            }
+            if (no < 0)
+                continue;
+            fr_buf[no++] = '\0';
+            if (!first)
+                resp = last = first = xmalloc (sizeof(*resp));
+            else
+            {
+                resp = xmalloc (sizeof(*resp));
+                last->next = resp;
+                last = resp;
+            }
+            resp->next = NULL;
+            resp->name = xmalloc (no);
+            strcpy (resp->name, fr_buf);
+            
+            while (fr_buf[no] == ' ')
+                no++;
+            val_size = 0;
+            while (1)
+            {
+                if (fr_buf[no] == '\0' || fr_buf[no] == '\n')
+                {
+                    val_buf[val_size++] = '\0';
+                    resp->value = xmalloc (val_size);
+                    strcpy (resp->value, val_buf);
+                    break;
+                }
+                else if (fr_buf[no] == '\\' && fr_buf[no+1] == '\n')
+                {
+                    line = fgets (fr_buf, 1023, fr);
+                    if (!line)
+                    {
+                        resp->value = xmalloc (val_size);
+                        strcpy (resp->value, val_buf);
+                        break;
+                    }
+                    no = 0;
+                }
+                else
+                    val_buf[val_size] = fr_buf[no++];
+            }
+        }
+    }                
+    xfree (val_buf);
+    fclose (fr);
+}
+
+
+const char *res_get (const char *name)
+{
+    if (!res_init)
+        reread ();
+    return NULL;
+}
+
+const char *res_put (const char *name, const char *value)
+{
+    if (!res_init)
+        reread ();
+    return NULL;
+}
+    
+int res_write (void)
+{
+    if (!res_init)
+        reread ();
+    return 0;
+}
+
+
+