Diffstat (limited to 'configfile.c')
| -rw-r--r-- | configfile.c | 71 |
1 files changed, 34 insertions, 37 deletions
diff --git a/configfile.c b/configfile.c index e039109..d98989c 100644 --- a/configfile.c +++ b/configfile.c @@ -1,12 +1,13 @@ /* configfile.c: parsing of config files * - * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com> + * Copyright (C) 2008 Lars Hjemli * * Licensed under GNU General Public License v2 * (see COPYING for full license text) */ -#include <git-compat-util.h> +#include <ctype.h> +#include <stdio.h> #include "configfile.h" static int next_char(FILE *f) @@ -30,47 +31,45 @@ static void skip_line(FILE *f) ; } -static int read_config_line(FILE *f, struct strbuf *name, struct strbuf *value) +static int read_config_line(FILE *f, char *line, const char **value, int bufsize) { - int c = next_char(f); + int i = 0, isname = 0; - strbuf_reset(name); - strbuf_reset(value); - - /* Skip comments and preceding spaces. */ - for(;;) { - if (c == EOF) - return 0; - else if (c == '#' || c == ';') + *value = NULL; + while (i < bufsize - 1) { + int c = next_char(f); + if (!isname && (c == '#' || c == ';')) { skip_line(f); - else if (!isspace(c)) - break; - c = next_char(f); - } - - /* Read variable name. */ - while (c != '=') { - if (c == '\n' || c == EOF) - return 0; - strbuf_addch(name, c); - c = next_char(f); - } + continue; + } + if (!isname && isspace(c)) + continue; - /* Read variable value. */ - c = next_char(f); - while (c != '\n' && c != EOF) { - strbuf_addch(value, c); - c = next_char(f); + if (c == '=' && !*value) { + line[i] = 0; + *value = &line[i + 1]; + } else if (c == '\n' && !isname) { + i = 0; + continue; + } else if (c == '\n' || c == EOF) { + line[i] = 0; + break; + } else { + line[i] = c; + } + isname = 1; + i++; } - - return 1; + line[i + 1] = 0; + return i; } int parse_configfile(const char *filename, configfile_value_fn fn) { static int nesting; - struct strbuf name = STRBUF_INIT; - struct strbuf value = STRBUF_INIT; + int len; + char line[256]; + const char *value; FILE *f; /* cancel deeply nested include-commands */ @@ -79,12 +78,10 @@ int parse_configfile(const char *filename, configfile_value_fn fn) if (!(f = fopen(filename, "r"))) return -1; nesting++; - while (read_config_line(f, &name, &value)) - fn(name.buf, value.buf); + while ((len = read_config_line(f, line, &value, sizeof(line))) > 0) + fn(line, value); nesting--; fclose(f); - strbuf_release(&name); - strbuf_release(&value); return 0; } |