47 |
|
* Initialize preferences |
48 |
|
*/ |
49 |
|
|
50 |
< |
void PrefsInit(int argc, char **argv) |
50 |
> |
void PrefsInit(int &argc, char **&argv) |
51 |
|
{ |
52 |
|
// Set defaults |
53 |
|
AddPrefsDefaults(); |
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); |
59 |
> |
// Override prefs with command line options |
60 |
> |
for (int i=1; i<argc; i++) { |
61 |
> |
|
62 |
> |
// Options are of the form '--keyword' |
63 |
> |
const char *option = argv[i]; |
64 |
> |
if (strlen(option) < 3 || option[0] != '-' || option[1] != '-') |
65 |
|
continue; |
66 |
< |
} |
67 |
< |
const char *keyword = *argv + 2; |
66 |
> |
const char *keyword = option + 2; |
67 |
> |
|
68 |
> |
// Find descriptor for keyword |
69 |
|
const prefs_desc *d = find_prefs_desc(keyword); |
70 |
< |
if (d == NULL) { |
71 |
< |
printf("WARNING: Unrecognized argument '%s'\n", *argv); |
70 |
> |
if (d == NULL) |
71 |
> |
continue; |
72 |
> |
argv[i] = NULL; |
73 |
> |
|
74 |
> |
// Get value |
75 |
> |
i++; |
76 |
> |
if (i >= argc) { |
77 |
> |
fprintf(stderr, "Option '%s' must be followed by a value\n", option); |
78 |
|
continue; |
79 |
|
} |
80 |
+ |
const char *value = argv[i]; |
81 |
+ |
argv[i] = NULL; |
82 |
|
|
83 |
|
// Add/replace prefs item |
84 |
|
switch (d->type) { |
85 |
|
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++; |
86 |
|
if (d->multiple) |
87 |
< |
PrefsAddString(keyword, *argv); |
87 |
> |
PrefsAddString(keyword, value); |
88 |
|
else |
89 |
< |
PrefsReplaceString(keyword, *argv); |
89 |
> |
PrefsReplaceString(keyword, value); |
90 |
|
break; |
91 |
|
|
92 |
< |
case TYPE_BOOLEAN: |
93 |
< |
if (argc > 1 && argv[1][0] != '-') { |
91 |
< |
argc--; argv++; |
92 |
< |
PrefsReplaceBool(keyword, !strcmp(*argv, "true") || !strcmp(*argv, "on")); |
93 |
< |
} else |
92 |
> |
case TYPE_BOOLEAN: { |
93 |
> |
if (!strcmp(value, "true") || !strcmp(value, "on") || !strcmp(value, "yes")) |
94 |
|
PrefsReplaceBool(keyword, true); |
95 |
+ |
else if (!strcmp(value, "false") || !strcmp(value, "off") || !strcmp(value, "no")) |
96 |
+ |
PrefsReplaceBool(keyword, false); |
97 |
+ |
else |
98 |
+ |
fprintf(stderr, "Value for option '%s' must be 'true' or 'false'\n", option); |
99 |
|
break; |
100 |
+ |
} |
101 |
|
|
102 |
|
case TYPE_INT32: |
103 |
< |
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)); |
103 |
> |
PrefsReplaceInt32(keyword, atoi(value)); |
104 |
|
break; |
105 |
|
|
106 |
|
default: |
107 |
|
break; |
108 |
|
} |
109 |
|
} |
110 |
+ |
|
111 |
+ |
// Remove processed arguments |
112 |
+ |
for (int i=1; i<argc; i++) { |
113 |
+ |
int k; |
114 |
+ |
for (k=i; k<argc; k++) |
115 |
+ |
if (argv[k] != NULL) |
116 |
+ |
break; |
117 |
+ |
if (k > i) { |
118 |
+ |
k -= i; |
119 |
+ |
for (int j=i+k; j<argc; j++) |
120 |
+ |
argv[j-k] = argv[j]; |
121 |
+ |
argc -= k; |
122 |
+ |
} |
123 |
+ |
} |
124 |
|
} |
125 |
|
|
126 |
|
|
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
+ |
/* |
146 |
+ |
* Print preferences options help |
147 |
+ |
*/ |
148 |
+ |
|
149 |
+ |
static void print_options(const prefs_desc *list) |
150 |
+ |
{ |
151 |
+ |
while (list->type != TYPE_END) { |
152 |
+ |
if (list->help) { |
153 |
+ |
const char *typestr, *defstr; |
154 |
+ |
char numstr[32]; |
155 |
+ |
switch (list->type) { |
156 |
+ |
case TYPE_STRING: |
157 |
+ |
typestr = "STRING"; |
158 |
+ |
defstr = PrefsFindString(list->name); |
159 |
+ |
if (defstr == NULL) |
160 |
+ |
defstr = "none"; |
161 |
+ |
break; |
162 |
+ |
case TYPE_BOOLEAN: |
163 |
+ |
typestr = "BOOL"; |
164 |
+ |
if (PrefsFindBool(list->name)) |
165 |
+ |
defstr = "true"; |
166 |
+ |
else |
167 |
+ |
defstr = "false"; |
168 |
+ |
break; |
169 |
+ |
case TYPE_INT32: |
170 |
+ |
typestr = "NUMBER"; |
171 |
+ |
sprintf(numstr, "%d", PrefsFindInt32(list->name)); |
172 |
+ |
defstr = numstr; |
173 |
+ |
break; |
174 |
+ |
default: |
175 |
+ |
typestr = "<unknown>"; |
176 |
+ |
defstr = "none"; |
177 |
+ |
break; |
178 |
+ |
} |
179 |
+ |
printf(" --%s %s\n %s [default=%s]\n", list->name, typestr, list->help, defstr); |
180 |
+ |
} |
181 |
+ |
list++; |
182 |
+ |
} |
183 |
+ |
} |
184 |
+ |
|
185 |
+ |
void PrefsPrintUsage(void) |
186 |
+ |
{ |
187 |
+ |
printf("\nGeneral options:\n"); |
188 |
+ |
print_options(common_prefs_items); |
189 |
+ |
printf("\nPlatform-specific options:\n"); |
190 |
+ |
print_options(platform_prefs_items); |
191 |
+ |
printf("\nBoolean options are specified as '--OPTION true|on|yes' or\n'--OPTION false|off|no'.\n"); |
192 |
+ |
} |
193 |
+ |
|
194 |
+ |
|
195 |
|
/* |
196 |
|
* Find preferences descriptor by keyword |
197 |
|
*/ |