2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.13 1995-09-04 12:34:05 adam
8 * Various cleanup. YAZ util used instead.
10 * Revision 1.12 1995/01/24 16:40:32 adam
13 * Revision 1.11 1994/10/05 16:54:52 adam
16 * Revision 1.10 1994/10/05 10:47:31 adam
19 * Revision 1.9 1994/09/16 14:41:12 quinn
20 * Added log warning to res_get_def
22 * Revision 1.8 1994/09/16 14:37:12 quinn
25 * Revision 1.7 1994/09/06 13:01:03 quinn
26 * Removed const from declaration of res_get
28 * Revision 1.6 1994/09/01 17:45:14 adam
29 * Work on resource manager.
31 * Revision 1.5 1994/08/18 11:02:28 adam
32 * Implementation of res_write.
34 * Revision 1.4 1994/08/18 10:02:01 adam
35 * Module alexpath moved from res.c to alexpath.c. Minor changes in res-test.c
37 * Revision 1.3 1994/08/18 09:43:51 adam
38 * Development of resource manager. Only missing is res_write.
40 * Revision 1.2 1994/08/18 08:23:26 adam
41 * Res.c now use handles. xmalloc defines xstrdup.
43 * Revision 1.1 1994/08/17 15:34:23 adam
44 * Initial version of resource manager.
54 static struct res_entry *add_entry (Res r)
56 struct res_entry *resp;
59 resp = r->last = r->first = xmalloc (sizeof(*resp));
62 resp = xmalloc (sizeof(*resp));
70 static void reread (Res r)
72 struct res_entry *resp;
75 int val_size, val_max = 256;
83 val_buf = xmalloc (val_max);
85 strcpy (path, alex_path(r->name));
87 fr = fopen (path, "r");
90 logf (LOG_FATAL|LOG_ERRNO, "Cannot open %s", path);
95 line = fgets (fr_buf, 1023, fr);
102 while (fr_buf[no] && fr_buf[no] != '\n')
106 resp = add_entry (r);
107 resp->name = xmalloc (no+1);
109 strcpy (resp->name, fr_buf);
116 if (fr_buf[no] == 0 || fr_buf[no] == '\n' )
121 if (fr_buf[no] == ':')
128 resp = add_entry (r);
129 resp->name = xmalloc (no);
130 strcpy (resp->name, fr_buf);
132 while (fr_buf[no] == ' ')
137 if (fr_buf[no] == '\0' || fr_buf[no] == '\n')
139 val_buf[val_size++] = '\0';
140 resp->value = xmalloc (val_size);
141 strcpy (resp->value, val_buf);
142 logf (LOG_DEBUG, "(name=%s,value=%s)",
143 resp->name, resp->value);
146 else if (fr_buf[no] == '\\' && fr_buf[no+1] == '\n')
148 line = fgets (fr_buf, 1023, fr);
151 resp->value = xmalloc (val_size);
152 strcpy (resp->value, val_buf);
159 val_buf[val_size++] = fr_buf[no++];
160 if (val_size+1 >= val_max)
164 nb = xmalloc (val_max+=1024);
165 memcpy (nb, val_buf, val_size);
177 Res res_open (const char *name)
180 if (access (name, R_OK))
181 logf (LOG_LOG|LOG_ERRNO, "Cannot access `%s'", name);
182 r = xmalloc (sizeof(*r));
184 r->first = r->last = NULL;
185 r->name = xstrdup (name);
189 void res_close (Res r)
194 struct res_entry *re, *re1;
195 for (re = r->first; re; re=re1)
208 char *res_get (Res r, const char *name)
210 struct res_entry *re;
215 for (re = r->first; re; re=re->next)
216 if (re->value && !strcmp (re->name, name))
221 char *res_get_def (Res r, const char *name, char *def)
225 if (!(t = res_get (r, name)))
227 logf (LOG_DEBUG, "CAUTION: Using default resource %s:%s", name, def);
234 void res_put (Res r, const char *name, const char *value)
236 struct res_entry *re;
241 for (re = r->first; re; re=re->next)
242 if (re->value && !strcmp (re->name, name))
245 re->value = xstrdup (value);
249 re->name = xstrdup (name);
250 re->value = xstrdup (value);
253 void res_trav (Res r, const char *prefix,
254 void (*f)(const char *name, const char *value))
256 struct res_entry *re;
264 for (re = r->first; re; re=re->next)
266 if (l==0 || !memcmp (re->name, prefix, l))
267 (*f)(re->name, re->value);
271 int res_write (Res r)
273 struct res_entry *re;
280 strcpy (path, alex_path(r->name));
282 fr = fopen (path, "w");
285 logf (LOG_FATAL|LOG_ERRNO, "Cannot create %s", path);
289 for (re = r->first; re; re=re->next)
292 int lefts = strlen(re->name)+2;
295 fprintf (fr, "%s\n", re->name);
298 fprintf (fr, "%s: ", re->name);
299 while (lefts + strlen(re->value+no) > 78)
302 int ind = no+ 78-lefts;
305 if (re->value[ind] == ' ')
310 ind = no + 78 - lefts;
311 for (i = no; i != ind; i++)
312 putc (re->value[i], fr);
313 fprintf (fr, "\\\n");
317 fprintf (fr, "%s\n", re->value+no);