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

Comment DirectoryRecord

parent 15d3386f
No related branches found
No related tags found
No related merge requests found
......@@ -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");
}
}
}
......
......@@ -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.
*/
......
......@@ -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;
......
/**
* @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];
......
/**
* @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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment