Skip to content
Snippets Groups Projects
Commit e14b25ba authored by Alec's avatar Alec
Browse files

save locally and main remade

parent acbd5bf3
No related branches found
No related tags found
No related merge requests found
#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
......@@ -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
NOM : Cavagna
PRENOM : Tanguy
TELEPHONE : 0766159228
DATE : 25 - MAI - 2001
\ No newline at end of file
NOM : Tyarks
PRENOM : Richard
TELEPHONE : 0788909566
DATE : 7 - OCTOBRE - 1999
\ No newline at end of file
NOM : Schmidt
PRENOM : Alec
TELEPHONE : 0789444425
DATE : 26 - MAI - 2001
\ No newline at end of file
NOM : Steinmann
PRENOM : Vincent
TELEPHONE : 0795787730
DATE : 24 - MAI - 1997
\ No newline at end of file
......@@ -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;
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);
person.birth = test;
/*
tree = bp_insert_val(tree, 6);
bp_print(tree, 0);
printf("Est-ce valide ? ('y' pour oui)\n");
bp_print_entry(person);
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);
scanf("%c", &select);
if (select != 'y') {
goto START;
}*/
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);
tree = bp_insert_val(tree, person);
break;
printf("\n|||||||||\n");
tree = bp_insert_val(tree, 11);
bp_print(tree, 0);
case 's':
bp_save(tree);
break;
printf("\n|||||||||\n");
tree = bp_insert_val(tree, 12);
bp_print(tree, 0);
case 'c':
bp_print_everything(tree);
break;
printf("\n|||||||||\n");
tree = bp_insert_val(tree, 7);
bp_print(tree, 0);
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;
printf("\n|||||||||\n");
tree = bp_insert_val(tree, 14);
case 'p':
bp_print(tree, 0);
printf("\n");
break;
printf("\n|||||||||\n");
tree = bp_insert_val(tree, 10);
bp_print(tree, 0);
case 'q':
quit = 1;
break;
printf("\n|||||||||\n");
tree = bp_insert_val(tree, 16);
bp_print(tree, 0);
default:
break;
}
}
printf("\n|||||||||\n");
tree = bp_insert_val(tree, 17);
bp_print(tree, 0);
// bp_print_as_ll(tree);
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
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 $^
......
months.h 0 → 100644
#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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment