]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
read config file into memory
authorBastian Dehn <hhaalo@arcor.de>
Sat, 2 Mar 2024 20:28:16 +0000 (21:28 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Sat, 2 Mar 2024 20:28:16 +0000 (21:28 +0100)
src/kds_s2000w_config.c
src/kds_s2000w_config.h
tests/kds_s2000w_read_config_tests.c
tools/read_config.c

index 373f916dbd7bffc988b9519d797ccf764f9f3d0d..0022629acab6aed1e12e38774c368353506d388d 100644 (file)
@@ -148,7 +148,23 @@ void get_value(config_line* line, config_value* value)
        value->value[value->value_length] = '\0';
 }
 
-void loadConfig(program_config* config, const char* config_stream)
+void read_config_file(const char* filename, char* config_stream)
+{
+       FILE* config_file = fopen(filename, "r");
+
+       int char_count = 0;
+       char* buf = malloc(sizeof(char));
+       while(fread(buf, 1, 1, config_file) != 0) {
+               char_count++;
+       }
+       rewind(config_file);
+
+       config_stream = malloc(sizeof(char) * char_count);
+       fread(config_stream, sizeof(char), char_count, config_file);
+       fclose(config_file);
+}
+
+void load_config(program_config* config, const char* config_stream)
 {
        config->scanner_url = NULL;
        if (strlen(config_stream) == 0) {
@@ -166,7 +182,6 @@ void loadConfig(program_config* config, const char* config_stream)
 
        int stream_pos = 0;
 
-
        while(strlen(config_stream) > stream_pos) {
                free(line.line);
                line.line = NULL;
index 518797973224cf60a2488b46204b511cbb1678fc..25166ee561e54d7179a85460df084dbd3818e0d6 100644 (file)
@@ -6,5 +6,6 @@ typedef struct {
        char* username;
 } program_config;
 
-void loadConfig(program_config* config, const char* config_stream);
+void read_config_file(const char* filename, char* config_stream);
+void load_config(program_config* config, const char* config_stream);
 #endif
\ No newline at end of file
index 304377faa5037e147e217c1978d8c5df1089d657..16928cd8c80e317f66337022b0c08db8f6bd46a4 100644 (file)
@@ -6,7 +6,7 @@ START_TEST(kds_s2000w_config_read_parameter)
        const char* input_stream = "   scanner_url   =   http://scanner.example.com    \n";
 
        program_config* config = malloc(sizeof(program_config));
-       loadConfig(config, input_stream);
+       load_config(config, input_stream);
 
        ck_assert_str_eq(config->scanner_url, "http://scanner.example.com");
 
@@ -22,7 +22,7 @@ START_TEST(kds_s2000w_config_read_parameters_without_trim)
        const char* input_stream = "scanner_url=http://scanner.example.com\n";
 
        program_config* config = malloc(sizeof(program_config));
-       loadConfig(config, input_stream);
+       load_config(config, input_stream);
 
        ck_assert_str_eq(config->scanner_url, "http://scanner.example.com");
 
@@ -38,7 +38,7 @@ START_TEST(kds_s2000w_config_multiple_parameters)
        const char* input_stream = "scanner_url=http://scanner.example.com\nusername=Mustermann\n";
 
        program_config* config = malloc(sizeof(program_config));
-       loadConfig(config, input_stream);
+       load_config(config, input_stream);
 
        ck_assert_str_eq(config->scanner_url, "http://scanner.example.com");
        ck_assert_str_eq(config->username, "Mustermann");
@@ -57,7 +57,7 @@ START_TEST(kds_s2000w_config_read_empty_config)
        const char* input_stream = "";
 
        program_config* config = malloc(sizeof(program_config));
-       loadConfig(config, input_stream);
+       load_config(config, input_stream);
 
        ck_assert_ptr_null(config->scanner_url);
 
index 7946eb01a3e10dc4289164c9b5e3e46933085370..a9e103a359499b33a3329e6c1c156681571f6548 100644 (file)
@@ -2,8 +2,15 @@
 #include <stdlib.h>
 #include "../src/kds_s2000w_config.h"
 
-int main()
+int main(int argc, char** argv)
 {
        program_config* config = malloc(sizeof(program_config));
+
+       char* config_stream = NULL;
+       read_config_file(argv[1], config_stream);
+
+       free(config_stream);
+       config_stream = NULL;
+
        return 0;
 }
\ No newline at end of file