-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathINI_Parser.cpp
260 lines (259 loc) · 6.02 KB
/
INI_Parser.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "INI_Parser.h"
#define activevar active->active
INI::Group* INI::DelGroup(INI::Group*cur,INI::Group*trgt) {
if (!cur) return 0;
if (cur==trgt) return cur->next;
cur->next=DelGroup(cur->next,trgt);
return cur;
}
INI::Var* INI::DelVar(INI::Var*cur,INI::Var*trgt) {
if (!cur) return 0;
if (cur==trgt) return cur->next;
cur->next=DelVar(cur->next,trgt);
return cur;
}
int INI::Hash(XS str) {
str=str.ToLower();
int h;
for(int i=0;i<str.Length();i++) h=(h+str[i])%253;
return h;
}
INI::INI() {
root=active=0;
}
INI::~INI() {
if (!root) return;
active=root;
Group*gt;
Var*vt;
do {
gt=active->next;
do {
vt=activevar->next;
delete activevar;
} while (activevar=vt);
delete active;
} while(active=gt);
}
bool INI::Load(const XS&name) {
static char automat[6][5]={
/*
0 - Èìÿ ïåðåìåííîé, 1 - Îæèäàíèå ñòðîêè
2 - Ñòðîêà ñ ', 3 - Ñòðîêà ñ "
4 - Ñòðîêà áåç " è ', (5 - êîíåö ñòðîêè
6 - ïîëó÷åíà ñòðîêà ñ ', 7 - ïîëó÷åíà ñòðîêà ñ ")
*/
1, 4, 2, 3, 4, // =
-1, 6, 5, 3, 4, // '
-1, 7, 2, 5, 4, // "
-1, 5, 2, 3, 5, // ; èëè #
0, 4, 2, 3, 4, // Ïðî÷èå ñèìâîëû
-1, 5,-1,-1, 5 // Êîíåö ñòðîêè
};
FILE *file;
int line=0;
char buf[1025],ins,state=0;
XS str;
if (!(file=fopen(name.c_str(),"r"))) return false;
while (fgets(buf,1024,file)) {
str=XS(buf).Trim();
XS vn,vs;
if (str[0]=='['&&str.Last()==']') AddGroup(str.SubStr(1,-1));
else for(int i=0;i<=str.Length();i++) {
if (i==str.Length()) ins=5;
else switch (str[i]) {
case '=': ins=0; break;
case '\'': ins=1; break;
case '"': ins=2; break;
case ';': case '#': ins=3; break;
default: ins=4;
}
state=automat[ins][state];
switch (state) {
case -1: MessageBox(0,(XS("Incorrect sequense on line ")+line).c_str(),
"INI Parser Warning",MB_ICONWARNING); i=str.Length(); state=0; break;
case 0: vn+=str[i];
case 1: break;
case 2: case 3: case 4: vs+=str[i]; break;
case 5: AddVar(vn.Trim()); if (!str.IsEmpty()) SetString(vs);
i=str.Length(); state=0; break;
case 6: case 7: state-=4; break;
}
}
line++;
}
fclose(file);
return true;
}
bool INI::Save(const XS&name) {
FILE *file=fopen(name.c_str(),"w");
Group *gt=root;
Var *vt=root->root;
if (!file) return false;
do {
fwrite(("["+gt->name+"]").c_str(),1,gt->name.Length(),file);
do fwrite((vt->name+"="+vt->value+"\n").c_str(),1,
vt->name.Length()+vt->value.Length()+2,file);
while (vt=vt->next);
} while(gt=gt->next);
fclose(file);
return true;
}
void INI::DelGroup() {
root=DelGroup(root,active);
active=root;
}
void INI::DelVar() {
active->root=DelVar(active->root,active->active);
activevar=active->root;
}
bool INI::FirstGroup() {
if (!root) return false;
active=root;
return true;
}
bool INI::FirstVar() {
if (!active||!activevar) return false;
activevar=active->root;
return true;
}
bool INI::NextGroup() {
if (!active||!active->next) return false;
active=active->next;
return true;
}
bool INI::NextVar() {
if (!active||!activevar||!activevar->next) return false;
activevar=activevar->next;
return true;
}
bool INI::GetGroup(XS&dest) {
if (!active) return false;
dest=active->name;
return true;
}
bool INI::GetVar(XS&dest) {
if (!active||!activevar) return false;
dest=activevar->name;
return true;
}
bool INI::AddGroup(const XS&name) {
int hash=Hash(name);
for(Group *gt=root;gt;gt=gt->next)
if (gt->hash==hash&>->name.Cmpi(name)) return false;
Group*group=new Group;
group->name=name;
group->hash=hash;
group->active=group->root=0;
if (active) {
group->next=active->next;
active->next=group;
active=group;
return true;
}
group->next=root;
active=root=group;
return true;
}
bool INI::AddVar(const XS&name) {
if (!active) return false;
int hash=Hash(name);
for(Var *var=active->root;var;var=var->next)
if (var->hash==hash&&var->name.Cmpi(name)) return false;
var=new Var;
var->name=name;
var->hash=hash;
if (active->active) {
var->next=activevar->next;
activevar->next=var;
activevar=var;
return true;
}
var->next=active->root;
activevar=active->root=var;
return true;
}
bool INI::RenGroup(const XS&name) {
if (!active) return false;
active->name=name;
active->hash=Hash(name);
return true;
}
bool INI::RenVar(const XS&name) {
if (!active||!activevar) return false;
activevar->name=name;
activevar->hash=Hash(name);
return true;
}
bool INI::SetBool(const bool&val) {
if (!active||!activevar) return false;
if (val) activevar->value="true";
else activevar->value="false";
return true;
}
bool INI::SetInt(const long&val) {
if (!active||!activevar) return false;
char buf[12];
buf[sprintf(buf,"%ld",val)]=0;
activevar->value=buf;
return true;
}
bool INI::SetFloat(const double&val) {
if (!active||!activevar) return false;
char buf[15];
buf[sprintf(buf,"%ld",val)]=0;
activevar->value=buf;
return true;
}
bool INI::SetString(const XS&str) {
if (!active||!activevar) return false;
activevar->value=str;
return true;
}
bool INI::GetBool(bool&var) {
if (!active||!activevar) return false;
XS &str=activevar->value;
if (!str.IsEmpty()) switch (str[0]) {
case 'T': case 't': case 'Y': case 'y': case '1':
var=true; break;
case 'F': case 'f': case 'N': case 'n': case '0':
var=false; break;
default:
return false;
}
else var=false;
return true;
}
bool INI::GetInt(long&var) {
if (!active||!activevar) return false;
sscanf(activevar->value.c_str(),"%ld",&var);
return true;
}
bool INI::GetFloat(double&var) {
if (!active||!activevar) return false;
sscanf(activevar->value.c_str(),"%Le",&var);
return true;
}
bool INI::GetString(XS&str) {
if (!active||!activevar) return false;
str=activevar->value;
return true;
}
bool INI::SelectGroup(const XS&name) {
Group *gt=active;
active=root;
int hash=Hash(name);
while(active) if (active->hash==hash&&active->name.Cmpi(name)) return true;
active=gt;
return false;
}
bool INI::SelectVar(const XS&name) {
if (!active) return false;
Var *vt=activevar;
activevar=active->root;
int hash=Hash(name);
while(activevar) if (activevar->hash==hash&&activevar->name.Cmpi(name)) return true;
activevar=vt;
return false;
}
#undef activevar