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

index 0022629acab6aed1e12e38774c368353506d388d..7f0796538393792ae12b6cefb069e8a6ecedebfe 100644 (file)
@@ -148,20 +148,24 @@ void get_value(config_line* line, config_value* value)
        value->value[value->value_length] = '\0';
 }
 
-void read_config_file(const char* filename, char* config_stream)
+void read_config_file(const char* filename, char** config_stream)
 {
+       free(*config_stream);
+       *config_stream = NULL;
+
        FILE* config_file = fopen(filename, "r");
 
        int char_count = 0;
        char* buf = malloc(sizeof(char));
-       while(fread(buf, 1, 1, config_file) != 0) {
+       while(fread(buf, sizeof(char), 1, config_file)) {
                char_count++;
        }
-       rewind(config_file);
+       fseek(config_file, 0L, SEEK_SET);
 
-       config_stream = malloc(sizeof(char) * char_count);
-       fread(config_stream, sizeof(char), char_count, config_file);
+       *config_stream = malloc(sizeof(char) * char_count);
+       fread(*config_stream, char_count, 1, config_file);
        fclose(config_file);
+       config_file = NULL;
 }
 
 void load_config(program_config* config, const char* config_stream)
index 25166ee561e54d7179a85460df084dbd3818e0d6..94ad4c420240221733f68b56d80f08f8008d4e20 100644 (file)
@@ -6,6 +6,6 @@ typedef struct {
        char* username;
 } program_config;
 
-void read_config_file(const char* filename, 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 a9e103a359499b33a3329e6c1c156681571f6548..964292bdce7dd1e01c55867ac3d324dc10d9f59a 100644 (file)
@@ -7,10 +7,21 @@ int main(int argc, char** argv)
        program_config* config = malloc(sizeof(program_config));
 
        char* config_stream = NULL;
-       read_config_file(argv[1], config_stream);
+       read_config_file(argv[1], &config_stream);
+       printf("config:\n%s\n", config_stream);
+       load_config(config, config_stream);
+
+       printf("scanner_url: %s", config->scanner_url);
+       printf("username %s", config->username);
 
        free(config_stream);
        config_stream = NULL;
+       free(config->scanner_url);
+       config->scanner_url = NULL;
+       free(config->username);
+       config->username = NULL;
+       free(config);
+       config = NULL;
 
        return 0;
 }
\ No newline at end of file