2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.4 1994-08-18 10:02:01 adam
8 * Module alexpath moved from res.c to alexpath.c. Minor changes in res-test.c
10 * Revision 1.3 1994/08/18 09:43:51 adam
11 * Development of resource manager. Only missing is res_write.
13 * Revision 1.2 1994/08/18 08:23:26 adam
14 * Res.c now use handles. xmalloc defines xstrdup.
16 * Revision 1.1 1994/08/17 15:34:23 adam
17 * Initial version of resource manager.
25 static void reread (Res r)
27 struct res_entry *resp;
30 int val_size, val_max = 1024;
37 val_buf = xmalloc (val_max);
39 strcpy (path, alex_path(r->name));
41 fr = fopen (path, "r");
44 log (LOG_FATAL|LOG_ERRNO, "cannot open %s", path);
49 line = fgets (fr_buf, 1023, fr);
56 while (fr_buf[no] && fr_buf[no] != '\n')
61 resp = r->last = r->first = xmalloc (sizeof(*resp));
64 resp = xmalloc (sizeof(*resp));
69 resp->name = xmalloc (no+1);
71 strcpy (resp->name, fr_buf);
78 if (fr_buf[no] == 0 || fr_buf[no] == '\n' )
83 if (fr_buf[no] == ':')
91 resp = r->last = r->first = xmalloc (sizeof(*resp));
94 resp = xmalloc (sizeof(*resp));
99 resp->name = xmalloc (no);
100 strcpy (resp->name, fr_buf);
102 while (fr_buf[no] == ' ')
107 if (fr_buf[no] == '\0' || fr_buf[no] == '\n')
109 val_buf[val_size++] = '\0';
110 resp->value = xmalloc (val_size);
111 strcpy (resp->value, val_buf);
112 log (LOG_DEBUG, "(name=%s,value=%s)",
113 resp->name, resp->value);
116 else if (fr_buf[no] == '\\' && fr_buf[no+1] == '\n')
118 line = fgets (fr_buf, 1023, fr);
121 resp->value = xmalloc (val_size);
122 strcpy (resp->value, val_buf);
128 val_buf[val_size++] = fr_buf[no++];
136 Res res_open (const char *name)
138 Res r = xmalloc (sizeof(*r));
140 r->name = xstrdup (name);
144 void res_close (Res r)
148 struct res_entry *re, *re1;
149 for (re = r->first; re; re=re1)
162 const char *res_get (Res r, const char *name)
164 struct res_entry *re;
168 for (re = r->first; re; re=re->next)
169 if (re->value && !strcmp (re->name, name))
174 void res_put (Res r, const char *name, const char *value)
176 struct res_entry *re;
180 for (re = r->first; re; re=re->next)
181 if (re->value && !strcmp (re->name, name))
184 re->value = xstrdup (value);
188 re = r->last = r->first = xmalloc (sizeof(*re));
191 re = xmalloc (sizeof(*re));
196 re->name = xstrdup (name);
197 re->value = xstrdup (value);
200 void res_trav (Res r, const char *prefix,
201 void (*f)(const char *name, const char *value))
203 struct res_entry *re;
210 for (re = r->first; re; re=re->next)
212 if (l==0 || !memcmp (re->name, prefix, l))
213 (*f)(re->name, re->value);
217 int res_write (Res r)