diff --git a/CMakeLists.txt b/CMakeLists.txt index 384886d..c4f869a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,7 @@ set(ARCHEUS_STD_SOURCES src/std/handler.c src/std/hashtable.c src/std/io.c + src/std/stack.c src/std/string.c src/std/vector.c src/std/defaults/config.c diff --git a/include/arc/std/bool.h b/include/arc/std/bool.h new file mode 100644 index 0000000..3a251ff --- /dev/null +++ b/include/arc/std/bool.h @@ -0,0 +1,18 @@ +#ifndef ARC_STD_BOOL_H_ +#define ARC_STD_BOOL_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +#define ARC_Bool bool +#define ARC_True true +#define ARC_False false + +#ifdef __cplusplus +} +#endif + +#endif //ARC_STD_BOOL_H_ \ No newline at end of file diff --git a/include/arc/std/queue.h b/include/arc/std/queue.h new file mode 100644 index 0000000..e833c48 --- /dev/null +++ b/include/arc/std/queue.h @@ -0,0 +1,57 @@ +#ifndef ARC_STD_QUEUE_H_ +#define ARC_STD_QUEUE_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +/** + * @brief a queue type + */ +typedef struct ARC_Queue ARC_Queue; + +/** + * @brief creates ARC_Queue type + * + * @param queue ARC_Queue to initialize + */ +void ARC_Queue_Create(ARC_Queue **queue); + +/** + * @brief destroyes ARC_Queue type + */ +void ARC_Queue_Destroy(ARC_Queue *queue); + +/** + * @brief pushes value to end of queue + * + * @param queue ARC_Queue to push to + * @param data data that is being pushed + */ +void ARC_Queue_Push(ARC_Queue *queue, void *data); + +/** + * @brief pops the front off of the ARC_Queue + * + * @param queue ARC_Queue to remove from + * + * @return the poped data from the queue as a void * + */ +void *ARC_Queue_Pop(ARC_Queue *queue); + +/** + * @brief gets size of queue + * + * @param vector ARC_Queue to get size from + * + * @return the queues size + */ +uint32_t ARC_Queue_Size(ARC_Queue *queue); + +#ifdef __cplusplus +} +#endif + +#endif //ARC_STD_QUEUE_H_ \ No newline at end of file diff --git a/include/arc/std/stack.h b/include/arc/std/stack.h new file mode 100644 index 0000000..daf1496 --- /dev/null +++ b/include/arc/std/stack.h @@ -0,0 +1,57 @@ +#ifndef ARC_STD_STACK_H_ +#define ARC_STD_STACK_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +/** + * @brief a stack type + */ +typedef struct ARC_Stack ARC_Stack; + +/** + * @brief creates ARC_Stack type + * + * @param stack ARC_Stack to initialize + */ +void ARC_Stack_Create(ARC_Stack **stack); + +/** + * @brief destroyes ARC_Stack type + */ +void ARC_Stack_Destroy(ARC_Stack *stack); + +/** + * @brief pushes value on stack + * + * @param stack ARC_Stack to push to + * @param data data that is being pushed + */ +void ARC_Stack_Push(ARC_Stack *stack, void *data); + +/** + * @brief pops top off of ARC_Stack + * + * @param stack ARC_Stack to remove from + * + * @return the poped data from the stack as a void * + */ +void *ARC_Stack_Pop(ARC_Stack *stack); + +/** + * @brief gets size of stack + * + * @param stack ARC_Stack to get size from + * + * @return the stacks size + */ +uint32_t ARC_Stack_Size(ARC_Stack *stack); + +#ifdef __cplusplus +} +#endif + +#endif //ARC_STD_STACK_H_ \ No newline at end of file diff --git a/include/arc/std/vector.h b/include/arc/std/vector.h index dcf4ff2..f38ec27 100644 --- a/include/arc/std/vector.h +++ b/include/arc/std/vector.h @@ -18,7 +18,7 @@ typedef struct ARC_Vector ARC_Vector; * @param a first data struct * @param b second data struct * - * @return 0 when a == b + * @return 0 when a == b, TODO: return and ARC_Bool */ typedef int32_t (* ARC_Vector_CompareDataFn)(void *a, void *b); @@ -45,8 +45,9 @@ void ARC_Vector_Add(ARC_Vector *vector, void *data); /** * @brief remove from ARC_Vector * - * @param vector ARC_Vector to remove from - * @param data data that is being removed + * @param vector ARC_Vector to remove from + * @param data data that is being removed + * @param compare comparison callback to get check if data should be removed */ void ARC_Vector_Remove(ARC_Vector *vector, void *data, ARC_Vector_CompareDataFn compare); @@ -62,6 +63,8 @@ void ARC_Vector_RemoveIndex(ARC_Vector *vector, uint32_t *index); * @brief gets size of vector * * @param vector ARC_Vector to get size from + * + * @return the pointer of the vectors size, TODO: don't pass as a pointer */ uint32_t *ARC_Vector_Size(ARC_Vector *vector); @@ -79,4 +82,4 @@ void *ARC_Vector_Get(ARC_Vector *vector, uint32_t *index); } #endif -#endif //ARC_STD_VECTOR_H_ +#endif //ARC_STD_VECTOR_H_ \ No newline at end of file diff --git a/src/std/queue.c b/src/std/queue.c new file mode 100644 index 0000000..1c1e10d --- /dev/null +++ b/src/std/queue.c @@ -0,0 +1,86 @@ +#include "arc/std/queue.h" + +#include "arc/std/errno.h" +#include + +typedef struct ARC_QueueNode ARC_QueueNode; + +struct ARC_QueueNode { + ARC_QueueNode *next; + void *data; +}; + +struct ARC_Queue { + uint32_t currentSize; + ARC_QueueNode *node; +}; + +void ARC_Queue_Create(ARC_Queue **queue){ + *queue = (ARC_Queue *)malloc(sizeof(ARC_Queue)); + (*queue)->currentSize = 0; + (*queue)->node = NULL; +} + +void ARC_Queue_Destroy(ARC_Queue *queue){ + if(queue->currentSize != 0 || queue->node != NULL){ + arc_errno = ARC_ERRNO_DATA; + ARC_DEBUG_ERR("ARC_Queue_Destroy(queue) called, but queue was not empty"); + return; + } + + free(queue); +} + +void ARC_Queue_Push(ARC_Queue *queue, void *data){ + if(queue->currentSize == ~(uint32_t)0){ + arc_errno = ARC_ERRNO_OVERFLOW; + ARC_DEBUG_ERR("ARC_Queue_Push(queue) called, size of queue is maxed, cannot add another node"); + return; + } + + ARC_QueueNode *end = queue->node; + for(uint32_t i = 0; i < queue->currentSize; i++){ + if(end == NULL){ + arc_errno = ARC_ERRNO_NULL; + ARC_DEBUG_ERR("ARC_Queue_Push(queue), when getting end node encountered a NULL node"); + return; + } + + end = end->next; + } + + ARC_QueueNode *node = (ARC_QueueNode *)malloc(sizeof(ARC_QueueNode)); + node->data = data; + node->next = NULL; + + end->next = node; + + queue->currentSize++; +} + +void *ARC_Queue_Pop(ARC_Queue *queue){ + if(queue->currentSize == 0){ + arc_errno = ARC_ERRNO_DATA; + ARC_DEBUG_ERR("ARC_Queue_Pop(queue) called, but queue was not empty"); + return NULL; + } + + ARC_QueueNode *node = queue->node; + if(node == NULL){ + arc_errno = ARC_ERRNO_NULL; + ARC_DEBUG_ERR("ARC_Queue_Pop(queue) called, but node was NULL"); + return NULL; + } + + queue->node = node->next; + void *nodeData = node->data; + free(node); + + queue->currentSize--; + + return nodeData; +} + +uint32_t ARC_Queue_Size(ARC_Queue *queue){ + return queue->currentSize; +} \ No newline at end of file diff --git a/src/std/stack.c b/src/std/stack.c new file mode 100644 index 0000000..ca4f8a2 --- /dev/null +++ b/src/std/stack.c @@ -0,0 +1,76 @@ +#include "arc/std/stack.h" + +#include "arc/std/errno.h" +#include + +typedef struct ARC_StackNode ARC_StackNode; + +struct ARC_StackNode { + ARC_StackNode *next; + void *data; +}; + +struct ARC_Stack { + uint32_t currentSize; + ARC_StackNode *node; +}; + +void ARC_Stack_Create(ARC_Stack **stack){ + *stack = (ARC_Stack *)malloc(sizeof(ARC_Stack)); + (*stack)->currentSize = 0; + (*stack)->node = NULL; +} + +void ARC_Stack_Destroy(ARC_Stack *stack){ + if(stack->currentSize != 0 || stack->node != NULL){ + arc_errno = ARC_ERRNO_DATA; + ARC_DEBUG_ERR("ARC_Stack_Destroy(stack) called, but stack was not empty"); + return; + } + + free(stack); +} + +void ARC_Stack_Push(ARC_Stack *stack, void *data){ + if(stack->currentSize == ~(uint32_t)0){ + arc_errno = ARC_ERRNO_OVERFLOW; + ARC_DEBUG_ERR("ARC_Stack_Push(stack) called, size of stack is maxed, cannot add another node"); + return; + } + + ARC_StackNode *node = (ARC_StackNode *)malloc(sizeof(ARC_StackNode)); + node->data = data; + node->next = NULL; + + node->next = stack->node; + stack->node = node; + + stack->currentSize++; +} + +void *ARC_Stack_Pop(ARC_Stack *stack){ + if(stack->currentSize == 0){ + arc_errno = ARC_ERRNO_DATA; + ARC_DEBUG_ERR("ARC_Stack_Pop(stack) called, but stack was not empty"); + return NULL; + } + + ARC_StackNode *node = stack->node; + if(node == NULL){ + arc_errno = ARC_ERRNO_NULL; + ARC_DEBUG_ERR("ARC_Stack_Pop(stack) called, but node was NULL"); + return NULL; + } + + stack->node = node->next; + void *nodeData = node->data; + free(node); + + stack->currentSize--; + + return nodeData; +} + +uint32_t ARC_Stack_Size(ARC_Stack *stack){ + return stack->currentSize; +} \ No newline at end of file diff --git a/src/std/vector.c b/src/std/vector.c index 7e9158b..6ddba58 100644 --- a/src/std/vector.c +++ b/src/std/vector.c @@ -107,4 +107,4 @@ void *ARC_Vector_Get(ARC_Vector *vector, uint32_t *index){ } return vector->data[*index]; -} +} \ No newline at end of file