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