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