From 8ed34b4593c8a66db21a66bb9b2fc55d4d8ebfc4 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Wed, 6 Jun 2018 20:29:02 +0200 Subject: [PATCH] =?UTF-8?q?La=20structure=20du=20projet=20avance,=20on=20p?= =?UTF-8?q?eut=20maintenant=20charger=20les=20r=C3=A9sidents=20et=20les=20?= =?UTF-8?q?visiteurs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Building/Building.c | 106 +++++++++++++++++++++++++++++++++++++++++++- Building/Building.h | 7 ++- CMakeLists.txt | 2 +- List/Element.c | 3 +- List/Element.h | 2 + List/List.c | 8 +++- List/List.h | 1 + Resident/Resident.c | 34 ++++++++++++++ Resident/Resident.h | 28 ++++++++++++ Visitor/Visitor.c | 33 ++++++++++++++ Visitor/Visitor.h | 27 +++++++++++ main.c | 12 ++++- residents.txt | 7 +++ visitors.txt | 6 +++ 14 files changed, 268 insertions(+), 8 deletions(-) create mode 100644 Resident/Resident.c create mode 100644 Resident/Resident.h create mode 100644 Visitor/Visitor.c create mode 100644 Visitor/Visitor.h create mode 100644 residents.txt create mode 100644 visitors.txt diff --git a/Building/Building.c b/Building/Building.c index d57756e..c62e3c0 100644 --- a/Building/Building.c +++ b/Building/Building.c @@ -1,13 +1,105 @@ // // Created by Antoine Bartuccio on 05/06/2018. // +#include #include "Building.h" +#include "../Resident/Resident.h" +#include "../Visitor/Visitor.h" + +#define LINE_BUFFER 256 GETTER(Building, Elevator **, elevators) +void remove_end_char(char * string, char character){ + size_t string_size = strlen(string); + if (string[string_size - 1] == character) + string[string_size - 1] = '\0'; +} + +void parse_residents_Building(THIS(Building), char * file){ + 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]; + 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'); + + 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)); + this->residents->insert_tail(this->residents, resident, sizeof(Resident)); + resident->name = NULL; + DELETE(resident); + + free(to_delete_bak); + i++; + } + + free(line); + fclose(f); +} + +void parse_visitors_Building(THIS(Building), char * file){ + FILE * f = fopen(file, "r"); + Visitor * visitor = NULL; + size_t len = LINE_BUFFER; + char * line = NULL; + int i = 0; + + if (f == NULL) + CRASH("File for visitors does not exist"); + + while (getline(&line, &len, f) > 0) { + remove_end_char(line, '\n'); + visitor = NEW(Visitor, i, line); + this->visitors->insert_tail(this->visitors, visitor, sizeof(Visitor)); + visitor->name = NULL; + DELETE(visitor); + i++; + } + + free(line); + fclose(f); +} + +void free_resident(void* data){ + Resident * r = (Resident *) data; + DELETE(r); +} + +void free_visitor(void* data){ + Visitor * v = (Visitor *) data; + DELETE(v); +} + void _free__Building(THIS(Building)){ int i = 0; + + this->residents->clear_custom(this->residents, free_resident); + this->visitors->clear_custom(this->visitors, free_visitor); + DELETE(this->residents); + DELETE(this->visitors); DELETE(this->box); for (i=0; ielevators[i]); @@ -16,21 +108,31 @@ void _free__Building(THIS(Building)){ free(this); } -Building *_init_Building(){ +Building *_init_Building(char * residents_file, char * visitors_file){ Building * new_building = malloc_or_die(sizeof(Building)); int i; new_building->floors = FLOORS; new_building->elevators = malloc_or_die(sizeof(Elevator*) * ELEVATOR_NB); new_building->residents = NEW(List); + new_building->visitors = NEW(List); new_building->box = NEW(CommunicationBox); for (i=0; ielevators[i] = NEW(Elevator); + LINK_ALL(Building, new_building, - get_elevators + get_elevators, + parse_residents, + parse_visitors ) + if (residents_file != NULL) + new_building->parse_residents(new_building, residents_file); + + if (visitors_file != NULL) + new_building->parse_visitors(new_building, visitors_file); + return new_building; } \ No newline at end of file diff --git a/Building/Building.h b/Building/Building.h index 6f34488..b300452 100644 --- a/Building/Building.h +++ b/Building/Building.h @@ -6,7 +6,7 @@ #define LO41_BUILDING_H #define ELEVATOR_NB 3 -#define FLOORS 10 +#define FLOORS 25 #include "../Objects.h" #include "../List/List.h" @@ -16,14 +16,17 @@ typedef struct o_Building { int floors; List * residents; + List * visitors; CommunicationBox * box; Elevator ** elevators; SYNCHRONIZE Elevator ** (*get_elevators)(_THIS(Building)); + PRIVATE void (*parse_residents)(_THIS(Building), char * file); + PRIVATE void (*parse_visitors)(_THIS(Building), char * file); DESTRUCTOR(Building); } Building; -Building *_init_Building(); +Building *_init_Building(char * residents_file, char * visitors_file); #endif //LO41_BUILDING_H diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c3a1e3..775b679 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,4 +4,4 @@ project(LO41 C) set(CMAKE_C_STANDARD 11) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall") -add_executable(LO41 main.c List/List.h List/List.c Objects.h List/Element.c List/Element.h Objects.c Building/Building.c Building/Building.h Elevator/Elevator.c Elevator/Elevator.h CommunicationBox/CommunicationBox.c CommunicationBox/CommunicationBox.h SharedData/SharedData.c SharedData/SharedData.h) \ No newline at end of file +add_executable(LO41 main.c List/List.h List/List.c Objects.h List/Element.c List/Element.h Objects.c Building/Building.c Building/Building.h Elevator/Elevator.c Elevator/Elevator.h CommunicationBox/CommunicationBox.c CommunicationBox/CommunicationBox.h SharedData/SharedData.c SharedData/SharedData.h Visitor/Visitor.c Visitor/Visitor.h Resident/Resident.c Resident/Resident.h) \ No newline at end of file diff --git a/List/Element.c b/List/Element.c index 87058ab..2c48459 100644 --- a/List/Element.c +++ b/List/Element.c @@ -31,7 +31,7 @@ void _free__Element(THIS(Element)){ list->size --; } if (this->data != NULL) - free(this->data); + this->data_free(this->data); free(this); } @@ -48,6 +48,7 @@ Element *_init_Element(void *data, size_t size, List *list){ el->data = new_data; el->previous = NULL; el->next = NULL; + el->data_free = free; if (el->list != NULL) el->list->size ++; diff --git a/List/Element.h b/List/Element.h index 09b38a0..3112382 100644 --- a/List/Element.h +++ b/List/Element.h @@ -14,6 +14,7 @@ struct o_Element { PRIVATE void *data; PRIVATE struct o_Element *next; PRIVATE struct o_Element *previous; + PRIVATE void (*data_free)(void *); PUBLIC struct o_Element *(*get_next)(_THIS(Element)); PUBLIC struct o_Element *(*get_previous)(_THIS(Element)); @@ -29,6 +30,7 @@ FRIENDLY(list, List) FRIENDLY(data, List) FRIENDLY(next, List) FRIENDLY(previous, List) +FRIENDLY(data_free, List) Element *_init_Element(void *data, size_t size, List *list); diff --git a/List/List.c b/List/List.c index 547a946..f042506 100644 --- a/List/List.c +++ b/List/List.c @@ -59,13 +59,14 @@ void _free__List(THIS(List)){ free(this); } -void clear_List(THIS(List)){ +void clear_custom_List(THIS(List), void (*custom_free)(void *)){ Element *current = this->head; Element *to_delete = NULL; while (current != NULL){ to_delete = current; current = current->next; to_delete->list = NULL; + to_delete->data_free = custom_free; DELETE(to_delete); } this->head = NULL; @@ -73,6 +74,10 @@ void clear_List(THIS(List)){ this->size = 0; } +void clear_List(THIS(List)){ + this->clear_custom(this, free); +} + void insert_tail_List(THIS(List), void * data, size_t data_size){ /* Create a new element */ Element *new_element = NEW(Element, data, data_size, this); @@ -146,6 +151,7 @@ List *_init_List(){ insert_inside, insert_tail, insert_head, + clear_custom, clear, remove_head, remove_tail diff --git a/List/List.h b/List/List.h index b4ac79c..ff8847d 100644 --- a/List/List.h +++ b/List/List.h @@ -33,6 +33,7 @@ struct o_List { PUBLIC void (*insert_tail)(_THIS(List), void * data, size_t data_size); PUBLIC void (*insert_head)(_THIS(List), void * data, size_t data_size); PUBLIC void (*clear)(_THIS(List)); + PUBLIC void (*clear_custom)(_THIS(List), void (*custom_free)(void *)); PUBLIC void (*remove_head)(_THIS(List)); PUBLIC void (*remove_tail)(_THIS(List)); diff --git a/Resident/Resident.c b/Resident/Resident.c new file mode 100644 index 0000000..5336b54 --- /dev/null +++ b/Resident/Resident.c @@ -0,0 +1,34 @@ +// +// Created by Antoine Bartuccio on 06/06/2018. +// + +#include +#include "Resident.h" + +GETTER(Resident, char *, name); +GETTER(Resident, int, id); +GETTER(Resident, int, apartment_floor); + +void _free__Resident(THIS(Resident)){ + if (this->name != NULL) + free(this->name); + free(this); +} + +Resident *_init_Resident(int id, char* name, int apartment_floor){ + Resident * new_resident = malloc_or_die(sizeof(Resident)); + new_resident->name = malloc_or_die(sizeof(char) * strlen(name)); + strcpy(new_resident->name, name); + new_resident->id = id; + new_resident->apartment_floor = apartment_floor; + new_resident->position = new_resident->apartment_floor; + new_resident->destination = -1; + + LINK_ALL(Resident, new_resident, + get_name, + get_id, + get_apartment_floor + ) + + return new_resident; +} diff --git a/Resident/Resident.h b/Resident/Resident.h new file mode 100644 index 0000000..09cd9c3 --- /dev/null +++ b/Resident/Resident.h @@ -0,0 +1,28 @@ +// +// Created by Antoine Bartuccio on 06/06/2018. +// + +#ifndef LO41_RESIDENT_H +#define LO41_RESIDENT_H + +#include "../Objects.h" + +typedef struct o_Resident { + PRIVATE int id; + PRIVATE int apartment_floor; + PRIVATE int destination; + PRIVATE int position; + PRIVATE char* name; + + PUBLIC char * (*get_name)(_THIS(Resident)); + PUBLIC int (*get_id)(_THIS(Resident)); + PUBLIC int (*get_apartment_floor)(_THIS(Resident)); + + DESTRUCTOR(Resident); +} Resident; + +FRIENDLY(name, Building) + +Resident *_init_Resident(int id, char * name, int apartment_floor); + +#endif //LO41_RESIDENT_H diff --git a/Visitor/Visitor.c b/Visitor/Visitor.c new file mode 100644 index 0000000..d2a82d6 --- /dev/null +++ b/Visitor/Visitor.c @@ -0,0 +1,33 @@ +// +// Created by Antoine Bartuccio on 06/06/2018. +// + +#include "Visitor.h" +#include + +GETTER(Visitor, char*, name); +GETTER(Visitor, int, id); + +void _free__Visitor(THIS(Visitor)){ + if (this->name != NULL) + free(this->name); + free(this); +} + +Visitor *_init_Visitor(int id, char* name){ + Visitor * new_visitor = malloc_or_die(sizeof(Visitor)); + size_t len = strlen(name); + new_visitor->name = malloc_or_die(sizeof(char) * len); + strcpy(new_visitor->name, name); + new_visitor->id = id; + new_visitor->contact_id = -1; + new_visitor->position = 0; + new_visitor->destination = -1; + + LINK_ALL(Visitor, new_visitor, + get_name, + get_id + ); + + return new_visitor; +} diff --git a/Visitor/Visitor.h b/Visitor/Visitor.h new file mode 100644 index 0000000..307c37b --- /dev/null +++ b/Visitor/Visitor.h @@ -0,0 +1,27 @@ +// +// Created by Antoine Bartuccio on 06/06/2018. +// + +#ifndef LO41_VISITOR_H +#define LO41_VISITOR_H + +#include "../Objects.h" + +typedef struct o_Visitor { + PRIVATE int id; + PRIVATE char * name; + PRIVATE int contact_id; + PRIVATE int position; + PRIVATE int destination; + + PUBLIC char * (*get_name)(_THIS(Visitor)); + PUBLIC int (*get_id)(_THIS(Visitor)); + + DESTRUCTOR(Visitor); +} Visitor; + +FRIENDLY(name, Building) + +Visitor *_init_Visitor(int id, char* name); + +#endif //LO41_VISITOR_H diff --git a/main.c b/main.c index fb2d757..6cc2162 100644 --- a/main.c +++ b/main.c @@ -3,9 +3,13 @@ #include "List/List.h" #include "Building/Building.h" #include "SharedData/SharedData.h" +#include "Visitor/Visitor.h" +#include "Resident/Resident.h" int main() { List *l = NEW(List); + Visitor * roger = NEW(Visitor, 8, "Roger"); + Resident * sli = NEW(Resident, 1, "Sli", 2); char text1[30] = "Patate"; char text2[30] = "Patator"; @@ -27,11 +31,17 @@ int main() { printf("La taille est de %d\n", l->get_size(l)); DELETE(l); - Building *main_building = NEW(Building); + 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); DELETE(main_building); printf("Hello, World!\n"); + printf("%s veut rentrer\n", roger->get_name(roger)); + printf("%s habite dans l'appartement %d\n", sli->get_name(sli), sli->get_apartment_floor(sli)); DELETE(GET_INSTANCE(SharedData)); + DELETE(roger); + DELETE(sli); return 0; } \ No newline at end of file diff --git a/residents.txt b/residents.txt new file mode 100644 index 0000000..19cbe2f --- /dev/null +++ b/residents.txt @@ -0,0 +1,7 @@ +Sli;2 +Jean;2 +Bro;1 +Ation;1 +Anium;1 +Lyne;1 +Crack;2 \ No newline at end of file diff --git a/visitors.txt b/visitors.txt new file mode 100644 index 0000000..f699949 --- /dev/null +++ b/visitors.txt @@ -0,0 +1,6 @@ +Roger +Ame +Tharazie +100Triques +Temal +Mara \ No newline at end of file