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)
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)
} 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