diff --git a/src/app_config.c b/src/app_config.c index 773686b..0755404 100644 --- a/src/app_config.c +++ b/src/app_config.c @@ -174,8 +174,9 @@ enum ConfigError parse_app_config(void) { struct IniConfig ini; memset(&ini, 0, sizeof(struct IniConfig)); - if (!open_config(&ini, "./divinus.yaml") && - !open_config(&ini, "/etc/divinus.yaml")) { + FILE *file; + open_app_config(&file, "r"); + if (!open_config(&ini, &file)) { printf("Can't find config divinus.yaml in:\n" " ./divinus.yaml\n /etc/divinus.yaml\n"); return -1; diff --git a/src/hal/config.c b/src/hal/config.c index 416bb55..01bc801 100644 --- a/src/hal/config.c +++ b/src/hal/config.c @@ -276,31 +276,30 @@ enum ConfigError parse_uint32( return CONFIG_OK; } -bool open_config(struct IniConfig *ini, const char *path) { - FILE *file = fopen(path, "rb"); - if (!file) +bool open_config(struct IniConfig *ini, FILE **file) { + if (!*file) return false; - fseek(file, 0, SEEK_END); - size_t length = ftell(file); - fseek(file, 0, SEEK_SET); + fseek(*file, 0, SEEK_END); + size_t length = ftell(*file); + fseek(*file, 0, SEEK_SET); ini->str = malloc(length + 1); if (!ini->str) { printf("Can't allocate buf in parse_sensor_config\n"); - fclose(file); + fclose(*file); return false; } - size_t n = fread(ini->str, 1, length, file); + size_t n = fread(ini->str, 1, length, *file); if (n != length) { - printf("Can't read all file %s\n", path); - fclose(file); + printf("Can't read all file\n"); + fclose(*file); free(ini->str); return false; } - fclose(file); + fclose(*file); ini->str[length] = 0; return true; diff --git a/src/hal/config.h b/src/hal/config.h index 5f2f863..8d201ac 100644 --- a/src/hal/config.h +++ b/src/hal/config.h @@ -34,7 +34,7 @@ enum ConfigError { CONFIG_SENSOR_NOT_FOUND, }; -bool open_config(struct IniConfig *ini, const char *path); +bool open_config(struct IniConfig *ini, FILE **file); enum ConfigError find_sections(struct IniConfig *ini); enum ConfigError section_pos( struct IniConfig *ini, const char *section, int *start_pos, int *end_pos); diff --git a/src/hal/hisi/v1_config.h b/src/hal/hisi/v1_config.h index 518e295..92342eb 100644 --- a/src/hal/hisi/v1_config.h +++ b/src/hal/hisi/v1_config.h @@ -449,7 +449,8 @@ static enum ConfigError v1_parse_sensor_config(char *path, v1_config_impl *confi enum ConfigError err; // load config file to string - if (!open_config(&ini, path)) + FILE *file = fopen(path, "r"); + if (!open_config(&ini, &file)) return (enum ConfigError)-1; find_sections(&ini); diff --git a/src/hal/hisi/v2_config.h b/src/hal/hisi/v2_config.h index 2c21003..5b046d1 100644 --- a/src/hal/hisi/v2_config.h +++ b/src/hal/hisi/v2_config.h @@ -447,7 +447,8 @@ static enum ConfigError v2_parse_sensor_config(char *path, v2_config_impl *confi enum ConfigError err; // load config file to string - if (!open_config(&ini, path)) + FILE *file = fopen(path, "r"); + if (!open_config(&ini, &file)) return (enum ConfigError)-1; find_sections(&ini); diff --git a/src/hal/hisi/v3_config.h b/src/hal/hisi/v3_config.h index 4da5cf0..6364987 100644 --- a/src/hal/hisi/v3_config.h +++ b/src/hal/hisi/v3_config.h @@ -445,7 +445,8 @@ static enum ConfigError v3_parse_sensor_config(char *path, v3_config_impl *confi enum ConfigError err; // load config file to string - if (!open_config(&ini, path)) + FILE *file = fopen(path, "r"); + if (!open_config(&ini, &file)) return (enum ConfigError)-1; find_sections(&ini); diff --git a/src/hal/hisi/v4_config.h b/src/hal/hisi/v4_config.h index 608e646..49a9d25 100644 --- a/src/hal/hisi/v4_config.h +++ b/src/hal/hisi/v4_config.h @@ -331,7 +331,8 @@ static enum ConfigError v4_parse_sensor_config(char *path, v4_config_impl *confi enum ConfigError err; // load config file to string - if (!open_config(&ini, path)) + FILE *file = fopen(path, "r"); + if (!open_config(&ini, &file)) return (enum ConfigError)-1; find_sections(&ini);