1 /* configfile.c: parsing of config files
3 * Copyright (C) 2008 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
11 #include "configfile.h"
13 static int next_char(FILE *f)
26 static void skip_line(FILE *f)
30 while ((c = next_char(f)) && c != '\n' && c != EOF)
34 static int read_config_line(FILE *f, struct strbuf *name, struct strbuf *value)
41 /* Skip comments and preceding spaces. */
43 if (c == '#' || c == ';')
50 /* Read variable name. */
52 if (c == '\n' || c == EOF)
54 strbuf_addch(name, c);
58 /* Read variable value. */
60 while (c != '\n' && c != EOF) {
61 strbuf_addch(value, c);
68 int parse_configfile(const char *filename, configfile_value_fn fn)
71 struct strbuf name = STRBUF_INIT;
72 struct strbuf value = STRBUF_INIT;
75 /* cancel deeply nested include-commands */
78 if (!(f = fopen(filename, "r")))
81 while (read_config_line(f, &name, &value))
82 fn(name.buf, value.buf);
85 strbuf_release(&name);
86 strbuf_release(&value);