ef161674dffef3c9d67d4d2790d737161dd7cae2
[dosdemo] / src / cfgopt.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include "cfgopt.h"
6
7 struct options opt = {
8         0,      /* start_scr */
9         0,      /* music */
10         0,      /* sball */
11         1       /* vsync */
12 };
13
14 int parse_args(int argc, char **argv)
15 {
16         int i;
17         char *scrname = 0;
18
19         for(i=1; i<argc; i++) {
20                 if(argv[i][0] == '-') {
21                         if(strcmp(argv[i], "-music") == 0) {
22                                 opt.music = 1;
23                         } else if(strcmp(argv[i], "-nomusic") == 0) {
24                                 opt.music = 0;
25                         } else if(strcmp(argv[i], "-scr") == 0 || strcmp(argv[i], "-screen") == 0) {
26                                 scrname = argv[++i];
27                         } else if(strcmp(argv[i], "-sball") == 0) {
28                                 opt.sball = !opt.sball;
29                         } else if(strcmp(argv[i], "-vsync") == 0) {
30                                 opt.vsync = 1;
31                         } else if(strcmp(argv[i], "-novsync") == 0) {
32                                 opt.vsync = 0;
33                         } else {
34                                 fprintf(stderr, "invalid option: %s\n", argv[i]);
35                                 return -1;
36                         }
37                 } else {
38                         if(scrname) {
39                                 fprintf(stderr, "unexpected option: %s\n", argv[i]);
40                                 return -1;
41                         }
42                         scrname = argv[i];
43                 }
44         }
45
46         if(scrname) {
47                 opt.start_scr = scrname;
48         }
49         return 0;
50 }
51
52 static char *strip_space(char *s)
53 {
54         int len;
55         char *end;
56
57         while(*s && isspace(*s)) ++s;
58         if(!*s) return 0;
59
60         if((end = strrchr(s, '#'))) {
61                 --end;
62         } else {
63                 len = strlen(s);
64                 end = s + len - 1;
65         }
66
67         while(end > s && isspace(*end)) *end-- = 0;
68         return end > s ? s : 0;
69 }
70
71 static int bool_value(char *s)
72 {
73         char *ptr = s;
74         while(*ptr) {
75                 *ptr = tolower(*ptr);
76                 ++ptr;
77         }
78
79         return strcmp(s, "true") == 0 || strcmp(s, "yes") == 0 || strcmp(s, "1") == 0;
80 }
81
82 int load_config(const char *fname)
83 {
84         FILE *fp;
85         char buf[256];
86         int nline = 0;
87
88         if(!(fp = fopen(fname, "rb"))) {
89                 return 0;       /* just ignore missing config files */
90         }
91
92         while(fgets(buf, sizeof buf, fp)) {
93                 char *line, *key, *value;
94
95                 ++nline;
96                 if(!(line = strip_space(buf))) {
97                         continue;
98                 }
99
100                 if(!(value = strchr(line, '='))) {
101                         fprintf(stderr, "%s:%d invalid key/value pair\n", fname, nline);
102                         return -1;
103                 }
104                 *value++ = 0;
105
106                 if(!(key = strip_space(line)) || !(value = strip_space(value))) {
107                         fprintf(stderr, "%s:%d invalid key/value pair\n", fname, nline);
108                         return -1;
109                 }
110
111                 if(strcmp(line, "music") == 0) {
112                         opt.music = bool_value(value);
113                 } else if(strcmp(line, "screen") == 0) {
114                         opt.start_scr = strdup(value);
115                 } else if(strcmp(line, "sball") == 0) {
116                         opt.sball = bool_value(value);
117                 } else if(strcmp(line, "vsync") == 0) {
118                         opt.vsync = bool_value(value);
119                 } else {
120                         fprintf(stderr, "%s:%d invalid option: %s\n", fname, nline, line);
121                         return -1;
122                 }
123         }
124         return 0;
125 }