-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.c
57 lines (51 loc) · 1.14 KB
/
reader.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
/*
* Sudoku E-solver - simple sudoku solver
*
* Copyright (c) 2015 Sasha Khapyorsky <sashakh@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "sudoku.h"
int sudoku_file_parse(const char *file_name, struct sudoku sudoku[],
unsigned size)
{
char line[4096];
s_type s[D * D];
int n = 0;
FILE *f;
f = fopen(file_name, "r");
if (!f) {
fprintf(stderr, "fopen(\'%s\'): %s\n", file_name,
strerror(errno));
return -1;
}
while (n < size && fgets(line, sizeof(line), f)) {
unsigned i = 0;
char *p = line;
//printf("--> %s", p);
while (*p && !(isdigit(*p)))
p++;
if (!*p)
continue;
while (isdigit(*p) && i < D * D)
s[i++] = *p++ - '0';
if (i < D * D)
continue;
#if 0
for (i = 0; i < 81; i++)
printf("%u", s[i]);
printf("\n");
#endif
sudoku_init(&sudoku[n], s);
n++;
}
fclose(f);
return n;
}