Парсинг config-файла на C++
09.11.2010, 19:49. Показов 4413. Ответов 0
Привет! Дали итоговое задание в универе. Всё написал. Осталось припаять парсер config-файла (это самое сложное). Покопался в нете и нашёл код. Компилируется. Но вот :
Помогите исправить пожалуйста.
main.cpp
| C++ | 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
| #include <iostream>
using namespace std;
#include "config.h"
//int main(int argc, char* argv[], char* envp[])
int main(char* envp[])
{
/* cout << "config demo program" << endl;
cout << "(c) 2008 Kai G. Schwebke" << endl;
cout << "This code is licensed under LGPL and comes with no warranty;" << endl;
cout << "see COPYING for details." << endl;
cout << endl;
*/
// read config file with environment variable expansion support
Config config("demo.config", envp);
// basic usage of properties
if (config.pBool("showHello")) {
int cnt = config.pInt("helloCount");
string msg = config.pString("helloMessage");
for (int i = 0; i < cnt; ++i) {
cout << msg << endl;
}
cout << endl;
}
// properties with expanded names (no difference in using)
cout << "tempFolder = '" << config.pString("tempFolder") << "'" << endl;
cout << "tempSubFolder = '" << config.pString("tempSubFolder") << "'" << endl;
cout << endl;
// get properties for all subgroups starting with prefix
map<string, Config*> messages = config.getGroups(); // all groups
const string messagePrefix = "message"; // prefix for group name
for (map<string, Config*>::iterator i = messages.begin(); i != messages.end(); ++i) {
string groupName = i->first;
Config* group = i->second;
// test group name for prefix
if (groupName.substr(0, messagePrefix.length()) == messagePrefix) {
// display group contents
cout << group->pString("name") << ":" << endl;
cout << " " << group->pString("text") << endl;
}
}
return 0;
} |
|
config.cpp
| C++ | 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
| #include "config.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#include "log.h"
Config::Config(string name, string parentDebugInfo) {
debugInfo = parentDebugInfo + ", " + name;
}
Config::Config(string configFile, char** envp) {
while (*envp) {
string envEntry = *envp;
size_t pos = envEntry.find('=');
if (pos != string::npos) {
string name = envEntry.substr(0, pos);
string value = envEntry.substr(pos+1, string::npos);
envSymbols[name] = value;
logDebug(cout << "environment symbol: '" << name << "' = '" << value << "'" << endl);
}
++envp;
}
debugInfo = configFile;
groupStack.push_front(this);
FILE* in = fopen(configFile.c_str(), "r");
if (!in) {
cerr << "cannot open input file '" << configFile << "'" << endl;
exit(2);
}
char buff[1024];
while (fgets(buff, 1024, in)) {
string line=buff;
if ( (line.length() > 2) && (line[0] != '#') && (line.find(')') == string::npos) ) {
string name;
string value;
split(line, name, value, '=');
if (value == "(") {
logDebug(cout << " config: new group '" << name << "'" << endl);
Config* newGroup = new Config(name, debugInfo);
groupStack.front()->groups[name] = newGroup;
groupStack.push_front(newGroup);
} else {
for (list<Config*>::reverse_iterator i = groupStack.rbegin(); i != groupStack.rend(); ++i) {
(*i)->symbolExpand(value);
}
envSymbolExpand(value);
logDebug(cout << " config: name = '" << name << "', value = '" << value << "'" << endl);
groupStack.front()->add(name, value);
}
}
if ( (line.length() > 0) && (line[0] != '#') && (line.find(')') != string::npos) ) {
logDebug(cout << " end of group" << endl);
groupStack.pop_front();
}
}
fclose(in);
}
Config::~Config() {
for (map<string, Config*>::iterator i = groups.begin(); i != groups.end(); ++i) {
delete i->second;
}
}
void Config::add(string name, string value) {
symbols[name] = value;
}
void Config::split(string in, string& left, string& right, char c) {
size_t pos = in.find_first_of(c);
if(pos == string::npos) {
left = in;
trim(left);
right = "";
} else if (pos <= 1) {
left = "";
right = in.substr(pos+1, string::npos);
trim(right);
} else {
left = in.substr(0, pos-1);
trim(left);
right = in.substr(pos+1, string::npos);
trim(right);
}
}
void Config::trim(string& s) {
while ( (s.length() > 1) && ( (s[0] == ' ') || (s[0] =='\t') ) ) {
s = s.substr(1, string::npos);
}
while ( (s.length() > 1) &&
( (s[s.length()-1] == ' ') ||
(s[s.length()-1] == '\t') ||
(s[s.length()-1] == '\n') ||
(s[s.length()-1] == '\r') ) ) {
s = s.substr(0, s.length()-1);
}
if ( (s.length() > 1) && (s[0] == '"') ) {
s = s.substr(1, string::npos);
}
if ( (s.length() > 1) && (s[s.length()-1] == '"') ) {
s = s.substr(0, s.length()-1);
}
}
void Config::symbolExpand(string& s) {
symbolExpand(symbols, s);
}
void Config::envSymbolExpand(string& s) {
symbolExpand(envSymbols, s);
}
void Config::symbolExpand(map<string, string>& symbols, string& s) {
bool expanded;
do {
expanded = false;
for (map<string, string>::iterator i = symbols.begin(); i != symbols.end(); ++i) {
string search = "%" + i->first + "%";
string replace = i->second;
size_t pos = s.find(search);
if (pos != string::npos) {
expanded = true;
s.replace(pos, search.length(), replace);
}
}
} while (expanded);
}
string Config::pString(string name) {
map<string, string>::iterator i = symbols.find(name);
if (i == symbols.end()) {
logError(cout << "access of missing property '" << name << "' (" << debugInfo << ")" << endl);
exit(4);
}
return i->second;
}
bool Config::pBool(string name) {
string val = pString(name);
if ( (val == "yes") ||
(val == "Yes") ||
(val == "YES") ||
(val == "true") ||
(val == "True") ||
(val == "TRUE"))
{
return true;
}
return false;
}
double Config::pDouble(string name) {
string val = pString(name);
return atof(val.c_str());
}
int Config::pInt(string name) {
string val = pString(name);
return atoi(val.c_str());
} |
|
config.h
| C++ | 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
| #ifndef IncConfig
#define IncConfig
#include <string>
#include <map>
#include <list>
using namespace std;
/*
Config
Parse structured config files
Config files contains lines with name-value assignements in the form "<name> = <value>".
Trailing and leading whitespace is stripped. Parsed config entries are stored in
a symbol map.
Lines beginning with '#' are a comment and ignored.
Config files may be structured (to arbitrary depth). To start a new config sub group
(or sub section) use a line in the form of "<name> = (".
Subsequent entries are stured in the sub group, until a line containing ")" is found.
Values may reuse already defined names as a variable which gets expanded during
the parsing process. Names for expansion are searched from the current sub group
upwards. Finally the process environment is searched, so also environment
variables may be used as expansion symbols in the config file.
Errors and warnings are handled by emitting logging messages (see log.h/log.cpp)
or by calling exit() for severe errors. Depending on project needs this may be replaced
by exeptions, error return codes, ...
*/
class Config {
public:
/* Parse config file 'configFile'. If the process environment
* is provided, environment variables can be used as expansion symbols.
*/
Config(string configFile, char** envp = 0);
~Config();
// get string config entry
string pString(string name);
/* get boolean config entry
* A value of Yes/yes/YES/true/True/TRUE leads to true,
* all other values leads to false.
*/
bool pBool(string name);
// get double config entry; value is parsed using atof()
double pDouble(string name);
// get int config entry; value is parsed using atoi()
int pInt(string name);
// get the symbol map (e.g. for iterating over all symbols)
inline map<string, string>& getSymbols() {
return symbols;
}
// get config sub group
inline Config* group(string name) {
return groups[name];
}
// get config sub group map (e.g. for iterating over all groups)
inline map<string, Config*>& getGroups() {
return groups;
}
private:
// private constructor for sub groups
Config(string name, string parentDebugInfo);
// helper functions for parsing
void add(string name, string value);
void split(string in, string& left, string& right, char c);
void trim(string& s);
void symbolExpand(string& s);
void symbolExpand(map<string, string>& symbols, string& s);
void envSymbolExpand(string& s);
// config group symbol map
map<string, string> symbols;
// environment symbol map
map<string, string> envSymbols;
// config sub group map
map<string, Config*> groups;
// stack of config groups for parsing (only used in top config element)
list<Config*> groupStack;
// debug info used for logging messages
string debugInfo;
};
#endif |
|
log.cpp
| C++ | 1
2
3
| #include "log.h"
LogLevel logLevel = LOG_INFO; |
|
log.h
| C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
| #ifndef IncLog
#define IncLog
enum LogLevel { LOG_QUIET, LOG_ERROR, LOG_INFO, LOG_DEBUG };
extern LogLevel logLevel;
#define logError(A) ((logLevel >= LOG_ERROR)?((A),0):(0))
#define logInfo(A) ((logLevel >= LOG_INFO)?((A),0):(0))
#define logDebug(A) ((logLevel >= LOG_DEBUG)?((A),0):(0))
void debugBreak();
#endif |
|
0
|