You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

127 lines
2.1 KiB

#include "settings-manager.hpp"
#include <fstream>
using namespace std;
SettingsManager::SettingsManager()
{
settings = list<SettingsEntry*>();
readAll();
}
SettingsManager::~SettingsManager()
{
}
void SettingsManager::Update(string key, string value)
{
for (auto entry : settings)
{
if (entry->key == key)
{
entry->value = value;
return;
}
}
settings.push_back(new SettingsEntry{
.key = key,
.value = value,
});
}
void SettingsManager::UpdateBool(string key, bool value)
{
if (value)
Update(key, "true");
else
Update(key, "false");
}
void SettingsManager::Save(string key, string value)
{
Update(key, value);
SaveAll();
}
void SettingsManager::SaveAll()
{
saveAll();
}
string SettingsManager::Get(string key)
{
for (auto entry : settings)
{
if (entry->key == key)
{
return entry->value;
}
}
return "";
}
string SettingsManager::GetWithDefault(string key, string defaultValue)
{
auto value = Get(key);
if (value == "")
return defaultValue;
return value;
}
bool SettingsManager::GetBool(string key)
{
if (Get(key) == "true")
return true;
return false;
}
bool SettingsManager::GetBoolWithDefault(string key, bool defaultValue)
{
auto value = Get(key);
if (value == "true")
return true;
if (value == "false")
return false;
return defaultValue;
}
void SettingsManager::readAll()
{
settings.clear();
ifstream fin;
string line;
fin.open("./config.cfg", ios::in);
while (getline(fin, line))
{
if (line.find("=") != std::string::npos)
{
settings.push_back(new SettingsEntry{
.key = line.substr(0, line.find("=")),
.value = line.substr(line.find("=")+1, line.length()),
});
}
}
}
void SettingsManager::saveAll()
{
ofstream fout;
fout.open("./config.cfg", ios::trunc);
for (auto entry : settings)
{
fout << entry->key << "=" << entry->value << endl;
}
}