Skip to content
Snippets Groups Projects
Commit acd4778e authored by Maxence Laurent's avatar Maxence Laurent
Browse files

[FIX] fuite de mémoire

parent bb887dc9
Branches
No related tags found
No related merge requests found
No preview for this file type
...@@ -95,7 +95,7 @@ void create_flight (Flight **fl, char **line, const char separators[]) ...@@ -95,7 +95,7 @@ void create_flight (Flight **fl, char **line, const char separators[])
} }
else else
{ {
(*fl)->airline = word; (*fl)->airline = strdup (word);
} }
word = strsep (line, separators); word = strsep (line, separators);
if (strcmp (word, "") == 0) if (strcmp (word, "") == 0)
...@@ -104,7 +104,7 @@ void create_flight (Flight **fl, char **line, const char separators[]) ...@@ -104,7 +104,7 @@ void create_flight (Flight **fl, char **line, const char separators[])
} }
else else
{ {
(*fl)->org_air = word; (*fl)->org_air = strdup (word);
} }
word = strsep (line, separators); word = strsep (line, separators);
if (strcmp (word, "") == 0) if (strcmp (word, "") == 0)
...@@ -113,7 +113,7 @@ void create_flight (Flight **fl, char **line, const char separators[]) ...@@ -113,7 +113,7 @@ void create_flight (Flight **fl, char **line, const char separators[])
} }
else else
{ {
(*fl)->dest_air = word; (*fl)->dest_air = strdup (word);
} }
word = strsep (line, separators); word = strsep (line, separators);
if (strcmp (word, "") == 0) if (strcmp (word, "") == 0)
...@@ -274,8 +274,9 @@ void load_flights (Table_hash_airlines *thal, Table_hash_airports *thap, FILE *f ...@@ -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. while (getline (&line, &len, fd) != -1) // on récupère chaque ligne.
{ {
char * line2 = line;
Flight *new_flight = malloc (sizeof (Flight)); 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)]; Liste_Airlines *l = thal->tab_airlines[hash_function_airline (new_flight->airline)];
...@@ -372,6 +373,9 @@ void free_all_airlines (Table_hash_airlines *thal) ...@@ -372,6 +373,9 @@ void free_all_airlines (Table_hash_airlines *thal)
{ {
Flight *tmp = thal->tab_airlines[i]->liste_flights; Flight *tmp = thal->tab_airlines[i]->liste_flights;
thal->tab_airlines[i]->liste_flights = NULL; thal->tab_airlines[i]->liste_flights = NULL;
free (tmp->airline);
free (tmp->org_air);
free (tmp->dest_air);
free (tmp); free (tmp);
} }
else else
...@@ -379,6 +383,9 @@ void free_all_airlines (Table_hash_airlines *thal) ...@@ -379,6 +383,9 @@ void free_all_airlines (Table_hash_airlines *thal)
Flight *tmp = thal->tab_airlines[i]->liste_flights; Flight *tmp = thal->tab_airlines[i]->liste_flights;
thal->tab_airlines[i]->liste_flights = thal->tab_airlines[i]->liste_flights->suiv_airline; thal->tab_airlines[i]->liste_flights = thal->tab_airlines[i]->liste_flights->suiv_airline;
tmp->suiv_airline = NULL; tmp->suiv_airline = NULL;
free (tmp->airline);
free (tmp->org_air);
free (tmp->dest_air);
free (tmp); free (tmp);
} }
} }
...@@ -409,3 +416,29 @@ void free_all_airports (Table_hash_airports *thap) ...@@ -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;
}
...@@ -114,3 +114,7 @@ int hash_function_airport (char *); ...@@ -114,3 +114,7 @@ int hash_function_airport (char *);
void free_all_airlines (Table_hash_airlines *); void free_all_airlines (Table_hash_airlines *);
void free_all_airports (Table_hash_airports *); void free_all_airports (Table_hash_airports *);
int is_empty_airlines (Table_hash_airlines);
int is_empty_airports (Table_hash_airports);
...@@ -26,11 +26,16 @@ int main () ...@@ -26,11 +26,16 @@ int main ()
load_airports (&thap, airport_file); 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); load_flights (&thal, &thap, flights_file);
} }
if (is_empty_airlines (thal) == 0 || is_empty_airports (thap) == 0)
{
printf ("Chargement des fichiers impossible, veuillez vérifier les fichiers !\n");
}
else
{
printf ("Chargement terminé !\n"); printf ("Chargement terminé !\n");
char * requetes = NULL; char * requetes = NULL;
...@@ -45,7 +50,8 @@ int main () ...@@ -45,7 +50,8 @@ int main ()
while (getline (&requetes, &len, stdin) != -1) while (getline (&requetes, &len, stdin) != -1)
{ {
commande = strsep (&requetes, " "); char *requetes2 = requetes;
commande = strsep (&requetes2, " ");
commande = strtok (commande, "\n"); commande = strtok (commande, "\n");
if (strcmp (commande, "quit") == 0) if (strcmp (commande, "quit") == 0)
{ {
...@@ -53,24 +59,24 @@ int main () ...@@ -53,24 +59,24 @@ int main ()
} }
else if (strcmp (commande, "show-airports") == 0) else if (strcmp (commande, "show-airports") == 0)
{ {
airline_id = strsep (&requetes, " \n"); airline_id = strsep (&requetes2, " \n");
printf ("> Resultat de la requete %s %s :\n", commande, airline_id); printf ("> Resultat de la requete %s %s :\n", commande, airline_id);
printf ("\n"); printf ("\n");
show_airports (airline_id, thal, thap); show_airports (airline_id, thal, thap);
} }
else if (strcmp (commande, "show-airlines") == 0) else if (strcmp (commande, "show-airlines") == 0)
{ {
airport_id1 = strsep (&requetes, " \n"); airport_id1 = strsep (&requetes2, " \n");
printf ("> Resultat de la requete %s %s :\n", commande, airport_id1); printf ("> Resultat de la requete %s %s :\n", commande, airport_id1);
printf ("\n"); printf ("\n");
show_airlines (airport_id1, thal, thap); show_airlines (airport_id1, thal, thap);
} }
else if (strcmp (commande, "show-flights") == 0) else if (strcmp (commande, "show-flights") == 0)
{ {
airport_id1 = strsep (&requetes, " \n"); airport_id1 = strsep (&requetes2, " \n");
date = strsep (&requetes, " \n"); date = strsep (&requetes2, " \n");
time = strsep (&requetes, " \n"); time = strsep (&requetes2, " \n");
limit = strsep (&requetes, " \n"); limit = strsep (&requetes2, " \n");
if (limit == NULL) if (limit == NULL)
{ {
...@@ -114,7 +120,7 @@ int main () ...@@ -114,7 +120,7 @@ int main ()
} }
else if (strcmp (commande, "delayed-airline") == 0) else if (strcmp (commande, "delayed-airline") == 0)
{ {
airline_id = strsep (&requetes, " \n"); airline_id = strsep (&requetes2, " \n");
printf ("> Resultat de la requete %s %s :\n", commande, airline_id); printf ("> Resultat de la requete %s %s :\n", commande, airline_id);
printf ("\n"); printf ("\n");
...@@ -122,7 +128,7 @@ int main () ...@@ -122,7 +128,7 @@ int main ()
} }
else if (strcmp (commande, "most-delayed-airlines-at-airport") == 0) else if (strcmp (commande, "most-delayed-airlines-at-airport") == 0)
{ {
airport_id1 = strsep (&requetes, " \n"); airport_id1 = strsep (&requetes2, " \n");
printf ("> Resultat de la requete %s %s :\n", commande, airport_id1); printf ("> Resultat de la requete %s %s :\n", commande, airport_id1);
printf ("\n"); printf ("\n");
...@@ -130,7 +136,7 @@ int main () ...@@ -130,7 +136,7 @@ int main ()
} }
else if (strcmp (commande, "changed-flights") == 0) else if (strcmp (commande, "changed-flights") == 0)
{ {
date = strsep (&requetes, " \n"); date = strsep (&requetes2, " \n");
printf ("> Resultat de la requete %s %s :\n", commande, date); printf ("> Resultat de la requete %s %s :\n", commande, date);
printf ("\n"); printf ("\n");
...@@ -138,8 +144,8 @@ int main () ...@@ -138,8 +144,8 @@ int main ()
} }
else if (strcmp (commande, "avg-flight-duration") == 0) else if (strcmp (commande, "avg-flight-duration") == 0)
{ {
airport_id1 = strsep (&requetes, " \n"); airport_id1 = strsep (&requetes2, " \n");
airport_id2 = strsep (&requetes, " \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"); printf ("\n");
...@@ -156,6 +162,17 @@ int main () ...@@ -156,6 +162,17 @@ int main ()
free_all_airlines (&thal); free_all_airlines (&thal);
free_all_airports (&thap); free_all_airports (&thap);
free (requetes);
}
if (is_empty_airlines (thal) == 1)
{
free_all_airlines (&thal);
}
if (is_empty_airports (thap) == 1)
{
free_all_airports (&thap);
}
fclose (airline_file); fclose (airline_file);
fclose (airport_file); fclose (airport_file);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment