]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
migrate to confuse config read
authorBastian Dehn <hhaalo@arcor.de>
Sun, 3 Mar 2024 08:18:11 +0000 (09:18 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Sun, 3 Mar 2024 08:18:11 +0000 (09:18 +0100)
src/kds_s2000w_config.c
src/kds_s2000w_debug.h

index 7f0796538393792ae12b6cefb069e8a6ecedebfe..efaa1fecf2cda2d7feb71588cc2898f9bf95c317 100644 (file)
@@ -1,153 +1,8 @@
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <confuse.h>
 #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 trim_key_whitespace_before(config_value* value)
-{
-       int before = 0;
-       while(value->key[before] == ' ' && before < value->key_length) {
-               before++;
-       }
-
-       if (value->key_length <= before)
-               return;
-
-       int new_length = value->key_length - before;
-       char* new_value = malloc(sizeof(char) * new_length);
-       memcpy(new_value, value->key + before, new_length);
-
-       free(value->key);
-       value->key = NULL;
-       value->key = new_value;
-       value->key_length = new_length;
-}
-
-void trim_key_whitespace_after(config_value* value)
-{
-       int after = 0;
-       while(value->key[after] != ' ' && after < value->key_length) {
-               after++;
-       }
-
-       if (value->key_length <= after)
-               return;
-
-       int new_length = after + 1;
-       char* new_value = malloc(sizeof(char) * new_length);
-       memcpy(new_value, value->key, new_length);
-       new_value[new_length - 1] = '\0';
-
-       free(value->key);
-       value->key = NULL;
-       value->key = new_value;
-       value->key_length = new_length;
-}
-
-void trim_value_whitespace_before(config_value* value)
-{
-       int before = 0;
-       while(value->value[before] == ' ' && before < value->value_length) {
-               before++;
-       }
-
-       if (value->value_length <= before)
-               return;
-
-       int new_length = value->value_length - before;
-       char* new_value = malloc(sizeof(char) * new_length);
-       memcpy(new_value, value->value + before, new_length);
-
-       free(value->value);
-       value->value = NULL;
-       value->value = new_value;
-       value->value_length = new_length;
-}
-
-void trim_value_whitespace_after(config_value* value)
-{
-       int after = 0;
-       while(value->value[after] != ' ' && after < value->value_length) {
-               after++;
-       }
-
-       if (value->value_length <= after)
-               return;
-
-       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_config_value(config_value* value)
-{
-       trim_key_whitespace_before(value);
-       trim_key_whitespace_after(value);
-       trim_value_whitespace_before(value);
-       trim_value_whitespace_after(value);
-}
-
-void get_line(const char* config_stream, config_line* line, int* pos)
-{
-       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 + *pos, line->length - 1);
-       line->line[line->length - 1] = '\0';
-       *pos += line->length;
-}
-
-void get_key(config_line* line, config_value* value)
-{
-       int delimiter = 0;
-       while(line->line[delimiter] != '=') {
-               delimiter++;
-       }
-       value->key_length = delimiter + 1;
-
-       value->key = malloc(sizeof(char) * value->key_length);
-       memcpy(value->key, line->line, value->key_length);
-       value->key[value->key_length - 1] = '\0';
-}
-
-void get_value(config_line* line, config_value* value)
-{
-       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 read_config_file(const char* filename, char** config_stream)
 {
        free(*config_stream);
@@ -171,55 +26,32 @@ void read_config_file(const char* filename, char** config_stream)
 void load_config(program_config* config, const char* config_stream)
 {
        config->scanner_url = NULL;
-       if (strlen(config_stream) == 0) {
+       config->username = NULL;
+
+       if (strlen(config_stream) <= 0) {
                return;
        }
 
-       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;
-
-       int stream_pos = 0;
+       cfg_opt_t opts[] = {
+               CFG_STR("username", "", CFGF_NONE),
+               CFG_STR("scanner_url", "", CFGF_NONE),
+               CFG_END()
+       };
 
-       while(strlen(config_stream) > stream_pos) {
-               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;
+       cfg_t* cfg = cfg_init(opts, 0);
 
-               get_line(config_stream, &line, &stream_pos);
-               get_key(&line, &value);
-               get_value(&line, &value);
-               trim_config_value(&value);
+       int status = cfg_parse_buf(cfg, config_stream);
 
-               if (strcmp(value.key, "scanner_url") == 0) {
-                       config->scanner_url = malloc(sizeof(char) * value.value_length);
-                       memcpy(config->scanner_url, value.value, value.value_length);
-               }
+       char* value_str = cfg_getstr(cfg, "scanner_url");
+       size_t str_length = strlen(value_str) + 1;
+       config->scanner_url = malloc(sizeof(char) * str_length);
+       memcpy(config->scanner_url, value_str, str_length);
 
-               if (strcmp(value.key, "username") == 0) {
-                       config->username = malloc(sizeof(char) * value.value_length);
-                       memcpy(config->username, value.value, value.value_length);
-               }
-       }
+       value_str = cfg_getstr(cfg, "username");
+       str_length = strlen(value_str) + 1;
+       config->username = malloc(sizeof(char) * str_length);
+       memcpy(config->username, value_str, str_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;
+       cfg_free(cfg);
+       cfg = NULL;
 }
\ No newline at end of file
index f535ead22874f789fbe9d6e64b56309ce14a13fa..03b00a69b549028a788f7b1a5d8f04130b5940d2 100644 (file)
@@ -9,7 +9,7 @@
 #define DEBUG 5
 #define ALL 6
 
-#define LOGLEVEL INFO
+#define LOGLEVEL DEBUG
 
 void debug_printf(int level, const char* message);
 void debug_printf_int(int level, const char* message, int value);