From 3fc8789f47df05675b1746e5df3703a23c1147af Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Sat, 2 Mar 2024 21:55:21 +0100 Subject: [PATCH] add read config file --- src/kds_s2000w_config.c | 14 +++++++++----- src/kds_s2000w_config.h | 2 +- tools/read_config.c | 13 ++++++++++++- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/kds_s2000w_config.c b/src/kds_s2000w_config.c index 0022629..7f07965 100644 --- a/src/kds_s2000w_config.c +++ b/src/kds_s2000w_config.c @@ -148,20 +148,24 @@ void get_value(config_line* line, config_value* value) value->value[value->value_length] = '\0'; } -void read_config_file(const char* filename, char* config_stream) +void read_config_file(const char* filename, char** config_stream) { + free(*config_stream); + *config_stream = NULL; + FILE* config_file = fopen(filename, "r"); int char_count = 0; char* buf = malloc(sizeof(char)); - while(fread(buf, 1, 1, config_file) != 0) { + while(fread(buf, sizeof(char), 1, config_file)) { char_count++; } - rewind(config_file); + fseek(config_file, 0L, SEEK_SET); - config_stream = malloc(sizeof(char) * char_count); - fread(config_stream, sizeof(char), char_count, config_file); + *config_stream = malloc(sizeof(char) * char_count); + fread(*config_stream, char_count, 1, config_file); fclose(config_file); + config_file = NULL; } void load_config(program_config* config, const char* config_stream) diff --git a/src/kds_s2000w_config.h b/src/kds_s2000w_config.h index 25166ee..94ad4c4 100644 --- a/src/kds_s2000w_config.h +++ b/src/kds_s2000w_config.h @@ -6,6 +6,6 @@ typedef struct { char* username; } program_config; -void read_config_file(const char* filename, char* config_stream); +void read_config_file(const char* filename, char** config_stream); void load_config(program_config* config, const char* config_stream); #endif \ No newline at end of file diff --git a/tools/read_config.c b/tools/read_config.c index a9e103a..964292b 100644 --- a/tools/read_config.c +++ b/tools/read_config.c @@ -7,10 +7,21 @@ int main(int argc, char** argv) program_config* config = malloc(sizeof(program_config)); char* config_stream = NULL; - read_config_file(argv[1], config_stream); + read_config_file(argv[1], &config_stream); + printf("config:\n%s\n", config_stream); + load_config(config, config_stream); + + printf("scanner_url: %s", config->scanner_url); + printf("username %s", config->username); free(config_stream); config_stream = NULL; + free(config->scanner_url); + config->scanner_url = NULL; + free(config->username); + config->username = NULL; + free(config); + config = NULL; return 0; } \ No newline at end of file -- 2.39.5