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

CLI

parent 9e699216
No related branches found
No related tags found
No related merge requests found
a.c 0 → 100644
#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;
}
#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;
}
}
......@@ -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
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
......
#ifndef BPTREE_H
#define BPTREE_h
#define BPTREE_H
#include <stdbool.h>
#include <stdint.h>
......
#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;
}
#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;
}
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