REFACTOR: Completely refactored and fixed the error handling
This commit is contained in:
+2
-2
@@ -17,12 +17,12 @@ add_library(libvec STATIC
|
||||
target_include_directories(libvec
|
||||
# PRIVATE: Internal headers under src/lib are only visible
|
||||
# to files being compiled into libvec (char.c, utils.c, libvec.c).
|
||||
PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/lib
|
||||
# PRIVATE
|
||||
|
||||
# PUBLIC: Consumers (and test_runner) are granted access ONLY to include/
|
||||
PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/lib # Turn back to private when ready
|
||||
)
|
||||
|
||||
add_executable(libvec_bin src/main.c)
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
```mermaid
|
||||
flowchart TD
|
||||
Start([Function Called]) --> CheckErr{Did an error occur?}
|
||||
|
||||
%% Error path
|
||||
CheckErr -->|Yes| PushBit["Bitwise OR error code into err<br/>err |= ERROR_BIT"]
|
||||
PushBit --> CheckOrigin{Is origin == NULL?}
|
||||
CheckOrigin -->|Yes| SetOrigin["Record location in origin<br/>origin = __func__ / __FILE__"]
|
||||
CheckOrigin -->|No| KeepOrigin[Preserve original root cause]
|
||||
|
||||
SetOrigin --> ReturnStruct
|
||||
KeepOrigin --> ReturnStruct
|
||||
|
||||
%% Success / Normal path
|
||||
CheckErr -->|No| ComputeValue["Write computation result<br/>value = result"]
|
||||
ComputeValue --> ReturnStruct
|
||||
|
||||
ReturnStruct(["Return / Pass Out _err_t"]) --> Caller{Caller Inspection}
|
||||
|
||||
Caller -->|Check errors| InspectBits[Inspect bit flags in err]
|
||||
Caller -->|Read payload| ReadValue[Access value data]
|
||||
Caller -->|Debug / Log| PrintOrigin[Print root cause origin]
|
||||
```
|
||||
Vendored
BIN
Binary file not shown.
Vendored
BIN
Binary file not shown.
+854
-781
File diff suppressed because it is too large
Load Diff
+53
-53
@@ -16,93 +16,93 @@ typedef struct
|
||||
size_t _internal_max_size;
|
||||
} _VecCharInternal;
|
||||
|
||||
unsigned int
|
||||
_vec_char_raise_err(const _VecCharInternal* ptr, unsigned int mask);
|
||||
void
|
||||
_vec_char_print_vec(const _VecCharInternal* ptr, err_t* err);
|
||||
|
||||
_VecCharInternal
|
||||
_vec_char_create(const char* src, const size_t size);
|
||||
_vec_char_cpy(const char* src, const size_t src_size, err_t* err);
|
||||
|
||||
size_t
|
||||
_vec_char_push_back(_VecCharInternal* vec, const char val);
|
||||
void
|
||||
_vec_char_push_back(_VecCharInternal* ptr, const char val, err_t* err);
|
||||
|
||||
size_t
|
||||
_vec_char_pop_back(_VecCharInternal* ptr);
|
||||
void
|
||||
_vec_char_pop_back(_VecCharInternal* ptr, err_t* err);
|
||||
|
||||
size_t
|
||||
_vec_char_reserve(_VecCharInternal* ptr, const size_t max_size);
|
||||
void
|
||||
_vec_char_reserve(_VecCharInternal* ptr, const size_t capacity, err_t* err);
|
||||
|
||||
size_t
|
||||
_vec_char_shrink_to_fit(_VecCharInternal* ptr);
|
||||
void
|
||||
_vec_char_shrink_to_fit(_VecCharInternal* ptr, err_t* err);
|
||||
|
||||
size_t
|
||||
_vec_char_swap(_VecCharInternal* ptr, _VecCharInternal* ptr_other);
|
||||
void
|
||||
_vec_char_swap(_VecCharInternal* ptr, _VecCharInternal* ptr_other, err_t* err);
|
||||
|
||||
char*
|
||||
_vec_char_data(const _VecCharInternal* ptr);
|
||||
_vec_char_data(const _VecCharInternal* ptr, err_t* err);
|
||||
|
||||
size_t
|
||||
_vec_char_capacity(const _VecCharInternal* ptr);
|
||||
_vec_char_capacity(const _VecCharInternal* ptr, err_t* err);
|
||||
|
||||
size_t
|
||||
_vec_char_max_size(const _VecCharInternal* ptr);
|
||||
_vec_char_max_size(const _VecCharInternal* ptr, err_t* err);
|
||||
|
||||
void
|
||||
_vec_char_delete(const _VecCharInternal* ptr);
|
||||
|
||||
size_t
|
||||
_vec_char_clear(_VecCharInternal* ptr);
|
||||
|
||||
char
|
||||
_vec_char_at(const _VecCharInternal* ptr, const size_t idx);
|
||||
_vec_char_delete(const _VecCharInternal* ptr, err_t* err);
|
||||
|
||||
void
|
||||
_vec_char_print_vec(const _VecCharInternal* ptr);
|
||||
|
||||
size_t
|
||||
_vec_char_begin(const _VecCharInternal* ptr);
|
||||
|
||||
size_t
|
||||
_vec_char_end(const _VecCharInternal* ptr);
|
||||
_vec_char_clear(_VecCharInternal* ptr, err_t* err);
|
||||
|
||||
char
|
||||
_vec_char_front(const _VecCharInternal* ptr);
|
||||
|
||||
char
|
||||
_vec_char_back(const _VecCharInternal* ptr);
|
||||
_vec_char_at(const _VecCharInternal* ptr, const size_t idx, err_t* err);
|
||||
|
||||
size_t
|
||||
_vec_char_empty(const _VecCharInternal* ptr);
|
||||
_vec_char_begin(const _VecCharInternal* ptr, err_t* err);
|
||||
|
||||
__attribute__((overloadable)) size_t
|
||||
_vec_char_erase(_VecCharInternal* ptr, const size_t iter);
|
||||
size_t
|
||||
_vec_char_end(const _VecCharInternal* ptr, err_t* err);
|
||||
|
||||
__attribute__((overloadable)) size_t
|
||||
char
|
||||
_vec_char_front(const _VecCharInternal* ptr, err_t* err);
|
||||
|
||||
char
|
||||
_vec_char_back(const _VecCharInternal* ptr, err_t* err);
|
||||
|
||||
size_t
|
||||
_vec_char_empty(const _VecCharInternal* ptr, err_t* err);
|
||||
|
||||
__attribute__((overloadable)) void
|
||||
_vec_char_erase(_VecCharInternal* ptr, const size_t iter, err_t* err);
|
||||
|
||||
__attribute__((overloadable)) void
|
||||
_vec_char_erase(_VecCharInternal* ptr, const size_t iter_start,
|
||||
const size_t iter_end);
|
||||
const size_t iter_end, err_t* err);
|
||||
|
||||
__attribute__((overloadable)) size_t
|
||||
_vec_char_resize(_VecCharInternal* ptr, const size_t size);
|
||||
__attribute__((overloadable)) void
|
||||
_vec_char_resize(_VecCharInternal* ptr, const size_t size, err_t* err);
|
||||
|
||||
__attribute__((overloadable)) size_t
|
||||
_vec_char_resize(_VecCharInternal* ptr, const size_t size, const char val);
|
||||
__attribute__((overloadable)) void
|
||||
_vec_char_resize(_VecCharInternal* ptr, const size_t size, const char val,
|
||||
err_t* err);
|
||||
|
||||
__attribute__((overloadable)) size_t
|
||||
__attribute__((overloadable)) void
|
||||
_vec_char_assign(_VecCharInternal* ptr, const _VecCharInternal* iterator,
|
||||
const size_t iter_start, const size_t iter_end);
|
||||
const size_t iter_start, const size_t iter_end, err_t* err);
|
||||
|
||||
__attribute__((overloadable)) size_t
|
||||
_vec_char_assign(_VecCharInternal* ptr, const size_t amount, const char val);
|
||||
__attribute__((overloadable)) void
|
||||
_vec_char_assign(_VecCharInternal* ptr, const size_t amount, const char val,
|
||||
err_t* err);
|
||||
|
||||
__attribute__((overloadable)) size_t
|
||||
_vec_char_insert(_VecCharInternal* ptr, const size_t iter, const char val);
|
||||
__attribute__((overloadable)) void
|
||||
_vec_char_insert(_VecCharInternal* ptr, const size_t iter, const char val,
|
||||
err_t* err);
|
||||
|
||||
__attribute__((overloadable)) size_t
|
||||
__attribute__((overloadable)) void
|
||||
_vec_char_insert(_VecCharInternal* ptr, const size_t iter, const size_t amount,
|
||||
const char val);
|
||||
const char val, err_t* err);
|
||||
|
||||
__attribute__((overloadable)) size_t
|
||||
__attribute__((overloadable)) void
|
||||
_vec_char_insert(_VecCharInternal* ptr, const _VecCharInternal* iterator,
|
||||
const size_t iter, const size_t iter_start,
|
||||
const size_t iter_end);
|
||||
const size_t iter_end, err_t* err);
|
||||
|
||||
#endif
|
||||
|
||||
+39
-4
@@ -17,16 +17,16 @@ vec_get_error_desc(ERROR_CODES err)
|
||||
}
|
||||
|
||||
void
|
||||
vec_print_errs(const unsigned int err)
|
||||
vec_print_errs(const err_t err)
|
||||
{
|
||||
if (err == SUCCESS)
|
||||
if (err.flags == SUCCESS)
|
||||
{
|
||||
printf("%s", vec_get_error_desc(SUCCESS));
|
||||
printf("%s: %s\n", err.origin, vec_get_error_desc(SUCCESS));
|
||||
return;
|
||||
}
|
||||
{
|
||||
#define X(name, val, desc) \
|
||||
if ((val) != 0 && ((err) & (val))) \
|
||||
if ((val) != 0 && ((err.flags) & (val))) \
|
||||
{ \
|
||||
printf("%s\n", vec_get_error_desc(name)); \
|
||||
}
|
||||
@@ -34,3 +34,38 @@ vec_print_errs(const unsigned int err)
|
||||
#undef X
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
print_bits(unsigned int num)
|
||||
{
|
||||
// Determine the number of bits in an unsigned int (usually 32)
|
||||
int total_bits = sizeof(num) * 8;
|
||||
|
||||
// Loop from the most significant bit (MSB) down to the least
|
||||
// significant bit (LSB)
|
||||
for (int i = total_bits - 1; i >= 0; i--)
|
||||
{
|
||||
// Shift a 1 to the i-th position and check if that bit is set
|
||||
int bit = (num >> i) & 1;
|
||||
printf("%d", bit);
|
||||
|
||||
// Optional: Add a space every 4 or 8 bits for readability
|
||||
if (i % 8 == 0)
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// void
|
||||
// _vec_update_origin(err_t* err, char* str)
|
||||
// {
|
||||
// err_t res;
|
||||
// if (err == nullptr)
|
||||
// {
|
||||
// res.err = 0;
|
||||
// res.value = 0;
|
||||
// }
|
||||
// if (!err->origin) err->origin = str;
|
||||
// }
|
||||
|
||||
+38
-10
@@ -6,18 +6,23 @@
|
||||
|
||||
#define CAPACITY 1024
|
||||
#define VEC_FAULT_BITS 32
|
||||
static const size_t VEC_MAX = SIZE_MAX / sizeof(int);
|
||||
|
||||
#define ERROR_LIST(X) \
|
||||
X(SUCCESS, 0, "No Errors Found") \
|
||||
X(ERR_ALLOC_FAIL, (1u << 1), "Calloc :- Failed to allocate") \
|
||||
X(ERR_VEC_NULL, (1u << 2), "Vector :- Given vector is null") \
|
||||
X(ERR_BUF_NULL, (1u << 3), "Buffer :- NULL") \
|
||||
X(ERR_SIZE_ZERO, (1u << 4), \
|
||||
X(ERR_ALLOC_FAIL, (1u << 0), "Alloc :- Failed to allocate") \
|
||||
X(ERR_VEC_NULL, (1u << 1), "Vector :- Given vector is null") \
|
||||
X(ERR_BUF_NULL, (1u << 2), "Buffer :- NULL") \
|
||||
X(ERR_SIZE_ZERO, (1u << 3), \
|
||||
"Size :- Size is or is less than 0 or invalid") \
|
||||
X(ERR_OUT_OF_BOUNDS, (1u << 5), "Bounds:- Value is out of bounds") \
|
||||
X(ERR_DOUBLE_BUF, (1u << 6), \
|
||||
X(ERR_OUT_OF_BOUNDS, (1u << 4), "Bounds:- Value is out of bounds") \
|
||||
X(ERR_DOUBLE_BUF, (1u << 5), \
|
||||
"Buffer :- Both buffers are copies of each other") \
|
||||
X(ERR_SIZE_OVERFLOW, (1u << 7), "Max :- Vector Max value is overloaded")
|
||||
X(ERR_SIZE_OVERFLOW, (1u << 6), \
|
||||
"Max :- Vector Max value is overloaded") \
|
||||
X(ERR_ORIGIN_UNALTERABLE, (1u << 7), \
|
||||
"Origin :- Origin is non-null and cannot be overridden") \
|
||||
X(ERR_STR_NULL, (1u << 8), "String :- String is null value")
|
||||
|
||||
typedef enum
|
||||
{
|
||||
@@ -26,11 +31,36 @@ typedef enum
|
||||
#undef X
|
||||
} ERROR_CODES;
|
||||
|
||||
/* NOTE
|
||||
* Value is the return value for functions like _vec_char_capacity
|
||||
* Err is the unsigned bit range for all the errors tracked
|
||||
* origin is a string for where the bug started and IS CHANGED ONLY WHEN NULL
|
||||
* */
|
||||
|
||||
typedef struct
|
||||
{
|
||||
unsigned int flags;
|
||||
char* origin; // Where the main bug started
|
||||
} err_t;
|
||||
|
||||
#define ERR_T_INIT { .flags = 0, .origin = NULL }
|
||||
#define VEC_INIT \
|
||||
{ ._internal_arr = NULL, \
|
||||
._internal_size = 0, \
|
||||
._internal_capacity = 0, \
|
||||
._internal_max_size = VEC_MAX }
|
||||
|
||||
const char*
|
||||
vec_get_error_desc(ERROR_CODES err);
|
||||
|
||||
void
|
||||
vec_print_errs(const unsigned int err);
|
||||
vec_print_errs(const err_t err);
|
||||
|
||||
// void
|
||||
// _vec_update_origin(err_t* err, char* str);
|
||||
|
||||
void
|
||||
print_bits(unsigned int num);
|
||||
|
||||
#define LOG(msg) printf("LOG: %s\n", msg) /**< Print log message*/
|
||||
#define DEBUG(msg) printf("DEBUB: %s\n", msg) /**< Print debug message*/
|
||||
@@ -40,6 +70,4 @@ vec_print_errs(const unsigned int err);
|
||||
|
||||
#define find_len(arr) sizeof(arr) / sizeof(arr[0])
|
||||
|
||||
static const size_t VEC_MAX = SIZE_MAX / sizeof(int);
|
||||
|
||||
#endif
|
||||
|
||||
+32
-123
@@ -12,143 +12,52 @@
|
||||
* String 32 Bits with suffix of 's'
|
||||
*/
|
||||
|
||||
#include <libvec.h>
|
||||
// #include <libvec.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lib/char/char.h"
|
||||
#include "lib/utils/utils.h"
|
||||
|
||||
void
|
||||
print_bits(unsigned int num);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char* _internal_arr;
|
||||
size_t _internal_size;
|
||||
size_t _internal_capacity;
|
||||
size_t _internal_max_size;
|
||||
} _VecCharInternal;
|
||||
|
||||
/* NOTE
|
||||
* Value is the return value for functions like _vec_char_capacity
|
||||
* Err is the unsigned bit range for all the errors tracked
|
||||
* origin is a string for where the bug started and IS CHANGED ONLY WHEN NULL
|
||||
* */
|
||||
|
||||
typedef struct
|
||||
{
|
||||
unsigned int err;
|
||||
size_t value;
|
||||
char* origin; // Where the main bug started
|
||||
} _err_t;
|
||||
|
||||
void
|
||||
_vec_update_origin(_err_t* err, char* str)
|
||||
{
|
||||
_err_t res;
|
||||
if (err == nullptr)
|
||||
{
|
||||
res.err = 0;
|
||||
res.value = 0;
|
||||
}
|
||||
if (!err->origin) err->origin = str;
|
||||
}
|
||||
|
||||
unsigned int
|
||||
_vec_char_raise_err(const _VecCharInternal* ptr, unsigned int mask)
|
||||
|
||||
{
|
||||
unsigned int res = SUCCESS;
|
||||
res |= mask;
|
||||
if (ptr == nullptr) res |= ERR_VEC_NULL;
|
||||
if (ptr->_internal_arr == nullptr) res |= ERR_BUF_NULL;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
_VecCharInternal
|
||||
_vec_char_create(const char* src, const size_t size)
|
||||
{
|
||||
unsigned int err = 0;
|
||||
_VecCharInternal self = {
|
||||
._internal_arr = NULL,
|
||||
._internal_size = 0,
|
||||
._internal_capacity = CAPACITY,
|
||||
._internal_max_size = VEC_MAX,
|
||||
};
|
||||
if (size >= self._internal_max_size)
|
||||
{
|
||||
LOG("If you need more memory to start, create a blank and "
|
||||
"reserve.");
|
||||
return self;
|
||||
}
|
||||
if (self._internal_capacity < size)
|
||||
{
|
||||
size_t dst_cap = self._internal_capacity;
|
||||
while (dst_cap < size && dst_cap * 2 < self._internal_max_size)
|
||||
dst_cap *= 2;
|
||||
self._internal_capacity = (dst_cap < size) ? size : dst_cap;
|
||||
}
|
||||
if (size > 0)
|
||||
{
|
||||
self._internal_arr =
|
||||
calloc(self._internal_capacity, sizeof(char));
|
||||
err = _vec_char_raise_err(&self, err);
|
||||
if ((err & SUCCESS) == 1)
|
||||
{
|
||||
self._internal_arr = NULL;
|
||||
vec_print_errs(err);
|
||||
free(self._internal_arr);
|
||||
return self;
|
||||
}
|
||||
}
|
||||
if (!src) err |= ERR_BUF_NULL;
|
||||
else
|
||||
{
|
||||
for (size_t i = 0; i < size; i++)
|
||||
{
|
||||
self._internal_arr[i] = src[i];
|
||||
self._internal_size++;
|
||||
}
|
||||
}
|
||||
vec_print_errs(err);
|
||||
return self;
|
||||
}
|
||||
|
||||
void
|
||||
print_bits(unsigned int num)
|
||||
{
|
||||
// Determine the number of bits in an unsigned int (usually 32)
|
||||
int total_bits = sizeof(num) * 8;
|
||||
|
||||
// Loop from the most significant bit (MSB) down to the least
|
||||
// significant bit (LSB)
|
||||
for (int i = total_bits - 1; i >= 0; i--)
|
||||
{
|
||||
// Shift a 1 to the i-th position and check if that bit is set
|
||||
int bit = (num >> i) & 1;
|
||||
printf("%d", bit);
|
||||
|
||||
// Optional: Add a space every 4 or 8 bits for readability
|
||||
if (i % 8 == 0)
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
// TODO:
|
||||
// Fix the error checking because you make a status to hold all local
|
||||
// errors but then in some cases you or them to the main err handler and
|
||||
// if the error doesnt need to be there like a good error but one that
|
||||
// exists (i.e push_back getting a blank array), then you will have a
|
||||
// bloated and broken error handler
|
||||
//
|
||||
// Your pop back is a great example of how to handle it and is a clean
|
||||
// if statement as a header to all functions.
|
||||
char res[4] = { 'a', 'b', 'c', 'd' };
|
||||
_VecCharInternal v1 = _vec_char_create(nullptr, 3);
|
||||
unsigned int mark = 1 << 0;
|
||||
_err_t err = { 0, 0, "Te" };
|
||||
_vec_update_origin(&err, "Test");
|
||||
printf("%s", err.origin);
|
||||
err_t status = ERR_T_INIT, err = ERR_T_INIT;
|
||||
status.flags |= ERR_BUF_NULL | ERR_VEC_NULL | ERR_STR_NULL;
|
||||
// _VecCharInternal v1 = VEC_INIT;
|
||||
// _vec_char_push_back(&v1, 'c', &status);
|
||||
// _vec_char_print_vec(&v1, &status);
|
||||
|
||||
unsigned int mask = 0;
|
||||
mask = ERR_BUF_NULL ^ status.flags;
|
||||
|
||||
print_bits(mask);
|
||||
print_bits(status.flags);
|
||||
|
||||
// if (status.origin)
|
||||
// {
|
||||
// printf("Error started in: %s\n", status.origin);
|
||||
// }
|
||||
// printf("%s: %s", err.origin, vec_print_errs(err));
|
||||
// vec_print_errs(status);
|
||||
// printf("%u\n", status.err);
|
||||
// _vec_update_origin(&err, "Test");
|
||||
// printf("%s", err.origin);
|
||||
// for (int i = 0; i < VEC_FAULT_BITS; i++)
|
||||
// {
|
||||
// print_bits(mark);
|
||||
|
||||
Reference in New Issue
Block a user