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/setting.cpp | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 src/setting.cpp (limited to 'src/setting.cpp') diff --git a/src/setting.cpp b/src/setting.cpp new file mode 100644 index 0000000..9e7ed70 --- /dev/null +++ b/src/setting.cpp @@ -0,0 +1,144 @@ +/*************************************************************************** + setting.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 "settingfilter.h" +#include "stringutils.h" + +#include +#include +#include + +using namespace std; + +Setting::~Setting() +{ + for(list::const_iterator itFilter = m_filters.begin(); itFilter != m_filters.end(); ++itFilter) + delete *itFilter; + m_filters.clear(); +} + +void Setting::pushFilter(SettingFilter* filter) +{ + if(!filter) + return; + + m_filters.push_back(filter); +} + +void Setting::popFilter() +{ + if(m_filters.empty()) + return; + + delete m_filters.back(); + m_filters.pop_back(); +} + +SettingFilter* Setting::findFilterWithId(const string& id) +{ + for(list::const_iterator itFilter = m_filters.begin(); itFilter != m_filters.end(); ++itFilter) + { + if((*itFilter)->getId() == id) + return *itFilter; + } + + return 0; +} + +const SettingFilter* Setting::findFilterWithId(const string& id) const +{ + for(list::const_iterator itFilter = m_filters.begin(); itFilter != m_filters.end(); ++itFilter) + { + if((*itFilter)->getId() == id) + return *itFilter; + } + + return 0; +} + +string Setting::getThroughFilter() const +{ + string valueCopy = m_value; + + for(list::const_reverse_iterator itFilter = m_filters.rbegin(); itFilter != m_filters.rend(); ++itFilter) + (*itFilter)->filterRead(valueCopy); + + return valueCopy; +} + +bool Setting::setThroughFilter(const string& value) +{ + string valueCopy = value; + + for(list::iterator itFilter = m_filters.begin(); itFilter != m_filters.end(); ++itFilter) + { + if(!(*itFilter)->filterWrite(valueCopy)) + return false; + } + + m_value = valueCopy; + return true; +} + +istream& operator>>(istream& in, Setting& setting) +{ + while(!in.eof()) + { + // extract line from stream + string line; + getline(in, line); + + if(in.fail()) + return in; + + // strip whitespace + line = trim(line); + + // skip empty lines + if(line.empty()) + continue; + + // skip comments + if(line[0] == '#') + continue; + + // split line into id and value + vector words = splitQuoted(line, "="); + + if(words.size() < 2 || words[0].empty()) + { + in.setstate(ios_base::failbit); + return in; + } + + setting.setId(words[0]); + setting.setThroughFilter(words[1]); + + break; + } + + return in; +} + +ostream& operator<<(ostream& out, const Setting& setting) +{ + out << setting.getId() << "=\"" << setting.getThroughFilter() << "\"" << endl; + return out; +} + + -- cgit v1.2.3