1
1
#include "Proj_Int.h"
2
2
3
3
/**
4
- * @brief
4
+ * @brief Function that reads the labyrinth of from the given file and dinamically stores the information contained in it
5
5
*
6
6
* @param fp_in
7
- * @param labyrinth
8
7
* @param filename
8
+ * @param test_mode
9
+ * @return int**
9
10
*/
10
11
11
- void Read_input_file (char * filename )
12
+ int * * Read_input_file (FILE * fp_in , int * * labyrinth , char * filename )
12
13
{
13
- // int **traversed_path = NULL;
14
- //int *size_traversed = NULL;
15
- int x = 0 , y = 0 , v = 0 , count = 0 , P = 0 , result = 0 ; //P= number of grey/black cells, (x,y) = grey/black cells coordinates, v=value of respective cell
16
- int C = 0 , L = 0 , a = 0 , b = 0 , c = 0 , d = 0 ; // C=columns, L=lines, (a,b)=coordinates of the cell we want to analyse, P= number of grey/black cells
17
- int * * labyrinth = NULL ;
18
- char test_mode [3 ];
19
- char * out_name = NULL ; //name of the output file
14
+
15
+ int * * traversed_path = NULL ;
16
+ int * size_traversed = NULL ;
20
17
21
- FILE * fp_in = NULL , * fp_out = NULL ;
22
18
23
- fp_in = open_inputfile (fp_in , filename );
24
19
25
- out_name = allocate_outputname (out_name , filename );
26
- fp_out = open_outputfile (fp_out , out_name );
27
- while ((fscanf (fp_in , "%d %d" , & L , & C )) == 2 )
20
+
21
+ int x = 0 , y = 0 , v = 0 , count = 0 , P = 0 , result = 0 ; //P= number of grey/black cells, (x,y) = grey/black cells coordinates, v=value of respective cell
22
+ int C = 0 , L = 0 , a = 0 , b = 0 , c = 0 , d = 0 , num_char = 0 ; // C=columns, L=lines, (a,b)=coordinates of the cell we want to analyse, P= number of grey/black cells
23
+ char * test_mode = (char * )calloc (3 , sizeof (char ));
24
+
25
+ //meter numa funcao
26
+ num_char = find_last_period (filename );
27
+ char * name = (char * )calloc ((num_char + 5 ), sizeof (char ));
28
+ if (name == NULL )
28
29
{
29
- if ((fscanf (fp_in , "%d %d %s" , & a , & b , test_mode )) != 3 )
30
- error (fp_in );
30
+ fprintf (stderr , "Not enough memory available!\n" );
31
+ exit (5 );
32
+ }
33
+ strncpy (name , filename , num_char );
34
+ strcat (name , "sol1" );
31
35
36
+ if ((fp_in = fopen (filename , "r" )) == NULL ) //inicio da leitura do ficheiro
37
+ {
38
+ printf ("Error when reading the input file1.\n" );
39
+ fclose (fp_in );
40
+ exit (EXIT_FAILURE );
41
+ }
42
+ while ((fscanf (fp_in , "%d %d" , & L , & C )) != EOF )
43
+ {
44
+ if ((fscanf (fp_in , "%d %d %s" , & a , & b , test_mode )) != 3 )
45
+ {
46
+ printf ("Error reading data from input file2.\n" );
47
+ exit (0 );
48
+ }
32
49
if (strcmp (test_mode , "A6" ) == 0 )
33
50
if ((fscanf (fp_in , "%d %d" , & c , & d )) != 2 )
34
- error (fp_in );
35
-
36
- labyrinth = allocate_table (labyrinth , C , L ); // Dynamic allocation of the main labyrinth
51
+ {
52
+ printf ("Error reading data from input file3.\n" );
53
+ exit (0 );
54
+ }
55
+ // Dynamic allocation of the main labyrinth
56
+ labyrinth = allocate_table (labyrinth , C , L );
37
57
38
58
if ((fscanf (fp_in , "%d" , & P )) != 1 )
39
59
{
40
- free_labyrinth ( labyrinth , C );
41
- error ( fp_in );
60
+ printf ( "Error reading data from input file.\n" );
61
+ exit ( 0 );
42
62
}
43
63
while ((fscanf (fp_in , "%d %d %d" , & x , & y , & v ) == 3 ) && (count < P ))
44
64
{
45
65
labyrinth [x - 1 ][y - 1 ] = v ; //remember that the coordinate (0,0) represents the (1,1) cell
46
66
count ++ ;
47
67
}
48
68
//print_table(labyrinth, L, C);
49
- /* result = choose_test(test_mode, labyrinth, L, C, a, b, c, d, traversed_path, size_traversed );
69
+ result = choose_test (test_mode , labyrinth , L , C , a , b , c , d );
50
70
write_output_file (name , result , filename );
51
- free_labyrinth(labyrinth, L, C);*/
52
- result = choose_test (test_mode , labyrinth , L , C , a , b );
53
- write_output (fp_out , result );
54
- free_labyrinth (labyrinth , C );
71
+ free_labyrinth (labyrinth , L , C );
55
72
count = 0 ;
56
73
}
57
- free (out_name );
58
74
fclose (fp_in );
59
- fclose ( fp_out );
60
- return ;
75
+ free ( test_mode );
76
+ return labyrinth ;
61
77
}
62
78
63
- void free_labyrinth (int * * labyrinth , int C )
79
+ void free_labyrinth (int * * labyrinth , int L , int C )
64
80
{
65
81
int i ;
66
82
for (i = 0 ; i < C ; i ++ )
@@ -69,48 +85,4 @@ void free_labyrinth(int **labyrinth, int C)
69
85
}
70
86
free (labyrinth );
71
87
return ;
72
- }
73
-
74
- char * allocate_outputname (char * out_name , char * in_name )
75
- {
76
- int num_char = 0 ;
77
- num_char = find_last_period (in_name );
78
- out_name = (char * )calloc ((num_char + 5 ), sizeof (char ));
79
- if (out_name == NULL )
80
- {
81
- fprintf (stderr , "Not enough memory available!\n" );
82
- exit (5 );
83
- }
84
- strncpy (out_name , in_name , num_char );
85
- strcat (out_name , "sol1" );
86
-
87
- return out_name ;
88
- }
89
-
90
- void error (FILE * fp )
91
- {
92
- printf ("Error when reading the input file.\n" );
93
- fclose (fp );
94
- exit (0 );
95
- }
96
-
97
- FILE * open_inputfile (FILE * fp_in , char * filename )
98
- {
99
- if ((fp_in = fopen (filename , "r" )) == NULL )
100
- {
101
- printf ("Error when reading the input file.\n" );
102
- exit (1 );
103
- }
104
- return fp_in ;
105
- }
106
-
107
- FILE * open_outputfile (FILE * fp_out , char * filename )
108
- {
109
- if ((fp_out = fopen (filename , "a" )) == NULL )
110
- {
111
- printf ("Error when writing the output file.\n" );
112
- free (filename );
113
- exit (1 );
114
- }
115
- return fp_out ;
116
88
}
0 commit comments