From 31d434f94c2adc6834ac897e2b8cffc868c0d258 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Sat, 2 Mar 2024 17:51:48 +0100 Subject: [PATCH] get config key --- src/kds_s2000w_config.c | 66 +++++++++++++++++++++++++++- src/kds_s2000w_config.h | 2 +- tests/kds_s2000w_read_config_tests.c | 2 +- 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/src/kds_s2000w_config.c b/src/kds_s2000w_config.c index 2a69dbf..0b1789f 100644 --- a/src/kds_s2000w_config.c +++ b/src/kds_s2000w_config.c @@ -1,6 +1,70 @@ +#include +#include +#include #include "kds_s2000w_config.h" +typedef struct { + int length; + char* line; +} config_line; + +typedef struct { + int key_length; + int value_length; + char* key; + char* value; +} config_value; + +void get_line(const char* config_stream, config_line* line) +{ + while(config_stream[line->length] != '\n') { + line->length++; + } + line->length++; + + line->line = malloc(sizeof(char) * line->length); + memcpy(line->line, config_stream, line->length - 1); + line->line[line->length - 1] = '\0'; +} + +void get_key(const char* line, config_value* value) +{ + value->key_length = 0; + while(line[value->key_length] != '=') { + value->key_length++; + } + + value->key = malloc(sizeof(char) * value->key_length); + memcpy(value->key, line, value->key_length - 1); + value->key[value->key_length - 1] = '\0'; +} + +void get_value(char* line, config_value* value) +{ + int i = 0; + while (line[i] != '=') { + i++; + } +} + void loadConfig(program_config* config, const char* config_stream) { - config->scanner_url = config_stream; + config_line line; + line.length = 0; + line.line = NULL; + config_value value; + value.key_length = 0; + value.key = NULL; + value.value_length = 0; + value.value = NULL; + + get_line(config_stream, &line); + get_key(line.line, &value); + + config->scanner_url = malloc(sizeof(char) * value.key_length); + memcpy(config->scanner_url, value.key, value.key_length); + + free(line.line); + line.line = NULL; + line.length = 0; } \ No newline at end of file diff --git a/src/kds_s2000w_config.h b/src/kds_s2000w_config.h index a4aed8e..80bb490 100644 --- a/src/kds_s2000w_config.h +++ b/src/kds_s2000w_config.h @@ -2,7 +2,7 @@ #define KDS_S2000W_CONFIG_H typedef struct { - const char* scanner_url; + char* scanner_url; } program_config; void loadConfig(program_config* config, const char* config_stream); diff --git a/tests/kds_s2000w_read_config_tests.c b/tests/kds_s2000w_read_config_tests.c index 14488bb..75bdd83 100644 --- a/tests/kds_s2000w_read_config_tests.c +++ b/tests/kds_s2000w_read_config_tests.c @@ -3,7 +3,7 @@ START_TEST(kds_s2000w_config_read_parameter) { - const char* input_stream = "scanner_url = http://scanner.example.com"; + const char* input_stream = "scanner_url = http://scanner.example.com\n"; program_config* config = malloc(sizeof(program_config)); loadConfig(config, input_stream); -- 2.39.5