]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
change read config file with open memstream
authorBastian Dehn <hhaalo@arcor.de>
Wed, 1 Jan 2025 20:12:19 +0000 (21:12 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Wed, 1 Jan 2025 20:12:19 +0000 (21:12 +0100)
src/kds_s2000w_client.c
src/kds_s2000w_config.c
src/kds_s2000w_config.h

index 4afca9d5d7e9e747d2d2a5410b621d482ce07bfd..e045c5c835a1f7eb7f64bbfa65c89b934dcf1d6a 100644 (file)
@@ -41,15 +41,12 @@ void _kds_s2000w_client_read_error_status(CURL* curl, response* resp)
 
 void _kds_s2000w_client_load_config()
 {
-       char* config_stream = read_config_file(CONFIG_FILE);
+       const char* config_stream = read_config_file(CONFIG_FILE);
 
        if (config_stream == NULL)
                return;
 
        config = load_config(config_stream);
-
-       free(config_stream);
-       config_stream = NULL;
 }
 
 void _kds_s2000w_client_stream_to_response(FILE* stream, response* resp, size_t* size)
index 9408e4fd81af0afffa1fd23815572d83077b585b..bc01ccd5e15a2c199c77c5ee9996ef6c5fcef7a6 100644 (file)
@@ -13,33 +13,33 @@ void free_config(program_config* config)
        config = NULL;
 }
 
-char* read_config_file(const char* filename)
+const char* read_config_file(const char* filename)
 {
-       FILE* config_file = NULL;
-       char* buf = NULL;
-       char* config_stream = NULL;
-       int char_count = 0;
+       FILE* config_file = fopen(filename, "r");
+       char* buf = malloc(sizeof(char));
+       char** mem = malloc(sizeof(char*));
+       size_t* sizeloc = malloc(sizeof(size_t));
+       FILE* config_stream = open_memstream(mem, sizeloc);
+
+       while (fread(buf, sizeof(char), 1, config_file) > 0) {
+               fwrite(buf, sizeof(char), 1, config_stream);
+       }
 
-       config_file = fopen(filename, "r");
+       fclose(config_file);
+       config_file = NULL;
+       fclose(config_stream);
+       config_stream = NULL;
 
-       if (config_file == NULL)
-               return NULL;
+       const char* config_string = *mem;
 
-       buf = malloc(sizeof(char));
-       while(fread(buf, sizeof(char), 1, config_file)) {
-               char_count++;
-       }
        free(buf);
        buf = NULL;
-       fseek(config_file, 0L, SEEK_SET);
-
-       config_stream = malloc(sizeof(char) * char_count);
-       fread(config_stream, char_count, 1, config_file);
-       config_stream[char_count - 1 ] = '\0';
-       fclose(config_file);
-       config_file = NULL;
+       free(sizeloc);
+       sizeloc = NULL;
+       free(mem);
+       mem = NULL;
 
-       return config_stream;
+       return config_string;
 }
 
 program_config* load_config(const char* config_stream)
index a739b54e0490094e019a17e988cb8476303c515a..6c545281d2fe59136bc9168920ece7ccfa391e71 100644 (file)
@@ -9,6 +9,6 @@ typedef struct {
 } program_config;
 
 void free_config(program_config* config);
-char* read_config_file(const char* filename);
+const char* read_config_file(const char* filename);
 program_config* load_config(const char* config_stream);
 #endif
\ No newline at end of file