X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=dosdemo;a=blobdiff_plain;f=src%2Fcfgopt.c;fp=src%2Fcfgopt.c;h=553a1457fcae6a25144a02c7fc26795e0adbefec;hp=0000000000000000000000000000000000000000;hb=493e590908a39324651cd2c81323e6969c0b4c4f;hpb=f524001d8739b7cf5efd8e0784a1fa650b4af27b diff --git a/src/cfgopt.c b/src/cfgopt.c new file mode 100644 index 0000000..553a145 --- /dev/null +++ b/src/cfgopt.c @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include "cfgopt.h" + +struct options opt; + +int parse_args(int argc, char **argv) +{ + int i; + char *scrname = 0; + + for(i=1; i s && isspace(*end)) *end-- = 0; + return end > s ? s : 0; +} + +static int bool_value(char *s) +{ + char *ptr = s; + while(*ptr) { + *ptr = tolower(*ptr); + ++ptr; + } + + return strcmp(s, "true") == 0 || strcmp(s, "yes") == 0 || strcmp(s, "1") == 0; +} + +int load_config(const char *fname) +{ + FILE *fp; + char buf[256]; + int nline = 0; + + if(!(fp = fopen(fname, "rb"))) { + return 0; /* just ignore missing config files */ + } + + while(fgets(buf, sizeof buf, fp)) { + char *line, *key, *value; + + ++nline; + if(!(line = strip_space(buf))) { + continue; + } + + if(!(value = strchr(line, '='))) { + fprintf(stderr, "%s:%d invalid key/value pair\n", fname, nline); + return -1; + } + *value++ = 0; + + if(!(key = strip_space(line)) || !(value = strip_space(value))) { + fprintf(stderr, "%s:%d invalid key/value pair\n", fname, nline); + return -1; + } + + if(strcmp(line, "music") == 0) { + opt.music = bool_value(value); + } else if(strcmp(line, "screen") == 0) { + opt.start_scr = strdup(value); + } else { + fprintf(stderr, "%s:%d invalid option: %s\n", fname, nline, line); + return -1; + } + } + return 0; +}