2018-06-05 23:15:23 +00:00
|
|
|
//
|
|
|
|
// Created by Antoine Bartuccio on 05/06/2018.
|
|
|
|
//
|
2018-06-06 18:29:02 +00:00
|
|
|
#include <string.h>
|
2018-06-05 23:15:23 +00:00
|
|
|
#include "Building.h"
|
2018-06-06 18:29:02 +00:00
|
|
|
#include "../Resident/Resident.h"
|
|
|
|
#include "../Visitor/Visitor.h"
|
|
|
|
|
|
|
|
#define LINE_BUFFER 256
|
2018-06-06 01:00:35 +00:00
|
|
|
|
2018-06-06 18:29:02 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-06-06 01:00:35 +00:00
|
|
|
void _free__Building(THIS(Building)){
|
|
|
|
int i = 0;
|
2018-06-06 18:29:02 +00:00
|
|
|
|
|
|
|
this->residents->clear_custom(this->residents, free_resident);
|
|
|
|
this->visitors->clear_custom(this->visitors, free_visitor);
|
|
|
|
|
2018-06-06 01:00:35 +00:00
|
|
|
DELETE(this->residents);
|
2018-06-06 18:29:02 +00:00
|
|
|
DELETE(this->visitors);
|
2018-06-06 01:00:35 +00:00
|
|
|
DELETE(this->box);
|
|
|
|
for (i=0; i<ELEVATOR_NB; i++)
|
|
|
|
DELETE(this->elevators[i]);
|
|
|
|
free(this->elevators);
|
|
|
|
|
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
2018-06-06 18:29:02 +00:00
|
|
|
Building *_init_Building(char * residents_file, char * visitors_file){
|
2018-06-06 01:00:35 +00:00
|
|
|
Building * new_building = malloc_or_die(sizeof(Building));
|
2018-06-07 20:04:30 +00:00
|
|
|
char elevator_name[] = "@";
|
2018-06-06 01:00:35 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
new_building->floors = FLOORS;
|
|
|
|
new_building->elevators = malloc_or_die(sizeof(Elevator*) * ELEVATOR_NB);
|
|
|
|
new_building->residents = NEW(List);
|
2018-06-06 18:29:02 +00:00
|
|
|
new_building->visitors = NEW(List);
|
2018-06-06 01:00:35 +00:00
|
|
|
new_building->box = NEW(CommunicationBox);
|
2018-06-07 20:04:30 +00:00
|
|
|
for (i=0; i<ELEVATOR_NB; i++) {
|
|
|
|
elevator_name[0]++;
|
|
|
|
new_building->elevators[i] = NEW(Elevator, elevator_name);
|
|
|
|
}
|
2018-06-06 01:00:35 +00:00
|
|
|
|
|
|
|
|
2018-06-06 18:29:02 +00:00
|
|
|
|
2018-06-06 01:00:35 +00:00
|
|
|
LINK_ALL(Building, new_building,
|
2018-06-06 18:29:02 +00:00
|
|
|
parse_residents,
|
|
|
|
parse_visitors
|
2018-06-06 01:00:35 +00:00
|
|
|
)
|
|
|
|
|
2018-06-06 18:29:02 +00:00
|
|
|
if (residents_file != NULL)
|
|
|
|
new_building->parse_residents(new_building, residents_file);
|
|
|
|
|
|
|
|
if (visitors_file != NULL)
|
|
|
|
new_building->parse_visitors(new_building, visitors_file);
|
|
|
|
|
2018-06-06 01:00:35 +00:00
|
|
|
return new_building;
|
|
|
|
}
|