Subversion Repositories HomeAutomation

Rev

Rev 969 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 969 Rev 976
1
/***************************************************************************
1
/***************************************************************************
2
 *   Copyright (C) November 27, 2008 by Mattias Runge                             *
2
 *   Copyright (C) November 27, 2008 by Mattias Runge                             *
3
 *   mattias@runge.se                                                      *
3
 *   mattias@runge.se                                                      *
4
 *   settings.cpp                                            *
4
 *   settings.cpp                                            *
5
 *                                                                         *
5
 *                                                                         *
6
 *   This program is free software; you can redistribute it and/or modify  *
6
 *   This program is free software; you can redistribute it and/or modify  *
7
 *   it under the terms of the GNU General Public License as published by  *
7
 *   it under the terms of the GNU General Public License as published by  *
8
 *   the Free Software Foundation; either version 2 of the License, or     *
8
 *   the Free Software Foundation; either version 2 of the License, or     *
9
 *   (at your option) any later version.                                   *
9
 *   (at your option) any later version.                                   *
10
 *                                                                         *
10
 *                                                                         *
11
 *   This program is distributed in the hope that it will be useful,       *
11
 *   This program is distributed in the hope that it will be useful,       *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU General Public License for more details.                          *
14
 *   GNU General Public License for more details.                          *
15
 *                                                                         *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU General Public License     *
16
 *   You should have received a copy of the GNU General Public License     *
17
 *   along with this program; if not, write to the                         *
17
 *   along with this program; if not, write to the                         *
18
 *   Free Software Foundation, Inc.,                                       *
18
 *   Free Software Foundation, Inc.,                                       *
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20
 ***************************************************************************/
20
 ***************************************************************************/
21
 
21
 
22
#include <string>
22
#include <string>
23
 
23
 
24
 
24
 
25
#include "settings.h"
25
#include "settings.h"
26
 
26
 
27
string Settings::myFilename;
27
string Settings::myFilename;
28
map<string, string> Settings::mySettings;
28
map<string, string> Settings::mySettings;
29
 
29
 
30
bool Settings::read(string filename)
30
bool Settings::read(string filename)
31
{
31
{
32
    SyslogStream &slog = SyslogStream::getInstance();
32
    SyslogStream &slog = SyslogStream::getInstance();
33
 
33
 
34
    myFilename = filename;
34
    myFilename = filename;
35
 
35
 
36
    ifstream file(myFilename.c_str());
36
    ifstream file(myFilename.c_str());
37
 
37
 
38
    if (!file)
38
    if (!file)
39
    {
39
    {
40
        slog << "Could not find settings file: " << myFilename << "\n";
40
        slog << "Could not find settings file: " + myFilename + ".\n";
41
        return false;
41
        return false;
42
    }
42
    }
43
 
43
 
44
    slog << "Loading settings from " << filename << ".\n";
44
    slog << "Loading settings from " + filename + ".\n";
45
 
45
 
46
    while (!file.eof())
46
    while (!file.eof())
47
    {
47
    {
48
        string line;
48
        string line;
49
        getline(file, line);
49
        getline(file, line);
50
 
50
 
51
        trim(line);
51
        trim(line);
52
 
52
 
53
        if (line.length() == 0)
53
        if (line.length() == 0)
54
            continue;
54
            continue;
55
 
55
 
56
        vector<string> parts = explode("=", line);
56
        vector<string> parts = explode("=", line);
57
 
57
 
58
        if (parts.size() < 2)
58
        if (parts.size() < 2)
59
            continue;
59
            continue;
60
 
60
 
61
        add(trim(parts[0]), trim(parts[1]));
61
        add(trim(parts[0]), trim(parts[1]));
62
    }
62
    }
63
 
63
 
64
    file.close();
64
    file.close();
65
 
65
 
66
    return true;
66
    return true;
67
}
67
}
68
 
68
 
69
void Settings::add(string name, string value)
69
void Settings::add(string name, string value)
70
{
70
{
71
    mySettings[name] = value;
71
    mySettings[name] = value;
72
}
72
}
73
 
73
 
74
string Settings::get(string name)
74
string Settings::get(string name)
75
{
75
{
76
    map<string, string>::const_iterator iterator = mySettings.find(name);
76
    map<string, string>::const_iterator iterator = mySettings.find(name);
77
 
77
 
78
    if (iterator == mySettings.end())
78
    if (iterator == mySettings.end())
79
        return NULL;
79
        return NULL;
80
 
80
 
81
    return iterator->second;
81
    return iterator->second;
82
}
82
}
83
 
83