forked from rofl0r/gnuboy
-
Notifications
You must be signed in to change notification settings - Fork 3
/
configfile_fk.c
272 lines (234 loc) · 8.76 KB
/
configfile_fk.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
// configfile_fk.c - handles loading and saving the FunKey configuration options
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include "configfile_fk.h"
#define ARRAY_LEN(arr) (sizeof(arr) / sizeof(arr[0]))
enum ConfigOptionType {
CONFIG_TYPE_BOOL,
CONFIG_TYPE_UINT,
CONFIG_TYPE_FLOAT,
CONFIG_TYPE_ASPECT_RATIO,
};
struct ConfigOption {
const char *name;
enum ConfigOptionType type;
union {
bool *boolValue;
unsigned int *uintValue;
float *floatValue;
};
};
#undef X
#define X(a, b) b,
const char *aspect_ratio_name[] = {ASPECT_RATIOS};
unsigned int aspect_ratio_factor_step = 10;
/*
*Config options and default values
*/
unsigned int aspect_ratio = ASPECT_RATIOS_TYPE_STRETCHED;
unsigned int aspect_ratio_factor_percent = 50;
static const struct ConfigOption options[] = {
{.name = "aspect_ratio", .type = CONFIG_TYPE_ASPECT_RATIO, .uintValue = &aspect_ratio},
{.name = "aspect_ratio_factor_percent", .type = CONFIG_TYPE_UINT, .uintValue = &aspect_ratio_factor_percent},
};
// Reads an entire line from a file (excluding the newline character) and returns an allocated string
// Returns NULL if no lines could be read from the file
static char *read_file_line(FILE *file) {
char *buffer;
size_t bufferSize = 64;
size_t offset = 0; // offset in buffer to write
buffer = (char*)malloc(bufferSize);
while (1) {
// Read a line from the file
if (fgets(buffer + offset, bufferSize - offset, file) == NULL) {
free(buffer);
return NULL; // Nothing could be read.
}
offset = strlen(buffer);
assert(offset > 0);
if (feof(file)) // EOF was reached
break;
// If a newline was found, remove the trailing newline and exit
if (buffer[offset - 1] == '\n') {
buffer[offset - 1] = '\0';
break;
}
// If no newline or EOF was reached, then the whole line wasn't read.
bufferSize *= 2; // Increase buffer size
buffer = (char*)realloc(buffer, bufferSize);
assert(buffer != NULL);
}
return buffer;
}
// Returns the position of the first non-whitespace character
static char *skip_whitespace(char *str) {
while (isspace(*str))
str++;
return str;
}
// Returns the position of the first non-whitespace or '=' character
static char *skip_whitespace_or_equal(char *str) {
while (isspace(*str) || *str=='=')
str++;
return str;
}
// NULL-terminates the current whitespace-delimited word, and returns a pointer to the next word
static char *word_split(char *str) {
// Precondition: str must not point to whitespace
assert(!isspace(*str));
// Find either the next whitespace, '=' or end of string
while (!isspace(*str) && *str != '\0' && *str != '=')
str++;
if (*str == '\0') // End of string
return str;
// Terminate current word
*(str++) = '\0';
// Skip whitespace to next word
return skip_whitespace_or_equal(str);
}
// Splits a string into words, and stores the words into the 'tokens' array
// 'maxTokens' is the length of the 'tokens' array
// Returns the number of tokens parsed
static unsigned int tokenize_string(char *str, int maxTokens, char **tokens) {
int count = 0;
str = skip_whitespace(str);
while (str[0] != '\0' && count < maxTokens) {
tokens[count] = str;
str = word_split(str);
count++;
}
return count;
}
// Loads the config file specified by 'filepath'
void configfile_load(const char *filepath) {
FILE *file;
char *line;
unsigned int cur_line = 0;
char *current_section = NULL;
printf("Loading configuration from '%s'\n", filepath);
// Open file or create it if it does not exist
file = fopen(filepath, "r");
if (file == NULL) {
// Create a new config file and save defaults
printf("Config file '%s' not found. Creating it.\n", filepath);
configfile_save(filepath);
return;
}
// Go through each line in the file
while ((line = read_file_line(file)) != NULL) {
char *p = line;
char *tokens[2];
int numTokens;
cur_line++;
// Get tokens
while (isspace(*p)) p++;
numTokens = tokenize_string(p, 2, tokens);
// Get content
if (numTokens != 0) {
// Pass comments
if(tokens[0][0]=='#') continue;
// Check sections - useless for now
if(tokens[0][0]=='['){
p=tokens[0];
while(*p != '\0' && *p!=']') p++;
if(*p == '\0') continue;
*p=0;
if(current_section) free(current_section);
current_section = (char*)malloc(strlen(tokens[0])); //strlen(tokens[0])-1+1
strcpy(current_section, &tokens[0][1]);
printf("New Section: %s\n", current_section);
continue;
}
if (numTokens == 2) {
const struct ConfigOption *option = NULL;
for (unsigned int i = 0; i < ARRAY_LEN(options); i++) {
if (strcmp(tokens[0], options[i].name) == 0) {
option = &options[i];
break;
}
}
if (option == NULL){
printf("Unknown option '%s'\n", tokens[0]);
}
else {
printf("Reading option: '%s', value: '%s'\n", tokens[0], tokens[1]);
switch (option->type) {
case CONFIG_TYPE_BOOL:
if (strcmp(tokens[1], "true") == 0)
*option->boolValue = true;
else if (strcmp(tokens[1], "false") == 0)
*option->boolValue = false;
else{
printf("Unknown CONFIG_TYPE_BOOL value: '%s', using default: %s\n",
tokens[1], (*option->boolValue)?"true":"false");
}
break;
case CONFIG_TYPE_UINT:
sscanf(tokens[1], "%u", option->uintValue);
break;
case CONFIG_TYPE_FLOAT:
sscanf(tokens[1], "%f", option->floatValue);
break;
case CONFIG_TYPE_ASPECT_RATIO:
;unsigned int cur_ar;
for(cur_ar=0; cur_ar<NB_ASPECT_RATIOS_TYPES; cur_ar++){
if(!strcmp(aspect_ratio_name[cur_ar], tokens[1])){
*option->uintValue = cur_ar;
break;
}
}
if(cur_ar >= NB_ASPECT_RATIOS_TYPES){
printf("Unknown CONFIG_TYPE_ASPECT_RATIO value: '%s', using default value: %s\n",
tokens[1], aspect_ratio_name[*option->uintValue]);
}
break;
default:
printf("Unknown option type '%d'\n", option->type);
break;
}
}
}
else{
fprintf(stderr, "Error in line %d: wrong format\n", cur_line);
}
}
free(line);
}
fclose(file);
}
// Writes the config file to 'filepath'
void configfile_save(const char *filepath) {
FILE *file;
printf("Saving configuration to '%s'\n", filepath);
file = fopen(filepath, "w");
if (file == NULL) {
// error
printf("Could not save\n");
return;
}
printf("Saved !\n");
for (unsigned int i = 0; i < ARRAY_LEN(options); i++) {
const struct ConfigOption *option = &options[i];
switch (option->type) {
case CONFIG_TYPE_BOOL:
fprintf(file, "%s = %s\n", option->name, *option->boolValue ? "true" : "false");
break;
case CONFIG_TYPE_UINT:
fprintf(file, "%s = %u\n", option->name, *option->uintValue);
break;
case CONFIG_TYPE_FLOAT:
fprintf(file, "%s = %f\n", option->name, *option->floatValue);
break;
case CONFIG_TYPE_ASPECT_RATIO:
fprintf(file, "%s = %s\n", option->name, aspect_ratio_name[*option->uintValue]);
break;
default:
assert(0); // unknown type
}
}
fclose(file);
}