-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathread.cpp
More file actions
73 lines (61 loc) · 1.46 KB
/
read.cpp
File metadata and controls
73 lines (61 loc) · 1.46 KB
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
#include "read.h"
#include "grid.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
static const char *filename;
static FILE *f;
static unsigned lineno;
static char buffer[4096];
static void error(const char *fmt, ...)
{
char errbuf[10000];
va_list ap;
va_start(ap, fmt);
snprintf(errbuf, sizeof(errbuf), "%s:%u: error: %s\n", filename, lineno, fmt);
vfprintf(stderr, errbuf, ap);
va_end(ap);
exit(1);
}
static void readLine() {
do {
if (!fgets(buffer, sizeof(buffer), f)) {
error("premature end of file");
}
++lineno;
} while (buffer[0] == '\n' ||
(buffer[0] == ';' && buffer[1] == ';'));
}
grid *readMap(const char * fname)
{
filename = fname;
lineno = 0;
f = fopen(filename, "r");
if (!f) {
fprintf(stderr, "%s: error: failed to open for some reason\n", filename);
exit (1);
}
// read the header
unsigned x, y, z;
readLine();
if (3 != sscanf(buffer, "%u %u %u", &x, &y, &z)) {
error("didn't read three unsigneds in the header");
}
grid *g = new grid(point(x, y, z));
// read the maps, one by one
for (unsigned k = 0; k < z; ++k) {
for (unsigned j = 0; j < y; ++j) {
readLine();
for (unsigned i = 0; i < x; ++i) {
if (!buffer[i]) {
error("premature end of line in column %u", i);
}
if (buffer[i] == ' ' || buffer[i] == '.' || buffer[i] == '1') {
g->dig(point(i, j, k));
}
}
}
}
fclose(f);
return g;
}