Skip to content
Snippets Groups Projects
Commit a6e9ca9e authored by Florian Burgener's avatar Florian Burgener
Browse files

All features have been implemented.

parent 116cf00b
Branches
No related tags found
No related merge requests found
// CAUTION : validation des entrées
#include <stdio.h>
#include <stdlib.h>
#include "Directory.h"
#include "DirectoryRecord.h"
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();
}
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);
if (Directory_append(directory, record)) {
printf("\n===>The record has been saved in the directory.\n");
} else {
printf("\n===>This phone number is already associated with a record. The record has not been saved.\n");
}
DirectoryRecord_destroy(&record);
}
}
void search_record(Directory *directory) {
printf("Enter the phone number that corresponds to the record you are looking for: ");
// TODO : crash phone number overflow
char phone_number[11];
scanf("%s", phone_number);
clear_buffer();
DirectoryRecord *record = Directory_search(directory, phone_number);
printf("\n");
if (record == NULL) {
printf("===>No records were found for this phone number.\n");
} else {
printf("========================\n");
DirectoryRecord_print(record);
DirectoryRecord_destroy(&record);
printf("========================\n");
}
}
int main() {
Directory *directory = Directory_init("directory_database");
while (true) {
printf("Enter 1 to add a member.\n");
printf("Enter 2 to search for a member via their phone number.\n");
printf("Enter 3 to delete a member.\n");
printf("Enter 4 to display all members.\n");
printf("Enter 5 to exit the program.\n");
printf("What do you want to do? ");
int action;
scanf("%d", &action);
clear_buffer();
if (action < 1 || action > 5) {
system("clear");
continue;
}
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");
}
Directory_destroy(&directory);
return EXIT_SUCCESS;
}
b.c 0 → 100644
#include <assert.h>
#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 < 30; 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);
}
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);
}
BPTree_delete(root, 65);
BPTree_delete(root, 57);
BPTree_delete(root, 47);
BPTree_delete(root, 28);
BPTree_delete(root, 51);
BPTree_insert(root, 100, 100 * 1000);
BPTree_delete(root, 48);
BPTree_delete(root, 41);
BPTree_delete(root, 60);
BPTree_delete(root, 5);
BPTree_delete(root, 8);
BPTree_delete(root, 21);
BPTree_delete(root, 86);
BPTree_delete(root, 100);
BPTree_delete(root, 88);
BPTree_delete(root, 95);
BPTree_delete(root, 49);
BPTree_delete(root, 56);
BPTree_delete(root, 55);
BPTree_print(root, 0);
IntegerArray_destroy(&keys);
BPTree_destroy(&root);
return EXIT_SUCCESS;
}
...@@ -51,10 +51,10 @@ static void rebuild_index(Directory *directory) { ...@@ -51,10 +51,10 @@ static void rebuild_index(Directory *directory) {
while (true) { while (true) {
uint64_t data_ptr = (uint64_t)ftell(fp); uint64_t data_ptr = (uint64_t)ftell(fp);
uint8_t deleted; uint8_t is_deleted;
fread(&deleted, 1, 1, fp); fread(&is_deleted, 1, 1, fp);
if ((bool)deleted) { if ((bool)is_deleted) {
fseek(fp, DirectoryRecord_size_on_disk() - 1, SEEK_CUR); fseek(fp, DirectoryRecord_size_on_disk() - 1, SEEK_CUR);
} else { } else {
char phone_number[PHONE_NUMBER_MAX_LENGTH]; char phone_number[PHONE_NUMBER_MAX_LENGTH];
...@@ -72,12 +72,11 @@ static void rebuild_index(Directory *directory) { ...@@ -72,12 +72,11 @@ static void rebuild_index(Directory *directory) {
fclose(fp); fclose(fp);
} }
Directory *Directory_init(char database_filename[100]) { Directory *Directory_init(char database_filename[FILENAME_MAX_LENGTH]) {
Directory *directory = (Directory *)malloc(sizeof(Directory)); Directory *directory = (Directory *)malloc(sizeof(Directory));
strcpy(directory->database_filename, database_filename); strcpy(directory->database_filename, database_filename);
directory->index = BPTree_init(DEFAULT_ORDER); directory->index = BPTree_init(DEFAULT_ORDER);
rebuild_index(directory); rebuild_index(directory);
BPTree_print(directory->index, 0);
return directory; return directory;
} }
...@@ -93,29 +92,34 @@ void Directory_print(Directory *directory) { ...@@ -93,29 +92,34 @@ void Directory_print(Directory *directory) {
if (fp == NULL) { if (fp == NULL) {
printf("===>The directory is empty.\n"); printf("===>The directory is empty.\n");
return;
} }
long file_size = get_file_size(fp); long file_size = get_file_size(fp);
printf("========================\n"); printf("========================\n");
int i = 0;
while (true) { while (true) {
ByteArray *byte_array = ByteArray_init(DirectoryRecord_size_on_disk()); ByteArray *byte_array = ByteArray_init(DirectoryRecord_size_on_disk());
fread(byte_array->items, 1, DirectoryRecord_size_on_disk(), fp); fread(byte_array->items, 1, DirectoryRecord_size_on_disk(), fp);
byte_array->size = DirectoryRecord_size_on_disk(); byte_array->size = DirectoryRecord_size_on_disk();
bool is_deleted = (bool)byte_array->items[0];
if (!(bool)byte_array->items[0]) { if (!is_deleted) {
DirectoryRecord *record = ByteArray_to_DirectoryRecord(byte_array); DirectoryRecord *record = ByteArray_to_DirectoryRecord(byte_array);
if (i != 0) {
printf("------------------------\n");
}
DirectoryRecord_print(record); DirectoryRecord_print(record);
DirectoryRecord_destroy(&record); DirectoryRecord_destroy(&record);
i++;
} }
ByteArray_destroy(&byte_array); ByteArray_destroy(&byte_array);
if (ftell(fp) == file_size) { if (ftell(fp) == file_size) {
break; break;
} else if (!(bool)byte_array->items[0]) {
printf("------------------------\n");
} }
} }
...@@ -131,7 +135,7 @@ bool Directory_append(Directory *directory, DirectoryRecord *record) { ...@@ -131,7 +135,7 @@ bool Directory_append(Directory *directory, DirectoryRecord *record) {
// Writes the record to the file. // Writes the record to the file.
FILE *fp; FILE *fp;
fp = fopen(directory->database_filename, "a+b"); fp = fopen(directory->database_filename, "ab");
if (fp == NULL) { if (fp == NULL) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
...@@ -171,3 +175,25 @@ DirectoryRecord *Directory_search(Directory *directory, char phone_number[11]) { ...@@ -171,3 +175,25 @@ DirectoryRecord *Directory_search(Directory *directory, char phone_number[11]) {
return NULL; return NULL;
} }
bool Directory_delete(Directory *directory, char phone_number[11]) {
uint64_t data_ptr;
if (BPTree_search(directory->index, hash_string(phone_number), &data_ptr)) {
FILE *fp;
fp = fopen(directory->database_filename, "r+b");
if (fp == NULL) {
exit(EXIT_FAILURE);
}
fseek(fp, data_ptr, SEEK_SET);
uint8_t is_deleted = (uint8_t) true;
fwrite(&is_deleted, 1, 1, fp);
fclose(fp);
BPTree_delete(directory->index, hash_string(phone_number));
return true;
}
return false;
}
...@@ -5,16 +5,18 @@ ...@@ -5,16 +5,18 @@
#include "BPTree.h" #include "BPTree.h"
#define DEFAULT_ORDER 2 #define DEFAULT_ORDER 2
#define FILENAME_MAX_LENGTH 100
typedef struct Directory { typedef struct Directory {
char database_filename[100]; char database_filename[FILENAME_MAX_LENGTH];
BPTreeNode *index; BPTreeNode *index;
} Directory; } Directory;
Directory *Directory_init(char database_filename[100]); Directory *Directory_init(char database_filename[FILENAME_MAX_LENGTH]);
void Directory_destroy(Directory **directory); void Directory_destroy(Directory **directory);
void Directory_print(Directory *directory); void Directory_print(Directory *directory);
bool Directory_append(Directory *directory, DirectoryRecord *record); bool Directory_append(Directory *directory, DirectoryRecord *record);
DirectoryRecord *Directory_search(Directory *directory, char phone_number[11]); DirectoryRecord *Directory_search(Directory *directory, char phone_number[11]);
bool Directory_delete(Directory *directory, char phone_number[11]);
#endif #endif
...@@ -17,9 +17,9 @@ int DirectoryRecord_size_on_disk() { ...@@ -17,9 +17,9 @@ int DirectoryRecord_size_on_disk() {
return size; return size;
} }
DirectoryRecord *DirectoryRecord_init(bool deleted, char phone_number[PHONE_NUMBER_MAX_LENGTH], char name[NAME_MAX_LENGTH], char surname[SURNAME_MAX_LENGTH], int birth_date_year, int birth_date_month, int birth_date_day) { DirectoryRecord *DirectoryRecord_init(bool is_deleted, char phone_number[PHONE_NUMBER_MAX_LENGTH], char name[NAME_MAX_LENGTH], char surname[SURNAME_MAX_LENGTH], int birth_date_year, int birth_date_month, int birth_date_day) {
DirectoryRecord *record = (DirectoryRecord *)malloc(sizeof(DirectoryRecord)); DirectoryRecord *record = (DirectoryRecord *)malloc(sizeof(DirectoryRecord));
record->deleted = deleted; record->is_deleted = is_deleted;
memset(record->phone_number, 0, PHONE_NUMBER_MAX_LENGTH); memset(record->phone_number, 0, PHONE_NUMBER_MAX_LENGTH);
strcpy(record->phone_number, phone_number); strcpy(record->phone_number, phone_number);
memset(record->name, 0, NAME_MAX_LENGTH); memset(record->name, 0, NAME_MAX_LENGTH);
...@@ -47,7 +47,7 @@ void DirectoryRecord_print(DirectoryRecord *record) { ...@@ -47,7 +47,7 @@ void DirectoryRecord_print(DirectoryRecord *record) {
ByteArray *DirectoryRecord_to_ByteArray(DirectoryRecord *record) { ByteArray *DirectoryRecord_to_ByteArray(DirectoryRecord *record) {
int capacity = DirectoryRecord_size_on_disk(); int capacity = DirectoryRecord_size_on_disk();
ByteArray *array = ByteArray_init(capacity); ByteArray *array = ByteArray_init(capacity);
ByteArray_append(array, (uint8_t)record->deleted); ByteArray_append(array, (uint8_t)record->is_deleted);
for (int j = 0; j < PHONE_NUMBER_MAX_LENGTH_WITHOUT_NULL_CHARACTER; j++) { for (int j = 0; j < PHONE_NUMBER_MAX_LENGTH_WITHOUT_NULL_CHARACTER; j++) {
ByteArray_append(array, (uint8_t)record->phone_number[j]); ByteArray_append(array, (uint8_t)record->phone_number[j]);
...@@ -74,7 +74,7 @@ ByteArray *DirectoryRecord_to_ByteArray(DirectoryRecord *record) { ...@@ -74,7 +74,7 @@ ByteArray *DirectoryRecord_to_ByteArray(DirectoryRecord *record) {
DirectoryRecord *ByteArray_to_DirectoryRecord(ByteArray *byte_array) { DirectoryRecord *ByteArray_to_DirectoryRecord(ByteArray *byte_array) {
int i = 0; int i = 0;
bool deleted = (bool)byte_array->items[i]; bool is_deleted = (bool)byte_array->items[i];
i++; i++;
char phone_number[PHONE_NUMBER_MAX_LENGTH]; char phone_number[PHONE_NUMBER_MAX_LENGTH];
...@@ -105,5 +105,5 @@ DirectoryRecord *ByteArray_to_DirectoryRecord(ByteArray *byte_array) { ...@@ -105,5 +105,5 @@ DirectoryRecord *ByteArray_to_DirectoryRecord(ByteArray *byte_array) {
int birth_date_month = (int)byte_array->items[i + 2]; int birth_date_month = (int)byte_array->items[i + 2];
int birth_date_day = (int)byte_array->items[i + 3]; int birth_date_day = (int)byte_array->items[i + 3];
return DirectoryRecord_init(deleted, phone_number, name, surname, birth_date_year, birth_date_month, birth_date_day); return DirectoryRecord_init(is_deleted, phone_number, name, surname, birth_date_year, birth_date_month, birth_date_day);
} }
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
#define BIRTH_DATE_SIZE_IN_BYTES 4 #define BIRTH_DATE_SIZE_IN_BYTES 4
typedef struct DirectoryRecord { typedef struct DirectoryRecord {
bool deleted; bool is_deleted;
char phone_number[PHONE_NUMBER_MAX_LENGTH]; char phone_number[PHONE_NUMBER_MAX_LENGTH];
char name[NAME_MAX_LENGTH]; char name[NAME_MAX_LENGTH];
char surname[SURNAME_MAX_LENGTH]; char surname[SURNAME_MAX_LENGTH];
...@@ -29,7 +29,7 @@ typedef struct DirectoryRecord { ...@@ -29,7 +29,7 @@ typedef struct DirectoryRecord {
int DirectoryRecord_size_on_disk(); int DirectoryRecord_size_on_disk();
DirectoryRecord *DirectoryRecord_init(bool deleted, char phone_number[PHONE_NUMBER_MAX_LENGTH], char name[NAME_MAX_LENGTH], char surname[SURNAME_MAX_LENGTH], int birth_date_year, int birth_date_month, int birth_date_day); DirectoryRecord *DirectoryRecord_init(bool is_deleted, char phone_number[PHONE_NUMBER_MAX_LENGTH], char name[NAME_MAX_LENGTH], char surname[SURNAME_MAX_LENGTH], int birth_date_year, int birth_date_month, int birth_date_day);
void DirectoryRecord_destroy(DirectoryRecord **record); void DirectoryRecord_destroy(DirectoryRecord **record);
void DirectoryRecord_print(DirectoryRecord *record); void DirectoryRecord_print(DirectoryRecord *record);
ByteArray *DirectoryRecord_to_ByteArray(DirectoryRecord *record); ByteArray *DirectoryRecord_to_ByteArray(DirectoryRecord *record);
......
#include <assert.h> // CAUTION : validation des entrées
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "BPTree.h" #include "Directory.h"
#include "DirectoryRecord.h"
int main() { void clear_buffer() {
BPTreeNode *root = BPTree_init(2); 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) ");
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}; char choice = getchar();
IntegerArray *keys = IntegerArray_init(40); if (choice != '\n') {
for (int i = 0; i < 30; i++) { getchar();
IntegerArray_append(keys, input[i]);
} }
IntegerArray_print(keys); 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);
for (int i = 0; i < keys->size; i++) { if (Directory_append(directory, record)) {
BPTree_insert(root, keys->items[i], keys->items[i] * 1000); printf("\n===>The record has been saved in the directory.\n");
} else {
printf("\n===>This phone number is already associated with a record. The record has not been saved.\n");
}
DirectoryRecord_destroy(&record);
} }
}
void search_record(Directory *directory) {
printf("Enter the phone number that corresponds to the record you are looking for: ");
// TODO : crash phone number overflow
char phone_number[11];
scanf("%s", phone_number);
clear_buffer();
DirectoryRecord *record = Directory_search(directory, phone_number);
printf("\n");
if (record == NULL) {
printf("===>No records were found for this phone number.\n");
} else {
printf("========================\n");
DirectoryRecord_print(record);
DirectoryRecord_destroy(&record);
printf("========================\n");
}
}
void delete_record(Directory *directory) {
printf("Enter the phone number that corresponds to the record you wish to delete: ");
char phone_number[11];
scanf("%s", phone_number);
clear_buffer();
DirectoryRecord *record = Directory_search(directory, phone_number);
printf("\n");
if (record == NULL) {
printf("===>No records were found for this phone number.\n");
} else {
printf("========================\n");
DirectoryRecord_print(record);
DirectoryRecord_destroy(&record);
printf("========================\n");
printf("\nAre you sure you want to delete this record? (Y/n) ");
char choice = getchar();
if (choice != '\n') {
getchar();
}
if (choice == 'n') {
printf("\n===>The procedure has been cancelled.\n");
} else {
Directory_delete(directory, phone_number);
printf("Delete done\n");
}
}
}
int main() {
Directory *directory = Directory_init("directory_database");
while (true) {
BPTree_print(directory->index, 0);
printf("Enter 1 to add a member.\n");
printf("Enter 2 to search for a member via their phone number.\n");
printf("Enter 3 to delete a member.\n");
printf("Enter 4 to display all members.\n");
printf("Enter 5 to exit the program.\n");
printf("What do you want to do? ");
int action;
scanf("%d", &action);
clear_buffer();
if (action < 1 || action > 5) {
system("clear");
continue;
}
if (action == 5) {
break;
}
printf("\n");
for (int i = 0; i < keys->size; i++) { switch (action) {
uint64_t data; case 1:
bool found = BPTree_search(root, keys->items[i], &data); append_record(directory);
break;
case 2:
search_record(directory);
break;
case 3:
delete_record(directory);
break;
case 4:
Directory_print(directory);
break;
}
assert(found == true); printf("\nPress enter to continue...");
assert(data == keys->items[i] * 1000); getchar();
system("clear");
} }
BPTree_delete(root, 65); Directory_destroy(&directory);
BPTree_delete(root, 57);
BPTree_delete(root, 47);
BPTree_delete(root, 28);
BPTree_delete(root, 51);
BPTree_insert(root, 100, 100 * 1000);
BPTree_delete(root, 48);
BPTree_delete(root, 41);
BPTree_delete(root, 60);
BPTree_delete(root, 5);
BPTree_delete(root, 8);
BPTree_delete(root, 21);
BPTree_delete(root, 86);
BPTree_delete(root, 100);
BPTree_delete(root, 88);
BPTree_delete(root, 95);
BPTree_delete(root, 49);
BPTree_delete(root, 56);
BPTree_delete(root, 55);
BPTree_print(root, 0);
IntegerArray_destroy(&keys);
BPTree_destroy(&root);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment