2925662bb1a6f6d4103b68dd9216d4d5d1ea74b4
[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         if(scrname) {
36                 opt.start_scr = scrname;
37         }
38         return 0;
39 }
40
41 static char *strip_space(char *s)
42 {
43         int len;
44         char *end;
45
46         while(*s && isspace(*s)) ++s;
47         if(!*s) return 0;
48
49         if((end = strrchr(s, '#'))) {
50                 --end;
51         } else {
52                 len = strlen(s);
53                 end = s + len - 1;
54         }
55
56         while(end > s && isspace(*end)) *end-- = 0;
57         return end > s ? s : 0;
58 }
59
60 static int bool_value(char *s)
61 {
62         char *ptr = s;
63         while(*ptr) {
64                 *ptr = tolower(*ptr);
65                 ++ptr;
66         }
67
68         return strcmp(s, "true") == 0 || strcmp(s, "yes") == 0 || strcmp(s, "1") == 0;
69 }
70
71 int load_config(const char *fname)
72 {
73         FILE *fp;
74         char buf[256];
75         int nline = 0;
76
77         if(!(fp = fopen(fname, "rb"))) {
78                 return 0;       /* just ignore missing config files */
79         }
80
81         while(fgets(buf, sizeof buf, fp)) {
82                 char *line, *key, *value;
83
84                 ++nline;
85                 if(!(line = strip_space(buf))) {
86                         continue;
87                 }
88
89                 if(!(value = strchr(line, '='))) {
90                         fprintf(stderr, "%s:%d invalid key/value pair\n", fname, nline);
91                         return -1;
92                 }
93                 *value++ = 0;
94
95                 if(!(key = strip_space(line)) || !(value = strip_space(value))) {
96                         fprintf(stderr, "%s:%d invalid key/value pair\n", fname, nline);
97                         return -1;
98                 }
99
100                 if(strcmp(line, "music") == 0) {
101                         opt.music = bool_value(value);
102                 } else if(strcmp(line, "screen") == 0) {
103                         opt.start_scr = strdup(value);
104                 } else {
105                         fprintf(stderr, "%s:%d invalid option: %s\n", fname, nline, line);
106                         return -1;
107                 }
108         }
109         return 0;
110 }