Skip to content

Commit 9d67261

Browse files
committed
read_file_to_board deals with spaces and unwelcome chars
1 parent 9c5de74 commit 9d67261

File tree

6 files changed

+87
-7
lines changed

6 files changed

+87
-7
lines changed

file_utils.c

+63-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#include <stdlib.h>
44
#include <string.h>
55

6+
int count_fixed(Board* board);
7+
int check_only_digits(const char* path);
8+
69
/* write current board to path in file format
710
mark if fixed. if in edit mode all is fixed.*/
811
int write_file_from_board (Board* board,const char* path){
@@ -46,20 +49,27 @@ int write_file_from_board (Board* board,const char* path){
4649
return -1 if didn't create_empty_board
4750
return 0 if not vaild file:
4851
correct format, enough values, correct range, fixed cells are legal*/
49-
int read_file_to_board (Board* board, const char* path, int check_errors){
52+
int read_file_to_board(Board* board, const char* path, int check_errors){
5053
int row,col,size;
5154
int value,count,count_scan,count_dot,count_char;
5255
int i,j;
5356
char ch;
5457
FILE* fptr;
5558
const char* r = "r";
59+
int count_dots;
5660

5761
fptr = fopen(path,r);
5862
if(fptr ==NULL){
5963
fclose(fptr);
6064
return -1;
6165
}
6266

67+
count_dots = check_only_digits(path);
68+
if(count_dots==-1){ /*contain unwelcome chars!*/
69+
fclose(fptr);
70+
return -1;
71+
}
72+
6373
row = 0;
6474
col = 0;
6575
count = 0;
@@ -107,7 +117,8 @@ int read_file_to_board (Board* board, const char* path, int check_errors){
107117
}
108118
if(count_dot == 1){
109119
if(value == 0){
110-
board->fixed_board[i][j]=BOARD_NULL_VALUE;
120+
fclose(fptr);
121+
return 0;
111122
}
112123
else{
113124
if(check_errors == 0){
@@ -145,6 +156,56 @@ int read_file_to_board (Board* board, const char* path, int check_errors){
145156
fclose(fptr);
146157
return 0;
147158
}
159+
if(count_dots != count_fixed(board)){
160+
fclose(fptr);
161+
return 0;
162+
}
148163
fclose(fptr);
149164
return 1;
165+
}
166+
167+
/* check if there are chars which different than spaces, numbers and dot
168+
return -1 if there is, otherwise return counter of dots in file*/
169+
int check_only_digits(const char* path){
170+
int count_dots = 0;
171+
FILE* fptr;
172+
const char* r = "r";
173+
char ch;
174+
unsigned char ch1;
175+
176+
fptr = fopen(path,r);
177+
/*called if opened ok, but to make sure:*/
178+
if(fptr ==NULL){
179+
fclose(fptr);
180+
return -1;
181+
}
182+
183+
while ((ch=getc(fptr))!=EOF)
184+
{
185+
ch1= (unsigned char) ch;
186+
if((ch1 < '0' || ch1 > '9') && ch1 != ' ' && ch1 != '\n' && ch1!='\t' && ch1!='.'){
187+
fclose(fptr);
188+
return -1;
189+
}
190+
if(ch1 == '.'){
191+
count_dots++;
192+
}
193+
}
194+
fclose(fptr);
195+
return count_dots;
196+
}
197+
198+
/* in order to check if equal to counter of dot in file*/
199+
int count_fixed(Board* board){
200+
int i, j;
201+
int count = 0;
202+
int size = (board->num_of_rows)*(board->num_of_columns);
203+
for (i=0; i<size; i++){
204+
for (j=0; j<size; j++){
205+
if(board->fixed_board[i][j]!=BOARD_NULL_VALUE){
206+
count++;
207+
}
208+
}
209+
}
210+
return count;
150211
}

sudoku_board_actions.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void print_board(Board* board){
120120

121121
for(b = 0; b < board->num_of_rows; b++){
122122
row = b + a*(board->num_of_rows);
123-
printf("| ");
123+
printf("|");
124124
for(c = 0; c < board->num_of_rows; c++){
125125
for(d = 0; d < board->num_of_columns; d++){
126126
col = d + c*board->num_of_columns;
@@ -142,7 +142,7 @@ void print_board(Board* board){
142142
}
143143
}
144144
if(c != (board->num_of_rows-1))
145-
printf("| ");
145+
printf("|");
146146
}
147147
printf("|\n");
148148
}

testnine

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
3 3
2+
1. 0 0 0 0 0 0 0 0
3+
0 2. 0 0 0 0 0 0 0
4+
0 0 3. 0 0 0 0 0 0
5+
0 0 0 4. 0 0 0 0 0
6+
0 0 0 0 5. 0 0 0 0
7+
0 0 0 0 0 6. 0 0 0
8+
0 0 0 0 0 0 7. 0 0
9+
0 0 0 0 0 0 0 8. 0
10+
0 0 0 0 0 0 0 0 9.

testspaces

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2 3
2+
1. 0 0
3+
0 0 0 0 2 0 0
4+
0 0 5 0 3 0 0
5+
6+
7+
8+
9+
0 0 0 0 4 0 0 0 2 0 0 0 0 0 0 0 0 0 0

winner renamed to testwinner

File renamed without changes.

writetry

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
2 2
2-
2. 0 0 0
3-
0 1 0 0
2+
2 0 0 0
3+
0 1. 0 0
44
0 0 0 0
5-
0 1 0 3
5+
0 1 0 3

0 commit comments

Comments
 (0)