Initial version of resource manager.
[idzebra-moved-to-github.git] / util / res.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: res.c,v $
7  * Revision 1.1  1994-08-17 15:34:23  adam
8  * Initial version of resource manager.
9  *
10  */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <util.h>
14
15 struct res {
16     char *name;
17     char *value;
18     struct res *next;
19 };
20
21 static struct res *first = NULL, *last = NULL;
22 static int res_init = 0;
23
24 static FILE *fr;
25
26 const char *alex_path (const char *name)
27 {
28     static char path[256];
29     char *alex_prefix;
30
31     if (!(alex_prefix = getenv ("ALEXPREFIX")))
32         alex_prefix = "./";
33     
34     if (*alex_prefix && alex_prefix[strlen(alex_prefix)-1] == '/')
35         sprintf (path, "%s%s", alex_prefix, name);
36     else
37         sprintf (path, "%s/%s", alex_prefix, name);
38     return path;
39 }
40
41 static void reread (void)
42 {
43     struct res *resp;
44     char *line;
45     char *alex_base;
46     char *val_buf;
47     int val_size, val_max = 1024;
48     char path[256];
49     char fr_buf[1024];
50
51     res_init = 1;
52
53     val_buf = xmalloc (val_max);
54
55     if (!(alex_base = getenv ("ALEXBASE")))
56         alex_base = "base";
57
58     strcpy (path, alex_path(alex_base));
59
60     fr = fopen (path, "r");
61     if (!fr)
62     {
63         log (LOG_FATAL, "cannot open %s", path);
64         exit (1);
65     }
66     while (1)
67     {
68         line = fgets (fr_buf, 1023, fr);
69         if (!line)
70             break;
71         if (*line == '#')
72         {
73             int no = 0;
74
75             while (fr_buf[no] && fr_buf[no] != '\n')
76                 no++;
77             fr_buf[no] = '\0';
78
79             if (!first)
80                 resp = last = first = xmalloc (sizeof(*resp));
81             else
82             {
83                 resp = xmalloc (sizeof(*resp));
84                 last->next = resp;
85                 last = resp;
86             }
87             resp->next = NULL;
88             resp->name = xmalloc (no+1);
89             resp->value = NULL;
90             strcpy (resp->name, fr_buf);
91         }
92         else
93         {
94             int no = 0;
95             while (1)
96             {
97                 if (fr_buf[no] == 0 || fr_buf[no] == '\n' )
98                 {
99                     no = -1;
100                     break;
101                 }
102                 if (fr_buf[no] == ':')
103                     break;
104                 no++;
105             }
106             if (no < 0)
107                 continue;
108             fr_buf[no++] = '\0';
109             if (!first)
110                 resp = last = first = xmalloc (sizeof(*resp));
111             else
112             {
113                 resp = xmalloc (sizeof(*resp));
114                 last->next = resp;
115                 last = resp;
116             }
117             resp->next = NULL;
118             resp->name = xmalloc (no);
119             strcpy (resp->name, fr_buf);
120             
121             while (fr_buf[no] == ' ')
122                 no++;
123             val_size = 0;
124             while (1)
125             {
126                 if (fr_buf[no] == '\0' || fr_buf[no] == '\n')
127                 {
128                     val_buf[val_size++] = '\0';
129                     resp->value = xmalloc (val_size);
130                     strcpy (resp->value, val_buf);
131                     break;
132                 }
133                 else if (fr_buf[no] == '\\' && fr_buf[no+1] == '\n')
134                 {
135                     line = fgets (fr_buf, 1023, fr);
136                     if (!line)
137                     {
138                         resp->value = xmalloc (val_size);
139                         strcpy (resp->value, val_buf);
140                         break;
141                     }
142                     no = 0;
143                 }
144                 else
145                     val_buf[val_size] = fr_buf[no++];
146             }
147         }
148     }                
149     xfree (val_buf);
150     fclose (fr);
151 }
152
153
154 const char *res_get (const char *name)
155 {
156     if (!res_init)
157         reread ();
158     return NULL;
159 }
160
161 const char *res_put (const char *name, const char *value)
162 {
163     if (!res_init)
164         reread ();
165     return NULL;
166 }
167     
168 int res_write (void)
169 {
170     if (!res_init)
171         reread ();
172     return 0;
173 }
174
175
176