Bump year
[yaz-moved-to-github.git] / src / readconf.c
1 /*
2  * Copyright (C) 1995-2005, Index Data ApS
3  * All rights reserved.
4  *
5  * $Id: readconf.c,v 1.5 2005-01-15 19:47:14 adam Exp $
6  */
7
8 /**
9  * \file readconf.c
10  * \brief Implements config file reading
11  */
12
13 #if HAVE_CONFIG_H
14 #include <config.h>
15 #endif
16
17 #include <stdio.h>
18 #include <ctype.h>
19
20 #include <yaz/log.h>
21 #include <yaz/readconf.h>
22
23 #define l_isspace(c) ((c) == '\t' || (c) == ' ' || (c) == '\n' || (c) == '\r')
24
25 int readconf_line(FILE *f, int *lineno, char *line, int len,
26                   char *argv[], int num)
27 {
28     char *p;
29     int argc;
30     
31     while ((p = fgets(line, len, f)))
32     {
33         (*lineno)++;
34         while (*p && l_isspace(*p))
35             p++;
36         if (*p && *p != '#')
37             break;
38     }
39     if (!p)
40         return 0;
41     
42     for (argc = 0; *p ; argc++)
43     {
44         if (*p == '#')  /* trailing comment */
45             break;
46         argv[argc] = p;
47         while (*p && !l_isspace(*p))
48             p++;
49         if (*p)
50         {
51             *(p++) = '\0';
52             while (*p && l_isspace(*p))
53                 p++;
54         }
55     }
56     return argc;
57 }
58
59 /*
60  * Read lines of a configuration file.
61  */
62 int readconf(char *name, void *rprivate,
63              int (*fun)(char *name, void *rprivate, int argc, char *argv[]))
64 {
65     FILE *f;
66     char line[512], *m_argv[50];
67     int m_argc;
68     int lineno = 0;
69     
70     if (!(f = fopen(name, "r")))
71     {
72         yaz_log(YLOG_WARN|YLOG_ERRNO, "readconf: %s", name);
73         return -1;
74     }
75     for (;;)
76     {
77         int res;
78         
79         if (!(m_argc = readconf_line(f, &lineno, line, 512, m_argv, 50)))
80         {
81             fclose(f);
82             return 0;
83         }
84
85         if ((res = (*fun)(name, rprivate, m_argc, m_argv)))
86         {
87             fclose(f);
88             return res;
89         }
90     }
91 }