diff options
author | David Sommerseth <dazo@users.sourceforge.net> | 2011-07-23 22:46:37 +0200 |
---|---|---|
committer | David Sommerseth <dazo@users.sourceforge.net> | 2011-07-25 14:01:30 +0200 |
commit | b87722e6ed97aaea7ded638ad585491081faaa8b (patch) | |
tree | 960a25c66bb06d9dda331c5cd58fe01ff13347a2 /eurephiadm | |
parent | ebf4e80250b525e173397fbe5c0018d922c5d42a (diff) | |
download | eurephia-b87722e6ed97aaea7ded638ad585491081faaa8b.tar.gz eurephia-b87722e6ed97aaea7ded638ad585491081faaa8b.tar.xz eurephia-b87722e6ed97aaea7ded638ad585491081faaa8b.zip |
Moved generic parts of the config file parser to the common library
Modified the eurephiadm client_config section to make use of the common
version as well.
Signed-off-by: David Sommerseth <dazo@users.sourceforge.net>
Diffstat (limited to 'eurephiadm')
-rw-r--r-- | eurephiadm/client_config.c | 97 |
1 files changed, 6 insertions, 91 deletions
diff --git a/eurephiadm/client_config.c b/eurephiadm/client_config.c index 0f5a01f..b01903c 100644 --- a/eurephiadm/client_config.c +++ b/eurephiadm/client_config.c @@ -1,6 +1,6 @@ /* client_config.c -- Handles reading and parsing of config files * - * GPLv2 only - Copyright (C) 2008 - 2010 + * GPLv2 only - Copyright (C) 2008 - 2011 * David Sommerseth <dazo@users.sourceforge.net> * * This program is free software; you can redistribute it and/or @@ -24,7 +24,7 @@ * @author David Sommerseth <dazo@users.sourceforge.net> * @date 2008-12-01 * - * @brief Config file parser + * @brief Config file parser for eurephiadm * */ @@ -36,6 +36,7 @@ #include <eurephia_nullsafe.h> #include <eurephia_values.h> +#include <eurephia_cfgfile.h> /** * Retrieve a the full path of a file name. Will try to look for the file in different places, like @@ -100,71 +101,6 @@ char *get_config_filename(const char *env, const char *file) { /** - * Parse one single configuration line into a eurephiaVALUES key/value pair. It will also ignore - * comment lines, and also remove the comments on the line of the configuration line so that only - * the key/value information is extracted. - * - * @param line Input configuration line - * - * @return eurephiaVALUES pointer containing the parsed result. On error or if no valid config - * line was found, NULL is returned. - */ -eurephiaVALUES *parse_config_line(const char *line) { - char *cp = NULL, *key = NULL, *val = NULL, *ptr = NULL;; - eurephiaVALUES *ret = NULL; - - if( *line == '#' ) { - return NULL; - } - - cp = strdup(line); - key = cp; - val = strpbrk(cp, "="); - if( val == NULL ) { - free_nullsafe(NULL, cp); - return NULL; - } - *val = '\0'; val++; - - // Discard comments at the end of a line - if( (ptr = strpbrk(val, "#")) != NULL ) { - *ptr = '\0'; - } - - // Left trim - while( ((*key == 0x20) || (*key == 0x0A) || (*key == 0x0D)) ) { - key++; - } - while( ((*val == 0x20) || (*val == 0x0A) || (*val == 0x0D)) ) { - val++; - } - - // Right trim - ptr = key + strlen_nullsafe(key) - 1; - while( ((*ptr == 0x20) || (*ptr == 0x0A) || (*ptr == 0x0D)) && (ptr > key) ) { - ptr--; - } - ptr++; - *ptr = '\0'; - - ptr = val + strlen_nullsafe(val) - 1; - while( ((*ptr == 0x20) || (*ptr == 0x0A) || (*ptr == 0x0D)) && (ptr > val) ) { - ptr--; - } - ptr++; - *ptr = '\0'; - - // Put key/value into a eurephiaVALUES struct and return it - ret = eCreate_value_space(NULL, 20); - ret->key = strdup(key); - ret->val = strdup(val); - - free_nullsafe(NULL, cp); - return ret; -} - - -/** * Parses a complete config file and puts it into an eurephiaVALUES key/value stack * * @param env Environment table, used for locating the config file @@ -175,10 +111,7 @@ eurephiaVALUES *parse_config_line(const char *line) { */ eurephiaVALUES *ReadConfig(const char *env, const char *cfgname) { char *fname = NULL; - FILE *fp = NULL; - char *buf = NULL; eurephiaVALUES *cfg = NULL; - struct stat fi; fname = get_config_filename(env, cfgname); if( fname == NULL ) { @@ -186,27 +119,9 @@ eurephiaVALUES *ReadConfig(const char *env, const char *cfgname) { return NULL; } - if( stat(fname, &fi) == -1 ) { - fprintf(stderr, "Could not open the config file: %s\n", fname); - return NULL; - } - - if( (fp = fopen(fname, "r")) == NULL ) { - fprintf(stderr, "Could not open the config file: %s\n", fname); - return NULL; + cfg = ecfg_ReadConfig(NULL, fname); + if( cfg == NULL ) { + fprintf(stderr, "Failed to parse the config file (%s)\n", cfgname); } - - buf = (char *) malloc_nullsafe(NULL, fi.st_size+2); - - cfg = eCreate_value_space(NULL, 20); - while( fgets(buf, fi.st_size, fp) != NULL ) { - eurephiaVALUES *prm = parse_config_line(buf); - if( prm != NULL ) { - eAdd_valuestruct(NULL, cfg, prm); - } - }; - free_nullsafe(NULL, buf); - fclose(fp); fp = NULL; - return cfg; } |