Subversion Repositories HomeAutomation

Rev

Rev 985 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 985 Rev 1024
Line 16... Line 16...
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
 
-
 
22
#include <string>
-
 
23
 
-
 
24
 
21
 
25
#include "settings.h"
22
#include "settings.h"
26
 
23
 
27
string Settings::myFilename;
24
string Settings::myFilename;
28
map<string, string> Settings::mySettings;
25
map<string, string> Settings::mySettings;
-
 
26
Mutex Settings::myMutex;
29
 
27
 
30
bool Settings::read(string filename)
28
bool Settings::read(string filename)
31
{
29
{
-
 
30
    myMutex.lock();
-
 
31
 
32
    SyslogStream &slog = SyslogStream::getInstance();
32
    Logger &log = Logger::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
        log.add("Could not find settings file: " + myFilename + ".\n");
-
 
41
        myMutex.unlock();
41
        return false;
42
        return false;
42
    }
43
    }
43
 
44
 
44
    slog << "Loading settings from " + filename + ".\n";
45
    log.add("Loading settings from " + filename + ".\n");
45
 
46
 
46
    while (!file.eof())
47
    while (!file.eof())
47
    {
48
    {
48
        string line;
49
        string line;
49
        getline(file, line);
50
        getline(file, line);
50
 
51
 
51
        trim(line);
52
        trim(line);
52
 
53
 
53
        if (line.length() == 0)
54
        if (line.length() == 0)
54
            continue;
55
            continue;
55
 
56
 
56
        vector<string> parts = explode("=", line);
57
        vector<string> parts = explode("=", line);
57
 
58
 
58
        if (parts.size() < 2)
59
        if (parts.size() < 2)
59
            continue;
60
            continue;
60
 
61
 
61
        add(trim(parts[0]), trim(parts[1]));
62
        add(trim(parts[0]), trim(parts[1]));
62
    }
63
    }
63
 
64
 
64
    file.close();
65
    file.close();
65
 
66
 
-
 
67
    myMutex.unlock();
66
    return true;
68
    return true;
67
}
69
}
68
 
70
 
69
void Settings::add(string name, string value)
71
void Settings::add(string name, string value)
70
{
72
{
71
    mySettings[name] = value;
73
    mySettings[name] = value;
72
}
74
}
73
 
75
 
74
string Settings::get(string name)
76
string Settings::get(string name)
75
{
77
{
-
 
78
    myMutex.lock();
-
 
79
 
76
    map<string, string>::const_iterator iterator = mySettings.find(name);
80
    map<string, string>::const_iterator iterator = mySettings.find(name);
77
 
81
 
-
 
82
    string value = "";
-
 
83
 
78
    if (iterator == mySettings.end())
84
    if (iterator != mySettings.end())
-
 
85
    {
-
 
86
        value = iterator->second;
-
 
87
    }
-
 
88
 
79
        return "";
89
    myMutex.unlock();
80
 
90
 
81
    return iterator->second;
91
    return value;
82
}
92
}