-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_routines.c
477 lines (413 loc) · 10.2 KB
/
file_routines.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include "file_routines.h"
#include "global_values.h"
#include "maze.h"
char* new_string() {
return (char*)calloc(STRSIZE, sizeof(char));
}
char* get_filename(char* filepath) {
char* filename = new_string();
int slashes = 0, i, j, last_index;
// counts how many slashes there are in the string
for (i = 0; filepath[i] != '\0'; i++) {
if (IS_WIN) {
if (filepath[i] == 92)
last_index = i;
}
else {
if (filepath[i] == 47)
last_index = i;
}
}
// copies file's name to filename
j = 0;
for (i = last_index; filepath[i-1] != '\0'; i++)
filename[j++] = filepath[i];
return filename;
}
void sys_remove_file(char* filepath) {
char* command = new_string();
char win_cmd[] = "DEL ";
char unix_cmd[] = "rm ";
if (IS_WIN)
strcpy(command, win_cmd);
else
strcpy(command, unix_cmd);
strcat(command, filepath);
system(command);
free(command);
}
void remove_file(int index) {
DIR *folder;
struct dirent *entry;
int dir_index = 0;
char *filepath = new_string();
while(1) {
// tries to open the directory
if (IS_WIN) {
folder = opendir(".\\mazes");
strcpy(filepath, ".\\mazes\\");
}
else {
folder = opendir("./mazes");
strcpy(filepath, "./mazes/");
}
// if directory doesn't exist, creates it
if (folder != NULL)
break;
system("mkdir mazes");
}
// searches for the file and then deletes it
while ((entry = readdir(folder))) {
dir_index++;
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
dir_index--;
if (dir_index == index) {
strcat(filepath, entry -> d_name);
sys_remove_file(filepath);
}
}
// error handling -- bad index
if (dir_index == 0) {
printf("Could not find file with this index.\n");
closedir(folder);
return;
}
free(filepath);
closedir(folder);
}
void sys_rename_file(char* filepath, char* new_name) {
char* command = new_string();
char win_cmd[] = "REN ";
char unix_cmd[] = "mv ";
char divider[] = " ";
if (IS_WIN)
strcpy(command, win_cmd);
else
strcpy(command, unix_cmd);
strcat(command, filepath);
strcat(command, divider);
strcat(command, new_name);
system(command);
free(command);
}
void rename_file(int index, char* new_name) {
DIR *folder;
struct dirent *entry;
int dir_index = 0;
char *filepath = new_string();
char *filepathOld = new_string();
while(1) {
// tries to open the directory
if (IS_WIN) {
folder = opendir(".\\mazes");
strcpy(filepath, ".\\mazes\\");
}
else {
folder = opendir("./mazes");
strcpy(filepath, "./mazes/");
}
// if directory doesn't exist, creates it
if (folder != NULL)
break;
system("mkdir mazes");
}
// searches for the file and then renames it
while ((entry = readdir(folder))) {
dir_index++;
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
dir_index--;
if (dir_index == index) {
strcpy(filepathOld,filepath);
strcat(filepathOld, entry -> d_name);
strcat(filepath, new_name);
sys_rename_file(filepathOld, filepath);
}
}
// error handling -- bad index
if (dir_index == 0) {
printf("Could not find file with this index.\n");
closedir(folder);
return;
}
free(filepath);
free(filepathOld);
closedir(folder);
}
int** read_text_file_to_matrix(char* filepath, int* rows, int* columns){
int**matrix = NULL;
int x=0, y=0;
int i, j;
int max_columns = 0;
int max_rows = 1;
char ch;
FILE* ptr = fopen(filepath, "r");
matrix = (int**)malloc(sizeof(int*));
matrix[0] = NULL;
while(max_columns = (max_columns>x) ? (max_columns) : (x), (ch=fgetc(ptr))!=EOF ) {
switch(ch) {
case 'I':
matrix[y] = (int*)realloc(matrix[y], sizeof(int)*(x+1));
matrix[y][x] = -1;
// printf("%c ", 'I');
x++;
break;
case 'F':
matrix[y] = (int*)realloc(matrix[y], sizeof(int)*(x+1));
matrix[y][x] = -2;
// printf("%c ", 'F');
x++;
break;
case ' ':
matrix[y] = (int*)realloc(matrix[y], sizeof(int)*(x+1));
matrix[y][x] = 0;
// printf("%c ", ' ');
x++;
break;
case '\n':y++;
max_rows = y+1;
matrix = (int**)realloc(matrix, sizeof(int*)*max_rows);
matrix[y] = NULL;
// printf("%c", '\n');
x = 0;
break;
default:
matrix[y] = (int*)realloc(matrix[y], sizeof(int)*(x+1));
matrix[y][x] = -3;
// printf("%c ", '#');
x++;
break;
}
}
for(i=0;i<max_rows;i++){
matrix[i] = (int*)realloc(matrix[i], sizeof(int)*max_columns);
for(j=0;j<max_columns;j++){
if(matrix[i][j]!=-1 &&matrix[i][j] != -2 && matrix[i][j] != -3){
matrix[i][j]=0;
}
}
}
*columns = max_columns;
*rows = max_rows;
fclose(ptr);
return matrix;
}
/* function used to get new mazes in the option 'carregar novo labirinto' ;
* the maze is saved in ./mazes folder already processed with the correct chars ;
* returns the filepath to the new loaded maze */
char* load_new_maze() {
int nrows, ncols, **maze;
char *path = new_string();
char *filepath = new_string();
DIR* folder;
scanf("%s", filepath);
//printf("\nTrying to get the name of the filepath %s\n", filepath);
char *filename = get_filename(filepath);
//printf("\nTrying to cocatenate the path with the filename %s\n", filename);
if (IS_WIN){
strcpy(path,".\\mazes\\");
strcat(path,filename);
}
else{
strcpy(path,"./mazes");
strcat(path,filename);
}
while(1) {
// tries to open the directory
if (IS_WIN) {
folder = opendir(".\\mazes");
}
else {
folder = opendir("./mazes");
}
// if directory doesn't exist, creates it
if (folder != NULL)
break;
system("mkdir mazes");
}
//printf("\nTrying to open the full path %s\n",path);
// opens new file to be saved
FILE *file = fopen(path, "w");
if (file == NULL) {
printf("Could not load file");
return NULL;
}
//printf("\nTrying to read text file to matrix with path %s\n", filepath);
maze = read_text_file_to_matrix(filepath, &nrows, &ncols);
// printf("\nWe got this matrix for the maze:\n");
// for(int i=0;i<nrows;i++){
// for(int j=0;j<ncols;j++){
// printf("%d ", maze[i][j]);
// }
// printf("\n");
// }
// this will loop through the matrix
// that is already mapped with only
//-3,-2,-1,0 or 1 flags
for(int i = 0; i < nrows; i++){
for(int j = 0; j < ncols; j++){
switch(maze[i][j]){
case -3:
fprintf(file, "#");
break;
case -2:
fprintf(file, "F");
break;
case -1:
fprintf(file, "I");
break;
case 0:
fprintf(file, " ");
break;
case 1:
fprintf(file, ".");
break;
}
}
fprintf(file, "\n");
}
fclose(file);
free(filepath);
return path;
}
char *get_path_from_index(int fileIndex){
DIR *folder;
struct dirent *entry;
int dir_index = 0,i_files=0;
char *filepath = new_string();
while(1) {
// tries to open the directory
if (IS_WIN) {
folder = opendir(".\\mazes");
strcpy(filepath, ".\\mazes\\");
}
else {
folder = opendir("./mazes");
strcpy(filepath, "./mazes/");
}
// if directory doesn't exist, creates it
if (folder != NULL)
break;
system("mkdir mazes");
}
while ((entry = readdir(folder))) {
i_files++;
//printf("\non first if on iteration %d with entry->d_name \n%s", i_files,entry->d_name);
if(entry->d_name[0] == '.'){
i_files--;
continue;
}
//printf("\noff first if on iteration %d with entry->d_name \n%s", i_files,entry->d_name);
if(i_files == fileIndex){
strcat(filepath, entry ->d_name);
break;
}
}
return filepath;
}
// This function is still not integrated with the system
// its purpose is to solve a maze from a filepath, meaning it would integrate solve_maze()
// with the file_routines (it is not intended to use maze.c functions inside display.c, so i created this)
// but if it returns a maze, the display_maze would need a file,
// so it would be needed to create another function (print_maze?)
// we also need to think about if the user will save the solved maze somewhere everytime or not
// if it does, the main will have to save or call some function to save it
// if it does not(seems to make more sense), we need to implement more UI so that the user gives a path to create a file
// creating files may be tricky and we need to get this right, so i didnt finish this funciton yet
int **solve_mazeFromfile(char * pathOriginalMaze, int* lines, int*columns){
int **maze;
maze = read_text_file_to_matrix(pathOriginalMaze,lines,columns);
solve_maze(maze,*lines,*columns);
FILE*ptr = fopen(pathOriginalMaze, "w");
for(int i = 0; i < (*lines); i++){//prints maze in .txt file
for(int j = 0; j < (*columns); j++){
switch(maze[i][j]){
case -3:
fprintf(ptr, "#");
break;
case -2:
fprintf(ptr, "F");
break;
case -1:
fprintf(ptr, "I");
break;
case 1:
fprintf(ptr, ".");
break;
case 0:
fprintf(ptr, " ");
break;
}
}
fprintf(ptr, "\n");
}
fclose(ptr);
return maze;
}
//Takes care of file routines within random maze creation
int** random_maze(int randomMazeRows, int randomMazeCols){
int** randomMaze;
randomMaze = create_random_maze(randomMazeRows, randomMazeCols);//Creates the random maze
return randomMaze;
}
void create_gen_maze_file(char* filename, int**genMaze, int genMazeRows, int genMazeCols){
DIR *folder;
FILE *maze;
char *path = new_string();
while(1) {
// tries to open the directory
if (IS_WIN) {
folder = opendir(".\\gen_mazes");
}
else {
folder = opendir("./gen_mazes");
}
// if directory doesn't exist, creates it
if (folder != NULL)
break;
system("mkdir gen_mazes");
}
if (IS_WIN){
strcpy(path,".\\gen_mazes\\");
strcat(path,filename);
}
else{
strcpy(path,"./gen_mazes/");
strcat(path,filename);
}
maze = fopen(path, "w");//creates new gen maze file
if(maze == NULL){
// File not created hence exit
printf("Unable to create file.\n");
}
for(int i = 0; i < genMazeRows; i++){//prints maze in .txt file
for(int j = 0; j < genMazeCols; j++){
switch(genMaze[i][j]){
case -3:
fprintf(maze, "#");
break;
case -2:
fprintf(maze, "F");
break;
case -1:
fprintf(maze, "I");
break;
case 0:
fprintf(maze, " ");
break;
}
}
fprintf(maze, "\n");
}
fclose(maze);
}
void free_matrix(int**matrix, int rows){
int i;
for(i=0;i<rows;i++){
free(matrix[i]);
}
free(matrix);
}