forked from rubenwardy/NodeBoxEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Configuration.cpp
134 lines (112 loc) · 3.02 KB
/
Configuration.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
#include "Configuration.h"
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
void Configuration::load(irr::core::stringc file){
std::string line;
std::ifstream myfile(file.c_str());
if (myfile.is_open()){
while (std::getline (myfile,line) )
{
doLine(irr::core::stringc(line.c_str()));
}
myfile.close();
}else
printf("Unable to open editor.conf\n");
return;
}
void Configuration::doLine(irr::core::stringc line){
irr::core::stringc l = line;
// Remove comments
irr::s32 first = l.findFirst(irr::c8('#'));
if (first!=-1)
l = l.subString(0,first);
l = l.trim(" \t\n\r");
// Check that string hash content
if (l==""){
return;
}
// Split string into hash and value per
first = l.findFirst(irr::c8('='));
irr::core::stringc hash = l.subString(0,first).trim();
irr::core::stringc value = l.subString(first+1,l.size()).trim();
#if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8
stringc lower = irr::core::stringc(value);
lower.make_lower();
#else
stringc lower = value.make_lower();
#endif
// convert boolean to lower case
if (lower=="true"){
value = "true";
}else if (lower=="false"){
value = "false";
}
// Create setting
setStringSetting(hash,value);
}
void Configuration::save(irr::core::stringc file){
std::ofstream myfile (file.c_str());
if (myfile.is_open()){
for (irr::core::list<setting*>::Iterator it=list->begin();it!=list->end();it++){
printf("Looping...\n");
setting* set = *it;
printf("%s = ",set->hash.c_str());
printf("%s\n",set->value.c_str());
myfile << set->hash.c_str();
myfile << " = ";
myfile << set->value.c_str();
myfile << "\n";
}
myfile.close();
}else
printf("Unable to write to file\n");
}
Configuration::setting* Configuration::getSetting(irr::core::stringc hash,bool warning_on_not_found) const{
for (irr::core::list<setting*>::Iterator it=list->begin();it!=list->end();it++){
setting* set = *it;
if (set->hash == hash){
return set;
}
}
if (warning_on_not_found)
printf("[Warning] Setting not found '%s'\n",hash.c_str());
return NULL;
}
void Configuration::setStringSetting(irr::core::stringc hash,irr::core::stringc value){
setting* set = getSetting(hash,false);
if (set){
set->value = value;
}else{
setting* newset = new setting();
newset->hash = hash;
newset->value = value;
list->push_back(newset);
}
}
irr::core::stringc* Configuration::getSettingAsString(irr::core::stringc hash) const{
setting* set = getSetting(hash);
if (set)
return &set->value;
return NULL;
}
bool Configuration::getSettingAsBool(irr::core::stringc hash) const{
setting* set = getSetting(hash);
if (set){
if (set->value == "true" || set->value == "1"){
return true;
}else if (set->value == "false" || set->value == "0"){
return false;
}
printf("[Warning] Setting '%s' is not a boolean!\n",hash.c_str());
}
return false;
}
int Configuration::getSettingAsInt(irr::core::stringc hash) const{
setting* set = getSetting(hash);
if (set){
return atoi(set->value.c_str());
}
return false;
}