-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathini.cpp
364 lines (302 loc) · 10.7 KB
/
ini.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
* The GINA Bot - a computer opponent for Valve's FPS game Half-Life
* Copyright (c) 2011, Wei Mingzhi <[email protected]>
*
* This file is part of The GINA Bot.
*
* The GINA Bot is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* The GINA Bot is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with The GINA Bot; if not, visit <http://www.gnu.org/licenses>.
*
* In addition, as a special exception, the author gives permission to
* link the code of this program with the Half-Life Game Engine ("HL
* Engine") and Modified Game Libraries ("MODs") developed by Valve,
* L.L.C ("Valve"). You must obey the GNU General Public License in all
* respects for all of the code used other than the HL Engine and MODs
* from Valve. If you modify this file, you may extend this exception
* to your version of the file, but you are not obligated to do so. If
* you do not wish to do so, delete this exception statement from your
* version.
*/
#include "ini.h"
void CIniFile::trim(char *str)
{
int pos = 0;
char *dest = str;
// skip leading blanks
while (str[pos] <= ' ' && str[pos] > 0)
pos++;
while (str[pos]) {
*(dest++) = str[pos];
pos++;
}
*(dest--) = '\0'; // store the null
// remove trailing blanks
while (dest >= str && *dest <= ' ' && *dest > 0)
*(dest--) = '\0';
}
CIniFile::CIniFile():
ini(NULL), key_count(0), current_size(0), m_Hash(NULL)
{
m_Hash = (ini_key_t **)calloc(INI_HASH_KEY_SIZE, sizeof(ini_key_t *));
if (m_Hash == NULL) {
printf("Memory allocation error !\n");
exit(1);
}
}
CIniFile::CIniFile(const char *filename):
ini(NULL), key_count(0), current_size(0), m_Hash(NULL)
{
m_Hash = (ini_key_t **)calloc(INI_HASH_KEY_SIZE, sizeof(ini_key_t *));
if (m_Hash == NULL) {
printf("Memory allocation error !\n");
exit(1);
}
Load(filename);
}
CIniFile::~CIniFile()
{
FreeAllTheStuff();
free(m_Hash);
}
void CIniFile::FreeAllTheStuff()
{
int i, j;
if (ini) {
// free all the memory allocated for this ini file
for (i = 0; i < key_count; i++) {
// delete all the values in this key...
for (j = 0; j < ini[i].value_count; j++) {
free(ini[i].values[j].value);
free(ini[i].values[j].value_name);
}
free(ini[i].values);
free(ini[i].key_name);
}
free(ini);
ini = NULL;
}
// null out the hash table
memset(m_Hash, 0, sizeof(ini_key_t *) * INI_HASH_KEY_SIZE);
}
void CIniFile::Load(const char *filename)
{
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("cannot load ini file %s\n", filename);
return;
}
// if we already have a file loaded, free it first
if (Valid()) {
FreeAllTheStuff();
}
char str[256], section[256];
section[0] = '\0';
while (fgets(str, 256, fp)) {
trim(str); // trim all the blanks or linefeeds
// skip all comment lines or empty lines
if (!str[0] || str[0] == ';' || str[0] == '/' || str[0] == '#')
continue;
int length = strlen(str);
char *p;
// check if this is a session line (e.g., [SECTION])
if (str[0] == '[' && str[length - 1] == ']') {
strcpy(section, &str[1]);
section[length - 2] = 0; // remove the ]
trim(section); // trim section name after removing []
} else if ((p = strchr(str, '=')) != NULL) {
*(p++) = '\0';
trim(str);
trim(p);
Set(section, str, p);
}
}
fclose(fp); // load completed; close the file
}
void CIniFile::Save(const char *filename)
{
FILE *fp = fopen(filename, "w");
if (fp == NULL) {
printf("cannot save to INI file: %s\n", filename);
return;
}
fprintf(fp, "; %s\n\n", filename);
for (int i = 0; i < key_count; i++) {
fprintf(fp, "[%s]\n", ini[i].key_name); // write the key name
// write all the values...
for (int j = 0; j < ini[i].value_count; j++) {
fprintf(fp, "%s=%s\n", ini[i].values[j].value_name, ini[i].values[j].value);
}
fprintf(fp, "\n"); // add a line feed
}
fclose(fp); // save completed; close the file
}
// set the value in ini file
void CIniFile::Set(const char *key, const char *value, const char *set)
{
ini_key_t *pKey = NULL;
ini_value_t *pValue = NULL;
int i;
// search if this key already exists...
// first search in the hash table...
unsigned short vhash = GetHashValue(key) % INI_HASH_KEY_SIZE;
if (m_Hash[vhash] != NULL && strcasecmp(key, m_Hash[vhash]->key_name) == 0) {
pKey = m_Hash[vhash]; // found this value in the hash table
}
if (pKey == NULL) {
// not found in the hash table, do a normal search...
for (i = 0; i < key_count; i++) {
if (strcasecmp(ini[i].key_name, key) == 0) {
pKey = &ini[i];
break;
}
}
}
// if this is a new key, try to allocate memory for it
if (pKey == NULL) {
key_count++;
// if we don't have enough room for this new key, try to allocate more memory
if (key_count > current_size) {
current_size += INI_SIZE_INCREMENT;
if (ini) {
ini = (ini_key_t *)realloc(ini, sizeof(ini_key_t) * current_size);
} else {
ini = (ini_key_t *)malloc(sizeof(ini_key_t) * current_size);
}
if (!ini) {
printf("Memory allocation error !");
exit(1);
}
}
pKey = &ini[key_count - 1];
pKey->key_name = strdup(key);
pKey->values = NULL;
pKey->value_count = 0;
pKey->current_size = 0;
memset(pKey->hash, 0, sizeof(pKey->hash)); // zero out the hash table
// store this new key in the hash table...
unsigned short vhash = GetHashValue(pKey->key_name) % INI_HASH_KEY_SIZE;
m_Hash[vhash] = pKey;
}
// search if the value is already in the key...
vhash = GetHashValue(value) % INI_HASH_VALUE_SIZE;
if (pKey->hash[vhash] != NULL && strcasecmp(value, pKey->hash[vhash]->value_name) == 0) {
pValue = pKey->hash[vhash]; // we have found the value in the hash table
}
if (pValue == NULL) {
// value is not found in the hash table, do a normal search...
for (i = 0; i < pKey->value_count; i++) {
if (strcasecmp(value, pKey->values[i].value_name) == 0) {
// we have found the value
pValue = &pKey->values[i];
break;
}
}
}
if (pValue != NULL) {
// this value already exists in the key...
free(pValue->value);
pValue->value = strdup(set);
if (pValue->value == NULL) {
printf("Memory allocation error !");
exit(1);
}
} else {
// this is a new value...
pKey->value_count++;
// if we don't have enough room for this new value, try to allocate more memory
if (pKey->value_count > pKey->current_size) {
pKey->current_size += INI_SIZE_INCREMENT;
if (pKey->values) {
pKey->values = (ini_value_t *)realloc(pKey->values, sizeof(ini_value_t) * pKey->current_size);
} else {
pKey->values = (ini_value_t *)malloc(sizeof(ini_value_t) * pKey->current_size);
}
if (pKey->values == NULL) {
printf("Memory allocation error !");
exit(1);
}
}
pKey->values[pKey->value_count - 1].value_name = strdup(value);
pKey->values[pKey->value_count - 1].value = strdup(set);
if (pKey->values[pKey->value_count - 1].value == NULL ||
pKey->values[pKey->value_count - 1].value_name == NULL) {
printf("Memory allocation error !");
exit(1);
}
trim(pKey->values[pKey->value_count - 1].value);
trim(pKey->values[pKey->value_count - 1].value_name);
// store this new value in the hash table
unsigned short vhash = GetHashValue(pKey->values[pKey->value_count - 1].value_name) % INI_HASH_VALUE_SIZE;
pKey->hash[vhash] = &pKey->values[pKey->value_count - 1];
}
}
// get a value from the ini file
const char *CIniFile::Get(const char *key, const char *value, const char *def)
{
ini_key_t *pKey = NULL;
int i;
// search for the key name...
// first search in the hash table...
unsigned short vhash = GetHashValue(key) % INI_HASH_KEY_SIZE;
if (m_Hash[vhash] != NULL && strcasecmp(key, m_Hash[vhash]->key_name) == 0) {
pKey = m_Hash[vhash]; // found this value in the hash table
}
if (pKey == NULL) {
// Not found in the hash table, do a normal search...
for (i = 0; i < key_count; i++) {
if (strcasecmp(ini[i].key_name, key) == 0) {
pKey = &ini[i];
break;
}
}
}
if (pKey != NULL) {
// key found, search for the value in this key...
vhash = GetHashValue(value) % INI_HASH_VALUE_SIZE;
if (pKey->hash[vhash] != NULL && strcasecmp(value, pKey->hash[vhash]->value_name) == 0) {
return pKey->hash[vhash]->value;
}
// not found in the hash table, do a normal linear search...
for (i = 0; i < pKey->value_count; i++) {
if (strcasecmp(pKey->values[i].value_name, value) == 0) {
return pKey->values[i].value;
}
}
}
return def; // value is not found; use default value
}
/**
* This hash function has been taken from an Article in Dr Dobbs Journal.
* This is normally a collision-free function, distributing keys evenly.
* Collision can be avoided by comparing the key itself in last resort.
*/
unsigned short CIniFile::GetHashValue(const char *sz)
{
unsigned short hash = 0;
while (*sz) {
// convert all the characters to be upper case, so
// that it will be case insensitive
char a = *sz;
if (a >= 'a' && a <= 'z') {
a -= 'a' - 'A';
}
hash += (unsigned short)a;
hash += (hash << 10);
hash ^= (hash >> 6);
sz++;
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}