summaryrefslogtreecommitdiff
path: root/src/setting.cpp
blob: 9e7ed709a44b3e839ef8e282f7b3f39cb27fe8fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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;
}