-
Notifications
You must be signed in to change notification settings - Fork 27
/
confighandler.cpp
181 lines (143 loc) · 4.57 KB
/
confighandler.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//////////////////////////////////////////////////////////////////////
// Yet Another Tibia Client
//////////////////////////////////////////////////////////////////////
// Stores and reads configuration files
//////////////////////////////////////////////////////////////////////
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//////////////////////////////////////////////////////////////////////
#include <string.h>
#include <stdio.h>
#include "confighandler.h"
#include "util.h"
#include "product.h"
ConfigHandler* configHandler = NULL;
ConfigHandler::ConfigHandler()
{
}
ConfigHandler::~ConfigHandler()
{
clear();
}
void ConfigHandler::clear()
{
for(SectionVector::iterator it = sections.begin(); it != sections.end(); it++){
for(KeyVector::iterator cit = (*it)->keys.begin(); cit != (*it)->keys.end(); cit++){
delete (*cit);
}
(*it)->keys.clear();
delete (*it);
}
sections.clear();
}
bool ConfigHandler::loadConfig(const char* filename)
{
FILE* f = yatc_fopen(filename, "rb");
if(!f){
return false;
}
//bool sector = false;
int currentSection = 0;
while(!feof(f)){
char read_buffer[2048] = {0};
char *buffer = fgets(read_buffer, sizeof read_buffer, f);
if (!buffer)
break;
if(buffer[0] != '#'){ // Not a comment: process it
while(buffer[strlen(buffer)-1] == '\n' || buffer[strlen(buffer)-1] == '\r')
buffer[strlen(buffer)-1] = '\0';
if(!readSection(buffer, currentSection)) { // Attempt to process as a section
readKey(buffer, currentSection);
}
}
}
currentSection = 0;
if(f)
fclose(f);
return true;
}
bool ConfigHandler::saveConfig(const char* filename)
{
FILE* f = yatc_fopen(filename, "wb");
if(!f){
return false;
}
fprintf(f, "%s", "# This is the config file for " PRODUCTLONGNAME "\r\n");
fprintf(f, "%s", "# Do not edit unless you know what you are doing!\r\n\r\n");
for(SectionVector::iterator it = sections.begin(); it != sections.end(); it++){
fprintf(f, "[%s]\r\n", (*it)->name.c_str());
for(KeyVector::iterator cit = (*it)->keys.begin(); cit != (*it)->keys.end(); cit++){
std::string val = (*cit)->value;
val = str_replace("\n", "<br>", val);
fprintf(f, "%s=\"%s\"\r\n", (*cit)->name.c_str(), val.c_str());
}
fprintf(f, "\r\n");
}
if(f)
fclose(f);
clear();
return true;
}
bool ConfigHandler::readSection(const char* buffer, int& currentSection)
{
if(strstr(buffer, "[") && strstr(buffer, "]")){
std::string str(buffer);
int open = str.find_first_of("[");
int close = str.find_first_of("]");
int len = close - open - 1;
std::string sectionName = str.substr(open+1, len);
newSection(sectionName);
currentSection = sections.size() - 1;
return true;
}
return false;
}
void ConfigHandler::readKey(const char* buffer, int currentSection)
{
if(strstr(buffer, "=")){
Section* section = sections[currentSection];
std::string str(buffer);
int eq = str.find_first_of("=");
std::string keyname = str.substr(0, eq);
int len = str.size() - eq - 2 - 1;
std::string value = str.substr(eq + 2, len);
value = str_replace("<br>", "\n", value);
Key* key = new Key(keyname, value);
section->keys.push_back(key);
}
}
bool ConfigHandler::keyExists(const std::string section, const std::string keyname)
{
for(SectionVector::iterator it = sections.begin(); it != sections.end(); it++){
if((*it)->name == section){
for(KeyVector::iterator cit = (*it)->keys.begin(); cit != (*it)->keys.end(); cit++){
if((*cit)->name == keyname)
return true;
}
}
}
return false;
}
std::string ConfigHandler::getKeyValue(const std::string section, const std::string keyname)
{
for(SectionVector::iterator it = sections.begin(); it != sections.end(); it++){
if((*it)->name == section){
for(KeyVector::iterator cit = (*it)->keys.begin(); cit != (*it)->keys.end(); cit++){
if((*cit)->name == keyname)
return (*cit)->value;
}
}
}
return "";
}