This repository has been archived by the owner on Nov 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
config.c
294 lines (228 loc) · 5.96 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
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
/*
VitaShell
Copyright (C) 2015-2016, TheFloW
This program 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.
This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "main.h"
#include "file.h"
#include "config.h"
void trim(char *str) {
int len = strlen(str);
int i;
for (i = len - 1; i >= 0; i--) {
if (str[i] == 0x20 || str[i] == '\t') {
str[i] = 0;
} else {
break;
}
}
}
int GetLine(char *buf, int size, char *str) {
uint8_t ch = 0;
int n = 0;
int i = 0;
uint8_t *s = (uint8_t *)str;
while (1) {
if (i >= size)
break;
ch = ((uint8_t *)buf)[i];
if (ch < 0x20 && ch != '\t') {
if (n != 0) {
i++;
break;
}
} else {
*str++ = ch;
n++;
}
i++;
}
trim((char *)s);
return i;
}
int getDecimal(char *str) {
return strtol(str, NULL, 0);
}
int getHexdecimal(char *str) {
return strtoul(str, NULL, 16);
}
int getBoolean(char *str) {
if (strcasecmp(str, "false") == 0 || strcasecmp(str, "off") == 0 || strcasecmp(str, "no") == 0)
return 0;
if (strcasecmp(str, "true") == 0 || strcasecmp(str, "on") == 0 || strcasecmp(str, "yes") == 0)
return 1;
return -1;
}
char *getString(char *str) {
if (str[0] != '"')
return NULL;
char *p = strchr(str + 1, '"');
if (!p)
return NULL;
int len = p - (str + 1);
char *out = malloc(len + 1);
strncpy(out, str + 1, len);
out[len] = '\0';
int i;
for (i = 0; i < len; i++) {
if (out[i] == '\\')
out[i] = '\n';
}
return out;
}
int readEntry(char *line, ConfigEntry *entries, int n_entries) {
// Trim at beginning
while (*line == ' ' || *line == '\t')
line++;
// Ignore comments #1
if (line[0] == '#') {
// debugPrintf("IGNORE %s\n", line);
return 0;
}
// Ignore comments #2
char *p = strchr(line, '#');
if (p) {
// debugPrintf("IGNORE %s\n", p);
*p = '\0';
}
// Get token
p = strchr(line, '=');
if (!p)
return -1;
// Name of entry
char name[MAX_CONFIG_NAME_LENGTH];
int len = p - line;
if (len > MAX_CONFIG_NAME_LENGTH - 1)
len = MAX_CONFIG_NAME_LENGTH - 1;
strncpy(name, line, len);
name[len] = '\0';
trim(name);
// debugPrintf("NAME: %s\n", name);
// String of entry
char *string = p + 1;
// Trim at beginning
while (*string == ' ' || *string == '\t')
string++;
// Trim at end
trim(string);
// debugPrintf("STRING: %s\n", string);
int i;
for (i = 0; i < n_entries; i++) {
if (strcasecmp(name, entries[i].name) == 0) {
switch (entries[i].type) {
case CONFIG_TYPE_DECIMAL:
*(uint32_t *)entries[i].value = getDecimal(string);
// debugPrintf("VALUE DECIMAL: %d\n", *(uint32_t *)entries[i].value);
break;
case CONFIG_TYPE_HEXDECIMAL:
*(uint32_t *)entries[i].value = getHexdecimal(string);
// debugPrintf("VALUE HEXDECIMAL: 0x%X\n", *(uint32_t *)entries[i].value);
break;
case CONFIG_TYPE_BOOLEAN:
*(uint32_t *)entries[i].value = getBoolean(string);
// debugPrintf("VALUE BOOLEAN: %d\n", *(uint32_t *)entries[i].value);
break;
case CONFIG_TYPE_STRING:
{
char *value = getString(string);
if (value) {
*(uint32_t *)entries[i].value = (uint32_t)value;
// debugPrintf("VALUE STRING: %s\n", *(uint32_t *)entries[i].value);
}
break;
}
}
break;
}
}
return 1;
}
int readConfigBuffer(void *buffer, int size, ConfigEntry *entries, int n_entries) {
int res = 0;
char line[MAX_CONFIG_LINE_LENGTH];
char *p = buffer;
// Skip UTF-8 bom
uint32_t bom = 0xBFBBEF;
if (memcmp(p, &bom, 3) == 0) {
p += 3;
size -= 3;
}
do {
memset(line, 0, sizeof(line));
res = GetLine(p, size, line);
if (res > 0) {
readEntry(line, entries, n_entries);
size -= res;
p += res;
}
} while (res > 0);
return 0;
}
int readConfig(char *path, ConfigEntry *entries, int n_entries) {
void *buffer = NULL;
int size = allocateReadFile(path, &buffer);
if (size < 0)
return size;
readConfigBuffer(buffer, size, entries, n_entries);
free(buffer);
return 0;
}
int writeEntry(SceUID fd, ConfigEntry *entry) {
int result;
if ((result = sceIoWrite(fd, entry->name, strlen(entry->name))) < 0)
return result;
if ((result = sceIoWrite(fd, " = ", 3)) < 0)
return result;
char *val;
char buffer[33];
switch (entry->type) {
case CONFIG_TYPE_BOOLEAN:
val = *(uint32_t *)entry->value != 0 ? "true" : "false";
result = sceIoWrite(fd, val, strlen(val));
break;
case CONFIG_TYPE_DECIMAL:
itoa(*(int *)entry->value, buffer, 10);
result = sceIoWrite(fd, buffer, strlen(buffer));
break;
case CONFIG_TYPE_HEXDECIMAL:
itoa(*(int *)entry->value, buffer, 16);
result = sceIoWrite(fd, buffer, strlen(buffer));
break;
case CONFIG_TYPE_STRING:
val = *(char **)entry->value;
sceIoWrite(fd, "\"", 1);
result = sceIoWrite(fd, val, strlen(val));
sceIoWrite(fd, "\"", 1);
break;
default:
return 1;
}
if (result < 0)
return result;
if ((sceIoWrite(fd, "\n", 1)) < 0)
return result;
return 0;
}
int writeConfig(char *path, ConfigEntry *entries, int n_entries) {
SceUID fd = sceIoOpen(path, SCE_O_WRONLY | SCE_O_CREAT | SCE_O_TRUNC, 0777);
if (fd < 0)
return fd;
int i;
for (i = 0; i < n_entries; i++) {
int result = writeEntry(fd, entries + i);
if (result != 0) {
return result;
}
}
sceIoClose(fd);
return 0;
}