SettingsInterface: Add support for string list settings
This commit is contained in:
parent
dbf651e493
commit
98214a9327
|
@ -3,6 +3,8 @@
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class SettingsInterface
|
class SettingsInterface
|
||||||
{
|
{
|
||||||
|
@ -17,6 +19,11 @@ public:
|
||||||
virtual void SetBoolValue(const char* section, const char* key, bool value) = 0;
|
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 void SetStringValue(const char* section, const char* key, const char* value) = 0;
|
||||||
|
|
||||||
|
virtual std::vector<std::string> GetStringList(const char* section, const char* key) = 0;
|
||||||
|
virtual void SetStringList(const char* section, const char* key, const std::vector<std::string_view>& 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;
|
virtual void DeleteValue(const char* section, const char* key) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
#include "sdl_settings_interface.h"
|
#include "sdl_settings_interface.h"
|
||||||
#include "YBaseLib/Log.h"
|
#include "YBaseLib/Log.h"
|
||||||
|
#include <algorithm>
|
||||||
Log_SetChannel(SDLSettingsInterface);
|
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);
|
SI_Error err = m_ini.LoadFile(filename);
|
||||||
if (err != SI_OK)
|
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)
|
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)
|
void SDLSettingsInterface::DeleteValue(const char* section, const char* key)
|
||||||
{
|
{
|
||||||
m_ini.DeleteValue(section, key, nullptr);
|
m_ini.Delete(section, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> SDLSettingsInterface::GetStringList(const char* section, const char* key)
|
||||||
|
{
|
||||||
|
std::list<CSimpleIniA::Entry> entries;
|
||||||
|
if (!m_ini.GetAllValues(section, key, entries))
|
||||||
|
return {};
|
||||||
|
|
||||||
|
std::vector<std::string> 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<std::string_view>& 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<CSimpleIniA::Entry> 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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,11 @@ public:
|
||||||
void SetStringValue(const char* section, const char* key, const char* value) override;
|
void SetStringValue(const char* section, const char* key, const char* value) override;
|
||||||
void DeleteValue(const char* section, const char* key) override;
|
void DeleteValue(const char* section, const char* key) override;
|
||||||
|
|
||||||
|
std::vector<std::string> GetStringList(const char* section, const char* key) override;
|
||||||
|
void SetStringList(const char* section, const char* key, const std::vector<std::string_view>& 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:
|
private:
|
||||||
std::string m_filename;
|
std::string m_filename;
|
||||||
CSimpleIniA m_ini;
|
CSimpleIniA m_ini;
|
||||||
|
|
Loading…
Reference in New Issue