-
Notifications
You must be signed in to change notification settings - Fork 162
/
pkgi_config.c
257 lines (230 loc) · 6.04 KB
/
pkgi_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
#include "pkgi_config.h"
#include "pkgi.h"
static char* skipnonws(char* text, char* end)
{
while (text < end && *text != ' ' && *text != '\n' && *text != '\r')
{
text++;
}
return text;
}
static char* skipws(char* text, char* end)
{
while (text < end && (*text == ' ' || *text == '\n' || *text == '\r'))
{
text++;
}
return text;
}
static DbSort parse_sort(const char* value, DbSort sort)
{
if (pkgi_stricmp(value, "title") == 0)
{
return SortByTitle;
}
else if (pkgi_stricmp(value, "region") == 0)
{
return SortByRegion;
}
else if (pkgi_stricmp(value, "name") == 0)
{
return SortByName;
}
else if (pkgi_stricmp(value, "size") == 0)
{
return SortBySize;
}
else
{
return sort;
}
}
static DbSortOrder parse_order(const char* value, DbSortOrder order)
{
if (pkgi_stricmp(value, "asc") == 0)
{
return SortAscending;
}
else if (pkgi_stricmp(value, "desc") == 0)
{
return SortDescending;
}
else
{
return order;
}
}
static DbSortOrder parse_filter(char* value, uint32_t filter)
{
uint32_t result = 0;
char* start = value;
for (;;)
{
char ch = *value;
if (ch == 0 || ch == ',')
{
*value = 0;
if (pkgi_stricmp(start, "ASA") == 0)
{
result |= DbFilterRegionASA;
}
else if (pkgi_stricmp(start, "EUR") == 0)
{
result |= DbFilterRegionEUR;
}
else if (pkgi_stricmp(start, "JPN") == 0)
{
result |= DbFilterRegionJPN;
}
else if (pkgi_stricmp(start, "USA") == 0)
{
result |= DbFilterRegionUSA;
}
else
{
return filter;
}
if (ch == 0)
{
break;
}
value++;
start = value;
}
else
{
value++;
}
}
return result;
}
void pkgi_load_config(Config* config, char* refresh_url, uint32_t refresh_len)
{
refresh_url[0] = 0;
config->sort = SortByName;
config->order = SortAscending;
config->filter = DbFilterAll;
config->no_version_check = 0;
char data[4096];
char path[256];
pkgi_snprintf(path, sizeof(path), "%s/config.txt", pkgi_get_config_folder());
LOG("config location: %s", path);
int loaded = pkgi_load(path, data, sizeof(data) - 1);
if (loaded > 0)
{
data[loaded] = '\n';
LOG("config.txt loaded, parsing");
char* text = data;
char* end = data + loaded + 1;
if (loaded > 3 && (uint8_t)text[0] == 0xef && (uint8_t)text[1] == 0xbb && (uint8_t)text[2] == 0xbf)
{
text += 3;
}
while (text < end)
{
char* key = text;
text = skipnonws(text, end);
if (text == end) break;
*text++ = 0;
text = skipws(text, end);
if (text == end) break;
char* value = text;
text = skipnonws(text, end);
if (text == end) break;
*text++ = 0;
text = skipws(text, end);
if (pkgi_stricmp(key, "url") == 0)
{
pkgi_strncpy(refresh_url, refresh_len, value);
}
else if (pkgi_stricmp(key, "sort") == 0)
{
config->sort = parse_sort(value, SortByName);
}
else if (pkgi_stricmp(key, "order") == 0)
{
config->order = parse_order(value, SortAscending);
}
else if (pkgi_stricmp(key, "filter") == 0)
{
config->filter = parse_filter(value, DbFilterAll);
}
else if (pkgi_stricmp(key, "no_version_check") == 0)
{
config->no_version_check = 1;
}
}
}
else
{
LOG("config.txt cannot be loaded, using default values");
}
}
static const char* sort_str(DbSort sort)
{
switch (sort)
{
case SortByTitle: return "title";
case SortByRegion: return "region";
case SortByName: return "name";
case SortBySize: return "size";
default: return "";
}
}
static const char* order_str(DbSortOrder order)
{
switch (order)
{
case SortAscending: return "asc";
case SortDescending: return "desc";
default: return "";
}
}
void pkgi_save_config(const Config* config, const char* update_url)
{
char data[4096];
int len = 0;
if (update_url && update_url[0] != 0)
{
len += pkgi_snprintf(data + len, sizeof(data) - len, "url %s\n", update_url);
}
len += pkgi_snprintf(data + len, sizeof(data) - len, "sort %s\n", sort_str(config->sort));
len += pkgi_snprintf(data + len, sizeof(data) - len, "order %s\n", order_str(config->order));
len += pkgi_snprintf(data + len, sizeof(data) - len, "filter ");
const char* sep = "";
if (config->filter & DbFilterRegionASA)
{
len += pkgi_snprintf(data + len, sizeof(data) - len, "%sASA", sep);
sep = ",";
}
if (config->filter & DbFilterRegionEUR)
{
len += pkgi_snprintf(data + len, sizeof(data) - len, "%sEUR", sep);
sep = ",";
}
if (config->filter & DbFilterRegionJPN)
{
len += pkgi_snprintf(data + len, sizeof(data) - len, "%sJPN", sep);
sep = ",";
}
if (config->filter & DbFilterRegionUSA)
{
len += pkgi_snprintf(data + len, sizeof(data) - len, "%sUSA", sep);
sep = ",";
}
len += pkgi_snprintf(data + len, sizeof(data) - len, "\n");
if (config->no_version_check)
{
len += pkgi_snprintf(data + len, sizeof(data) - len, "no_version_check 1\n");
}
char path[256];
pkgi_snprintf(path, sizeof(path), "%s/config.txt", pkgi_get_config_folder());
if (pkgi_save(path, data, len))
{
LOG("saved config.txt");
}
else
{
LOG("cannot save config.txt");
}
}