Updated footer comment
[yaz-moved-to-github.git] / src / readconf.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2009 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /**
7  * \file readconf.c
8  * \brief Implements config file reading
9  */
10
11 #if HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14
15 #include <stdio.h>
16 #include <ctype.h>
17
18 #include <yaz/log.h>
19 #include <yaz/readconf.h>
20
21 #define l_isspace(c) ((c) == '\t' || (c) == ' ' || (c) == '\n' || (c) == '\r')
22
23 int readconf_line(FILE *f, int *lineno, char *line, int len,
24                   char *argv[], int num)
25 {
26     char *p;
27     int argc;
28     
29     while ((p = fgets(line, len, f)))
30     {
31         (*lineno)++;
32         while (*p && l_isspace(*p))
33             p++;
34         if (*p && *p != '#')
35             break;
36     }
37     if (!p)
38         return 0;
39     
40     for (argc = 0; *p ; argc++)
41     {
42         if (*p == '#')  /* trailing comment */
43             break;
44         argv[argc] = p;
45         while (*p && !l_isspace(*p))
46             p++;
47         if (*p)
48         {
49             *(p++) = '\0';
50             while (*p && l_isspace(*p))
51                 p++;
52         }
53     }
54     return argc;
55 }
56
57 /*
58  * Read lines of a configuration file.
59  */
60 int readconf(char *name, void *rprivate,
61              int (*fun)(char *name, void *rprivate, int argc, char *argv[]))
62 {
63     FILE *f;
64     char line[512], *m_argv[50];
65     int m_argc;
66     int lineno = 0;
67     
68     if (!(f = fopen(name, "r")))
69     {
70         yaz_log(YLOG_WARN|YLOG_ERRNO, "readconf: %s", name);
71         return -1;
72     }
73     for (;;)
74     {
75         int res;
76         
77         if (!(m_argc = readconf_line(f, &lineno, line, 512, m_argv, 50)))
78         {
79             fclose(f);
80             return 0;
81         }
82
83         if ((res = (*fun)(name, rprivate, m_argc, m_argv)))
84         {
85             fclose(f);
86             return res;
87         }
88     }
89 }
90 /*
91  * Local variables:
92  * c-basic-offset: 4
93  * c-file-style: "Stroustrup"
94  * indent-tabs-mode: nil
95  * End:
96  * vim: shiftwidth=4 tabstop=8 expandtab
97  */
98