fullscreen
[andemo] / 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,      /* screen name */
9         0,      /* fullscreen */
10         1,      /* vsync */
11         0,      /* music */
12 };
13
14 static const char *usagefmt = "Usage: %s [options]\n"
15         "Options:\n"
16         "  -music/-nomusic      toggle music playback\n"
17         "  -scr,-screen <name>  ignore demoscript, run specific screen\n"
18         "  -fs/-win             run fullscreen/windowed\n"
19         "  -vsync/-novsync      toggle vsync\n"
20         "  -h,-help             print usage and exit\n";
21
22 int parse_args(int argc, char **argv)
23 {
24         int i;
25         char *scrname = 0;
26
27         for(i=1; i<argc; i++) {
28                 if(argv[i][0] == '-') {
29                         if(strcmp(argv[i], "-music") == 0) {
30                                 opt.music = 1;
31                         } else if(strcmp(argv[i], "-nomusic") == 0) {
32                                 opt.music = 0;
33                         } else if(strcmp(argv[i], "-scr") == 0 || strcmp(argv[i], "-screen") == 0) {
34                                 scrname = argv[++i];
35                         } else if(strcmp(argv[i], "-vsync") == 0) {
36                                 opt.vsync = 1;
37                         } else if(strcmp(argv[i], "-novsync") == 0) {
38                                 opt.vsync = 0;
39                         } else if(strcmp(argv[i], "-fs") == 0) {
40                                 opt.fullscreen = 1;
41                         } else if(strcmp(argv[i], "-win") == 0) {
42                                 opt.fullscreen = 0;
43                         } else if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-help") == 0) {
44                                 printf(usagefmt, argv[0]);
45                                 exit(0);
46                         } else {
47                                 fprintf(stderr, "invalid option: %s\n", argv[i]);
48                                 fprintf(stderr, usagefmt, argv[0]);
49                                 return -1;
50                         }
51                 } else {
52                         if(scrname) {
53                                 fprintf(stderr, "unexpected option: %s\n", argv[i]);
54                                 fprintf(stderr, usagefmt, argv[0]);
55                                 return -1;
56                         }
57                         scrname = argv[i];
58                 }
59         }
60
61         if(scrname) {
62                 opt.scrname = scrname;
63         }
64         return 0;
65 }
66
67
68 static char *strip_space(char *s)
69 {
70         int len;
71         char *end;
72
73         while(*s && isspace(*s)) ++s;
74         if(!*s) return 0;
75
76         if((end = strrchr(s, '#'))) {
77                 --end;
78         } else {
79                 len = strlen(s);
80                 end = s + len - 1;
81         }
82
83         while(end > s && isspace(*end)) *end-- = 0;
84         return end > s ? s : 0;
85 }
86
87 static int bool_value(char *s)
88 {
89         char *ptr = s;
90         while(*ptr) {
91                 *ptr = tolower(*ptr);
92                 ++ptr;
93         }
94
95         return strcmp(s, "true") == 0 || strcmp(s, "yes") == 0 || strcmp(s, "1") == 0;
96 }
97
98 int load_config(const char *fname)
99 {
100         FILE *fp;
101         char buf[256];
102         int nline = 0;
103
104         if(!(fp = fopen(fname, "rb"))) {
105                 return 0;       /* just ignore missing config files */
106         }
107
108         while(fgets(buf, sizeof buf, fp)) {
109                 char *line, *key, *value;
110
111                 ++nline;
112                 if(!(line = strip_space(buf))) {
113                         continue;
114                 }
115
116                 if(!(value = strchr(line, '='))) {
117                         fprintf(stderr, "%s:%d invalid key/value pair\n", fname, nline);
118                         return -1;
119                 }
120                 *value++ = 0;
121
122                 if(!(key = strip_space(line)) || !(value = strip_space(value))) {
123                         fprintf(stderr, "%s:%d invalid key/value pair\n", fname, nline);
124                         return -1;
125                 }
126
127                 if(strcmp(line, "music") == 0) {
128                         opt.music = bool_value(value);
129                 } else if(strcmp(line, "screen") == 0) {
130                         opt.scrname = strdup(value);
131                 } else if(strcmp(line, "vsync") == 0) {
132                         opt.vsync = bool_value(value);
133                 } else if(strcmp(line, "fullscreen") == 0) {
134                         opt.fullscreen = bool_value(value);
135                 } else {
136                         fprintf(stderr, "%s:%d invalid option: %s\n", fname, nline, line);
137                         return -1;
138                 }
139         }
140         return 0;
141 }