-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.cpp
163 lines (139 loc) · 4.02 KB
/
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
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
#include <windows.h>
#include "str.h"
#include "config0.h"
#define MAX_CONFIG_SIZE (10 * 1024 * 1024)
void Config0::Init(void)
{
}
void Config0::Uninit(void)
{
}
bool Config0::_ParseFile(LPWSTR pszFileName, CFGDATA *pCD)
{
bool r = false;
Mem::_zero(pCD, sizeof(CFGDATA));
//Open the file.
Fs::MEMFILE fl;
if(Fs::_fileToMem(pszFileName, &fl, 0))
{
if(fl.size <= MAX_CONFIG_SIZE)
{
LPSTR pCur = (LPSTR)fl.data;
LPSTR pEnd = pCur + fl.size;
bool error = false;
VAR **pTree = NULL;
DWORD dwTree = 0;
DWORD dwStr = 0;
while(!error && pCur < pEnd)
{
//Ischim end of the line.
LPSTR pp = pCur;
while(*pp != '\n' && *pp != '\r' && pp < pEnd)pp++;
//Obtain the arguments.
LPSTR *pArgs = NULL;
DWORD dwArgsCount = Str::_getArgumentsA(pCur, (DWORD)(pp - pCur), &pArgs, 0);
if(dwArgsCount > 0 && dwArgsCount != (DWORD)-1 && pArgs[0][0] != 0 && pArgs[0][0] != ';')
{
//End of the enumeration of descendants.
if(CWA(kernel32, lstrcmpiA)(CONFIG0_NAME_SECTION_END, pArgs[0]) == 0)
{
if(dwTree == 0)error = true;
else dwTree--;
Mem::freeArrayOfPointers(pArgs, dwArgsCount);
}
else
{
//Creating a variable.
VAR *pNewVar = _AddVar(dwTree ? pTree[dwTree - 1] : NULL, pCD, pArgs, (BYTE)dwArgsCount);
if(pNewVar == NULL)
{
Mem::freeArrayOfPointers(pArgs, dwArgsCount);
error = true;
}
//Start listing of descendants.
else if(CWA(kernel32, lstrcmpiA)(CONFIG0_NAME_SECTION_BEGIN, pArgs[0]) == 0)
{
if(!Mem::reallocEx(&pTree, sizeof(VAR *) * (dwTree + 1)))error = true;
else pTree[dwTree++] = pNewVar;
}
}
}
else if(dwArgsCount != (DWORD)-1)Mem::freeArrayOfPointers(pArgs, dwArgsCount);
pCur = pp + 1;
dwStr++;
}
Mem::free(pTree);
if(error)_Close(pCD);
else r = true;
}
Fs::_closeMemFile(&fl);
}
return r;
}
void Config0::_Close(CFGDATA *pCD)
{
while(pCD->dwVarsCount--)_FreeVar(&pCD->pVars[pCD->dwVarsCount]);
Mem::free(pCD->pVars);
Mem::_zero(pCD, sizeof(CFGDATA));
}
void Config0::_FreeVar(VAR *pVar)
{
Mem::freeArrayOfPointers(pVar->pValues, pVar->bValuesCount);
pVar->pValues = NULL;
while(pVar->dwChildsCount--)_FreeVar(&pVar->pChilds[pVar->dwChildsCount]);
Mem::free(pVar->pChilds);
pVar->pChilds = NULL;
}
Config0::VAR *Config0::_AddVar(VAR *pvParent, CFGDATA *pCD, LPSTR *pValues, BYTE bValuesCount)
{
if(pValues != NULL && bValuesCount != 0 && (pvParent != NULL || pCD != NULL))
{
LPDWORD pdwVarsCount;
VAR **ppVars;
if(pvParent)
{
pdwVarsCount = &pvParent->dwChildsCount;
ppVars = &pvParent->pChilds;
}
else
{
pdwVarsCount = &pCD->dwVarsCount;
ppVars = &pCD->pVars;
}
if(Mem::reallocEx(ppVars, sizeof(VAR) * ((*pdwVarsCount) + 1)))
{
VAR *pNew = &(*ppVars)[(*pdwVarsCount)++];
pNew->pValues = pValues;
pNew->bValuesCount = bValuesCount;
pNew->dwChildsCount = 0;
pNew->pChilds = NULL;
return pNew;
}
}
return NULL;
}
Config0::VAR *Config0::_GetVar(VAR *pvParent, CFGDATA *pCD, LPSTR pstrName, LPSTR pstrValue)
{
DWORD dwVarsCount;
VAR *pVars;
if(pvParent)
{
dwVarsCount = pvParent->dwChildsCount;
pVars = pvParent->pChilds;
}
else
{
dwVarsCount = pCD->dwVarsCount;
pVars = pCD->pVars;
}
if(pstrName == NULL)pstrName = CONFIG0_NAME_SECTION_BEGIN;
for(DWORD i = 0; i < dwVarsCount; i++)if(pVars[i].bValuesCount > 0 && CWA(kernel32, lstrcmpiA)(pstrName, pVars[i].pValues[0]) == 0)
{
if(pstrValue)
{
if(pVars[i].bValuesCount > 1 && CWA(kernel32, lstrcmpiA)(pstrValue, pVars[i].pValues[1]) == 0)return &pVars[i];
}
else return &pVars[i];
}
return NULL;
}