-
Notifications
You must be signed in to change notification settings - Fork 6
/
csdm_config.cpp
123 lines (107 loc) · 2.43 KB
/
csdm_config.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
#include "amxxmodule.h"
#include "csdm_amxx.h"
#include "csdm_config.h"
Config g_Config;
void fstrncpy(char *dest, const char *src, size_t length)
{
while (length-- && *src)
*dest++ = *src++;
*dest = '\0';
}
void Config::AddHook(const char *section, int forward)
{
cfghook *pHook = new cfghook;
pHook->forward = forward;
pHook->section.assign(section);
m_Hooks.push_back(pHook);
}
Config::~Config()
{
Clear();
}
void Config::Clear()
{
List<cfghook *>::iterator iter;
for (iter=m_Hooks.begin(); iter!=m_Hooks.end(); iter++)
{
if ( (*iter) )
delete (*iter);
}
m_Hooks.clear();
}
CfgError Config::ReadConfig(const char *file)
{
FILE *fp = fopen(file, "rt");
if (!fp)
return Config_NoFile;
cfghook *pHook;
List<cfghook *>::iterator iter;
char buffer[255];
char section[32] = {0};
String temp;
size_t length = 0;
const char *ptr;
while (!feof(fp))
{
buffer[0] = '\0';
fgets(buffer, sizeof(buffer)-1, fp);
if (buffer[0] == '\0' || buffer[0] == '\n')
continue;
if (buffer[0] == ';' || (buffer[0] == '\\' || buffer[1] == '\\'))
continue;
length = strlen(buffer);
if (buffer[length-1] == '\n')
buffer[--length] = '\0';
temp.assign(buffer);
temp.trim();
if (temp.size() < 1)
continue;
ptr = temp.c_str();
if (ptr[0] == '[')
{
size_t pos = 0;
//quick state machine!11 :o
for (size_t i=1; i<length; i++)
{
if (ptr[i] == ']')
pos = i;
}
if (pos < 2)
continue; //invalid almost-section
if (section[0] != '\0')
{
for (iter=m_Hooks.begin(); iter!=m_Hooks.end(); iter++)
{
pHook = (*iter);
if (pHook->section.compare(section)==0)
MF_ExecuteForward(pHook->forward, (cell)CFG_DONE, "", section);
}
}
fstrncpy(section, &(ptr[1]), pos-1);
for (iter=m_Hooks.begin(); iter!=m_Hooks.end(); iter++)
{
pHook = (*iter);
if (pHook->section.compare(section)==0)
MF_ExecuteForward(pHook->forward, (cell)CFG_RELOAD, "", section);
}
} else if (section[0] != '\0') {
for (iter=m_Hooks.begin(); iter!=m_Hooks.end(); iter++)
{
pHook = (*iter);
if (pHook->section.compare(section)==0)
MF_ExecuteForward(pHook->forward, (cell)CFG_READ, ptr, section);
}
}
}
if (section[0] != '\0')
{
for (iter=m_Hooks.begin(); iter!=m_Hooks.end(); iter++)
{
pHook = (*iter);
if (pHook->section.compare(section)==0)
MF_ExecuteForward(pHook->forward, (cell)CFG_DONE, "", section);
}
}
fclose(fp);
return Config_Ok;
}