-
Notifications
You must be signed in to change notification settings - Fork 5
/
io.c
255 lines (207 loc) · 5.13 KB
/
io.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include "io.h"
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include <ctype.h>
static inline size_t _count_tabs(char *line) {
size_t i = 0;
while (line[i] == '\t') ++i;
return i;
};
static ssize_t _read_splits(FILE *f, unsigned *id, unsigned level, struct split **out, char **trailing_line) {
size_t splits_alloc = 8;
size_t nsplits = 0;
struct split *splits = malloc(splits_alloc * sizeof splits[0]);
char *line = NULL;
while (true) {
if (!line) {
size_t n = 0;
if (getline(&line, &n, f) == -1) {
free(line);
*trailing_line = NULL;
break;
}
}
if (!strcmp(line, "\n")) {
line = NULL;
continue;
}
size_t ntabs = _count_tabs(line);
if (ntabs != level) {
*trailing_line = line;
break;
}
if (splits_alloc == nsplits) {
splits_alloc *= 2;
splits = realloc(splits, splits_alloc * sizeof splits[0]);
}
size_t len = strlen(line + ntabs);
char *name = malloc(len);
strncpy(name, line + ntabs, len - 1);
name[len - 1] = 0;
char *next = NULL;
splits[nsplits].name = name;
struct split *subsplits;
ssize_t nsubsplits = _read_splits(f, id, level + 1, &subsplits, &next);
if (nsubsplits < 1) {
// No subsplits, so it's an actual split
splits[nsplits].is_group = false;
splits[nsplits].split.id = (*id)++;
splits[nsplits].split.times = (struct times){
.cur = UINT64_MAX,
.pb = UINT64_MAX,
.best = UINT64_MAX,
.golded_this_run = false,
};
} else {
// We have subsplits, so it's a group
splits[nsplits].is_group = true;
splits[nsplits].group.expanded = false;
splits[nsplits].group.nsplits = nsubsplits;
splits[nsplits].group.splits = subsplits;
}
free(line);
line = next;
++nsplits;
}
if (nsplits == 0) {
free(splits);
}
*out = splits;
return nsplits;
}
ssize_t read_splits_file(const char *path, struct split **out) {
FILE *f = fopen(path, "r");
if (!f) {
return -1;
}
unsigned id = 0;
char *trailing;
ssize_t nsplits = _read_splits(f, &id, 0, out, &trailing);
if (nsplits != -1 && trailing) {
fprintf(stderr, "trailing line in splits file: %s", trailing); // We don't print a newline as the trailing line includes one
free(trailing);
free_splits(*out, nsplits);
return -1;
}
fclose(f);
return nsplits;
}
static bool _read_times(FILE *f, size_t off, struct split *splits, size_t nsplits) {
for (size_t i = 0; i < nsplits; ++i) {
if (splits[i].is_group) {
if (!_read_times(f, off, splits[i].group.splits, splits[i].group.nsplits)) {
return false;
}
} else {
uint64_t val;
int dummy = -1;
if (fscanf(f, "-\n%n", &dummy) == 0 && dummy != -1) {
*(uint64_t *)((char *)&splits[i].split.times + off) = UINT64_MAX;
} else if (fscanf(f, "%"PRIu64"\n", &val) == 1) {
*(uint64_t *)((char *)&splits[i].split.times + off) = val;
} else {
return false;
}
}
}
return true;
}
static void _clear_times(size_t off, struct split *splits, size_t nsplits) {
for (size_t i = 0; i < nsplits; ++i) {
if (splits[i].is_group) {
_clear_times(off, splits[i].group.splits, splits[i].group.nsplits);
} else {
*(uint64_t *)((char *)&splits[i].split.times + off) = UINT64_MAX;
}
}
}
bool read_times(struct split *splits, size_t nsplits, const char *path, size_t off) {
FILE *f = fopen(path, "r");
if (!f) {
return false;
}
bool success = _read_times(f, off, splits, nsplits);
// TODO: enforce eof
fclose(f);
if (!success) {
_clear_times(off, splits, nsplits);
}
return success;
}
bool _save_times(FILE *f, size_t off, struct split *splits, size_t nsplits) {
for (size_t i = 0; i < nsplits; ++i) {
if (splits[i].is_group) {
_save_times(f, off, splits[i].group.splits, splits[i].group.nsplits);
} else {
uint64_t time = *(uint64_t *)((char *)&splits[i].split.times + off);
if (time == UINT64_MAX) {
if (fputs("-\n", f) == EOF) {
return false;
}
} else {
if (fprintf(f, "%"PRIu64"\n", time) == EOF) {
return false;
}
}
}
}
return true;
}
bool save_times(struct split *splits, size_t nsplits, const char *path, size_t off) {
FILE *f = fopen(path, "w");
if (!f) {
return false;
}
bool success = _save_times(f, off, splits, nsplits);
fclose(f);
return success;
}
// TODO: this leaks memory, might be worth not doing that
bool read_config(const char *path, struct cfgdict *cfg) {
FILE *f = fopen(path, "r");
if (!f) {
return false;
}
while (true) {
size_t allocd = 0;
char *line = NULL;
size_t n = getline(&line, &allocd, f);
if (n == -1) {
break;
}
// strip trailing whitespace
{
char *end = line;
while (*end) ++end;
--end;
while (isspace(*end)) --end;
end[1] = 0;
}
if (n > 0) {
int p = -1;
char *k;
int matched = sscanf(line, "%ms %n", &k, &p);
if (matched != 1 || p == -1) {
if (matched >= 1) free(k);
// TODO: clear cfg
fclose(f);
return false;
}
char *v = strdup(line + p);
int ret = cfgdict_put(cfg, k, v);
if (ret == -1) {
free(k);
free(v);
// TODO: clear cfg
fclose(f);
return false;
} else if (ret == 1) {
fprintf(stderr, "Warning: duplicate config key %s\n", k);
}
}
}
fclose(f);
return true;
}