mirror of
https://gitlab.com/klmp200/LO41.git
synced 2025-07-11 20:29:24 +00:00
La structure du projet avance, on peut maintenant charger les résidents et les visiteurs
This commit is contained in:
@ -1,13 +1,105 @@
|
||||
//
|
||||
// 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
|
||||
|
||||
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; i<ELEVATOR_NB; i++)
|
||||
DELETE(this->elevators[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; i<ELEVATOR_NB; i++)
|
||||
new_building->elevators[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;
|
||||
}
|
@ -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
|
||||
|
Reference in New Issue
Block a user