37 |
|
}; |
38 |
|
|
39 |
|
// List of prefs nodes |
40 |
< |
static prefs_node *the_prefs; |
40 |
> |
static prefs_node *the_prefs = NULL; |
41 |
> |
|
42 |
> |
// Prototypes |
43 |
> |
static const prefs_desc *find_prefs_desc(const char *name); |
44 |
|
|
45 |
|
|
46 |
|
/* |
47 |
|
* Initialize preferences |
48 |
|
*/ |
49 |
|
|
50 |
< |
void PrefsInit(void) |
50 |
> |
void PrefsInit(int argc, char **argv) |
51 |
|
{ |
49 |
– |
// Start with empty list |
50 |
– |
the_prefs = NULL; |
51 |
– |
|
52 |
|
// Set defaults |
53 |
|
AddPrefsDefaults(); |
54 |
|
AddPlatformPrefsDefaults(); |
55 |
|
|
56 |
|
// Load preferences from settings file |
57 |
|
LoadPrefs(); |
58 |
+ |
|
59 |
+ |
// Override prefs with command line arguments |
60 |
+ |
argc--; argv++; |
61 |
+ |
for (; argc>0; argc--, argv++) { |
62 |
+ |
|
63 |
+ |
// Arguments are of the form '--keyword' |
64 |
+ |
if (strlen(*argv) < 3 || argv[0][0] != '-' || argv[0][1] != '-') { |
65 |
+ |
printf("WARNING: Unrecognized argument '%s'\n", *argv); |
66 |
+ |
continue; |
67 |
+ |
} |
68 |
+ |
const char *keyword = *argv + 2; |
69 |
+ |
const prefs_desc *d = find_prefs_desc(keyword); |
70 |
+ |
if (d == NULL) { |
71 |
+ |
printf("WARNING: Unrecognized argument '%s'\n", *argv); |
72 |
+ |
continue; |
73 |
+ |
} |
74 |
+ |
|
75 |
+ |
// Add/replace prefs item |
76 |
+ |
switch (d->type) { |
77 |
+ |
case TYPE_STRING: |
78 |
+ |
if (argc < 2) { |
79 |
+ |
printf("WARNING: Argument '%s' must be followed by value\n", *argv); |
80 |
+ |
break; |
81 |
+ |
} |
82 |
+ |
argc--; argv++; |
83 |
+ |
if (d->multiple) |
84 |
+ |
PrefsAddString(keyword, *argv); |
85 |
+ |
else |
86 |
+ |
PrefsReplaceString(keyword, *argv); |
87 |
+ |
break; |
88 |
+ |
|
89 |
+ |
case TYPE_BOOLEAN: |
90 |
+ |
if (argc > 1 && argv[1][0] != '-') { |
91 |
+ |
argc--; argv++; |
92 |
+ |
PrefsReplaceBool(keyword, !strcmp(*argv, "true") || !strcmp(*argv, "on")); |
93 |
+ |
} else |
94 |
+ |
PrefsReplaceBool(keyword, true); |
95 |
+ |
break; |
96 |
+ |
|
97 |
+ |
case TYPE_INT32: |
98 |
+ |
if (argc < 2) { |
99 |
+ |
printf("WARNING: Argument '%s' must be followed by value\n", *argv); |
100 |
+ |
break; |
101 |
+ |
} |
102 |
+ |
argc--; argv++; |
103 |
+ |
PrefsReplaceInt32(keyword, atoi(*argv)); |
104 |
+ |
break; |
105 |
+ |
|
106 |
+ |
default: |
107 |
+ |
break; |
108 |
+ |
} |
109 |
+ |
} |
110 |
|
} |
111 |
|
|
112 |
|
|
142 |
|
return NULL; |
143 |
|
} |
144 |
|
|
145 |
+ |
static const prefs_desc *find_prefs_desc(const char *name) |
146 |
+ |
{ |
147 |
+ |
const prefs_desc *d = find_prefs_desc(name, common_prefs_items); |
148 |
+ |
if (d == NULL) |
149 |
+ |
d = find_prefs_desc(name, platform_prefs_items); |
150 |
+ |
return d; |
151 |
+ |
} |
152 |
+ |
|
153 |
|
|
154 |
|
/* |
155 |
|
* Set prefs items |
185 |
|
add_data(name, TYPE_BOOLEAN, &b, sizeof(bool)); |
186 |
|
} |
187 |
|
|
128 |
– |
void PrefsAddInt16(const char *name, int16 val) |
129 |
– |
{ |
130 |
– |
add_data(name, TYPE_INT16, &val, sizeof(int16)); |
131 |
– |
} |
132 |
– |
|
188 |
|
void PrefsAddInt32(const char *name, int32 val) |
189 |
|
{ |
190 |
|
add_data(name, TYPE_INT32, &val, sizeof(int32)); |
230 |
|
add_data(name, TYPE_BOOLEAN, &b, sizeof(bool)); |
231 |
|
} |
232 |
|
|
178 |
– |
void PrefsReplaceInt16(const char *name, int16 val) |
179 |
– |
{ |
180 |
– |
prefs_node *p = find_node(name, TYPE_INT16); |
181 |
– |
if (p) |
182 |
– |
*(int16 *)(p->data) = val; |
183 |
– |
else |
184 |
– |
add_data(name, TYPE_INT16, &val, sizeof(int16)); |
185 |
– |
} |
186 |
– |
|
233 |
|
void PrefsReplaceInt32(const char *name, int32 val) |
234 |
|
{ |
235 |
|
prefs_node *p = find_node(name, TYPE_INT32); |
262 |
|
return false; |
263 |
|
} |
264 |
|
|
219 |
– |
int16 PrefsFindInt16(const char *name) |
220 |
– |
{ |
221 |
– |
prefs_node *p = find_node(name, TYPE_INT16, 0); |
222 |
– |
if (p) |
223 |
– |
return *(int16 *)(p->data); |
224 |
– |
else |
225 |
– |
return 0; |
226 |
– |
} |
227 |
– |
|
265 |
|
int32 PrefsFindInt32(const char *name) |
266 |
|
{ |
267 |
|
prefs_node *p = find_node(name, TYPE_INT32, 0); |
329 |
|
char *value = p; |
330 |
|
int32 i = atol(value); |
331 |
|
|
332 |
< |
// Look for keyword first in common item list, then in platform specific list |
333 |
< |
const prefs_desc *desc = find_prefs_desc(keyword, common_prefs_items); |
297 |
< |
if (desc == NULL) |
298 |
< |
desc = find_prefs_desc(keyword, platform_prefs_items); |
332 |
> |
// Look for keyword first in prefs item list |
333 |
> |
const prefs_desc *desc = find_prefs_desc(keyword); |
334 |
|
if (desc == NULL) { |
335 |
|
printf("WARNING: Unknown preferences keyword '%s'\n", keyword); |
336 |
|
continue; |
347 |
|
case TYPE_BOOLEAN: |
348 |
|
PrefsReplaceBool(keyword, !strcmp(value, "true")); |
349 |
|
break; |
315 |
– |
case TYPE_INT16: |
316 |
– |
PrefsReplaceInt16(keyword, i); |
317 |
– |
break; |
350 |
|
case TYPE_INT32: |
351 |
|
PrefsReplaceInt32(keyword, i); |
352 |
|
break; |
375 |
|
case TYPE_BOOLEAN: |
376 |
|
fprintf(f, "%s %s\n", list->name, PrefsFindBool(list->name) ? "true" : "false"); |
377 |
|
break; |
346 |
– |
case TYPE_INT16: |
347 |
– |
fprintf(f, "%s %d\n", list->name, PrefsFindInt16(list->name)); |
348 |
– |
break; |
378 |
|
case TYPE_INT32: |
379 |
|
fprintf(f, "%s %d\n", list->name, PrefsFindInt32(list->name)); |
380 |
|
break; |