MCPcopy Create free account
hub / github.com/OpenKneeboard/OpenKneeboard / MaybeSaveJSON

Function MaybeSaveJSON

src/app/app-common/Settings.cpp:51–86  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

49
50template <class T>
51static void MaybeSaveJSON(
52 const T& parentValue,
53 const T& value,
54 const std::filesystem::path& path) {
55 const auto fullPath
56 = path.is_absolute() ? path : Filesystem::GetSettingsDirectory() / path;
57
58 nlohmann::json j;
59 // If a profile already modified a setting, keep that setting even if it
60 // matches the parent now
61 if (std::filesystem::exists(fullPath)) {
62 std::ifstream f(fullPath);
63 try {
64 f >> j;
65 } catch (const nlohmann::json::exception& e) {
66 dprint(
67 "Error reading JSON from file '{}': {}", fullPath.string(), e.what());
68 }
69 }
70
71 const auto parentPath = fullPath.parent_path();
72 if (!std::filesystem::exists(parentPath)) {
73 std::filesystem::create_directories(parentPath);
74 }
75
76 to_json_with_default(j, parentValue, value);
77 if (j.is_object() && j.size() > 0) {
78 std::ofstream f(fullPath);
79 f << std::setw(2) << j << std::endl;
80 return;
81 }
82
83 if (std::filesystem::exists(fullPath)) {
84 std::filesystem::remove(fullPath);
85 }
86}
87
88/** Used for GamesList and TabsList, where we don't want to merge configs -
89 * either inherit, or overwrite */

Callers

nothing calls this directly

Calls 2

GetSettingsDirectoryFunction · 0.85
to_json_with_defaultFunction · 0.85

Tested by

no test coverage detected