-
Florian Burgener authoredFlorian Burgener authored
DirectoryRecord.c 3.72 KiB
#include "DirectoryRecord.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Array.h"
int DirectoryRecord_size_on_disk() {
int size = 1;
size += PHONE_NUMBER_MAX_LENGTH_WITHOUT_NULL_CHARACTER;
size += NAME_MAX_LENGTH_WITHOUT_NULL_CHARACTER;
size += SURNAME_MAX_LENGTH_WITHOUT_NULL_CHARACTER;
size += BIRTH_DATE_SIZE_IN_BYTES;
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 *record = (DirectoryRecord *)malloc(sizeof(DirectoryRecord));
record->deleted = deleted;
memset(record->phone_number, 0, PHONE_NUMBER_MAX_LENGTH);
strcpy(record->phone_number, phone_number);
memset(record->name, 0, NAME_MAX_LENGTH);
strcpy(record->name, name);
memset(record->surname, 0, SURNAME_MAX_LENGTH);
strcpy(record->surname, surname);
record->birth_date_year = birth_date_year;
record->birth_date_month = birth_date_month;
record->birth_date_day = birth_date_day;
return record;
}
void DirectoryRecord_destroy(DirectoryRecord **record) {
free(*record);
*record = NULL;
}
void DirectoryRecord_print(DirectoryRecord *record) {
printf("Phone Number: %s\n", record->phone_number);
printf("Name: %s\n", record->name);
printf("Surname: %s\n", record->surname);
printf("Birth Date: %d-%.2d-%.2d\n", record->birth_date_year, record->birth_date_month, record->birth_date_day);
}
ByteArray *DirectoryRecord_to_ByteArray(DirectoryRecord *record) {
int capacity = DirectoryRecord_size_on_disk();
ByteArray *array = ByteArray_init(capacity);
ByteArray_append(array, (uint8_t)record->deleted);
for (int j = 0; j < PHONE_NUMBER_MAX_LENGTH_WITHOUT_NULL_CHARACTER; j++) {
ByteArray_append(array, (uint8_t)record->phone_number[j]);
}
for (int j = 0; j < NAME_MAX_LENGTH_WITHOUT_NULL_CHARACTER; j++) {
ByteArray_append(array, (uint8_t)record->name[j]);
}
for (int j = 0; j < SURNAME_MAX_LENGTH_WITHOUT_NULL_CHARACTER; j++) {
ByteArray_append(array, (uint8_t)record->surname[j]);
}
// Birth Date : Year
ByteArray_append(array, (uint8_t)((record->birth_date_year >> 8) & 255));
ByteArray_append(array, (uint8_t)(record->birth_date_year & 255));
// Birth Date : Month
ByteArray_append(array, (uint8_t)record->birth_date_month);
// Birth Date : Day
ByteArray_append(array, (uint8_t)record->birth_date_day);
return array;
}
DirectoryRecord *ByteArray_to_DirectoryRecord(ByteArray *byte_array) {
int i = 0;
bool deleted = (bool)byte_array->items[i];
i++;
char phone_number[PHONE_NUMBER_MAX_LENGTH];
phone_number[PHONE_NUMBER_MAX_LENGTH - 1] = '\0';
for (int j = 0; j < PHONE_NUMBER_MAX_LENGTH_WITHOUT_NULL_CHARACTER; j++) {
phone_number[j] = (char)byte_array->items[i];
i++;
}
char name[NAME_MAX_LENGTH];
name[NAME_MAX_LENGTH - 1] = '\0';
for (int j = 0; j < NAME_MAX_LENGTH_WITHOUT_NULL_CHARACTER; j++) {
name[j] = (char)byte_array->items[i];
i++;
}
char surname[SURNAME_MAX_LENGTH];
surname[SURNAME_MAX_LENGTH - 1] = '\0';
for (int j = 0; j < SURNAME_MAX_LENGTH_WITHOUT_NULL_CHARACTER; j++) {
surname[j] = (char)byte_array->items[i];
i++;
}
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];
return DirectoryRecord_init(deleted, phone_number, name, surname, birth_date_year, birth_date_month, birth_date_day);
}