-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.c
59 lines (57 loc) · 1008 Bytes
/
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
#include "io.h"
int parseInput(struct Board *board, FILE *stream)
{
int i, j;
char *buf = NULL, *cellstr = NULL;
long cell = 0;
size_t size = 0;
ssize_t numChars = 0;
for(i = 0; i < 9; i++)
{
numChars = getline(&buf, &size, stream);
if(-1 == numChars) {
return -1;
}
cellstr = strtok(buf, ",\n");
if(!cellstr) {
return -1;
}
cell = strtol(cellstr, NULL, 10);
if(cell < 0 || cell > 9) {
return -1;
}
j = 0;
setCell(board, cell, i, j);
for(j = 1; j < 9; j++)
{
cellstr = strtok(NULL, ",\n");
if(!cellstr) {
return -1;
}
cell = strtol(cellstr, NULL, 10);
if(cell < 0 || cell > 9) {
return -1;
}
setCell(board, cell, i, j);
}
}
return 0;
}
int printBoard(struct Board *board, FILE *stream)
{
int i, j;
unsigned int cell;
for(i = 0; i < 9; i++)
{
for(j = 0; j < 9; j++)
{
getCell(board, &cell, i, j);
fprintf(stream, "%d", cell);
if(j != 8) {
fprintf(stream, ",");
}
}
fprintf(stream, "\n");
}
return 0;
}