]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
get value without trim
authorBastian Dehn <hhaalo@arcor.de>
Sat, 2 Mar 2024 17:12:36 +0000 (18:12 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Sat, 2 Mar 2024 17:12:36 +0000 (18:12 +0100)
src/kds_s2000w_config.c
tests/kds_s2000w_read_config_tests.c

index 0b1789f9f534a9a551f1679e9c97903766d70410..c9951a5731474507db886b67f04acaca049173df 100644 (file)
@@ -27,24 +27,30 @@ void get_line(const char* config_stream, config_line* line)
        line->line[line->length - 1] = '\0';
 }
 
-void get_key(const char* line, config_value* value)
+void get_key(config_line* line, config_value* value)
 {
-       value->key_length = 0;
-       while(line[value->key_length] != '=') {
-               value->key_length++;
+       int delimiter = 0;
+       while(line->line[delimiter] != '=') {
+               delimiter++;
        }
+       value->key_length = delimiter;
 
        value->key = malloc(sizeof(char) * value->key_length);
-       memcpy(value->key, line, value->key_length - 1);
+       memcpy(value->key, line->line, value->key_length - 1);
        value->key[value->key_length - 1] = '\0';
 }
 
-void get_value(char* line, config_value* value)
+void get_value(config_line* line, config_value* value)
 {
-       int i = 0;
-       while (line[i] != '=') {
-               i++;
+       int delimiter = 0;
+       while(line->line[delimiter] != '=') {
+               delimiter++;
        }
+
+       value->value_length = line->length - delimiter;
+       value->value = malloc(sizeof(char) * value->value_length + 1);
+       memcpy(value->value, line->line + delimiter + 1, value->value_length);
+       value->value[value->value_length] = '\0';
 }
 
 void loadConfig(program_config* config, const char* config_stream)
@@ -59,12 +65,19 @@ void loadConfig(program_config* config, const char* config_stream)
        value.value = NULL;
 
        get_line(config_stream, &line);
-       get_key(line.line, &value);
+       get_key(&line, &value);
+       get_value(&line, &value);
 
        config->scanner_url = malloc(sizeof(char) * value.key_length);
-       memcpy(config->scanner_url, value.key, value.key_length);
+       memcpy(config->scanner_url, value.value, value.value_length);
 
        free(line.line);
        line.line = NULL;
        line.length = 0;
+       free(value.value);
+       value.value = NULL;
+       value.value_length = 0;
+       free(value.key);
+       value.key = NULL;
+       value.key_length = 0;
 }
\ No newline at end of file
index 75bdd837db3e0493a456c45fb2476c4a7da4a801..b0fd42009e72d1193383833cf83fd9de1ba24e5a 100644 (file)
@@ -10,6 +10,8 @@ START_TEST(kds_s2000w_config_read_parameter)
 
        ck_assert_str_eq(config->scanner_url, "http://scanner.example.com");
 
+       free(config->scanner_url);
+       config->scanner_url = NULL;
        free(config);
        config = NULL;
 }