Skip to content

Commit

Permalink
Regenerate json settings on error (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
isivisi committed Apr 21, 2024
1 parent 596104b commit 75e5f70
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
9 changes: 8 additions & 1 deletion .vscode/settings.json
Expand Up @@ -97,6 +97,13 @@
"stdfloat": "cpp",
"typeindex": "cpp",
"valarray": "cpp",
"variant": "cpp"
"variant": "cpp",
"__bit_reference": "cpp",
"__config": "cpp",
"__debug": "cpp",
"__mutex_base": "cpp",
"__split_buffer": "cpp",
"__threading_support": "cpp",
"__verbose_abort": "cpp"
}
}
16 changes: 14 additions & 2 deletions src/settings.hpp
Expand Up @@ -29,9 +29,15 @@ struct UserSettings {
std::string settingFileName;
json_t* settingCache = nullptr;

std::function<json_t*(json_t*)> initFunction = nullptr;
const std::function<json_t*(json_t*)>* migrations = nullptr;

UserSettings(std::string fn, std::function<json_t*(json_t*)> initFunction, const std::function<json_t*(json_t*)>* migrations) {
settingFileName = fn;

this->initFunction = initFunction;
this->migrations = migrations;

if (initFunction) {
json_t* json = readSettings();
UserSettings::json_create_if_not_exists(json, "settingsVersion", json_integer(settingsVersion));
Expand Down Expand Up @@ -159,13 +165,19 @@ struct UserSettings {
if (!file) {
return json_object();
}

json_error_t error;
json_t *rootJ = json_loadf(file, 0, &error);

fclose(file);

if (!rootJ) {
return json_object();
}

settingCache = rootJ;
return rootJ;

} else return settingCache;
}

Expand Down

0 comments on commit 75e5f70

Please sign in to comment.