From 6167f5d84d117976fb8465bedb5c0f155f24dfe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20MARQUET?= Date: Mon, 12 Jun 2023 15:29:58 +0200 Subject: [PATCH] add the table for the time search --- BackEnd/Filmotheque.c | 65 ++++++++++++++++++++++++++++++++++++++++++- BackEnd/Filmotheque.h | 4 ++- BackEnd/List.c | 7 +++++ BackEnd/main.c | 21 ++++++++++++++ 4 files changed, 95 insertions(+), 2 deletions(-) diff --git a/BackEnd/Filmotheque.c b/BackEnd/Filmotheque.c index eb5c631..33033f5 100644 --- a/BackEnd/Filmotheque.c +++ b/BackEnd/Filmotheque.c @@ -1,4 +1,5 @@ #define NUMBER_OF_CHAR 100 +#define LENGTH 12600 #include "Filmotheque.h" @@ -29,7 +30,6 @@ void addMovie(struct Filmotheque* filmotheque, char* real, char* movie, char* ti } node = node->children[27]; } - //If the character is a space, we put the node in the 28th case of the array else if (real[i] == ' ') { if (node->children[28] == NULL) { node->children[28] = createEmptyNodeTrie(); @@ -139,4 +139,67 @@ char* toLower(char* name) { } lower[length] = '\0'; return lower; +} + +int stopServer(){ + return 8; +} + +struct List* createTable(char* nameFile){ + FILE* fichier; + fichier = fopen(nameFile, "r"); + + if (fichier == NULL) { + printf("Erreur lors de l'ouverture du fichier"); + exit(1); + } + + char* real = malloc(sizeof(char)); + if (real == NULL) { + printf("error malloc"); + return NULL; + } + char* movie = malloc(sizeof(char)); + if (movie == NULL) { + printf("error malloc"); + return NULL; + } + char* time = malloc(sizeof(char)); + if (time == NULL) { + printf("error malloc"); + return NULL; + } + char* category = malloc(sizeof(char)); + if (category == NULL) { + printf("error malloc"); + return NULL; + } + char line[NUMBER_OF_CHAR]; + + struct List* tableau[LENGTH]; + for(int i = 0; i < LENGTH; i++){ + tableau[i] = NULL; + } + + while(fgets(line,sizeof(line),fichier) != NULL){ + line[strcspn(line, "\r\n")] = '\0'; + real = toLower((strtok(line, ";"))); + movie = strtok(NULL, ";"); + time = strtok(NULL, ";"); + category = strtok(NULL, ";"); + addMovieInTable(tableau,real,movie,time,category); + } + + fclose(fichier); + + return tableau; +} + + +void addMovieInTable(struct List** tableau, char* real, char* movie, char* time, char* category){ + int index = atoi(time); + if(tableau[index] == NULL){ + tableau[index] = createEmptyList(); + } + addFirst(tableau[index],real,movie,time,category); } \ No newline at end of file diff --git a/BackEnd/Filmotheque.h b/BackEnd/Filmotheque.h index c56f542..7a311f5 100644 --- a/BackEnd/Filmotheque.h +++ b/BackEnd/Filmotheque.h @@ -1,7 +1,7 @@ #ifndef PROJETCGROUPE8_MAIN_FILMOTHEQUE_H #define PROJETCGROUPE8_MAIN_FILMOTHEQUE_H #define NUMBER_OF_CHAR 100 - +#define LENGTH 12600 #include "NodeTrie.h" #include @@ -21,5 +21,7 @@ struct List* searchByDirector(struct Filmotheque* filmotheque, char* director); void deleteFilmotheque(struct Filmotheque** filmotheque); struct Filmotheque* recupInfo(char* nameFile); char* toLower(char* name); +struct List* createTable(char* nameFile); +void addMovieInTable(struct List** tableau, char* real, char* movie, char* time, char* category); #endif //PROJETCGROUPE8_MAIN_FILMOTHEQUE_H diff --git a/BackEnd/List.c b/BackEnd/List.c index 9e2c42c..ece00aa 100644 --- a/BackEnd/List.c +++ b/BackEnd/List.c @@ -46,6 +46,9 @@ void addFirst(struct List* l, char* director, char* name, char* time, char* cate } bool isListEmpty(struct List* l){ + if(l == NULL){ + return true; + } return l->size == 0; } @@ -67,6 +70,10 @@ void deleteList(struct List** l){ } void printList(struct List* l){ + if(isListEmpty(l)){ + printf("List is empty\n"); + return; + } struct Cell* tmp = l->head; while(tmp != NULL){ printf("%s %s %s %s\n", tmp->director, tmp->name, tmp->time, tmp->category); diff --git a/BackEnd/main.c b/BackEnd/main.c index 29f66c1..ac79df0 100644 --- a/BackEnd/main.c +++ b/BackEnd/main.c @@ -5,15 +5,36 @@ #include "List.h" #include "Filmotheque.h" +#include int main() { struct Filmotheque* filmotheque2 = recupInfo("../BD_small.txt"); struct List* test3 = searchByDirector(filmotheque2, "fincher"); printList(test3); + printf("\n"); struct List* test1456 = searchByDirector(filmotheque2, "johnson"); printList(test1456); deleteList(&test1456); deleteList(&test3); + struct List* test7854 = createTable("../BD_small.txt"); + printList(test7854); deleteFilmotheque(&filmotheque2); + /* + clock_t t2; + t2 = clock(); + struct Filmotheque* filmotheque = recupInfo("../BD_big.txt"); + t2 = clock() - t2; + double time_taken2 = ((double)t2)/CLOCKS_PER_SEC; + clock_t t; + t = clock(); + struct List* test = searchByDirector(filmotheque, "leonard"); + t = clock() - t; + double time_taken = ((double)t)/CLOCKS_PER_SEC; + printList(test); + deleteList(&test); + deleteFilmotheque(&filmotheque); + printf("recupInfo() took %f seconds to execute \n", time_taken2); + printf("searchByDirector() took %f seconds to execute \n", time_taken); + */ return 0; } \ No newline at end of file