aux/util: Move read_content to u_file

This commit is contained in:
Christoph Haag 2021-02-17 01:38:44 +01:00
parent ff16eab9df
commit aa9298f70d
3 changed files with 28 additions and 26 deletions

View file

@ -104,4 +104,29 @@ u_file_open_file_in_config_dir(const char *filename, const char *mode)
return fopen(file_str, mode);
}
char *
u_file_read_content(FILE *file)
{
// Go to the end of the file.
fseek(file, 0L, SEEK_END);
size_t file_size = ftell(file);
// Return back to the start of the file.
fseek(file, 0L, SEEK_SET);
char *buffer = (char *)calloc(file_size + 1, sizeof(char));
if (buffer == NULL) {
return NULL;
}
// Do the actual reading.
size_t ret = fread(buffer, sizeof(char), file_size, file);
if (ret != file_size) {
free(buffer);
return NULL;
}
return buffer;
}
#endif

View file

@ -28,6 +28,8 @@ u_file_get_path_in_config_dir(const char *suffix, char *out_path, size_t out_pat
FILE *
u_file_open_file_in_config_dir(const char *filename, const char *mode);
char *
u_file_read_content(FILE *file);
#ifdef __cplusplus
}

View file

@ -21,31 +21,6 @@
DEBUG_GET_ONCE_OPTION(active_config, "P_OVERRIDE_ACTIVE_CONFIG", NULL)
char *
read_content(FILE *file)
{
// Go to the end of the file.
fseek(file, 0L, SEEK_END);
size_t file_size = ftell(file);
// Return back to the start of the file.
fseek(file, 0L, SEEK_SET);
char *buffer = (char *)calloc(file_size + 1, sizeof(char));
if (buffer == NULL) {
return NULL;
}
// Do the actual reading.
size_t ret = fread(buffer, sizeof(char), file_size, file);
if (ret != file_size) {
free(buffer);
return NULL;
}
return buffer;
}
void
p_json_open_or_create_main_file(struct prober *p)
{
@ -66,7 +41,7 @@ p_json_open_or_create_main_file(struct prober *p)
p->json.file_loaded = true;
char *str = read_content(file);
char *str = u_file_read_content(file);
fclose(file);
if (str == NULL) {
U_LOG_E("Could not read the contents of '%s'!", tmp);