Skip to content

Commit

Permalink
Merge pull request #8 from dreamer-coding/api_update
Browse files Browse the repository at this point in the history
API update, new features and security
  • Loading branch information
dreamer-coding authored Oct 21, 2024
2 parents 2baa356 + 0f5bffc commit 1ff3c1a
Show file tree
Hide file tree
Showing 35 changed files with 1,089 additions and 293 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Before using ToFu, ensure you have the following installed:
# ======================
[wrap-git]
url = https://github.com/fossillogic/fossil-tofu.git
revision = v0.1.2
revision = v0.1.3

[provide]
fossil-tofu = fossil_tofu_dep
Expand Down
105 changes: 87 additions & 18 deletions code/logic/arrayof.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#include <stdio.h>

// Function to create an arrayof with an initial set of elements
fossil_tofu_arrayof_t fossil_tofu_arrayof_create(char *type, size_t size, ...) {
fossil_tofu_arrayof_t arrayof;
fossil_array_t fossil_array_create(char *type, size_t size, ...) {
fossil_array_t arrayof;
arrayof.size = size;
arrayof.capacity = size > 0 ? size : 1; // Ensure at least capacity of 1
arrayof.array = (fossil_tofu_t *)fossil_tofu_alloc(arrayof.capacity * sizeof(fossil_tofu_t));
Expand All @@ -41,57 +41,126 @@ fossil_tofu_arrayof_t fossil_tofu_arrayof_create(char *type, size_t size, ...) {
}

// Function to destroy arrayof and free allocated memory
void fossil_tofu_arrayof_erase(fossil_tofu_arrayof_t *arrayof) {
void fossil_array_destroy(fossil_array_t *arrayof) {
for (size_t i = 0; i < arrayof->size; ++i) {
fossil_tofu_erase(&(arrayof->array[i]));
fossil_tofu_destroy(&(arrayof->array[i]));
}
fossil_tofu_free(arrayof->array);
arrayof->size = 0;
arrayof->capacity = 0;
}

// Function to add a fossil_tofu_t element to the end of the arrayof
void fossil_tofu_arrayof_add(fossil_tofu_arrayof_t *arrayof, fossil_tofu_t tofu) {
void fossil_array_add(fossil_array_t *arrayof, fossil_tofu_t tofu) {
if (arrayof == NULL || arrayof->array == NULL) {
fprintf(stderr, "Invalid arrayof\n");
return;
}
if (arrayof->size >= arrayof->capacity) {
arrayof->capacity *= 2;
arrayof->array = (fossil_tofu_t *)fossil_tofu_realloc(arrayof->array, arrayof->capacity * sizeof(fossil_tofu_t));
if (arrayof->array == NULL) {
fossil_tofu_t *new_array = (fossil_tofu_t *)fossil_tofu_realloc(arrayof->array, arrayof->capacity * sizeof(fossil_tofu_t));
if (new_array == NULL) {
fprintf(stderr, "Memory allocation failed while expanding arrayof\n");
exit(EXIT_FAILURE);
}
arrayof->array = new_array;
}
arrayof->array[arrayof->size++] = tofu;
}

// Function to retrieve the fossil_tofu_t element at a specified index
fossil_tofu_t fossil_tofu_arrayof_get(const fossil_tofu_arrayof_t *arrayof, size_t index) {
void fossil_array_add_at(fossil_array_t *arrayof, size_t index, fossil_tofu_t tofu) {
if (arrayof == NULL || arrayof->array == NULL) {
fprintf(stderr, "Invalid arrayof\n");
return;
}
if (index > arrayof->size) {
fprintf(stderr, "Index out of bounds\n");
return;
}
if (arrayof->size >= arrayof->capacity) {
arrayof->capacity *= 2;
fossil_tofu_t *new_array = (fossil_tofu_t *)fossil_tofu_realloc(arrayof->array, arrayof->capacity * sizeof(fossil_tofu_t));
if (new_array == NULL) {
fprintf(stderr, "Memory allocation failed while expanding arrayof\n");
exit(EXIT_FAILURE);
}
arrayof->array = new_array;
}
for (size_t i = arrayof->size; i > index; --i) {
arrayof->array[i] = arrayof->array[i - 1];
}
arrayof->array[index] = tofu;
++arrayof->size;
}

void fossil_array_remove(fossil_array_t *arrayof, size_t index) {
if (arrayof == NULL || arrayof->array == NULL) {
fprintf(stderr, "Invalid arrayof\n");
return;
}
if (index >= arrayof->size) {
fprintf(stderr, "Index out of bounds\n");
return;
}
fossil_tofu_destroy(&(arrayof->array[index]));
for (size_t i = index; i < arrayof->size - 1; ++i) {
arrayof->array[i] = arrayof->array[i + 1];
}
--arrayof->size;
}

fossil_tofu_t fossil_array_get(const fossil_array_t *arrayof, size_t index) {
if (arrayof == NULL || arrayof->array == NULL) {
fprintf(stderr, "Invalid arrayof\n");
exit(EXIT_FAILURE);
}
if (index >= arrayof->size) {
fprintf(stderr, "Index out of bounds\n");
exit(EXIT_FAILURE);
}
return arrayof->array[index];
}

// Function to return the current size of the arrayof
size_t fossil_tofu_arrayof_size(const fossil_tofu_arrayof_t *arrayof) {
size_t fossil_array_capacity(const fossil_array_t *arrayof) {
if (arrayof == NULL) {
fprintf(stderr, "Invalid arrayof\n");
return 0;
}
return arrayof->capacity;
}

size_t fossil_array_size(const fossil_array_t *arrayof) {
if (arrayof == NULL) {
fprintf(stderr, "Invalid arrayof\n");
return 0;
}
return arrayof->size;
}

// Function to check if the arrayof is empty
bool fossil_tofu_arrayof_is_empty(const fossil_tofu_arrayof_t *arrayof) {
bool fossil_array_is_empty(const fossil_array_t *arrayof) {
if (arrayof == NULL) {
fprintf(stderr, "Invalid arrayof\n");
return true;
}
return arrayof->size == 0;
}

// Function to clear all elements from the arrayof
void fossil_tofu_arrayof_clear(fossil_tofu_arrayof_t *arrayof) {
void fossil_array_clear(fossil_array_t *arrayof) {
if (arrayof == NULL || arrayof->array == NULL) {
fprintf(stderr, "Invalid arrayof\n");
return;
}
for (size_t i = 0; i < arrayof->size; ++i) {
fossil_tofu_erase(&(arrayof->array[i]));
fossil_tofu_destroy(&(arrayof->array[i]));
}
arrayof->size = 0;
}

// Function to print all elements of the arrayof
void fossil_tofu_arrayof_print(const fossil_tofu_arrayof_t *arrayof) {
void fossil_array_print(const fossil_array_t *arrayof) {
if (arrayof == NULL || arrayof->array == NULL) {
fprintf(stderr, "Invalid arrayof\n");
return;
}
for (size_t i = 0; i < arrayof->size; ++i) {
fossil_tofu_print(arrayof->array[i]);
}
Expand Down
14 changes: 13 additions & 1 deletion code/logic/doublylist.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <string.h>

fossil_dlist_t* fossil_dlist_create(char* type) {
if (!type) return NULL; // Error checking for null type

fossil_dlist_t* dlist = (fossil_dlist_t*)fossil_tofu_alloc(sizeof(fossil_dlist_t));
if (dlist) {
dlist->head = NULL;
Expand All @@ -26,7 +28,7 @@ fossil_dlist_t* fossil_dlist_create(char* type) {
return dlist;
}

void fossil_dlist_erase(fossil_dlist_t* dlist) {
void fossil_dlist_destroy(fossil_dlist_t* dlist) {
if (!dlist) return;

fossil_dlist_node_t* current = dlist->head;
Expand Down Expand Up @@ -63,6 +65,8 @@ int32_t fossil_dlist_insert(fossil_dlist_t* dlist, fossil_tofu_t data) {
}

int32_t fossil_dlist_remove(fossil_dlist_t* dlist, fossil_tofu_t* data) {
if (!dlist || !data) return FOSSIL_TOFU_FAILURE; // Error checking for null pointers

if (fossil_dlist_is_empty(dlist)) {
return FOSSIL_TOFU_FAILURE; // Empty list
}
Expand All @@ -85,6 +89,8 @@ int32_t fossil_dlist_remove(fossil_dlist_t* dlist, fossil_tofu_t* data) {
}

int32_t fossil_dlist_search(const fossil_dlist_t* dlist, fossil_tofu_t data) {
if (!dlist) return FOSSIL_TOFU_FAILURE; // Error checking for null list

fossil_dlist_node_t* current = dlist->head;
while (current) {
if (fossil_tofu_equals(current->data, data)) {
Expand Down Expand Up @@ -142,6 +148,8 @@ void fossil_dlist_reverse_backward(fossil_dlist_t* dlist) {
}

size_t fossil_dlist_size(const fossil_dlist_t* dlist) {
if (!dlist) return 0; // Error checking for null list

size_t count = 0;
fossil_dlist_node_t* current = dlist->head;
while (current) {
Expand All @@ -152,6 +160,8 @@ size_t fossil_dlist_size(const fossil_dlist_t* dlist) {
}

fossil_tofu_t* fossil_dlist_getter(fossil_dlist_t* dlist, fossil_tofu_t data) {
if (!dlist) return NULL; // Error checking for null list

fossil_dlist_node_t* current = dlist->head;
while (current) {
if (fossil_tofu_equals(current->data, data)) {
Expand All @@ -163,6 +173,8 @@ fossil_tofu_t* fossil_dlist_getter(fossil_dlist_t* dlist, fossil_tofu_t data) {
}

int32_t fossil_dlist_setter(fossil_dlist_t* dlist, fossil_tofu_t data) {
if (!dlist) return FOSSIL_TOFU_FAILURE; // Error checking for null list

fossil_dlist_node_t* current = dlist->head;
while (current) {
if (fossil_tofu_equals(current->data, data)) {
Expand Down
63 changes: 58 additions & 5 deletions code/logic/dqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,30 @@
#include <string.h>

fossil_dqueue_t* fossil_dqueue_create(char* type) {
if (!type) {
fprintf(stderr, "Error: type cannot be NULL\n");
return NULL;
}

fossil_dqueue_t* dqueue = (fossil_dqueue_t*)fossil_tofu_alloc(sizeof(fossil_dqueue_t));
if (dqueue) {
dqueue->front = NULL;
dqueue->rear = NULL;
dqueue->type = fossil_tofu_strdup(type); // Duplicate the type string to manage its memory
if (!dqueue) {
fprintf(stderr, "Error: Memory allocation failed for dqueue\n");
return NULL;
}

dqueue->front = NULL;
dqueue->rear = NULL;
dqueue->type = fossil_tofu_strdup(type); // Duplicate the type string to manage its memory
if (!dqueue->type) {
fprintf(stderr, "Error: Memory allocation failed for type string\n");
fossil_tofu_free(dqueue);
return NULL;
}

return dqueue;
}

void fossil_dqueue_erase(fossil_dqueue_t* dqueue) {
void fossil_dqueue_destroy(fossil_dqueue_t* dqueue) {
if (!dqueue) return;

fossil_dqueue_node_t* current = dqueue->front;
Expand All @@ -42,8 +56,14 @@ void fossil_dqueue_erase(fossil_dqueue_t* dqueue) {
}

int32_t fossil_dqueue_insert(fossil_dqueue_t* dqueue, fossil_tofu_t data) {
if (!dqueue) {
fprintf(stderr, "Error: dqueue cannot be NULL\n");
return FOSSIL_TOFU_FAILURE;
}

fossil_dqueue_node_t* new_node = (fossil_dqueue_node_t*)fossil_tofu_alloc(sizeof(fossil_dqueue_node_t));
if (!new_node) {
fprintf(stderr, "Error: Memory allocation failed for new node\n");
return FOSSIL_TOFU_FAILURE; // Allocation failed
}

Expand All @@ -66,6 +86,11 @@ int32_t fossil_dqueue_insert(fossil_dqueue_t* dqueue, fossil_tofu_t data) {
}

int32_t fossil_dqueue_remove(fossil_dqueue_t* dqueue, fossil_tofu_t* data) {
if (!dqueue || !data) {
fprintf(stderr, "Error: dqueue and data cannot be NULL\n");
return FOSSIL_TOFU_FAILURE;
}

if (fossil_dqueue_is_empty(dqueue)) {
return FOSSIL_TOFU_FAILURE; // Empty queue
}
Expand All @@ -87,6 +112,11 @@ int32_t fossil_dqueue_remove(fossil_dqueue_t* dqueue, fossil_tofu_t* data) {
}

int32_t fossil_dqueue_search(const fossil_dqueue_t* dqueue, fossil_tofu_t data) {
if (!dqueue) {
fprintf(stderr, "Error: dqueue cannot be NULL\n");
return FOSSIL_TOFU_FAILURE;
}

fossil_dqueue_node_t* current = dqueue->front;
while (current) {
if (fossil_tofu_equals(current->data, data)) {
Expand All @@ -98,6 +128,11 @@ int32_t fossil_dqueue_search(const fossil_dqueue_t* dqueue, fossil_tofu_t data)
}

size_t fossil_dqueue_size(const fossil_dqueue_t* dqueue) {
if (!dqueue) {
fprintf(stderr, "Error: dqueue cannot be NULL\n");
return 0;
}

size_t count = 0;
fossil_dqueue_node_t* current = dqueue->front;
while (current) {
Expand All @@ -108,6 +143,11 @@ size_t fossil_dqueue_size(const fossil_dqueue_t* dqueue) {
}

fossil_tofu_t* fossil_dqueue_getter(fossil_dqueue_t* dqueue, fossil_tofu_t data) {
if (!dqueue) {
fprintf(stderr, "Error: dqueue cannot be NULL\n");
return NULL;
}

fossil_dqueue_node_t* current = dqueue->front;
while (current) {
if (fossil_tofu_equals(current->data, data)) {
Expand All @@ -119,6 +159,11 @@ fossil_tofu_t* fossil_dqueue_getter(fossil_dqueue_t* dqueue, fossil_tofu_t data)
}

int32_t fossil_dqueue_setter(fossil_dqueue_t* dqueue, fossil_tofu_t data) {
if (!dqueue) {
fprintf(stderr, "Error: dqueue cannot be NULL\n");
return FOSSIL_TOFU_FAILURE;
}

fossil_dqueue_node_t* current = dqueue->front;
while (current) {
if (fossil_tofu_equals(current->data, data)) {
Expand All @@ -132,6 +177,10 @@ int32_t fossil_dqueue_setter(fossil_dqueue_t* dqueue, fossil_tofu_t data) {
}

bool fossil_dqueue_not_empty(const fossil_dqueue_t* dqueue) {
if (!dqueue) {
fprintf(stderr, "Error: dqueue cannot be NULL\n");
return false;
}
return dqueue->front != NULL;
}

Expand All @@ -140,6 +189,10 @@ bool fossil_dqueue_not_cnullptr(const fossil_dqueue_t* dqueue) {
}

bool fossil_dqueue_is_empty(const fossil_dqueue_t* dqueue) {
if (!dqueue) {
fprintf(stderr, "Error: dqueue cannot be NULL\n");
return true;
}
return dqueue->front == NULL;
}

Expand Down
Loading

0 comments on commit 1ff3c1a

Please sign in to comment.