-
Notifications
You must be signed in to change notification settings - Fork 4
/
configreader.cpp
61 lines (52 loc) · 1.25 KB
/
configreader.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
#include "configreader.h"
#include <ctype.h>
bool configreader::parse(char * data)
{
this->items.reset();
hashmap<string,sub_inner>* group=&this->items.get("").items;
while (data)
{
char * nextline=strchr(data, '\n');
if (nextline) { *nextline='\0'; nextline++; }
while (isspace(*data)) data++;
if (*data=='#' || *data==';' || *data=='\0') goto next;
char * lineend;
lineend=strchr(data, '\0');
while (isspace(lineend[-1])) lineend--;
*lineend='\0';
if (*data=='[')
{
if (lineend[-1]==']' && (isalpha(data[1]) || data[1]=='_'))
{
lineend[-1]='\0';
group=&this->items.get(data+1).items;
puts(data+1);
}
else goto fail;
goto next;
}
if (isalpha(*data) || *data=='_')
{
char * keyend=data;
while (isalnum(*keyend) || *keyend=='_') keyend++;
char * valstart=keyend;
while (isspace(*valstart)) valstart++;
if (*valstart!='=') goto fail;
valstart++;
while (isspace(*valstart)) valstart++;
*keyend='\0';
if (group) group->set(data, valstart);
else goto fail;
goto next;
}
goto fail;
next:
data=nextline;
}
set_group(NULL);
return true;
fail:
this->items.reset();
this->group=NULL;
return false;
}