Subversion Repositories HomeAutomation

Rev

Rev 1620 | Go to most recent revision | Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1619 runge 1
/*
2
 *
3
 *  Copyright (C) 2010  Mattias Runge
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License along
16
 *  with this program; if not, write to the Free Software Foundation, Inc.,
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
19
 */
20
 
21
#include "Store.h"
22
 
23
#include <fstream>
24
#include <stdexcept>
25
 
26
#include <boost/algorithm/string.hpp>
27
 
28
#include "type/common.h"
29
 
30
namespace atom {
31
namespace storage {
32
 
33
Store::Store(std::string filename)
34
{
35
    this->filename_ = filename;
36
    this->modified_ = false;
37
 
38
    std::ifstream file(filename.data());
39
 
40
    if (!file.is_open())
41
    {
42
        file.close();
43
        return;
44
    }
45
 
46
    std::string line;
47
    size_t pos;
48
 
49
    while (!file.eof())
50
    {
51
        std::getline(file, line);
52
 
53
        if (file.eof())
54
        {
55
            break;
56
        }
57
 
58
        pos = line.find_first_of("=");
59
 
60
        if (pos == std::string::npos)
61
        {
62
            continue;
63
        }
64
 
65
        this->parameters_[line.substr(0, pos)] = line.substr(pos + 1);
66
    }
67
 
68
    file.close();
69
}
70
 
71
Store::~Store()
72
{
73
 
74
}
75
 
76
void Store::Flush()
77
{
78
    if (this->modified_)
79
    {
80
        std::ofstream file(this->filename_.data());
81
 
82
        if (!file.is_open())
83
        {
84
            throw std::runtime_error("Could not open " + this->filename_);
85
            return;
86
        }
87
 
88
        for (ParameterList::iterator it = this->parameters_.begin(); it != this->parameters_.end(); it++)
89
        {
90
            file << it->first << "=" << it->second << "\n";
91
        }
92
 
93
        file.close();
94
        this->modified_ = false;
95
    }
96
}
97
 
98
Store::ParameterList& Store::GetParameters()
99
{
100
    return this->parameters_;
101
}
102
 
103
std::string Store::GetParameter(std::string name)
104
{
105
    ParameterList::iterator it = this->parameters_.find(name);
106
 
107
    if (it == this->parameters_.end())
108
    {
109
        throw std::runtime_error("No such parameter");
110
        return "";
111
    }
112
 
113
    return it->second;
114
}
115
 
116
void Store::SetParameter(std::string name, std::string value)
117
{
118
    if (this->parameters_[name] != value)
119
    {
120
        this->parameters_[name] = value;
121
        this->modified_ = true;
122
 
123
        this->Flush();
124
    }
125
}
126
 
127
}; // namespace storage
128
}; // namespace atom