Changed space criteria to support 8-bit characters
[yaz-moved-to-github.git] / util / readconf.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: readconf.c,v $
7  * Revision 1.2  1996-05-29 10:05:01  quinn
8  * Changed space criteria to support 8-bit characters
9  *
10  * Revision 1.1  1995/11/01  13:55:06  quinn
11  * Minor adjustments
12  *
13  * Revision 1.2  1995/10/30  13:54:27  quinn
14  * iRemoved fclose().
15  *
16  * Revision 1.1  1995/10/10  16:28:18  quinn
17  * Initial revision
18  *
19  *
20  */
21
22 #include <stdio.h>
23 #include <ctype.h>
24
25 #include <log.h>
26
27 #define l_isspace(c) ((c) == '\t' || (c) == ' ')
28
29 int readconf_line(FILE *f, char *line, int len, char *argv[], int num)
30 {
31     char *p;
32     int argc;
33
34     while ((p = fgets(line, len, f)))
35     {
36         while (*p && isspace(*p))
37             p++;
38         if (*p && *p != '#')
39             break;
40     }
41     if (!p)
42         return 0;
43
44     for (argc = 0; *p ; argc++)
45     {
46         if (*p == '#')  /* trailing comment */
47             break;
48         argv[argc] = p;
49         while (*p && !l_isspace(*p))
50             p++;
51         if (*p)
52         {
53             *(p++) = '\0';
54             while (*p && l_isspace(*p))
55                 p++;
56         }
57     }
58     return argc;
59 }
60
61 /*
62  * Read lines of a configuration file.
63  */
64 int readconf(char *name, void *private,
65     int (*fun)(char *name, void *private, int argc, char *argv[]))
66 {
67     FILE *f;
68     char line[512], *m_argv[50];
69     int m_argc;
70
71     if (!(f = fopen(name, "r")))
72     {
73         logf(LOG_WARN|LOG_ERRNO, "readconf: %s", name);
74         return -1;
75     }
76     for (;;)
77     {
78         int res;
79
80         if (!(m_argc = readconf_line(f, line, 512, m_argv, 50)))
81         {
82             fclose(f);
83             return 0;
84         }
85
86         if ((res = (*fun)(name, private, m_argc, m_argv)))
87         {
88             fclose(f);
89             return res;
90         }
91     }
92 }