-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreadfile.c
207 lines (162 loc) · 6.29 KB
/
readfile.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
#include <string.h>
#include <stdio.h>
#include "universe.h"
#include "readwrite.h"
void read_life105(FILE *f,
void (*cb)(void *, char, int, int, int, char),
void (*paramcb)(void *, const char *, const char *),
void *cbdata) {
char linebuff[10000];
char parambuff[30];
char valuebuff[50];
int gen, xpos, ypos, value;
char area = 'P';
while(fgets(linebuff, sizeof linebuff, f)) {
char *p = strchr(linebuff, '\r');
if(p) *p = 0;
p = strchr(linebuff, '\n');
if(p) *p = 0;
if(linebuff[0] == '#') switch(linebuff[1]) {
case 'L':
// version header; ignore
break;
case 'D':
// description; ignore
break;
case 'N':
// use normal Life rules
break;
case 'P':
// set co-ordinates
area = 'P';
gen = 0;
if(sscanf(linebuff + 2, "%d %d", &xpos, &ypos) != 2) {
fprintf(stderr, "Bad line: %s\n", linebuff);
return;
}
break;
case 'F':
// set co-ordinates
area = 'F';
if(sscanf(linebuff + 2, "%d %d %d", &gen, &xpos, &ypos) != 3) {
fprintf(stderr, "Bad line: %s\n", linebuff);
return;
}
break;
case 'S':
// search space control parameter
if(sscanf(linebuff + 2, "%s %s", parambuff, valuebuff) != 2) {
fprintf(stderr, "Bad line: %s\n", linebuff);
return;
}
if(paramcb)
paramcb(cbdata, parambuff, valuebuff);
break;
default:
fprintf(stderr, "Unknown line: %s\n", linebuff);
break;
} else {
// Process as data.
int x = xpos;
for(p = linebuff; *p; p++) {
cb(cbdata, area, gen, x, ypos, *p);
x++;
}
ypos++;
}
}
}
static void life105cb(void *u_, char area, int gen, int x, int y, char c) {
universe *u = (universe *)u_;
cellvalue v = OFF;
if((area == 'P') && (gen == 0)) {
switch(c) {
case '.': v = OFF; break;
case '*': v = ON; break;
case '?': v = UNKNOWN_STABLE; break;
case '@': v = UNKNOWN; break;
}
generation_set_cell(u->first, x, y, v);
}
}
static void read_rle(FILE *f, universe *u) {
char linebuff[10000];
int xpos = 0, ypos = 0;
// read header lines
while(fgets(linebuff, sizeof linebuff, f)) {
if(!strncmp(linebuff, "x = ", 4)) {
if(linebuff[0] == '#') {
fprintf(stderr, "%s\n", linebuff);
} else if(sscanf(linebuff, "x = %d, y = %d", &xpos, &ypos) == 2) {
break;
}
}
}
// read data
int xcur = xpos;
int count = 0;
while(fgets(linebuff, sizeof linebuff, f)) {
char *p = linebuff;
while(*p) {
if((*p >= '0') && (*p <= '9')) {
count = (count * 10) + (*p - '0');
} else if(*p == 'b') {
if(count == 0)
count = 1;
while(count--) {
generation_set_cell(u->first, xcur++, ypos, OFF);
}
count = 0;
} else if(*p == 'o') {
if(count == 0)
count = 1;
while(count--)
generation_set_cell(u->first, xcur++, ypos, ON);
count = 0;
} else if(*p == '$') {
if(count == 0)
count = 1;
while(count--) {
ypos++;
xcur = xpos;
}
count = 0;
} else if(*p == '\n') {
// ignore
} else if(*p == '!') {
return;
} else {
fprintf(stderr, "Bad char '%c' in RLE\n", *p);
}
p++;
}
}
}
universe *read_text(const char *filename) {
char firstline[1000];
FILE *f = fopen(filename, "r");
if(!f) {
perror(filename);
return NULL;
}
universe *u = universe_new(OFF);
if(fgets(firstline, sizeof firstline, f) == NULL) {
perror(filename);
fclose(f);
return NULL;
}
fseek(f, 0, SEEK_SET);
if(!strncmp(firstline, "#CXRLE ", 7)) {
read_rle(f, u);
} else if((firstline[0] == '#') &&
(firstline[1] != 0) &&
(firstline[2] == ' ')) {
read_life105(f, life105cb, NULL, u);
} else {
fprintf(stderr, "Unknown format '%s'\n", firstline);
fclose(f);
return NULL;
}
fclose(f);
return u;
}