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