Use git for version control.
[netwalk.git] / config.c
blob86ed851a06ff4ce6d08253281983325ff47226a3
1 #include "config.h"
2 #include "util.h"
4 static void parse_option(config_ptr config, char *s1, char *s2)
6 if (!strcmp(s1, "showmoves")) {
7 config->showmoves = atoi(s2);
9 if (!strcmp(s1, "fontsize")) {
10 config->fontsize = atoi(s2);
12 if (!strcmp(s1, "font")) {
13 config->fontname = clonestr(s2);
15 if (!strcmp(s1, "hiscores")) {
16 config->hsfile = clonestr(s2);
20 static int is_whitespace(char c)
22 if (strchr(" \t\r\n", c)) return -1;
23 return 0;
26 static void skip_whitespace(FILE *fp)
28 for (;;) {
29 int c;
30 c = getc(fp);
31 if (feof(fp)) return;
32 if (!is_whitespace(c)) {
33 ungetc(c, fp);
34 break;
40 static void read_word(char *s, FILE *fp)
42 int i = 0;
44 skip_whitespace(fp);
45 if (feof(fp)) return;
47 for (;;) {
48 int c;
49 c = getc(fp);
50 if (feof(fp)) return;
51 if (is_whitespace(c)) {
52 ungetc(c, fp);
53 break;
55 s[i] = c;
56 i++;
57 if (i >= 128 - 1) break;
59 s[i] = 0;
63 static void read_line(char *s, FILE *fp)
65 int i = 0;
67 for (;;) {
68 int c;
69 c = getc(fp);
70 if (feof(fp)) return;
71 if (c == '\r') {
72 //safest thing to do?
73 continue;
75 if (c == '\n') {
76 ungetc(c, fp);
77 break;
79 s[i] = c;
80 i++;
81 if (i >= 1024 - 1) break;
83 s[i] = 0;
86 void config_load(config_ptr config)
88 FILE *fp;
90 fp = config_get_fp();
92 for(;;) {
93 int i;
94 char s1[1024], *s2;
96 skip_whitespace(fp);
97 if (feof(fp)) {
98 break;
100 read_line(s1, fp);
101 if (feof(fp)) {
102 break;
105 i = 0;
106 for(;;) {
107 if (!s1[i]) {
108 s2 = &s1[i];
109 break;
111 if (is_whitespace(s1[i])) {
112 s1[i] = 0;
113 i++;
114 for(;;) {
115 if (!s1[i] || !is_whitespace(s1[i])) {
116 s2 = &s1[i];
117 break;
120 break;
122 i++;
125 parse_option(config, s1, s2);
128 fclose(fp);