From 0e03940802cebefdf6b0597a154bd9395e1af4d2 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Wed, 18 Jun 2014 15:45:45 +0200 Subject: Add the vanilla version of the project This version can still be found here: http://www.roland-riegel.de/nload/index.html --- src/settingstore.cpp | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 src/settingstore.cpp (limited to 'src/settingstore.cpp') diff --git a/src/settingstore.cpp b/src/settingstore.cpp new file mode 100644 index 0000000..1dcb626 --- /dev/null +++ b/src/settingstore.cpp @@ -0,0 +1,98 @@ +/*************************************************************************** + settingstore.cpp + ------------------- + begin : Tue Nov 06 2007 + copyright : (C) 2007 - 2012 by Roland Riegel + email : feedback@roland-riegel.de + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include "setting.h" +#include "settingstore.h" + +#include + +using namespace std; + +map SettingStore::m_settings; + +Setting& SettingStore::get(const string& key) +{ + return m_settings[key]; +} + +void SettingStore::add(const Setting& setting) +{ + m_settings[setting.getId()] = setting; +} + +void SettingStore::remove(const string& key) +{ + m_settings.erase(key); +} + +bool SettingStore::exists(const string& key) +{ + return m_settings.find(key) != m_settings.end(); +} + +map& SettingStore::getAll() +{ + return m_settings; +} + +bool SettingStore::readFromFile(const std::string& file) +{ + if(file.empty()) + return false; + + // open file + ifstream fin(file.c_str()); + if(!fin.is_open()) + return false; + + // parse file + while(fin.good()) + { + Setting setting; + + fin >> setting; + if(setting.getId().empty()) + break; + + if(exists(setting.getId())) + get(setting.getId()).setThroughFilter(setting.getValue()); + else + add(setting); + } + + return !fin.fail(); +} + +bool SettingStore::writeToFile(const std::string& file) +{ + if(file.empty()) + return false; + + // open file + ofstream fout(file.c_str()); + if(!fout.is_open()) + return false; + + fout << "Version=\"1\"\n"; + + // output settings + for(map::const_iterator itSetting = m_settings.begin(); itSetting != m_settings.end(); ++itSetting) + fout << itSetting->second; + + return fout.good(); +} + -- cgit v1.2.3