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