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

index d23ff291023605028aacd9cb501fd3c121395633..34ae85dda7f69f25244cbf10e90fca9b641145e3 100644 (file)
@@ -15,7 +15,7 @@ typedef struct {
        char* value;
 } config_value;
 
-void trim_whitespace_before(config_value* value)
+void trim_value_whitespace_before(config_value* value)
 {
        int before = 0;
        while(value->value[before] == ' ') {
@@ -33,6 +33,30 @@ void trim_whitespace_before(config_value* value)
        value->value_length = new_length;
 }
 
+void trim_value_whitespace_after(config_value* value)
+{
+       int after = 0;
+       while(value->value[after] != ' ') {
+               after++;
+       }
+
+       int new_length = after + 1;
+       char* new_value = malloc(sizeof(char) * new_length);
+       memcpy(new_value, value->value, new_length);
+       new_value[new_length - 1] = '\0';
+
+       free(value->value);
+       value->value = NULL;
+       value->value = new_value;
+       value->value_length = new_length;
+}
+
+void trim_value(config_value* value)
+{
+       trim_value_whitespace_before(value);
+       trim_value_whitespace_after(value);
+}
+
 void get_line(const char* config_stream, config_line* line)
 {
        while(config_stream[line->length] != '\n') {
@@ -85,7 +109,7 @@ void loadConfig(program_config* config, const char* config_stream)
        get_line(config_stream, &line);
        get_key(&line, &value);
        get_value(&line, &value);
-       trim_whitespace_before(&value);
+       trim_value(&value);
 
        config->scanner_url = malloc(sizeof(char) * value.value_length);
        memcpy(config->scanner_url, value.value, value.value_length);
index b0fd42009e72d1193383833cf83fd9de1ba24e5a..8786239ca1c0bda7b57aefbd9befb770577a3af9 100644 (file)
@@ -3,7 +3,7 @@
 
 START_TEST(kds_s2000w_config_read_parameter)
 {
-       const char* input_stream = "scanner_url = http://scanner.example.com\n";
+       const char* input_stream = "scanner_url = http://scanner.example.com    \n";
 
        program_config* config = malloc(sizeof(program_config));
        loadConfig(config, input_stream);