1
0
mirror of https://gitlab.com/klmp200/LO41.git synced 2025-07-11 20:29:24 +00:00

Meilleure gestion des scénari

This commit is contained in:
2018-06-10 17:57:39 +02:00
parent 74194d46cb
commit bd5860579e
8 changed files with 60 additions and 45 deletions

View File

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