-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.c
256 lines (180 loc) · 5.08 KB
/
config.c
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
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "message.h"
#include "defs.h"
static FILE* cfgfile = NULL;
static long section_offset = 0;
static void separateValue(char* str);
static bool checkPattern(char* pattern, char* str);
static void trimString(char *str)
{
register char *start = str;
register char *end = str + strlen(str) - 1;
while(*start == ' ') start++;
while((*end == '\n' || *end == ' ' || *end == '\r') && (end != start)) end--;
unsigned int sz = end - start + 1;
memmove(str, start, sz);
str[sz] = '\0';
}
static bool checkPattern(char *pattern, char *str)
{
register char *cs = str;
register char *cp = pattern;
while(*cs == ' ') cs++;
while(*cs != '\0' && *cp != '\0' && *cp == *cs) cp++,cs++;
if((cp - pattern) == strlen(pattern))
return true;
return false;
}
static void separateValue(char *str)
{
register char *cs = str;
while(*cs != '=' && *cs != '\0') cs++;
if(*cs == '\0')
{
message_WarningEx("config: Could not find value in '%s'.\n", str);
return;
}
cs++;
register char *cr = str;
while(*cs != '\n' && *cs != '\0' && *cs != '\r')
{
*cr = *cs;
cs++,cr++;
}
*cr = '\0';
}
bool cfg_Open(const char *filepath)
{
if((cfgfile = fopen(filepath, "r")) == NULL)
{
message_CriticalErrorEx("config: Could not open file '%s'.\n", filepath);
return false;
}
return true;
}
void cfg_Close()
{
if(cfgfile != NULL)
fclose(cfgfile);
else
message_Warning("config: Trying to close file, but none opened.\n");
}
bool cfg_SeekSection(char *sectionName)
{
char buffer[_STR_BUFLEN];
char pattern[_STR_BUFLEN];
bool found = false;
sprintf(pattern, "[%s]", sectionName);
rewind(cfgfile);
do fgets(buffer, _STR_BUFLEN, cfgfile);
while(!feof(cfgfile) && !(found = checkPattern(pattern, buffer)));
if(found)
{
section_offset = ftell(cfgfile);
return true;
}
message_WarningEx("config: Could not find section '%s'.\n", pattern);
return false;
}
bool cfg_NextSection(char *sectionName)
{
char buffer[_STR_BUFLEN];
char pattern[_STR_BUFLEN];
int found = false;
sprintf(pattern, "[");
do fgets(buffer, _STR_BUFLEN, cfgfile);
while(!feof(cfgfile) && !(found = checkPattern(pattern, buffer)));
if(found)
{
char *start = buffer + 1;
char *end = strrchr(start, ']') - 1;
memcpy(sectionName, start, end - start + 1);
sectionName[end - start + 1] = '\0';
section_offset = ftell(cfgfile);
return true;
}
return false;
}
bool cfg_GetIntValue(char *propertyName, int *value)
{
char buffer[_STR_BUFLEN];
if(cfg_GetStringValue(propertyName, buffer))
{
int val;
if(sscanf(buffer, "%d", &val) < 1)
{
message_WarningEx("config: The value '%s=%s' is not an integer.\n", propertyName, buffer);
return false;
}
*value = val;
return true;
}
message_WarningEx("config: Could not find value '%s' within current section.\n", propertyName);
return false;
}
bool cfg_GetDoubleValue(char *propertyName, double *value)
{
char buffer[_STR_BUFLEN];
if(cfg_GetStringValue(propertyName, buffer))
{
double val;
if(sscanf(buffer, "%lf", &val) < 1)
{
message_WarningEx("config: The value '%s=%s' is not a double.\n", propertyName, buffer);
return false;
}
*value = val;
return true;
}
message_WarningEx("config: Could not find value '%s' within current section.\n", propertyName);
return false;
}
bool cfg_FindPattern(char *pattern, char *buffer)
{
bool found = false;
fseek(cfgfile, section_offset, SEEK_SET);
do fgets(buffer, _STR_BUFLEN, cfgfile);
while(!feof(cfgfile) && !checkPattern("[", buffer) && !(found = checkPattern(pattern, buffer)));
return found;
}
bool cfg_GetStringValue(char *propertyName, char *str)
{
char buffer[_STR_BUFLEN];
char pattern[_STR_BUFLEN];
sprintf(pattern, "%s=", propertyName);
if(cfg_FindPattern(pattern, buffer))
{
separateValue(buffer);
trimString(buffer);
strcpy(str, buffer);
return true;
}
message_WarningEx("config: Could not find value '%s' within current section.\n", propertyName);
return false;
}
bool cfg_GetTag(char *tagName)
{
char buffer[_STR_BUFLEN];
char pattern[_STR_BUFLEN];
sprintf(pattern, "+%s", tagName);
return cfg_FindPattern(pattern, buffer);
}
bool cfg_GetBool(char *propertyName)
{
char buffer[_STR_BUFLEN];
char pattern[_STR_BUFLEN];
sprintf(pattern, "%s=", propertyName);
if(cfg_FindPattern(pattern, buffer))
{
separateValue(buffer);
trimString(buffer);
if(strcmp(buffer, "1") == 0 || strcmp(buffer, "yes") == 0 ||
strcmp(buffer, "y") == 0 || strcmp(buffer, "true") == 0 ||
strcmp(buffer, "t") == 0)
return true;
}
return false;
}