From: Bastian Dehn Date: Sat, 2 Mar 2024 19:57:12 +0000 (+0100) Subject: read multiple lines X-Git-Tag: v1.0.0^2~144^2~7 X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=3ed19fdccead55f8fd962f98e6eef0c12b44b50e;p=sane-kds-s2000w-net.git read multiple lines --- diff --git a/src/kds_s2000w_config.c b/src/kds_s2000w_config.c index b2c68a6..4323737 100644 --- a/src/kds_s2000w_config.c +++ b/src/kds_s2000w_config.c @@ -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;