forked from sunjay/sudoku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboardparser.c
48 lines (42 loc) · 862 Bytes
/
boardparser.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "sudoku.h"
#include "inputhandler.h"
#define LETTER_0 '0'
SudokuBoard* readBoard(FILE* fp) {
SudokuBoard* board = newSudokuBoard();
int row_i = 0;
char* line;
while (true) {
line = getline(fp);
if (line == NULL) { // EOF + Not enough rows given
return NULL;
}
short row[BOARD_SIZE];
int found = 0;
for (int i = 0, n = strlen(line); i < n; i++) {
char c = line[i];
if (c == EOL) {
break;
}
if (!isdigit(c) || found > BOARD_SIZE) {
printf("invalid row item");
free(line);
return NULL;
}
row[found++] = c - LETTER_0;
}
free(line);
if (found != BOARD_SIZE) {
printf("invalid row size");
return NULL;
}
setBoardRowValues(board, row_i++, row);
if (row_i >= BOARD_SIZE) {
break;
}
}
return board;
}