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