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