3
3
#include <stdlib.h>
4
4
#include <string.h>
5
5
6
+ int count_fixed (Board * board );
7
+ int check_only_digits (const char * path );
8
+
6
9
/* write current board to path in file format
7
10
mark if fixed. if in edit mode all is fixed.*/
8
11
int write_file_from_board (Board * board ,const char * path ){
@@ -46,20 +49,27 @@ int write_file_from_board (Board* board,const char* path){
46
49
return -1 if didn't create_empty_board
47
50
return 0 if not vaild file:
48
51
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 ){
50
53
int row ,col ,size ;
51
54
int value ,count ,count_scan ,count_dot ,count_char ;
52
55
int i ,j ;
53
56
char ch ;
54
57
FILE * fptr ;
55
58
const char * r = "r" ;
59
+ int count_dots ;
56
60
57
61
fptr = fopen (path ,r );
58
62
if (fptr == NULL ){
59
63
fclose (fptr );
60
64
return -1 ;
61
65
}
62
66
67
+ count_dots = check_only_digits (path );
68
+ if (count_dots == -1 ){ /*contain unwelcome chars!*/
69
+ fclose (fptr );
70
+ return -1 ;
71
+ }
72
+
63
73
row = 0 ;
64
74
col = 0 ;
65
75
count = 0 ;
@@ -107,7 +117,8 @@ int read_file_to_board (Board* board, const char* path, int check_errors){
107
117
}
108
118
if (count_dot == 1 ){
109
119
if (value == 0 ){
110
- board -> fixed_board [i ][j ]= BOARD_NULL_VALUE ;
120
+ fclose (fptr );
121
+ return 0 ;
111
122
}
112
123
else {
113
124
if (check_errors == 0 ){
@@ -145,6 +156,56 @@ int read_file_to_board (Board* board, const char* path, int check_errors){
145
156
fclose (fptr );
146
157
return 0 ;
147
158
}
159
+ if (count_dots != count_fixed (board )){
160
+ fclose (fptr );
161
+ return 0 ;
162
+ }
148
163
fclose (fptr );
149
164
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 ;
150
211
}
0 commit comments