]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
get config key
authorBastian Dehn <hhaalo@arcor.de>
Sat, 2 Mar 2024 16:51:48 +0000 (17:51 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Sat, 2 Mar 2024 16:51:48 +0000 (17:51 +0100)
src/kds_s2000w_config.c
src/kds_s2000w_config.h
tests/kds_s2000w_read_config_tests.c

index 2a69dbf3595edfb1e68302aba1d3b4072e3a9752..0b1789f9f534a9a551f1679e9c97903766d70410 100644 (file)
@@ -1,6 +1,70 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.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 get_line(const char* config_stream, config_line* line)
+{
+       while(config_stream[line->length] != '\n') {
+               line->length++;
+       }
+       line->length++;
+
+       line->line = malloc(sizeof(char) * line->length);
+       memcpy(line->line, config_stream, line->length - 1);
+       line->line[line->length - 1] = '\0';
+}
+
+void get_key(const char* line, config_value* value)
+{
+       value->key_length = 0;
+       while(line[value->key_length] != '=') {
+               value->key_length++;
+       }
+
+       value->key = malloc(sizeof(char) * value->key_length);
+       memcpy(value->key, line, value->key_length - 1);
+       value->key[value->key_length - 1] = '\0';
+}
+
+void get_value(char* line, config_value* value)
+{
+       int i = 0;
+       while (line[i] != '=') {
+               i++;
+       }
+}
+
 void loadConfig(program_config* config, const char* config_stream)
 {
-       config->scanner_url = config_stream;
+       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;
+
+       get_line(config_stream, &line);
+       get_key(line.line, &value);
+
+       config->scanner_url = malloc(sizeof(char) * value.key_length);
+       memcpy(config->scanner_url, value.key, value.key_length);
+
+       free(line.line);
+       line.line = NULL;
+       line.length = 0;
 }
\ No newline at end of file
index a4aed8eeb1e5950be095a925c30d9a423766fe53..80bb490a6b9d17dd63b8b364156778577f2fefb1 100644 (file)
@@ -2,7 +2,7 @@
 #define KDS_S2000W_CONFIG_H
 
 typedef struct {
-       const char* scanner_url;
+       char* scanner_url;
 } program_config;
 
 void loadConfig(program_config* config, const char* config_stream);
index 14488bb3cb828ef259a5ba7057a9c3e43b68f926..75bdd837db3e0493a456c45fb2476c4a7da4a801 100644 (file)
@@ -3,7 +3,7 @@
 
 START_TEST(kds_s2000w_config_read_parameter)
 {
-       const char* input_stream = "scanner_url = http://scanner.example.com";
+       const char* input_stream = "scanner_url = http://scanner.example.com\n";
 
        program_config* config = malloc(sizeof(program_config));
        loadConfig(config, input_stream);