From bd5860579ed8fd4c32c37edd1185e3d14c320b05 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Sun, 10 Jun 2018 17:57:39 +0200 Subject: [PATCH] =?UTF-8?q?Meilleure=20gestion=20des=20sc=C3=A9nari?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Building/Building.c | 46 +++++++++++++++++++++++++-------------------- Resident/Resident.c | 5 +++-- Resident/Resident.h | 2 +- Visitor/Visitor.c | 10 ++++++++-- Visitor/Visitor.h | 4 ++-- main.c | 12 +++++++----- residents.txt | 14 +++++++------- visitors.txt | 12 ++++++------ 8 files changed, 60 insertions(+), 45 deletions(-) diff --git a/Building/Building.c b/Building/Building.c index 229d3ad..2f4b915 100644 --- a/Building/Building.c +++ b/Building/Building.c @@ -14,42 +14,43 @@ void remove_end_char(char * string, char character){ string[string_size - 1] = '\0'; } +void split(char * line, int number_to_split, char output[][LINE_BUFFER], char separator){ + char * token = NULL; + char * to_delete = NULL; + char * to_delete_back = NULL; + int i = 0; + + + to_delete = strdup(line); + to_delete_back = to_delete; + while(i < number_to_split && (token = strsep(&to_delete, &separator)) != NULL){ + strcpy(output[i], token); + i++; + } + free(to_delete_back); +} + void parse_residents_Building(THIS(Building), char * file){ + /* File format is name;appartment_floor;destination */ FILE * f = fopen(file, "r"); Resident * resident = NULL; size_t len = LINE_BUFFER; char * line = NULL; - char * token = NULL; - char * to_delete = NULL; - char * to_delete_bak = NULL; char * trash; - char separator = ';'; - char data[2][LINE_BUFFER]; + char data[3][LINE_BUFFER]; int i = 0; - int j = 0; if (f == NULL) CRASH("File for residents does not exist"); while (getline(&line, &len, f) > 0) { - j = 0; remove_end_char(line, '\n'); + split(line, 3, data, ';'); - to_delete = strdup(line); - to_delete_bak = to_delete; - - /* Split on ; */ - while (j < 2 && (token = strsep(&to_delete, &separator)) != NULL){ - strcpy(data[j], token); - j++; - } - - resident = NEW(Resident, i, data[0], (int) strtol(data[1], &trash, 10)); + resident = NEW(Resident, i, data[0], (int) strtol(data[1], &trash, 10), (int) strtol(data[2], &trash, 10)); this->residents->insert_tail(this->residents, resident, sizeof(Resident)); resident->name = NULL; DELETE(resident); - - free(to_delete_bak); i++; } @@ -58,10 +59,12 @@ void parse_residents_Building(THIS(Building), char * file){ } void parse_visitors_Building(THIS(Building), char * file){ + /* File format is name;contact_name */ FILE * f = fopen(file, "r"); Visitor * visitor = NULL; size_t len = LINE_BUFFER; char * line = NULL; + char data[2][LINE_BUFFER]; int i = 0; if (f == NULL) @@ -69,8 +72,11 @@ void parse_visitors_Building(THIS(Building), char * file){ while (getline(&line, &len, f) > 0) { remove_end_char(line, '\n'); - visitor = NEW(Visitor, i, line); + split(line, 2, data, ';'); + + visitor = NEW(Visitor, i, data[0], data[1]); this->visitors->insert_tail(this->visitors, visitor, sizeof(Visitor)); + visitor->contact_name = NULL; visitor->name = NULL; DELETE(visitor); i++; diff --git a/Resident/Resident.c b/Resident/Resident.c index e99e77d..95575c4 100644 --- a/Resident/Resident.c +++ b/Resident/Resident.c @@ -15,13 +15,14 @@ void _free__Resident(THIS(Resident)){ free(this); } -Resident *_init_Resident(int id, char* name, int apartment_floor){ +Resident *_init_Resident(int id, char* name, int apartment_floor, int destination){ + /* If the destination is the same as the apartment_floor or negative, the resident will not move */ Resident * new_resident = malloc_or_die(sizeof(Resident)); new_resident->name = strdup(name); new_resident->id = id; new_resident->apartment_floor = apartment_floor; new_resident->position = new_resident->apartment_floor; - new_resident->destination = -1; + new_resident->destination = destination; LINK_ALL(Resident, new_resident, get_name, diff --git a/Resident/Resident.h b/Resident/Resident.h index 09cd9c3..54b4dc7 100644 --- a/Resident/Resident.h +++ b/Resident/Resident.h @@ -23,6 +23,6 @@ typedef struct o_Resident { FRIENDLY(name, Building) -Resident *_init_Resident(int id, char * name, int apartment_floor); +Resident *_init_Resident(int id, char * name, int apartment_floor, int destination); #endif //LO41_RESIDENT_H diff --git a/Visitor/Visitor.c b/Visitor/Visitor.c index f9ebe3a..9d4ff93 100644 --- a/Visitor/Visitor.c +++ b/Visitor/Visitor.c @@ -11,17 +11,23 @@ GETTER(Visitor, int, id); void _free__Visitor(THIS(Visitor)){ if (this->name != NULL) free(this->name); + if (this->contact_name != NULL) + free(this->contact_name); free(this); } -Visitor *_init_Visitor(int id, char* name){ +Visitor *_init_Visitor(int id, char* name, char * contact_name){ Visitor * new_visitor = malloc_or_die(sizeof(Visitor)); new_visitor->name = strdup(name); new_visitor->id = id; - new_visitor->contact_id = -1; new_visitor->position = 0; new_visitor->destination = -1; + if (contact_name != NULL) + new_visitor->contact_name = strdup(contact_name); + else + new_visitor->contact_name = NULL; + LINK_ALL(Visitor, new_visitor, get_name, get_id diff --git a/Visitor/Visitor.h b/Visitor/Visitor.h index 307c37b..e1fdc06 100644 --- a/Visitor/Visitor.h +++ b/Visitor/Visitor.h @@ -10,7 +10,7 @@ typedef struct o_Visitor { PRIVATE int id; PRIVATE char * name; - PRIVATE int contact_id; + PRIVATE char * contact_name; PRIVATE int position; PRIVATE int destination; @@ -22,6 +22,6 @@ typedef struct o_Visitor { FRIENDLY(name, Building) -Visitor *_init_Visitor(int id, char* name); +Visitor *_init_Visitor(int id, char* name, char* contact_name); #endif //LO41_VISITOR_H diff --git a/main.c b/main.c index 9c4922b..0fd7dfa 100644 --- a/main.c +++ b/main.c @@ -26,8 +26,8 @@ void clean_exit(int error_code){ int main() { List *l = NEW(List); - Visitor * roger = NEW(Visitor, 8, "Roger"); - Resident * sli = NEW(Resident, 1, "Sli", 2); + Visitor * roger = NEW(Visitor, 8, "Roger", "Sli"); + Resident * sli = NEW(Resident, 1, "Sli", 2, 2); char text1[30] = "Patate"; char text2[30] = "Patator"; pthread_mutex_t mutex; @@ -62,14 +62,16 @@ int main() { Building *main_building = NEW(Building, "../residents.txt", "../visitors.txt"); printf("Il y a %d étages dans l'immeuble\n", main_building->floors); - printf("%s est le second visiteur\n", ((Visitor *) main_building->visitors->get_element_data(main_building->visitors, 1))->name); - printf("%s est le second resident\n", ((Resident *) main_building->residents->get_element_data(main_building->residents, 1))->name); + printf("%s est le second visiteur, il veut aller voir %s\n", ((Visitor *) main_building->visitors->get_element_data(main_building->visitors, 1))->name, + ((Visitor *) main_building->visitors->get_element_data(main_building->visitors, 1))->contact_name); + printf("%s est le second resident, il veut aller à l'étage %d\n", ((Resident *) main_building->residents->get_element_data(main_building->residents, 1))->name, + ((Resident *) main_building->residents->get_element_data(main_building->residents, 1))->destination); for (int i=0; i < ELEVATOR_NB; i++) printf("Ascenseur %s\n", main_building->elevators[i]->name); DELETE(main_building); printf("Hello, World!\n"); - printf("%s veut rentrer\n", roger->get_name(roger)); + printf("%s veut rentrer et aller voir %s\n", roger->get_name(roger), roger->contact_name); printf("%s habite dans l'appartement %d\n", sli->get_name(sli), sli->get_apartment_floor(sli)); DELETE(GET_INSTANCE(SharedData)); diff --git a/residents.txt b/residents.txt index 19cbe2f..cc39648 100644 --- a/residents.txt +++ b/residents.txt @@ -1,7 +1,7 @@ -Sli;2 -Jean;2 -Bro;1 -Ation;1 -Anium;1 -Lyne;1 -Crack;2 \ No newline at end of file +Sli;2;2 +Jean;2;8 +Bro;1;1 +Ation;1;3 +Anium;1;3 +Lyne;1;1 +Crack;2;2 \ No newline at end of file diff --git a/visitors.txt b/visitors.txt index f699949..2fb9ff8 100644 --- a/visitors.txt +++ b/visitors.txt @@ -1,6 +1,6 @@ -Roger -Ame -Tharazie -100Triques -Temal -Mara \ No newline at end of file +Roger;Sli +Ame;Sli +Tharazie;Anium +100Triques;Jean +Temal;Anium +Mara;Sli \ No newline at end of file