diff --git a/bp_tree.c b/bp_tree.c index 97fa2ef668ca3f73019be0815a3f25f8f343aa8d..3c8eca4eaaaa50211d96827529784b65f9357553 100644 --- a/bp_tree.c +++ b/bp_tree.c @@ -1,10 +1,14 @@ #include "bp_tree.h" #include "hash.h" +#include "months.h" #include <inttypes.h> #include <math.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> // UTILS ---------------------------------------------------------- @@ -17,6 +21,8 @@ typedef struct control { const control CONST_CONTR = {.value = 0, .lhs = NULL, .rhs = NULL}; const entry CONST_ENTR = {.phone = "ERROR\0"}; +struct stat st = {0}; // used for directory management + node *bp_create_node() { node *nd = malloc(sizeof(node)); @@ -333,6 +339,11 @@ void bp_print_everything(node *tree) { // if we reached the end of the list if (tree == NULL) return; + if (tree->data[0] == 0) { + printf("Cet arbre est vide\n"); + return; + } + // search for the first leaf on the tree if (!bp_is_leaf(tree)) { return bp_print_everything(tree->childs[0]); @@ -350,48 +361,7 @@ void bp_print_everything(node *tree) { // if we reached the end of the list void print_date(date item) { printf("%d - ", item.day); - - switch (item.month) { - case 1: - printf("JANVIER"); - break; - case 2: - printf("FEVRIER"); - break; - case 3: - printf("MARS"); - break; - case 4: - printf("AVRIL"); - break; - case 5: - printf("MAI"); - break; - case 6: - printf("JUIN"); - break; - case 7: - printf("JUILLET"); - break; - case 8: - printf("AOUT"); - break; - case 9: - printf("SEPTEMBRE"); - break; - case 10: - printf("OCTOBRE"); - break; - case 11: - printf("NOVEMBRE"); - break; - case 12: - printf("DECEMBRE"); - break; - default: - break; - } - + printf("%s", months[item.month]); printf(" - %d\n", item.year + 1900); } @@ -459,3 +429,48 @@ void bp_print(node *root, int depth) { return bp_print(root->childs[i + 1], depth + 1); } } + +// SAVE -------------------------------------------------------------- + +node *bp_find_first(node *nd) { + // search for the first leaf on the tree + if (!bp_is_leaf(nd)) + return bp_find_first(nd->childs[0]); + + return nd; +} + +void bp_save(node *tree) { + + // if the folder doesn't existe, create it + if (stat("database", &st) == -1) + mkdir("database", 0700); + + FILE *fp; + char filename[50]; + + node *nd = bp_find_first(tree); + + while (nd != NULL) { + + for (int i = 0; i < M; i++) { + // Create the file name, here I use the phone number + sprintf(filename, "database/%s.txt", nd->user[i].phone); + + // Write the data in the file + fp = fopen(filename, "w"); + fprintf( + fp, + "NOM : %s\nPRENOM : %s\nTELEPHONE : %s\nDATE : %d - %s - %d", + nd->user[i].l_name, nd->user[i].f_name, nd->user[i].phone, + nd->user[i].birth.day, months[nd->user[i].birth.month], + nd->user[i].birth.year + 1900); + + fclose(fp); + if (nd->data[i + 1] == 0) + break; + } + + nd = nd->next; + } +} \ No newline at end of file diff --git a/bp_tree.h b/bp_tree.h index b33052fecddf4a1ad2368c716895df4d2b4fbd23..9190756da8b4caa074657e62510e83be5a8d84eb 100644 --- a/bp_tree.h +++ b/bp_tree.h @@ -72,4 +72,7 @@ node *bp_insert_val(node *tree, entry person); * @param root Pointer to the root of the tree to print */ void bp_print_as_ll(node *root); + +void bp_save(node *tree); + #endif diff --git a/database/0766159228.txt b/database/0766159228.txt new file mode 100644 index 0000000000000000000000000000000000000000..c669dd3160cb02b99ee9d99c85460b7405382573 --- /dev/null +++ b/database/0766159228.txt @@ -0,0 +1,4 @@ +NOM : Cavagna +PRENOM : Tanguy +TELEPHONE : 0766159228 +DATE : 25 - MAI - 2001 \ No newline at end of file diff --git a/database/0788909566.txt b/database/0788909566.txt new file mode 100644 index 0000000000000000000000000000000000000000..3ac9b5d96b6ef72c827529fa4ef4764cd520113d --- /dev/null +++ b/database/0788909566.txt @@ -0,0 +1,4 @@ +NOM : Tyarks +PRENOM : Richard +TELEPHONE : 0788909566 +DATE : 7 - OCTOBRE - 1999 \ No newline at end of file diff --git a/database/0789444425.txt b/database/0789444425.txt new file mode 100644 index 0000000000000000000000000000000000000000..79ab4ee072ab7a84007f0c142d7da31df1c91954 --- /dev/null +++ b/database/0789444425.txt @@ -0,0 +1,4 @@ +NOM : Schmidt +PRENOM : Alec +TELEPHONE : 0789444425 +DATE : 26 - MAI - 2001 \ No newline at end of file diff --git a/database/0795787730.txt b/database/0795787730.txt new file mode 100644 index 0000000000000000000000000000000000000000..5fcf3e9b8093794ea73af86eb0c29cff3715717b --- /dev/null +++ b/database/0795787730.txt @@ -0,0 +1,4 @@ +NOM : Steinmann +PRENOM : Vincent +TELEPHONE : 0795787730 +DATE : 24 - MAI - 1997 \ No newline at end of file diff --git a/main.c b/main.c index 89374afcf87bf78dbba2f4b4031fa455de5af897..d9b60476b24e338f9d4707fd8628ff283aaa340c 100644 --- a/main.c +++ b/main.c @@ -4,13 +4,13 @@ #include <stdlib.h> int main() { node *tree = bp_create_node(); - + date test = {.day = 26, .month = 5, .year = 101}; entry person; - + /* strcpy(person.f_name, "Alec\0"); strcpy(person.l_name, "Schmidt\0"); strcpy(person.phone, "0789444425\0"); - date test = {.day = 26, .month = 5, .year = 101}; + person.birth = test; tree = bp_insert_val(tree, person); @@ -40,77 +40,98 @@ int main() { test.year = 97; person.birth = test; tree = bp_insert_val(tree, person); +*/ + int quit = 0; + + while (!quit) { + printf("==================================\n" + "Que souhaitez-vous faire ?\n" + "a - Ajout d'une nouvelle entrée\n" + "c - Consulter toutes les entrées\n" + "r - Rechercher une entrée\n" + "s - Sauvegarder l'état actuel\n" + "q - Quitter le programme\n" + "p - Print l'arbre dans son état actuel\n" + "==================================\n"); + + char select; + scanf("%c", &select); + // printf("%c\n", select); + + switch (select) { + case 'a': + START: + printf("Veuillez enter le nom de famille :\n"); + char string[21]; + scanf("%s", string); + strcpy(person.l_name, string); + + printf("Veuillez enter le prénom :\n"); + scanf("%s", string); + strcpy(person.f_name, string); + + printf("Veuillez enter le numéro de téléphone :\n"); + scanf("%s", string); + strcpy(person.phone, string); + + printf("Veuillez entrer le jour de naissance :\n"); + int val; + scanf("%d", &val); + test.day = val; + + printf("Veuillez entrer le mois de naissance :\n"); + scanf("%d", &val); + test.month = val; + + printf("Veuillez entrer l'année de naissance :\n"); + scanf("%d", &val); + test.year = val - 1900; + + person.birth = test; + /* + printf("Est-ce valide ? ('y' pour oui)\n"); + bp_print_entry(person); + + scanf("%c", &select); + if (select != 'y') { + goto START; + }*/ + + tree = bp_insert_val(tree, person); + break; + + case 's': + bp_save(tree); + break; + + case 'c': + bp_print_everything(tree); + break; + + case 'r': + printf("Veuillez entrer le numéro à chercher :"); + char phone_search[11]; + scanf("%s", phone_search); + printf("\nRecherche par n° de téléphone : \n"); + bp_search(tree, phone_search); + break; + + case 'p': + bp_print(tree, 0); + printf("\n"); + break; + + case 'q': + quit = 1; + break; + + default: + break; + } + } + + // bp_print_as_ll(tree); - char phone_search[11] = {"0795787730"}; - bp_print(tree, 0); - printf("\n|||||||||\n"); - - bp_print_everything(tree); - printf("\nRecherche par n° de téléphone : \n"); - bp_search(tree, phone_search); - - /* - tree = bp_insert_val(tree, 6); - bp_print(tree, 0); - - printf("\n|||||||||\n"); - tree = bp_insert_val(tree, 5); - bp_print(tree, 0); - - printf("\n|||||||||\n"); - tree = bp_insert_val(tree, 4); - bp_print(tree, 0); - - printf("\n|||||||||\n"); - tree = bp_insert_val(tree, 8); - bp_print(tree, 0); - - printf("\n|||||||||\n"); - tree = bp_insert_val(tree, 13); - bp_print(tree, 0); - - printf("\n|||||||||\n"); - tree = bp_insert_val(tree, 9); - bp_print(tree, 0); - - printf("\n|||||||||\n"); - tree = bp_insert_val(tree, 15); - bp_print(tree, 0); - - printf("\n|||||||||\n"); - tree = bp_insert_val(tree, 11); - bp_print(tree, 0); - - printf("\n|||||||||\n"); - tree = bp_insert_val(tree, 12); - bp_print(tree, 0); - - printf("\n|||||||||\n"); - tree = bp_insert_val(tree, 7); - bp_print(tree, 0); - - printf("\n|||||||||\n"); - tree = bp_insert_val(tree, 14); - bp_print(tree, 0); - - printf("\n|||||||||\n"); - tree = bp_insert_val(tree, 10); - bp_print(tree, 0); - - printf("\n|||||||||\n"); - tree = bp_insert_val(tree, 16); - bp_print(tree, 0); - - printf("\n|||||||||\n"); - tree = bp_insert_val(tree, 17); - bp_print(tree, 0); - - printf("\n|||||||||\n"); - tree = bp_insert_val(tree, 18); - bp_print(tree, 0); - printf("\n|||||||||\n"); - */ - bp_print_as_ll(tree); bp_destroy(tree); return 0; } \ No newline at end of file diff --git a/makefile b/makefile index cfd646ea774eed6258c4d96490adfa42726f4c12..1b95f842a7b2d0196bd449c25b7cff2c34f1fcb0 100644 --- a/makefile +++ b/makefile @@ -1,6 +1,6 @@ CC=gcc FLAGS= -Werror -CFLAGS= -g -std=c99 -lssl -lcrypto +CFLAGS= -g -std=c99 LDFLAGS= -fsanitize=address -fsanitize=leak @@ -8,7 +8,7 @@ exe: main ./$^ main: main.o bp_tree.o hash.o - $(CC) $^ -o $@ $(CFLAGS) $(LDFLAGS) -lm + $(CC) $^ -o $@ $(CFLAGS) $(LDFLAGS) -lm -lssl -lcrypto main.o: main.c $(CC) $(FLAGS) $(CFLAGS) $(LDFLAGS) -c $^ diff --git a/months.h b/months.h new file mode 100644 index 0000000000000000000000000000000000000000..ce322411c174cad9f48e90cadbca0bce8530a2ce --- /dev/null +++ b/months.h @@ -0,0 +1,18 @@ +#ifndef _MONTHS_H_ +#define _MONTHS_H_ + +char months[13][11] = {{"ERROR\0"}, + {"JANVIER\0"}, + {"FEVRIER\0"}, + {"MARS\0"}, + {"AVRIL\0"}, + {"MAI\0"}, + {"JUIN\0"}, + {"JUILLET\0"}, + {"AOUT\0"}, + {"SEPTEMBRE\0"}, + {"OCTOBRE\0"}, + {"NOVEMBRE\0"}, + {"DECEMBRE\0"}}; + +#endif \ No newline at end of file