La structure du projet avance, on peut maintenant charger les résidents et les visiteurs

This commit is contained in:
Antoine Bartuccio 2018-06-06 20:29:02 +02:00
parent 8b29c54d76
commit 8ed34b4593
Signed by: klmp200
GPG Key ID: E7245548C53F904B
14 changed files with 268 additions and 8 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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)
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)

View File

@ -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 ++;

View File

@ -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);

View File

@ -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

View File

@ -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));

34
Resident/Resident.c Normal file
View File

@ -0,0 +1,34 @@
//
// Created by Antoine Bartuccio on 06/06/2018.
//
#include <string.h>
#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;
}

28
Resident/Resident.h Normal file
View File

@ -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

33
Visitor/Visitor.c Normal file
View File

@ -0,0 +1,33 @@
//
// Created by Antoine Bartuccio on 06/06/2018.
//
#include "Visitor.h"
#include <string.h>
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;
}

27
Visitor/Visitor.h Normal file
View File

@ -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

12
main.c
View File

@ -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;
}

7
residents.txt Normal file
View File

@ -0,0 +1,7 @@
Sli;2
Jean;2
Bro;1
Ation;1
Anium;1
Lyne;1
Crack;2

6
visitors.txt Normal file
View File

@ -0,0 +1,6 @@
Roger
Ame
Tharazie
100Triques
Temal
Mara