forked from VestigeJ/dataminer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.cpp
90 lines (72 loc) · 1.99 KB
/
settings.cpp
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
#include <iostream>
#include <fstream>
#include "settings.h"
Settings::Settings()
{
m_dirty = false;
}
void Settings::set(string &key, string sequence)
{
map<string, string>::iterator i = m_collection.find(key);
if (i != m_collection.end())
i->second = sequence;
else
m_collection.insert(pair<string, string>(key, sequence));
m_dirty = true;
}
string Settings::get(string &key)
{
std::map<string, string>::iterator i = m_collection.find(key);
if (i != m_collection.end())
return i->second;
return "";
}
bool Settings::check(string &key, string sequence)
{
map<string, string>::iterator i = m_collection.find(key);
if (i == m_collection.end())
{
m_collection.insert(pair<string, string>(key, sequence));
return true;
}
else if (sequence.compare(i->second))
{
i->second = sequence;
return true;
}
else
return false;
}
void Settings::dump()
{
for (map<string, string>::iterator p = m_collection.begin(); p != m_collection.end(); p++)
std::cout << p->first << ": " << p->second << std::endl;
}
void Settings::restore(string filename)
{
std::ifstream os(filename);
string input;
while (std::getline(os, input))
{
unsigned long pos = input.find_first_of("|");
string key = input.substr(0, pos);
string value = input.substr(pos + 1);
std::cout << "Restoring - " << key << " = " << value << std::endl;
m_collection.insert(pair<string, string>(key, value));
}
os.close();
m_dirty = false;
}
void Settings::save(string filename, bool checkDirty)
{
if (checkDirty && m_dirty == false)
return;
std::ofstream os(filename);
for (map<string, string>::iterator p = m_collection.begin(); p != m_collection.end(); p++)
{
std::cout << "Updating - " << p->first << " = " << p->second << std::endl;
os << p->first << "|" << p->second << std::endl;
}
os.close();
m_dirty = false;
}