From e3a794633b02411c3d3adc4443e98541f045f41a Mon Sep 17 00:00:00 2001 From: Stephen Gallagher Date: Mon, 28 Sep 2009 09:36:00 -0400 Subject: INI Add config_from_fd() to ini_config Patch adds ability to read configuration using already open file descriptor. Started by Steve G and refined a bit by me. --- common/ini/ini_config_ut.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) (limited to 'common/ini/ini_config_ut.c') diff --git a/common/ini/ini_config_ut.c b/common/ini/ini_config_ut.c index 6fbade6e2..52e89cb15 100644 --- a/common/ini/ini_config_ut.c +++ b/common/ini/ini_config_ut.c @@ -23,6 +23,7 @@ #include #include #include +#include #define TRACE_HOME #include "ini_config.h" #include "collection.h" @@ -133,6 +134,78 @@ int single_file(void) return 0; } +int single_fd(void) +{ + int error; + struct collection_item *ini_config = NULL; + struct collection_item *error_set = NULL; + struct collection_item *lines = NULL; + + int fd = open("./ini.conf", O_RDONLY); + if (fd < 0) { + error = errno; + printf("Attempt to read configuration returned error: %d\n", error); + return error; + } + + error = config_from_fd("test", fd, "./ini.conf", &ini_config, + INI_STOP_ON_NONE, &error_set); + if (error) { + printf("Attempt to read configuration returned error: %d\n",error); + return error; + } + + col_debug_collection(ini_config, COL_TRAVERSE_DEFAULT); + col_print_collection(ini_config); + col_print_collection(error_set); + + printf("\n\n----------------------\n"); + /* Output parsing errors (if any) */ + print_file_parsing_errors(stdout, error_set); + printf("----------------------\n\n\n"); + + + free_ini_config(ini_config); + free_ini_config_errors(error_set); + close(fd); + + ini_config = NULL; + error_set = NULL; + + printf("TEST WITH LINES\n"); + + fd = open("./ini.conf", O_RDONLY); + if (fd < 0) { + error = errno; + printf("Attempt to read configuration returned error: %d\n", error); + return error; + } + error = config_from_fd_with_lines("test", fd, + "./ini.conf", + &ini_config, + INI_STOP_ON_NONE, + &error_set, &lines); + if (error) { + printf("Attempt to read configuration returned error: %d\n",error); + return error; + } + + col_debug_collection(ini_config, COL_TRAVERSE_DEFAULT); + col_debug_collection(lines, COL_TRAVERSE_DEFAULT); + + printf("\n\n----------------------\n"); + /* Output parsing errors (if any) */ + print_file_parsing_errors(stdout, error_set); + printf("----------------------\n\n\n"); + + + free_ini_config(ini_config); + free_ini_config_errors(error_set); + free_ini_config_lines(lines); + + return 0; +} + int negative_test(void) { int error; @@ -828,6 +901,7 @@ int main(int argc, char *argv[]) if ((error = basic_test()) || (error = single_file()) || + (error = single_fd()) || (error = real_test(NULL)) || /* This should result in merged configuration */ (error = real_test("./ini.conf")) || -- cgit