-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.cpp
162 lines (141 loc) · 5.02 KB
/
session.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "session.h"
#include "logger.h"
#include "translator.h"
#include "utility.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include <ctime>
using namespace std;
// Initialize singleton_ to nullptr
Session* Session::singleton_ = nullptr;
Session* Session::instance() {
if( singleton_ == nullptr ) {
singleton_ = new Session();
Logger::debug() << "creating Session singleton" << Logger::endl;
singleton_->loadSession();
}
return singleton_;
}
void Session::kill(bool save) {
if( singleton_ != nullptr ) {
Logger::debug() << "destroying Session singleton" << Logger::endl;
if( save ) {
singleton_->saveSession();
}
delete singleton_;
singleton_ = nullptr;
}
}
static string option_filename = "bakhar.opt";
void Session::loadSession() {
ifstream file(option_filename);
if (!file) {
Logger::error() << tr("Unable to read resource file: ") << option_filename << Logger::endl;
return;
}
std::string str;
while (std::getline(file, str)) {
if( !Utility::startsWith(str, "*") ) {
continue;
}
int index = str.find(':');
string label = str.substr(0, index);
string value = str.substr(index+1);
index = value.find('#');
string desc = "# no description";
if( index >= 0 ) {
desc = value.substr(index);
value = value.substr(0,index);
}
value = Utility::trim(value);
ValueDesc vd = std::pair<string, string>(value, desc);
dictionary_.insert(std::pair<string,ValueDesc>(label, vd));
}
}
void Session::saveSession() {
std::ofstream file(option_filename);
if (!file) {
Logger::error() << tr("unable to open file for save: ") << option_filename << Logger::endl;
return;
}
Logger::info() << tr("Saving ") << option_filename << "..." << Logger::endl;
file << "# edit at your own risk !!" << std::endl;
//time_t now = time(0);
//char* date = ctime(&now);
//file << "# date " << date << std::endl;
for( auto attr : dictionary_ ) {
file << attr.first << ": " << attr.second.first << " " << attr.second.second << std::endl;
}
file.close();
}
bool Session::getBoolean(const string& label, bool default_value) {
map<string, ValueDesc>::iterator it = dictionary_.find(label);
if( it != dictionary_.end() ) {
const string& value = it->second.first;
if( value == "true" ) {
return true;
} else {
return false;
}
}
ValueDesc vd = std::pair<string, string>(default_value?"true":"false", "");
dictionary_.insert(std::pair<string,ValueDesc>(label, vd));
return default_value;
}
void Session::setBoolean(const string& label, bool value) {
map<string, ValueDesc>::iterator it = dictionary_.find(label);
if( it != dictionary_.end() ) {
dictionary_.erase(it);
}
ValueDesc vd = std::pair<string, string>(value?"true":"false", "");
dictionary_.insert(std::pair<string,ValueDesc>(label, vd));
}
int Session::getInteger(const string& label, int default_value) {
map<string, ValueDesc>::iterator it = dictionary_.find(label);
int value = default_value;
if( it != dictionary_.end() ) {
const string& str_value = it->second.first;
value = ::atoi(str_value.c_str());
}
ValueDesc vd = std::pair<string, string>(Utility::itos(default_value), "");
dictionary_.insert(std::pair<string,ValueDesc>(label, vd));
return value;
}
void Session::setInteger(const string& label, int value) {
map<string, ValueDesc>::iterator it = dictionary_.find(label);
if( it != dictionary_.end() ) {
dictionary_.erase(it);
}
ValueDesc vd = std::pair<string, string>(Utility::itos(value), "");
dictionary_.insert(std::pair<string,ValueDesc>(label, vd));
}
float Session::getFloat(const string& label, float default_value) {
map<string, ValueDesc>::iterator it = dictionary_.find(label);
float value = default_value;
if( it != dictionary_.end() ) {
const string& str_value = it->second.first;
value = ::atof(str_value.c_str());
}
ValueDesc vd = std::pair<string, string>(Utility::ftos(default_value), "");
dictionary_.insert(std::pair<string,ValueDesc>(label, vd));
return value;
}
string Session::getString(const string& label, string default_value) {
map<string, ValueDesc>::iterator it = dictionary_.find(label);
string value = default_value;
if( it != dictionary_.end() ) {
value = it->second.first;
}
ValueDesc vd = std::pair<string, string>(default_value, "");
dictionary_.insert(std::pair<string,ValueDesc>(label, vd));
return value;
}
void Session::setString(const string& label, const std::string& value) {
map<string, ValueDesc>::iterator it = dictionary_.find(label);
if( it != dictionary_.end() ) {
dictionary_.erase(it);
}
ValueDesc vd = std::pair<string, string>(value, "");
dictionary_.insert(std::pair<string,ValueDesc>(label, vd));
}