1515
1616int csv_column ;
1717int char_column_index ;
18- CITY * start_city ;
19- CITY * dest_city ;
20- FILE * map_file ;
21- DICTIONARY * dictionary ;
18+ CITY * start_city ;
19+ CITY * dest_city ;
20+ FILE * map_file ;
21+ DICTIONARY * dictionary ;
2222
2323/**
2424 * Initializes the global variables for parsing a new csv row.
@@ -45,13 +45,13 @@ void next_column() {
4545 * @param dest_city destination city
4646 * @param dist distance between start and destination city
4747 */
48- bool add_connection_to_dict (DICTIONARY * dictionary , const CITY * start_city ,
49- const CITY * dest_city , const int dist ) {
48+ bool add_connection_to_dict (DICTIONARY * dictionary , const CITY * start_city ,
49+ const CITY * dest_city , const int dist ) {
5050 if (start_city == nullptr || dest_city == nullptr ) {
5151 return false;
5252 }
5353
54- VALUE * val = get_value (dictionary , start_city -> name );
54+ VALUE * val = get_value (dictionary , start_city -> name );
5555 if (val == nullptr ) {
5656 val = malloc (sizeof (VALUE ));
5757 if (val == nullptr ) {
@@ -66,7 +66,7 @@ bool add_connection_to_dict(DICTIONARY *dictionary, const CITY *start_city,
6666 }
6767
6868 const CONNECTION connection = (CONNECTION ){
69- .city = * start_city , .destination = * dest_city , .distance = dist };
69+ .city = * start_city , .destination = * dest_city , .distance = dist };
7070
7171 if (connection_in_values (val , & connection )) {
7272 return true;
@@ -79,7 +79,7 @@ bool add_connection_to_dict(DICTIONARY *dictionary, const CITY *start_city,
7979/**
8080 * Frees given parameters and closes file stream.
8181 */
82- void free_mem (FILE * file , char * val ) {
82+ void free_mem (FILE * file , char * val ) {
8383 free (val );
8484 fclose (file );
8585}
@@ -111,7 +111,7 @@ bool malloc_start_dest_city() {
111111 * @param city city to set the name
112112 * @return a bool if the name was set successfully
113113 */
114- bool set_city_name (const char * name , CITY * city ) {
114+ bool set_city_name (const char * name , CITY * city ) {
115115 city -> name = malloc (strlen (name ) + 1 );
116116 if (city -> name == nullptr ) {
117117 print_error_general ("Name of city is NULL" );
@@ -127,11 +127,11 @@ bool set_city_name(const char *name, CITY *city) {
127127 * @param val string to convert
128128 * @return the converted integer
129129 */
130- int get_distance_from_str (const char * val ) {
130+ int get_distance_from_str (const char * val ) {
131131 char tmp [char_column_index + 1 ];
132132 strncpy (tmp , val + '\0' , char_column_index );
133133 tmp [char_column_index ] = '\0' ;
134- return (int ) strtoumax (tmp , nullptr , 10 );
134+ return (int )strtoumax (tmp , nullptr , 10 );
135135}
136136
137137/**
@@ -140,7 +140,7 @@ int get_distance_from_str(const char *val) {
140140 * @param current_word
141141 * @return
142142 */
143- bool handle_csv_column (const char * current_word ) {
143+ bool handle_csv_column (const char * current_word ) {
144144 switch (csv_column ) {
145145 case 0 : {
146146 set_city_name (current_word , start_city );
@@ -189,13 +189,14 @@ bool init_parser(const char path[]) {
189189 * @param current_word string to resize.
190190 * @return true if reallocation succeeded, false otherwise.
191191 */
192- bool resize_buffer (int * buf_size , char * * current_word ) {
193- if (current_word == nullptr || * current_word == nullptr || buf_size == nullptr ) {
192+ bool resize_buffer (int * buf_size , char * * current_word ) {
193+ if (current_word == nullptr || * current_word == nullptr || buf_size ==
194+ nullptr ) {
194195 return false;
195196 }
196197
197198 * buf_size += BUFFER_SIZE ;
198- char * tmp = realloc (* current_word , * buf_size );
199+ char * tmp = realloc (* current_word , * buf_size );
199200 if (tmp == NULL ) {
200201 return false;
201202 }
@@ -227,7 +228,7 @@ DICTIONARY* parse(const char path[]) {
227228 return nullptr ;
228229 }
229230
230- char * current_word = malloc (32 * sizeof (char ));
231+ char * current_word = malloc (32 * sizeof (char ));
231232 if (current_word == NULL || !malloc_start_dest_city ()) {
232233 fclose (map_file );
233234 free (dictionary );
@@ -251,6 +252,14 @@ DICTIONARY* parse(const char path[]) {
251252 if (current_char == '\n' || current_char == EOF ) {
252253 line_number ++ ;
253254 first_column_printable_char = false;
255+ // if char_column_index is 0 at this point, we have two consecutive line breaks, either skip or abort if not allowed
256+ if (char_column_index == 0 ) {
257+ if (current_char == EOF ) {
258+ break ;
259+ } else {
260+ continue ;
261+ }
262+ }
254263 const int dist_str = get_distance_from_str (current_word );
255264 if (dist_str == 0 ) {
256265 print_error_general ("Distance has to be 1 or greater" );
@@ -259,7 +268,8 @@ DICTIONARY* parse(const char path[]) {
259268
260269 add_connection_to_dict (dictionary , start_city , dest_city , dist_str );
261270
262- if (start_city == NULL || dest_city == NULL || !malloc_start_dest_city ()) {
271+ if (start_city == NULL || dest_city == NULL || !
272+ malloc_start_dest_city ()) {
263273 free_mem (map_file , current_word );
264274 return nullptr ;
265275 }
@@ -291,7 +301,7 @@ DICTIONARY* parse(const char path[]) {
291301 }
292302
293303 if (is_printable_char (current_char ) || first_column_printable_char ) {
294- current_word [char_column_index ++ ] = (char ) current_char ;
304+ current_word [char_column_index ++ ] = (char )current_char ;
295305 first_column_printable_char = true;
296306 }
297307 increment_char_index ();
0 commit comments