mirror of
https://gitlab.com/klmp200/LO41.git
synced 2024-11-21 16:23:22 +00:00
Meilleure gestion des scénari
This commit is contained in:
parent
74194d46cb
commit
bd5860579e
@ -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++;
|
||||
|
@ -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,
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
12
main.c
12
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));
|
||||
|
@ -1,7 +1,7 @@
|
||||
Sli;2
|
||||
Jean;2
|
||||
Bro;1
|
||||
Ation;1
|
||||
Anium;1
|
||||
Lyne;1
|
||||
Crack;2
|
||||
Sli;2;2
|
||||
Jean;2;8
|
||||
Bro;1;1
|
||||
Ation;1;3
|
||||
Anium;1;3
|
||||
Lyne;1;1
|
||||
Crack;2;2
|
12
visitors.txt
12
visitors.txt
@ -1,6 +1,6 @@
|
||||
Roger
|
||||
Ame
|
||||
Tharazie
|
||||
100Triques
|
||||
Temal
|
||||
Mara
|
||||
Roger;Sli
|
||||
Ame;Sli
|
||||
Tharazie;Anium
|
||||
100Triques;Jean
|
||||
Temal;Anium
|
||||
Mara;Sli
|
Loading…
Reference in New Issue
Block a user