Skip to content

Commit

Permalink
Keeping the possible config filepaths locally
Browse files Browse the repository at this point in the history
  • Loading branch information
wberube committed Aug 16, 2024
1 parent c62408f commit 52056be
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/app_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 10 additions & 11 deletions src/hal/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/hal/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/hal/hisi/v1_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/hal/hisi/v2_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/hal/hisi/v3_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/hal/hisi/v4_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 52056be

Please sign in to comment.