summaryrefslogtreecommitdiff
path: root/load.c
diff options
context:
space:
mode:
Diffstat (limited to 'load.c')
-rw-r--r--load.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/load.c b/load.c
new file mode 100644
index 0000000..bc2b875
--- /dev/null
+++ b/load.c
@@ -0,0 +1,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;
+}