diff --git a/Compte Rendu PA.pdf b/Compte Rendu PA.pdf index 7895d5fdb0032babba0eb5d739c150dccc625c6d..75a71f46975f39a409f0b35a8d60e7aed0775842 100644 Binary files a/Compte Rendu PA.pdf and b/Compte Rendu PA.pdf differ diff --git a/initializeData.c b/initializeData.c index 297a735713d34a979c0aaef66470407d3abb1263..d095891053de4754ebafd7f8fd70401464a2d50f 100644 --- a/initializeData.c +++ b/initializeData.c @@ -95,7 +95,7 @@ void create_flight (Flight **fl, char **line, const char separators[]) } else { - (*fl)->airline = word; + (*fl)->airline = strdup (word); } word = strsep (line, separators); if (strcmp (word, "") == 0) @@ -104,7 +104,7 @@ void create_flight (Flight **fl, char **line, const char separators[]) } else { - (*fl)->org_air = word; + (*fl)->org_air = strdup (word); } word = strsep (line, separators); if (strcmp (word, "") == 0) @@ -113,7 +113,7 @@ void create_flight (Flight **fl, char **line, const char separators[]) } else { - (*fl)->dest_air = word; + (*fl)->dest_air = strdup (word); } word = strsep (line, separators); if (strcmp (word, "") == 0) @@ -274,8 +274,9 @@ void load_flights (Table_hash_airlines *thal, Table_hash_airports *thap, FILE *f while (getline (&line, &len, fd) != -1) // on récupère chaque ligne. { + char * line2 = line; Flight *new_flight = malloc (sizeof (Flight)); - create_flight (&new_flight, &line, separators); + create_flight (&new_flight, &line2, separators); Liste_Airlines *l = thal->tab_airlines[hash_function_airline (new_flight->airline)]; @@ -372,6 +373,9 @@ void free_all_airlines (Table_hash_airlines *thal) { Flight *tmp = thal->tab_airlines[i]->liste_flights; thal->tab_airlines[i]->liste_flights = NULL; + free (tmp->airline); + free (tmp->org_air); + free (tmp->dest_air); free (tmp); } else @@ -379,6 +383,9 @@ void free_all_airlines (Table_hash_airlines *thal) Flight *tmp = thal->tab_airlines[i]->liste_flights; thal->tab_airlines[i]->liste_flights = thal->tab_airlines[i]->liste_flights->suiv_airline; tmp->suiv_airline = NULL; + free (tmp->airline); + free (tmp->org_air); + free (tmp->dest_air); free (tmp); } } @@ -409,3 +416,29 @@ void free_all_airports (Table_hash_airports *thap) } } } + +int is_empty_airlines (Table_hash_airlines thal) +{ + for (int i = 0; i < AIRLINE_SIZE_TABLE; i++) + { + if (thal.tab_airlines[i] != NULL) + { + return 1; + } + } + + return 0; +} + +int is_empty_airports (Table_hash_airports thap) +{ + for (int i = 0; i < AIRPORT_SIZE_TABLE; i++) + { + if (thap.tab_airports[i] != NULL) + { + return 1; + } + } + + return 0; +} diff --git a/initializeData.h b/initializeData.h index 56d9f6cb40315a012ee21f1dac761308bee80be5..185be28b862526b442da85f9d23f65b8e155df61 100644 --- a/initializeData.h +++ b/initializeData.h @@ -114,3 +114,7 @@ int hash_function_airport (char *); void free_all_airlines (Table_hash_airlines *); void free_all_airports (Table_hash_airports *); + +int is_empty_airlines (Table_hash_airlines); + +int is_empty_airports (Table_hash_airports); diff --git a/main.c b/main.c index 6fe2927bc6e8dfb56f3af64e1937dec65d862cdb..2c4e1283edebaa2cca63615b741784fa9361304d 100644 --- a/main.c +++ b/main.c @@ -26,136 +26,153 @@ int main () load_airports (&thap, airport_file); } - if (flights_file != NULL) + if (flights_file != NULL && (is_empty_airlines (thal) == 1 && is_empty_airports (thap) == 1)) { load_flights (&thal, &thap, flights_file); } - - printf ("Chargement terminé !\n"); - - char * requetes = NULL; - char * commande = NULL; - char * airline_id = NULL; - char * airport_id1 = NULL; - char * airport_id2 = NULL; - char * date = NULL; - char * time = ""; - char * limit = ""; - size_t len = 0; - - while (getline (&requetes, &len, stdin) != -1) + if (is_empty_airlines (thal) == 0 || is_empty_airports (thap) == 0) { - commande = strsep (&requetes, " "); - commande = strtok (commande, "\n"); - if (strcmp (commande, "quit") == 0) - { - break; - } - else if (strcmp (commande, "show-airports") == 0) - { - airline_id = strsep (&requetes, " \n"); - printf ("> Resultat de la requete %s %s :\n", commande, airline_id); - printf ("\n"); - show_airports (airline_id, thal, thap); - } - else if (strcmp (commande, "show-airlines") == 0) - { - airport_id1 = strsep (&requetes, " \n"); - printf ("> Resultat de la requete %s %s :\n", commande, airport_id1); - printf ("\n"); - show_airlines (airport_id1, thal, thap); - } - else if (strcmp (commande, "show-flights") == 0) + printf ("Chargement des fichiers impossible, veuillez vérifier les fichiers !\n"); + } + else + { + printf ("Chargement terminé !\n"); + + char * requetes = NULL; + char * commande = NULL; + char * airline_id = NULL; + char * airport_id1 = NULL; + char * airport_id2 = NULL; + char * date = NULL; + char * time = ""; + char * limit = ""; + size_t len = 0; + + while (getline (&requetes, &len, stdin) != -1) { - airport_id1 = strsep (&requetes, " \n"); - date = strsep (&requetes, " \n"); - time = strsep (&requetes, " \n"); - limit = strsep (&requetes, " \n"); - - if (limit == NULL) + char *requetes2 = requetes; + commande = strsep (&requetes2, " "); + commande = strtok (commande, "\n"); + if (strcmp (commande, "quit") == 0) + { + break; + } + else if (strcmp (commande, "show-airports") == 0) + { + airline_id = strsep (&requetes2, " \n"); + printf ("> Resultat de la requete %s %s :\n", commande, airline_id); + printf ("\n"); + show_airports (airline_id, thal, thap); + } + else if (strcmp (commande, "show-airlines") == 0) { + airport_id1 = strsep (&requetes2, " \n"); printf ("> Resultat de la requete %s %s :\n", commande, airport_id1); printf ("\n"); - show_flights (airport_id1, thap, date, 0); + show_airlines (airport_id1, thal, thap); } - else + else if (strcmp (commande, "show-flights") == 0) { - if (strcmp (time, "") != 0 && strcmp (limit, "") != 0) + airport_id1 = strsep (&requetes2, " \n"); + date = strsep (&requetes2, " \n"); + time = strsep (&requetes2, " \n"); + limit = strsep (&requetes2, " \n"); + + if (limit == NULL) { - printf ("> Resultat de la requete %s %s %s %s :\n", commande, airport_id1, time, limit); + printf ("> Resultat de la requete %s %s :\n", commande, airport_id1); printf ("\n"); - show_flights (airport_id1, thap, date, 2, time, limit); + show_flights (airport_id1, thap, date, 0); } - if (strcmp (time, "") != 0 && strcmp (limit, "") == 0) + else { - printf ("> Resultat de la requete %s %s %s :\n", commande, airport_id1, time); - printf ("\n"); - show_flights (airport_id1, thap, date, 1, time); - } - if (strcmp (time, "") == 0 && strcmp (limit, "") != 0) - { - printf ("> Resultat de la requete %s %s %s :\n", commande, airport_id1, limit); - printf ("\n"); - show_flights (airport_id1, thap, date, 1, limit); + if (strcmp (time, "") != 0 && strcmp (limit, "") != 0) + { + printf ("> Resultat de la requete %s %s %s %s :\n", commande, airport_id1, time, limit); + printf ("\n"); + show_flights (airport_id1, thap, date, 2, time, limit); + } + if (strcmp (time, "") != 0 && strcmp (limit, "") == 0) + { + printf ("> Resultat de la requete %s %s %s :\n", commande, airport_id1, time); + printf ("\n"); + show_flights (airport_id1, thap, date, 1, time); + } + if (strcmp (time, "") == 0 && strcmp (limit, "") != 0) + { + printf ("> Resultat de la requete %s %s %s :\n", commande, airport_id1, limit); + printf ("\n"); + show_flights (airport_id1, thap, date, 1, limit); + } } } - } - else if (strcmp (commande, "most-delayed-flights") == 0) - { - printf ("> Resultat de la requete %s :\n", commande); - printf ("\n"); - most_delayed_flights (thal); - } - else if (strcmp (commande, "most-delayed-airlines") == 0) - { - printf ("> Resultat de la requete %s :\n", commande); - printf ("\n"); - most_delayed_airlines (thal); - } - else if (strcmp (commande, "delayed-airline") == 0) - { - airline_id = strsep (&requetes, " \n"); + else if (strcmp (commande, "most-delayed-flights") == 0) + { + printf ("> Resultat de la requete %s :\n", commande); + printf ("\n"); + most_delayed_flights (thal); + } + else if (strcmp (commande, "most-delayed-airlines") == 0) + { + printf ("> Resultat de la requete %s :\n", commande); + printf ("\n"); + most_delayed_airlines (thal); + } + else if (strcmp (commande, "delayed-airline") == 0) + { + airline_id = strsep (&requetes2, " \n"); - printf ("> Resultat de la requete %s %s :\n", commande, airline_id); - printf ("\n"); - delayed_airline (airline_id, thal); - } - else if (strcmp (commande, "most-delayed-airlines-at-airport") == 0) - { - airport_id1 = strsep (&requetes, " \n"); + printf ("> Resultat de la requete %s %s :\n", commande, airline_id); + printf ("\n"); + delayed_airline (airline_id, thal); + } + else if (strcmp (commande, "most-delayed-airlines-at-airport") == 0) + { + airport_id1 = strsep (&requetes2, " \n"); - printf ("> Resultat de la requete %s %s :\n", commande, airport_id1); - printf ("\n"); - most_delayed_airlines_at_airport (airport_id1, thal, thap); - } - else if (strcmp (commande, "changed-flights") == 0) - { - date = strsep (&requetes, " \n"); + printf ("> Resultat de la requete %s %s :\n", commande, airport_id1); + printf ("\n"); + most_delayed_airlines_at_airport (airport_id1, thal, thap); + } + else if (strcmp (commande, "changed-flights") == 0) + { + date = strsep (&requetes2, " \n"); - printf ("> Resultat de la requete %s %s :\n", commande, date); - printf ("\n"); - changed_flights (date, thal); - } - else if (strcmp (commande, "avg-flight-duration") == 0) - { - airport_id1 = strsep (&requetes, " \n"); - airport_id2 = strsep (&requetes, " \n"); + printf ("> Resultat de la requete %s %s :\n", commande, date); + printf ("\n"); + changed_flights (date, thal); + } + else if (strcmp (commande, "avg-flight-duration") == 0) + { + airport_id1 = strsep (&requetes2, " \n"); + airport_id2 = strsep (&requetes2, " \n"); - printf ("> Resultat de la requete %s %s %s :\n", commande, airport_id1, airport_id2); + printf ("> Resultat de la requete %s %s %s :\n", commande, airport_id1, airport_id2); + printf ("\n"); + avg_flight_duration (airport_id1, airport_id2, thap); + } + else + { + printf ("Vous avez entré une mauvaise requête ! Veuillez réessayer !\n"); + } printf ("\n"); - avg_flight_duration (airport_id1, airport_id2, thap); } - else - { - printf ("Vous avez entré une mauvaise requête ! Veuillez réessayer !\n"); - } - printf ("\n"); - } - printf ("Le programme va prendre fin ...\n"); + printf ("Le programme va prendre fin ...\n"); + + free_all_airlines (&thal); + free_all_airports (&thap); + free (requetes); + } - free_all_airlines (&thal); - free_all_airports (&thap); + if (is_empty_airlines (thal) == 1) + { + free_all_airlines (&thal); + } + if (is_empty_airports (thap) == 1) + { + free_all_airports (&thap); + } fclose (airline_file); fclose (airport_file);