summaryrefslogtreecommitdiff
path: root/src/setting.cpp
diff options
context:
space:
mode:
authorOlivier Gayot <duskcoder@gmail.com>2014-06-18 15:45:45 +0200
committerOlivier Gayot <duskcoder@gmail.com>2014-06-18 15:49:38 +0200
commit0e03940802cebefdf6b0597a154bd9395e1af4d2 (patch)
tree409a58499128227dd57943515d003074190551f5 /src/setting.cpp
Add the vanilla version of the project
This version can still be found here: http://www.roland-riegel.de/nload/index.html
Diffstat (limited to 'src/setting.cpp')
-rw-r--r--src/setting.cpp144
1 files changed, 144 insertions, 0 deletions
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 <istream>
+#include <ostream>
+#include <sstream>
+
+using namespace std;
+
+Setting::~Setting()
+{
+ for(list<SettingFilter*>::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<SettingFilter*>::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<SettingFilter*>::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<SettingFilter*>::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<SettingFilter*>::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<string> 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;
+}
+
+