summaryrefslogtreecommitdiff
path: root/load.c
blob: bc2b8751bf020b2e6e25f003f07b03babceeb895 (plain)
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
#include <stdio.h>

#include "cell.h"

int load(FILE *f, struct cell (*b)[9][9])
{
	int x = 0;
	int y = 0;
	char c = '\0';

	while (y < 9) {
		c = fgetc(f);

		switch(c) {
			case ' ':
			case '\n':
			case '\t':
			case '|':
				/* skip whitespace, | */
				continue;

			case '_':
			case '?':
				/* blank = same as 0 */
				c = '0';
				/* fallthrough */
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
				(*b)[x][y].val = c - '0';
				break;

			case EOF:
				printf("Unexpected EOF when filling (%d,%d)\n", x, y);
				return -1;
			default:
				printf("Unexpected '%c' when filling (%d,%d)\n", c, x, y);
				return -1;
		}

		/* move to next position in board */
		if (++x == 9) {
			x = 0;
			y++;
		}
	}
	return 0;
}