15
15
16
16
int csv_column ;
17
17
int 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 ;
22
22
23
23
/**
24
24
* Initializes the global variables for parsing a new csv row.
@@ -45,13 +45,13 @@ void next_column() {
45
45
* @param dest_city destination city
46
46
* @param dist distance between start and destination city
47
47
*/
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 ) {
50
50
if (start_city == nullptr || dest_city == nullptr ) {
51
51
return false;
52
52
}
53
53
54
- VALUE * val = get_value (dictionary , start_city -> name );
54
+ VALUE * val = get_value (dictionary , start_city -> name );
55
55
if (val == nullptr ) {
56
56
val = malloc (sizeof (VALUE ));
57
57
if (val == nullptr ) {
@@ -66,7 +66,7 @@ bool add_connection_to_dict(DICTIONARY *dictionary, const CITY *start_city,
66
66
}
67
67
68
68
const CONNECTION connection = (CONNECTION ){
69
- .city = * start_city , .destination = * dest_city , .distance = dist };
69
+ .city = * start_city , .destination = * dest_city , .distance = dist };
70
70
71
71
if (connection_in_values (val , & connection )) {
72
72
return true;
@@ -79,7 +79,7 @@ bool add_connection_to_dict(DICTIONARY *dictionary, const CITY *start_city,
79
79
/**
80
80
* Frees given parameters and closes file stream.
81
81
*/
82
- void free_mem (FILE * file , char * val ) {
82
+ void free_mem (FILE * file , char * val ) {
83
83
free (val );
84
84
fclose (file );
85
85
}
@@ -111,7 +111,7 @@ bool malloc_start_dest_city() {
111
111
* @param city city to set the name
112
112
* @return a bool if the name was set successfully
113
113
*/
114
- bool set_city_name (const char * name , CITY * city ) {
114
+ bool set_city_name (const char * name , CITY * city ) {
115
115
city -> name = malloc (strlen (name ) + 1 );
116
116
if (city -> name == nullptr ) {
117
117
print_error_general ("Name of city is NULL" );
@@ -127,11 +127,11 @@ bool set_city_name(const char *name, CITY *city) {
127
127
* @param val string to convert
128
128
* @return the converted integer
129
129
*/
130
- int get_distance_from_str (const char * val ) {
130
+ int get_distance_from_str (const char * val ) {
131
131
char tmp [char_column_index + 1 ];
132
132
strncpy (tmp , val + '\0' , char_column_index );
133
133
tmp [char_column_index ] = '\0' ;
134
- return (int ) strtoumax (tmp , nullptr , 10 );
134
+ return (int )strtoumax (tmp , nullptr , 10 );
135
135
}
136
136
137
137
/**
@@ -140,7 +140,7 @@ int get_distance_from_str(const char *val) {
140
140
* @param current_word
141
141
* @return
142
142
*/
143
- bool handle_csv_column (const char * current_word ) {
143
+ bool handle_csv_column (const char * current_word ) {
144
144
switch (csv_column ) {
145
145
case 0 : {
146
146
set_city_name (current_word , start_city );
@@ -189,13 +189,14 @@ bool init_parser(const char path[]) {
189
189
* @param current_word string to resize.
190
190
* @return true if reallocation succeeded, false otherwise.
191
191
*/
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 ) {
194
195
return false;
195
196
}
196
197
197
198
* buf_size += BUFFER_SIZE ;
198
- char * tmp = realloc (* current_word , * buf_size );
199
+ char * tmp = realloc (* current_word , * buf_size );
199
200
if (tmp == NULL ) {
200
201
return false;
201
202
}
@@ -227,7 +228,7 @@ DICTIONARY* parse(const char path[]) {
227
228
return nullptr ;
228
229
}
229
230
230
- char * current_word = malloc (32 * sizeof (char ));
231
+ char * current_word = malloc (32 * sizeof (char ));
231
232
if (current_word == NULL || !malloc_start_dest_city ()) {
232
233
fclose (map_file );
233
234
free (dictionary );
@@ -251,6 +252,14 @@ DICTIONARY* parse(const char path[]) {
251
252
if (current_char == '\n' || current_char == EOF ) {
252
253
line_number ++ ;
253
254
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
+ }
254
263
const int dist_str = get_distance_from_str (current_word );
255
264
if (dist_str == 0 ) {
256
265
print_error_general ("Distance has to be 1 or greater" );
@@ -259,7 +268,8 @@ DICTIONARY* parse(const char path[]) {
259
268
260
269
add_connection_to_dict (dictionary , start_city , dest_city , dist_str );
261
270
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 ()) {
263
273
free_mem (map_file , current_word );
264
274
return nullptr ;
265
275
}
@@ -291,7 +301,7 @@ DICTIONARY* parse(const char path[]) {
291
301
}
292
302
293
303
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 ;
295
305
first_column_printable_char = true;
296
306
}
297
307
increment_char_index ();
0 commit comments