10#include "misc/Error.h"
13#include "toml11/single_include/toml.hpp"
19 enum class ConfigCategory {
107 [[nodiscard]]
static std::optional<Error>
108 removeFile(ConfigCategory category, std::string_view sFileName);
118 static void removeFile(std::filesystem::path pathToConfigFile);
136 [[nodiscard]] std::optional<Error>
loadFile(ConfigCategory category, std::string_view sFileName);
154 [[nodiscard]] std::optional<Error>
loadFile(std::filesystem::path pathToConfigFile);
195 template <
typename T>
196 T
getValue(std::string_view sSection, std::string_view sKey, T defaultValue)
const;
207 template <
typename T>
208 std::optional<T>
getValueOrFail(std::string_view sSection, std::string_view sKey)
const;
276 template <
typename T>
278 setValue(std::string_view sSection, std::string_view sKey, T value, std::string_view sComment =
"");
304 [[nodiscard]] std::optional<Error>
saveFile(ConfigCategory category, std::string_view sFileName);
321 [[nodiscard]] std::optional<Error>
322 saveFile(std::filesystem::path pathToConfigFile,
bool bEnableBackup);
344 static std::variant<std::filesystem::path, Error>
356 const std::set<std::string>& usedFileNames,
const std::string& sFileNamePrefix =
"");
368 template <
typename T>
370 if (sSection.empty()) {
371 return toml::find_or(
tomlData, sKey.data(), defaultValue);
373 return toml::find_or(
tomlData, sSection.data(), sKey.data(), defaultValue);
377 template <
typename T>
379 std::string_view sSection, std::string_view sKey, T value, std::string_view sComment) {
380 if (sSection.empty()) {
381 if (sComment.empty()) {
382 tomlData[sKey.data()] = toml::value(value);
384 tomlData[sKey.data()] = toml::value(value, {sComment.data()});
387 if (sComment.empty()) {
388 tomlData[sSection.data()][sKey.data()] = toml::value(value);
390 tomlData[sSection.data()][sKey.data()] = toml::value(value, {sComment.data()});
395 template <
typename T>
398 if (sSection.empty()) {
399 return toml::find<T>(
tomlData, sKey.data());
401 return toml::find<T>(
tomlData, sSection.data(), sKey.data());
Definition: ConfigManager.h:27
static std::set< std::string > getAllFileNames(ConfigCategory category)
Definition: ConfigManager.cpp:15
static std::string getBackupFileExtension()
Definition: ConfigManager.cpp:13
static std::variant< std::filesystem::path, Error > constructFilePath(ConfigCategory category, std::string_view sFileName)
Definition: ConfigManager.cpp:283
std::filesystem::path getFilePath() const
Definition: ConfigManager.cpp:280
std::optional< Error > loadFile(ConfigCategory category, std::string_view sFileName)
Definition: ConfigManager.cpp:116
static std::filesystem::path getCategoryDirectory(ConfigCategory category)
Definition: ConfigManager.cpp:56
static std::string getFreeProgressProfileName()
Definition: ConfigManager.cpp:51
toml::value tomlData
Definition: ConfigManager.h:359
std::filesystem::path filePath
Definition: ConfigManager.h:362
static std::string getConfigFormatExtension()
Definition: ConfigManager.cpp:11
static const char * sBackupFileExtension
Definition: ConfigManager.h:365
void setValue(std::string_view sSection, std::string_view sKey, T value, std::string_view sComment="")
Definition: ConfigManager.h:378
std::vector< std::string > getAllSections()
Definition: ConfigManager.cpp:163
std::optional< Error > saveFile(ConfigCategory category, std::string_view sFileName)
Definition: ConfigManager.cpp:199
T getValue(std::string_view sSection, std::string_view sKey, T defaultValue) const
Definition: ConfigManager.h:369
static std::string generateFreeFileName(const std::set< std::string > &usedFileNames, const std::string &sFileNamePrefix="")
Definition: ConfigManager.cpp:318
static std::optional< Error > removeFile(ConfigCategory category, std::string_view sFileName)
Definition: ConfigManager.cpp:75
std::optional< T > getValueOrFail(std::string_view sSection, std::string_view sKey) const
Definition: ConfigManager.h:396
std::variant< std::vector< std::string >, Error > getAllKeysOfSection(std::string_view sSection) const
Definition: ConfigManager.cpp:177