]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
read multiple lines
authorBastian Dehn <hhaalo@arcor.de>
Sat, 2 Mar 2024 19:57:12 +0000 (20:57 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Sat, 2 Mar 2024 19:57:12 +0000 (20:57 +0100)
src/kds_s2000w_config.c

index b2c68a6f00f730ae4cde1e8c9eea934246fe94fe..43237378a97b5388ab1316001cf6b7427f0a08a5 100644 (file)
@@ -37,7 +37,6 @@ void trim_key_whitespace_before(config_value* value)
 
 void trim_key_whitespace_after(config_value* value)
 {
-       printf("key %s0\n", value->key);
        int after = 0;
        while(value->key[after] != ' ' && after < value->key_length) {
                after++;
@@ -106,16 +105,21 @@ void trim_config_value(config_value* value)
        trim_value_whitespace_after(value);
 }
 
-void get_line(const char* config_stream, config_line* line)
+void get_line(const char* config_stream, config_line* line, int* pos)
 {
-       while(config_stream[line->length] != '\n') {
+       free(line->line);
+       line->line = NULL;
+
+       line->length = 0;
+       while(config_stream[*pos + line->length] != '\n') {
                line->length++;
        }
        line->length++;
 
        line->line = malloc(sizeof(char) * line->length);
-       memcpy(line->line, config_stream, line->length - 1);
+       memcpy(line->line, config_stream + *pos, line->length - 1);
        line->line[line->length - 1] = '\0';
+       *pos += line->length;
 }
 
 void get_key(config_line* line, config_value* value)
@@ -160,7 +164,9 @@ void loadConfig(program_config* config, const char* config_stream)
        value.value_length = 0;
        value.value = NULL;
 
-       get_line(config_stream, &line);
+       int stream_pos = 0;
+
+       get_line(config_stream, &line, &stream_pos);
        get_key(&line, &value);
        get_value(&line, &value);
        trim_config_value(&value);
@@ -170,6 +176,26 @@ void loadConfig(program_config* config, const char* config_stream)
                memcpy(config->scanner_url, value.value, value.value_length);
        }
 
+       free(line.line);
+       line.line = NULL;
+       line.length = 0;
+       free(value.key);
+       value.key = NULL;
+       value.key_length = 0;
+       free(value.value);
+       value.value = NULL;
+       value.value_length = 0;
+
+       get_line(config_stream, &line, &stream_pos);
+       get_key(&line, &value);
+       get_value(&line, &value);
+       trim_config_value(&value);
+
+       if (strcmp(value.key, "username") == 0) {
+               config->username = malloc(sizeof(char) * value.value_length);
+               memcpy(config->username, value.value, value.value_length);
+       }
+
        free(line.line);
        line.line = NULL;
        line.length = 0;