-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreferences.cpp
87 lines (78 loc) · 1.78 KB
/
Preferences.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
#include <apollo/Preferences.h>
#include <apollo/Utilities/ResourceManager.h>
#include <map>
namespace Preferences
{
typedef std::map<std::string, std::string> PreferenceMap;
PreferenceMap preferences;
std::string Get ( const std::string& key, const std::string& defaultValue )
{
PreferenceMap::iterator iter = preferences.find(key);
if (iter == preferences.end())
{
return defaultValue;
}
else
{
return iter->second;
}
}
void Set ( const std::string& key, const std::string& defaultValue )
{
preferences[key] = defaultValue;
}
void Clear ( const std::string& key )
{
PreferenceMap::iterator iter = preferences.find(key);
if (iter != preferences.end())
{
preferences.erase(iter);
}
}
static void ParseLine ( const std::string& line )
{
if (line != "")
{
std::string::size_type n = line.find_first_of(':');
std::string k = line.substr(0, n);
std::string v = line.substr(n + 1, std::string::npos);
preferences[k] = v;
}
}
void Load ()
{
preferences.clear();
SDL_RWops* ops = ResourceManager::OpenFile("Config/Preferences.cfg");
if (!ops)
return;
size_t len;
char* buffer = (char*)ResourceManager::ReadFull(&len, ops, 1);
std::string preferenceLine;
char* tempPointer = buffer;
while (*tempPointer)
{
char ch = *tempPointer;
if (ch == '\n')
{
ParseLine(preferenceLine);
preferenceLine.clear();
}
else
{
preferenceLine.push_back(ch);
}
tempPointer++;
}
free((void*)buffer);
}
void Save ()
{
std::string buffer;
for ( PreferenceMap::iterator iter = preferences.begin(); iter != preferences.end(); iter++ )
{
buffer.append(iter->first + ":" + iter->second + "\n");
}
ResourceManager::WriteFile("Config/Preferences.cfg", buffer.data(), buffer.length());
ResourceManager::WriteFile("Config/Preferences.cfg", buffer.data(), sizeof(buffer.data()));
}
}