diff --git a/src/core/settings.h b/src/core/settings.h index 4170f8888..f8b110c94 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -3,6 +3,8 @@ #include #include #include +#include +#include class SettingsInterface { @@ -17,6 +19,11 @@ public: virtual void SetBoolValue(const char* section, const char* key, bool value) = 0; virtual void SetStringValue(const char* section, const char* key, const char* value) = 0; + virtual std::vector GetStringList(const char* section, const char* key) = 0; + virtual void SetStringList(const char* section, const char* key, const std::vector& items) = 0; + virtual bool RemoveFromStringList(const char* section, const char* key, const char* item) = 0; + virtual bool AddToStringList(const char* section, const char* key, const char* item) = 0; + virtual void DeleteValue(const char* section, const char* key) = 0; }; diff --git a/src/duckstation/sdl_settings_interface.cpp b/src/duckstation/sdl_settings_interface.cpp index fa09601db..5cbd8afca 100644 --- a/src/duckstation/sdl_settings_interface.cpp +++ b/src/duckstation/sdl_settings_interface.cpp @@ -1,8 +1,9 @@ #include "sdl_settings_interface.h" #include "YBaseLib/Log.h" +#include Log_SetChannel(SDLSettingsInterface); -SDLSettingsInterface::SDLSettingsInterface(const char* filename) : m_filename(filename), m_ini(true) +SDLSettingsInterface::SDLSettingsInterface(const char* filename) : m_filename(filename), m_ini(true, true) { SI_Error err = m_ini.LoadFile(filename); if (err != SI_OK) @@ -54,10 +55,51 @@ void SDLSettingsInterface::SetBoolValue(const char* section, const char* key, bo void SDLSettingsInterface::SetStringValue(const char* section, const char* key, const char* value) { - m_ini.SetValue(section, key, value); + m_ini.SetValue(section, key, value, nullptr, true); } void SDLSettingsInterface::DeleteValue(const char* section, const char* key) { - m_ini.DeleteValue(section, key, nullptr); + m_ini.Delete(section, key); +} + +std::vector SDLSettingsInterface::GetStringList(const char* section, const char* key) +{ + std::list entries; + if (!m_ini.GetAllValues(section, key, entries)) + return {}; + + std::vector ret; + ret.reserve(entries.size()); + std::transform(entries.begin(), entries.end(), std::back_inserter(ret), + [](const CSimpleIniA::Entry& it) { return std::string(it.pItem); }); + return ret; +} + +void SDLSettingsInterface::SetStringList(const char* section, const char* key, + const std::vector& items) +{ + m_ini.Delete(section, key); + + for (const std::string_view& sv : items) + m_ini.SetValue(section, key, std::string(sv).c_str(), nullptr, false); +} + +bool SDLSettingsInterface::RemoveFromStringList(const char* section, const char* key, const char* item) +{ + return m_ini.DeleteValue(section, key, item, true); +} + +bool SDLSettingsInterface::AddToStringList(const char* section, const char* key, const char* item) +{ + std::list entries; + if (m_ini.GetAllValues(section, key, entries) && + std::find_if(entries.begin(), entries.end(), + [item](const CSimpleIniA::Entry& e) { return (std::strcmp(e.pItem, item) == 0); }) != entries.end()) + { + return false; + } + + m_ini.SetValue(section, key, item, nullptr, false); + return true; } diff --git a/src/duckstation/sdl_settings_interface.h b/src/duckstation/sdl_settings_interface.h index 5612adbff..64159f918 100644 --- a/src/duckstation/sdl_settings_interface.h +++ b/src/duckstation/sdl_settings_interface.h @@ -20,6 +20,11 @@ public: void SetStringValue(const char* section, const char* key, const char* value) override; void DeleteValue(const char* section, const char* key) override; + std::vector GetStringList(const char* section, const char* key) override; + void SetStringList(const char* section, const char* key, const std::vector& items) override; + bool RemoveFromStringList(const char* section, const char* key, const char* item) override; + bool AddToStringList(const char* section, const char* key, const char* item) override; + private: std::string m_filename; CSimpleIniA m_ini;