mirror of
https://gitlab.com/klmp200/LO41.git
synced 2024-11-25 10:34:17 +00:00
138 lines
3.0 KiB
C
138 lines
3.0 KiB
C
//
|
|
// Created by Antoine Bartuccio on 05/06/2018.
|
|
//
|
|
#include <string.h>
|
|
#include "Building.h"
|
|
#include "../Resident/Resident.h"
|
|
#include "../Visitor/Visitor.h"
|
|
|
|
#define LINE_BUFFER 256
|
|
|
|
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; i<ELEVATOR_NB; i++)
|
|
DELETE(this->elevators[i]);
|
|
free(this->elevators);
|
|
|
|
free(this);
|
|
}
|
|
|
|
Building *_init_Building(char * residents_file, char * visitors_file){
|
|
Building * new_building = malloc_or_die(sizeof(Building));
|
|
char elevator_name[] = "@";
|
|
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; i<ELEVATOR_NB; i++) {
|
|
elevator_name[0]++;
|
|
new_building->elevators[i] = NEW(Elevator, elevator_name);
|
|
}
|
|
|
|
|
|
|
|
LINK_ALL(Building, new_building,
|
|
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;
|
|
} |