diff --git a/main.c b/main.c index 3d06a977a6157fa1876e319bbd047339f7b1133c..335f9299cca7eb6c3d6052c76afe283b8d1bce47 100644 --- a/main.c +++ b/main.c @@ -100,7 +100,7 @@ void delete_record(Directory *directory) { printf("\n===>The procedure has been cancelled.\n"); } else { Directory_delete(directory, phone_number); - printf("Delete done\n"); + printf("The record has been deleted from the directory.\n"); } } } diff --git a/src/Array.h b/src/Array.h index 179c22ee92c627e69b7e706d31f2c56311cf166b..dd08a7a95d1109d2848fecf7ca4d85314a5f633b 100644 --- a/src/Array.h +++ b/src/Array.h @@ -31,7 +31,7 @@ typedef struct IntegerArray { IntegerArray *IntegerArray_init(int capacity); /** - * @brief Destroy the array and free its memory. + * @brief Destroys the array and free its memory. * * @param array The array to be destroyed. */ @@ -135,7 +135,7 @@ typedef struct BPTreeNodeArray { BPTreeNodeArray *BPTreeNodeArray_init(int capacity); /** - * @brief Destroy the array and free its memory. + * @brief Destroys the array and free its memory. * * @param array The array to be destroyed. */ @@ -210,7 +210,7 @@ typedef struct ByteArray { ByteArray *ByteArray_init(int capacity); /** - * @brief Destroy the array and free its memory. + * @brief Destroys the array and free its memory. * * @param array The array to be destroyed. */ diff --git a/src/Directory.c b/src/Directory.c index bbfce591177e15d0a5d131a4dbe31ccc3bfb5e0b..f1eef3971da47d6ca8b73c8fd380a268272c316f 100644 --- a/src/Directory.c +++ b/src/Directory.c @@ -16,7 +16,6 @@ #include "Array.h" #include "BPTree.h" #include "DirectoryRecord.h" -// CAUTION : Je devrais refactor l'implémentation avec le FILE. static uint64_t hash_string(char *str) { SHA256_CTX sha256; diff --git a/src/DirectoryRecord.c b/src/DirectoryRecord.c index bb0fb134dab4a8b128513eed9d824cea9e02e5d6..55e4da42492bd54c0e374b99b83b89012d9b2401 100644 --- a/src/DirectoryRecord.c +++ b/src/DirectoryRecord.c @@ -1,7 +1,7 @@ /** * @file DirectoryRecord.c * @author Florian Burgener (florian.burgener@etu.hesge.ch) - * @brief + * @brief A record that represents a member of the organization. * @version 1.0 * @date 2022-06-XX */ @@ -16,7 +16,8 @@ #include "Array.h" int DirectoryRecord_size_on_disk() { - int size = 1; + int size = 0; + size += IS_DELETED_SIZE_IN_BYTES; size += PHONE_NUMBER_MAXLEN_WITHOUT_NULL_CHARACTER; size += NAME_MAXLEN_WITHOUT_NULL_CHARACTER; size += SURNAME_MAXLEN_WITHOUT_NULL_CHARACTER; @@ -54,26 +55,31 @@ void DirectoryRecord_print(DirectoryRecord *record) { ByteArray *DirectoryRecord_to_ByteArray(DirectoryRecord *record) { int capacity = DirectoryRecord_size_on_disk(); ByteArray *array = ByteArray_init(capacity); + + // Convert the "is_deleted" field to byte. ByteArray_append(array, (uint8_t)record->is_deleted); - for (int j = 0; j < PHONE_NUMBER_MAXLEN_WITHOUT_NULL_CHARACTER; j++) { - ByteArray_append(array, (uint8_t)record->phone_number[j]); + // Converts each character of the phone number to byte. + for (int i = 0; i < PHONE_NUMBER_MAXLEN_WITHOUT_NULL_CHARACTER; i++) { + ByteArray_append(array, (uint8_t)record->phone_number[i]); } - for (int j = 0; j < NAME_MAXLEN_WITHOUT_NULL_CHARACTER; j++) { - ByteArray_append(array, (uint8_t)record->name[j]); + // Converts each character of the name to byte. + for (int i = 0; i < NAME_MAXLEN_WITHOUT_NULL_CHARACTER; i++) { + ByteArray_append(array, (uint8_t)record->name[i]); } - for (int j = 0; j < SURNAME_MAXLEN_WITHOUT_NULL_CHARACTER; j++) { - ByteArray_append(array, (uint8_t)record->surname[j]); + // Converts each character of the surname to byte. + for (int i = 0; i < SURNAME_MAXLEN_WITHOUT_NULL_CHARACTER; i++) { + ByteArray_append(array, (uint8_t)record->surname[i]); } - // Birth Date : Year + // Converts the year of birth to bytes. ByteArray_append(array, (uint8_t)((record->birth_date_year >> 8) & 255)); ByteArray_append(array, (uint8_t)(record->birth_date_year & 255)); - // Birth Date : Month + // Converts the month of birth to byte. ByteArray_append(array, (uint8_t)record->birth_date_month); - // Birth Date : Day + // Converts the day of birth to byte. ByteArray_append(array, (uint8_t)record->birth_date_day); return array; @@ -81,12 +87,15 @@ ByteArray *DirectoryRecord_to_ByteArray(DirectoryRecord *record) { DirectoryRecord *ByteArray_to_DirectoryRecord(ByteArray *byte_array) { int i = 0; + + // Initliases the "is_deleted" variable with the first byte. bool is_deleted = (bool)byte_array->items[i]; i++; char phone_number[PHONE_NUMBER_MAXLEN]; phone_number[PHONE_NUMBER_MAXLEN - 1] = '\0'; + // Read the bytes of the phone number and reconstruct the string of characters. for (int j = 0; j < PHONE_NUMBER_MAXLEN_WITHOUT_NULL_CHARACTER; j++) { phone_number[j] = (char)byte_array->items[i]; i++; @@ -95,6 +104,7 @@ DirectoryRecord *ByteArray_to_DirectoryRecord(ByteArray *byte_array) { char name[NAME_MAXLEN]; name[NAME_MAXLEN - 1] = '\0'; + // Read the bytes of the name and reconstruct the string of characters. for (int j = 0; j < NAME_MAXLEN_WITHOUT_NULL_CHARACTER; j++) { name[j] = (char)byte_array->items[i]; i++; @@ -103,11 +113,13 @@ DirectoryRecord *ByteArray_to_DirectoryRecord(ByteArray *byte_array) { char surname[SURNAME_MAXLEN]; surname[SURNAME_MAXLEN - 1] = '\0'; + // Read the bytes of the surname and reconstruct the string of characters. for (int j = 0; j < SURNAME_MAXLEN_WITHOUT_NULL_CHARACTER; j++) { surname[j] = (char)byte_array->items[i]; i++; } + // Reconstitutes the date of birth from the bytes. int birth_date_year = (int)byte_array->items[i] << 8 | (int)byte_array->items[i + 1]; int birth_date_month = (int)byte_array->items[i + 2]; int birth_date_day = (int)byte_array->items[i + 3]; diff --git a/src/DirectoryRecord.h b/src/DirectoryRecord.h index 3466d9ad26dbe12e4b775c610dd9e9b8c1f6fee4..d14e0e25fcaf3fd9bdddcea2569a9bfd2359b69c 100644 --- a/src/DirectoryRecord.h +++ b/src/DirectoryRecord.h @@ -1,7 +1,7 @@ /** * @file DirectoryRecord.h * @author Florian Burgener (florian.burgener@etu.hesge.ch) - * @brief + * @brief A record that represents a member of the organization. * @version 1.0 * @date 2022-06-XX */ @@ -12,6 +12,8 @@ #include "Array.h" +#define IS_DELETED_SIZE_IN_BYTES 1 + #define PHONE_NUMBER_MAXLEN_WITHOUT_NULL_CHARACTER 10 #define PHONE_NUMBER_MAXLEN 1 + PHONE_NUMBER_MAXLEN_WITHOUT_NULL_CHARACTER @@ -23,6 +25,10 @@ #define BIRTH_DATE_SIZE_IN_BYTES 4 +/** + * @brief Data structure that represents a record. + * + */ typedef struct DirectoryRecord { bool is_deleted; char phone_number[PHONE_NUMBER_MAXLEN]; @@ -33,13 +39,55 @@ typedef struct DirectoryRecord { int birth_date_day; } DirectoryRecord; +/** + * @brief Size in bytes that a record takes when written on disk. + * + * @return int Size in bytes. + */ int DirectoryRecord_size_on_disk(); +/** + * @brief Initializes the "DirectoryRecord" data structure. + * + * @param is_deleted false = the record is not deleted, true = the record is deleted. + * @param phone_number The phone number. + * @param name The name. + * @param surname The surname. + * @param birth_date_year The year of birth. + * @param birth_date_month The month of birth. + * @param birth_date_day The day of birth. + * @return DirectoryRecord* The record initialized with the data. + */ DirectoryRecord *DirectoryRecord_init(bool is_deleted, char phone_number[PHONE_NUMBER_MAXLEN], char name[NAME_MAXLEN], char surname[SURNAME_MAXLEN], int birth_date_year, int birth_date_month, int birth_date_day); + +/** + * @brief Destroys the record and free its memory. + * + * @param record The record to be destroyed. + */ void DirectoryRecord_destroy(DirectoryRecord **record); +/** + * @brief Displays the record on the console. + * + * @param record The record to display. + */ void DirectoryRecord_print(DirectoryRecord *record); + +/** + * @brief Converts a record into an array of bytes. + * + * @param record The record to be converted. + * @return ByteArray* The resulting byte array. + */ ByteArray *DirectoryRecord_to_ByteArray(DirectoryRecord *record); + +/** + * @brief Converts an array of bytes into a record. + * + * @param byte_array The array of bytes to be converted. + * @return DirectoryRecord* The resulting record. + */ DirectoryRecord *ByteArray_to_DirectoryRecord(ByteArray *byte_array); #endif