2020-02-28 07:00:14 +00:00
|
|
|
#include "ini_settings_interface.h"
|
2020-08-08 18:40:15 +00:00
|
|
|
#include "common/file_system.h"
|
2020-01-10 03:31:12 +00:00
|
|
|
#include "common/log.h"
|
2019-12-31 02:41:21 +00:00
|
|
|
#include <algorithm>
|
2020-08-10 16:59:58 +00:00
|
|
|
#include <iterator>
|
|
|
|
|
2020-02-28 07:00:14 +00:00
|
|
|
Log_SetChannel(INISettingsInterface);
|
2019-12-30 11:22:22 +00:00
|
|
|
|
2020-04-05 12:57:29 +00:00
|
|
|
INISettingsInterface::INISettingsInterface(std::string filename) : m_filename(std::move(filename)), m_ini(true, true)
|
2019-12-30 11:22:22 +00:00
|
|
|
{
|
2020-08-08 18:40:15 +00:00
|
|
|
SI_Error err = SI_FAIL;
|
|
|
|
std::FILE* fp = FileSystem::OpenCFile(m_filename.c_str(), "rb");
|
|
|
|
if (fp)
|
|
|
|
{
|
|
|
|
err = m_ini.LoadFile(fp);
|
|
|
|
std::fclose(fp);
|
|
|
|
}
|
|
|
|
|
2019-12-30 11:22:22 +00:00
|
|
|
if (err != SI_OK)
|
2020-04-05 12:57:29 +00:00
|
|
|
Log_WarningPrintf("Settings could not be loaded from '%s', defaults will be used.", m_filename.c_str());
|
2019-12-30 11:22:22 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 07:00:14 +00:00
|
|
|
INISettingsInterface::~INISettingsInterface()
|
2019-12-30 11:22:22 +00:00
|
|
|
{
|
2020-02-15 15:14:42 +00:00
|
|
|
if (m_dirty)
|
2020-04-10 14:00:21 +00:00
|
|
|
Save();
|
|
|
|
}
|
|
|
|
|
2020-05-27 23:36:41 +00:00
|
|
|
bool INISettingsInterface::Save()
|
2020-04-10 14:00:21 +00:00
|
|
|
{
|
2020-08-08 18:40:15 +00:00
|
|
|
SI_Error err = SI_FAIL;
|
|
|
|
std::FILE* fp = FileSystem::OpenCFile(m_filename.c_str(), "wb");
|
|
|
|
if (fp)
|
|
|
|
{
|
|
|
|
err = m_ini.SaveFile(fp, false);
|
|
|
|
std::fclose(fp);
|
|
|
|
}
|
|
|
|
|
2020-04-10 14:00:21 +00:00
|
|
|
if (err != SI_OK)
|
2020-05-27 23:36:41 +00:00
|
|
|
{
|
2020-04-10 14:00:21 +00:00
|
|
|
Log_WarningPrintf("Failed to save settings to '%s'.", m_filename.c_str());
|
2020-05-27 23:36:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_dirty = false;
|
|
|
|
return true;
|
2019-12-30 11:22:22 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 07:00:14 +00:00
|
|
|
void INISettingsInterface::Clear()
|
2020-02-28 07:00:09 +00:00
|
|
|
{
|
|
|
|
m_ini.Reset();
|
|
|
|
}
|
|
|
|
|
2020-02-28 07:00:14 +00:00
|
|
|
int INISettingsInterface::GetIntValue(const char* section, const char* key, int default_value /*= 0*/)
|
2019-12-30 11:22:22 +00:00
|
|
|
{
|
|
|
|
return static_cast<int>(m_ini.GetLongValue(section, key, default_value));
|
|
|
|
}
|
|
|
|
|
2020-02-28 07:00:14 +00:00
|
|
|
float INISettingsInterface::GetFloatValue(const char* section, const char* key, float default_value /*= 0.0f*/)
|
2019-12-30 11:22:22 +00:00
|
|
|
{
|
|
|
|
return static_cast<float>(m_ini.GetDoubleValue(section, key, default_value));
|
|
|
|
}
|
|
|
|
|
2020-02-28 07:00:14 +00:00
|
|
|
bool INISettingsInterface::GetBoolValue(const char* section, const char* key, bool default_value /*= false*/)
|
2019-12-30 11:22:22 +00:00
|
|
|
{
|
|
|
|
return m_ini.GetBoolValue(section, key, default_value);
|
|
|
|
}
|
|
|
|
|
2020-02-28 07:00:14 +00:00
|
|
|
std::string INISettingsInterface::GetStringValue(const char* section, const char* key,
|
2019-12-30 11:22:22 +00:00
|
|
|
const char* default_value /*= ""*/)
|
|
|
|
{
|
|
|
|
return m_ini.GetValue(section, key, default_value);
|
|
|
|
}
|
|
|
|
|
2020-02-28 07:00:14 +00:00
|
|
|
void INISettingsInterface::SetIntValue(const char* section, const char* key, int value)
|
2019-12-30 11:22:22 +00:00
|
|
|
{
|
2020-02-15 15:14:42 +00:00
|
|
|
m_dirty = true;
|
2020-01-16 04:36:31 +00:00
|
|
|
m_ini.SetLongValue(section, key, static_cast<long>(value), nullptr, false, true);
|
2019-12-30 11:22:22 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 07:00:14 +00:00
|
|
|
void INISettingsInterface::SetFloatValue(const char* section, const char* key, float value)
|
2019-12-30 11:22:22 +00:00
|
|
|
{
|
2020-02-15 15:14:42 +00:00
|
|
|
m_dirty = true;
|
2020-01-16 04:36:31 +00:00
|
|
|
m_ini.SetDoubleValue(section, key, static_cast<double>(value), nullptr, true);
|
2019-12-30 11:22:22 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 07:00:14 +00:00
|
|
|
void INISettingsInterface::SetBoolValue(const char* section, const char* key, bool value)
|
2019-12-30 11:22:22 +00:00
|
|
|
{
|
2020-02-15 15:14:42 +00:00
|
|
|
m_dirty = true;
|
2020-01-16 04:36:31 +00:00
|
|
|
m_ini.SetBoolValue(section, key, value, nullptr, true);
|
2019-12-30 11:22:22 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 07:00:14 +00:00
|
|
|
void INISettingsInterface::SetStringValue(const char* section, const char* key, const char* value)
|
2019-12-30 11:22:22 +00:00
|
|
|
{
|
2020-02-15 15:14:42 +00:00
|
|
|
m_dirty = true;
|
2019-12-31 02:41:21 +00:00
|
|
|
m_ini.SetValue(section, key, value, nullptr, true);
|
2019-12-30 11:22:22 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 07:00:14 +00:00
|
|
|
void INISettingsInterface::DeleteValue(const char* section, const char* key)
|
2019-12-30 11:22:22 +00:00
|
|
|
{
|
2020-02-15 15:14:42 +00:00
|
|
|
m_dirty = true;
|
2019-12-31 02:41:21 +00:00
|
|
|
m_ini.Delete(section, key);
|
|
|
|
}
|
|
|
|
|
2020-12-20 03:23:33 +00:00
|
|
|
void INISettingsInterface::ClearSection(const char* section)
|
|
|
|
{
|
|
|
|
m_dirty = true;
|
|
|
|
m_ini.Delete(section, nullptr);
|
|
|
|
m_ini.SetValue(section, nullptr, nullptr);
|
|
|
|
}
|
|
|
|
|
2020-02-28 07:00:14 +00:00
|
|
|
std::vector<std::string> INISettingsInterface::GetStringList(const char* section, const char* key)
|
2019-12-31 02:41:21 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-21 09:49:04 +00:00
|
|
|
void INISettingsInterface::SetStringList(const char* section, const char* key, const std::vector<std::string>& items)
|
2019-12-31 02:41:21 +00:00
|
|
|
{
|
2020-02-15 15:14:42 +00:00
|
|
|
m_dirty = true;
|
2019-12-31 02:41:21 +00:00
|
|
|
m_ini.Delete(section, key);
|
|
|
|
|
2020-07-21 09:49:04 +00:00
|
|
|
for (const std::string& sv : items)
|
|
|
|
m_ini.SetValue(section, key, sv.c_str(), nullptr, false);
|
2019-12-31 02:41:21 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 07:00:14 +00:00
|
|
|
bool INISettingsInterface::RemoveFromStringList(const char* section, const char* key, const char* item)
|
2019-12-31 02:41:21 +00:00
|
|
|
{
|
2020-02-15 15:14:42 +00:00
|
|
|
m_dirty = true;
|
2019-12-31 02:41:21 +00:00
|
|
|
return m_ini.DeleteValue(section, key, item, true);
|
|
|
|
}
|
|
|
|
|
2020-02-28 07:00:14 +00:00
|
|
|
bool INISettingsInterface::AddToStringList(const char* section, const char* key, const char* item)
|
2019-12-31 02:41:21 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-02-15 15:14:42 +00:00
|
|
|
m_dirty = true;
|
2019-12-31 02:41:21 +00:00
|
|
|
m_ini.SetValue(section, key, item, nullptr, false);
|
|
|
|
return true;
|
2019-12-30 11:22:22 +00:00
|
|
|
}
|