diff --git a/a.c b/a.c new file mode 100644 index 0000000000000000000000000000000000000000..70b5b9aff0b5af4f20193d014db73e020807bb84 --- /dev/null +++ b/a.c @@ -0,0 +1,37 @@ +#include <assert.h> +// Warning : stdbool +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> + +#include "bptree.h" + +int main() { + BPTreeNode *root = bptree_init(2); + + uint64_t input[] = {8, 12, 11, 47, 22, 95, 86, 40, 33, 78, 28, 5, 75, 88, 21, 56, 82, 51, 93, 66, 48, 70, 57, 65, 35, 4, 60, 41, 49, 55, 68, 72, 23, 31, 30, 42, 18, 87, 24, 58}; + IntegerArray *keys = IntegerArray_init(40); + for (int i = 0; i < 40; i++) { + IntegerArray_append(keys, input[i]); + } + + IntegerArray_print(keys); + + for (int i = 0; i < keys->size; i++) { + bptree_insert(root, keys->items[i], keys->items[i] * 1000); + } + + bptree_print(root, 0); + + for (int i = 0; i < keys->size; i++) { + uint64_t data; + bool found = bptree_search(root, keys->items[i], &data); + + assert(found == true); + assert(data == keys->items[i] * 1000); + } + + IntegerArray_destroy(&keys); + bptree_destroy(&root); + return EXIT_SUCCESS; +} diff --git a/src/Directory.c b/src/Directory.c index 796581f27b7fb65a58d648b7218d7391f0892007..874f0dede9b0bdfd813540b18ac60f5bb52e8d14 100644 --- a/src/Directory.c +++ b/src/Directory.c @@ -1,15 +1,39 @@ #include "Directory.h" +#include <openssl/sha.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include "DirectoryRecord.h" +#include "bptree.h" + +static uint64_t hash_string(char *str) { + SHA256_CTX sha256; + SHA256_Init(&sha256); + SHA256_Update(&sha256, str, strlen(str)); + unsigned char hash[SHA256_DIGEST_LENGTH]; + SHA256_Final(hash, &sha256); + + uint64_t truncated_hash = 0; + for (int i = 0; i < 8; i++) { + truncated_hash |= hash[i]; + + if (i != 7) { + // No shift because it is the last byte. + truncated_hash <<= 8; + } + } + + return truncated_hash; +} Directory *Directory_init() { Directory *directory = (Directory *)malloc(sizeof(Directory)); directory->records_length = 0; // WARNING directory->records = (DirectoryRecord **)malloc(sizeof(Directory *) * 1000); + directory->index = bptree_init(DEFAULT_ORDER); return directory; } @@ -19,11 +43,17 @@ void Directory_destroy(Directory **directory) { } free((*directory)->records); + bptree_destroy(&(*directory)->index); free(*directory); *directory = NULL; } void Directory_print(Directory *directory) { + if (directory->records_length == 0) { + printf("===>There are no recorded members in the directory.\n"); + return; + } + printf("========================\n"); for (int i = 0; i < directory->records_length; i++) { @@ -37,7 +67,18 @@ void Directory_print(Directory *directory) { printf("========================\n"); } -void Directory_insert(Directory *directory, DirectoryRecord *record) { - directory->records[directory->records_length] = record; +void Directory_append(Directory *directory, DirectoryRecord *record) { + int index = directory->records_length; + directory->records[index] = record; directory->records_length += 1; + bptree_insert(directory->index, hash_string(record->phone_number), (uint64_t)index); +} + +DirectoryRecord *Directory_search(Directory *directory, char phone_number[11]) { + uint64_t index; + if (bptree_search(directory->index, hash_string(phone_number), &index)) { + return directory->records[(int)index]; + } else { + return NULL; + } } diff --git a/src/Directory.h b/src/Directory.h index 6063a5ff31f8dc4d4d07c6143fadd26fe8318c98..1245d91b09140d25fa6c80410f00dfa1d710d2d5 100644 --- a/src/Directory.h +++ b/src/Directory.h @@ -2,15 +2,20 @@ #define DIRECTORY_H #include "DirectoryRecord.h" +#include "bptree.h" + +#define DEFAULT_ORDER 2 typedef struct Directory { int records_length; DirectoryRecord **records; + BPTreeNode *index; } Directory; Directory *Directory_init(); void Directory_destroy(Directory **directory); void Directory_print(Directory *directory); -void Directory_insert(Directory *directory, DirectoryRecord *record); +void Directory_append(Directory *directory, DirectoryRecord *record); +DirectoryRecord *Directory_search(Directory *directory, char phone_number[11]); #endif diff --git a/src/Makefile b/src/Makefile index bc8383b49240801ba1809020bae83d1506fe0588..f5a31feed18d46f8b1a665c8636e9369d4f77d78 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,7 +1,7 @@ TARGET = program -LIBS = -lm +LIBS = -lm -lssl -lcrypto CC = gcc -CFLAGS = -g -Wall -Wextra -pedantic -Ofast +CFLAGS = -g -Wall -Wextra -pedantic CFLAGS += -fsanitize=address -fsanitize=leak .PHONY: default all clean diff --git a/src/bptree.h b/src/bptree.h index dcf15be76b524de37acb5956f952dd092d3f7d9f..80b5e83569c686fab472844d868ff51bf37b887e 100644 --- a/src/bptree.h +++ b/src/bptree.h @@ -1,5 +1,5 @@ #ifndef BPTREE_H -#define BPTREE_h +#define BPTREE_H #include <stdbool.h> #include <stdint.h> diff --git a/src/main.c b/src/main.c index 70b5b9aff0b5af4f20193d014db73e020807bb84..b271d5211248afd0dc402bbc98b1758ca0c8fa3e 100644 --- a/src/main.c +++ b/src/main.c @@ -1,37 +1,118 @@ -#include <assert.h> -// Warning : stdbool -#include <stdbool.h> #include <stdio.h> #include <stdlib.h> -#include "bptree.h" +#include "Directory.h" +#include "DirectoryRecord.h" -int main() { - BPTreeNode *root = bptree_init(2); +void clear_buffer() { + int c; + while ((c = getchar()) != '\n' && c != EOF) { + } +} + +void append_record(Directory *directory) { + char phone_number[11]; + printf("Enter the phone number: "); + scanf("%s", phone_number); + clear_buffer(); + + char name[21]; + printf("Enter the name: "); + scanf("%s", name); + clear_buffer(); + + char surname[21]; + printf("Enter the surname: "); + scanf("%s", surname); + clear_buffer(); + + printf("Enter the birth date (Y-m-d): "); + // TODO : check more + int birth_date_year, birth_date_month, birth_date_day; + scanf("%d-%d-%d", &birth_date_year, &birth_date_month, &birth_date_day); + clear_buffer(); + + printf("\nIs the information entered correct? (Y/n) "); + + char choice = getchar(); + if (choice != '\n') { + getchar(); + } - uint64_t input[] = {8, 12, 11, 47, 22, 95, 86, 40, 33, 78, 28, 5, 75, 88, 21, 56, 82, 51, 93, 66, 48, 70, 57, 65, 35, 4, 60, 41, 49, 55, 68, 72, 23, 31, 30, 42, 18, 87, 24, 58}; - IntegerArray *keys = IntegerArray_init(40); - for (int i = 0; i < 40; i++) { - IntegerArray_append(keys, input[i]); + if (choice == 'n') { + printf("\n===>The procedure has been cancelled.\n"); + } else { + DirectoryRecord *record = DirectoryRecord_init(false, phone_number, name, surname, birth_date_year, birth_date_month, birth_date_day); + Directory_append(directory, record); + printf("\n===>The record has been successfully saved.\n"); } +} - IntegerArray_print(keys); +void search_record(Directory *directory) { + printf("Enter the phone number that corresponds to the record you are looking for: "); + char phone_number[11]; + scanf("%s", phone_number); + clear_buffer(); + DirectoryRecord *record = Directory_search(directory, phone_number); + printf("\n"); - for (int i = 0; i < keys->size; i++) { - bptree_insert(root, keys->items[i], keys->items[i] * 1000); + if (record == NULL) { + printf("===>There are no records for this phone number.\n"); + } else { + printf("========================\n"); + DirectoryRecord_print(record); + printf("========================\n"); } +} + +int main() { + Directory *directory = Directory_init(); - bptree_print(root, 0); + while (true) { + printf("Press 1 to add a member.\n"); + printf("Press 2 to search for a member via their phone number.\n"); + printf("Press 3 to delete a member.\n"); + printf("Press 4 to display all members.\n"); + printf("Press 5 to exit the program.\n"); + printf("What do you want to do? "); + int action; + scanf("%d", &action); + clear_buffer(); - for (int i = 0; i < keys->size; i++) { - uint64_t data; - bool found = bptree_search(root, keys->items[i], &data); + if (action < 1 || action > 5) { + system("clear"); + continue; + } - assert(found == true); - assert(data == keys->items[i] * 1000); + if (action == 5) { + break; + } + + printf("\n"); + + switch (action) { + case 1: + append_record(directory); + break; + case 2: + search_record(directory); + break; + case 3: + break; + case 4: + Directory_print(directory); + break; + } + + printf("\nPress enter to continue..."); + getchar(); + system("clear"); } - IntegerArray_destroy(&keys); - bptree_destroy(&root); + // Directory_append(directory, DirectoryRecord_init(false, "0794592180", "Florian", "Burgener", 2000, 10, 03)); + // Directory_append(directory, DirectoryRecord_init(false, "0799494969", "Daisy", "Luna", 1995, 05, 31)); + // Directory_print(directory); + + Directory_destroy(&directory); return EXIT_SUCCESS; } diff --git a/tmp.c b/tmp.c deleted file mode 100644 index 595ab6c8b8aafb3264fb05718728c56e4731c38e..0000000000000000000000000000000000000000 --- a/tmp.c +++ /dev/null @@ -1,22 +0,0 @@ -#include <assert.h> -// Warning : stdbool -#include <stdbool.h> -#include <stdio.h> -#include <stdlib.h> - -#include "Directory.h" -#include "DirectoryRecord.h" -#include "bptree.h" - -int main() { - BPTreeNode *index = bptree_init(2); - - Directory *directory = Directory_init(); - Directory_insert(directory, DirectoryRecord_init(false, "0794592180", "Florian", "Burgener", 2000, 10, 03)); - Directory_insert(directory, DirectoryRecord_init(false, "0799494969", "Daisy", "Luna", 1995, 05, 31)); - Directory_print(directory); - - Directory_destroy(&directory); - bptree_destroy(&index); - return EXIT_SUCCESS; -}