REFACTOR: Error handling
This commit is contained in:
Vendored
BIN
Binary file not shown.
Vendored
BIN
Binary file not shown.
+764
-766
File diff suppressed because it is too large
Load Diff
+2
-2
@@ -16,8 +16,8 @@ typedef struct
|
||||
size_t _internal_max_size;
|
||||
} _VecCharInternal;
|
||||
|
||||
size_t
|
||||
_vec_char_is_valid(const _VecCharInternal* ptr);
|
||||
unsigned int
|
||||
_vec_char_raise_err(const _VecCharInternal* ptr, unsigned int mask);
|
||||
|
||||
_VecCharInternal
|
||||
_vec_char_create(const char* src, const size_t size);
|
||||
|
||||
+34
-10
@@ -1,12 +1,36 @@
|
||||
#include <stdio.h>
|
||||
#include <utils/utils.h>
|
||||
|
||||
const char* error_messages[] = {
|
||||
[ERR_ALLOC_FAIL] = "Calloc :- Failed to allocate",
|
||||
[ERR_BUF_NULL] = "Buffer :- NULL",
|
||||
[ERR_SIZE_ZERO] = "Size :- Size is or is less than 0 or invalid",
|
||||
[ERR_VECTOR_NULL] = "Vector :- Given vector is null",
|
||||
[ERR_MAX_PASSED] = "Max :- Vector Max value is overloaded",
|
||||
[ERR_OUT_OF_BOUNDS] = "Bounds:- Value is out of bounds",
|
||||
[ERR_NULL] = "Null:- Vector or buffer is null",
|
||||
[ERR_DOUBLE_BUF] = "Buffer :- Both buffers are copies of each other",
|
||||
};
|
||||
const char*
|
||||
vec_get_error_desc(ERROR_CODES err)
|
||||
{
|
||||
switch (err)
|
||||
{
|
||||
#define X(name, val, desc) \
|
||||
case name: \
|
||||
return desc;
|
||||
ERROR_LIST(X)
|
||||
#undef X
|
||||
default:
|
||||
"Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
vec_print_errs(const unsigned int err)
|
||||
{
|
||||
if (err == SUCCESS)
|
||||
{
|
||||
printf("%s", vec_get_error_desc(SUCCESS));
|
||||
return;
|
||||
}
|
||||
{
|
||||
#define X(name, val, desc) \
|
||||
if ((val) != 0 && ((err) & (val))) \
|
||||
{ \
|
||||
printf("%s\n", vec_get_error_desc(name)); \
|
||||
}
|
||||
ERROR_LIST(X)
|
||||
#undef X
|
||||
}
|
||||
}
|
||||
|
||||
+26
-12
@@ -5,21 +5,35 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#define CAPACITY 1024
|
||||
#define SUCCESS 0
|
||||
#define VEC_FAULT_BITS 32
|
||||
|
||||
#define FAILURE 1
|
||||
#define ERR_ALLOC_FAIL 20
|
||||
#define ERR_BUF_NULL 21
|
||||
#define ERR_SIZE_ZERO 22
|
||||
#define ERR_VECTOR_NULL 23
|
||||
#define ERR_MAX_PASSED 24
|
||||
#define ERR_OUT_OF_BOUNDS 25
|
||||
#define ERR_NULL 26
|
||||
#define ERR_DOUBLE_BUF 27
|
||||
#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), \
|
||||
"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), \
|
||||
"Buffer :- Both buffers are copies of each other") \
|
||||
X(ERR_SIZE_OVERFLOW, (1u << 7), "Max :- Vector Max value is overloaded")
|
||||
|
||||
extern const char* error_messages[];
|
||||
typedef enum
|
||||
{
|
||||
#define X(name, val, desc) name = (val),
|
||||
ERROR_LIST(X)
|
||||
#undef X
|
||||
} ERROR_CODES;
|
||||
|
||||
#define LOG(msg) printf("LOG: %s\n", msg) /**< Print log message*/
|
||||
const char*
|
||||
vec_get_error_desc(ERROR_CODES err);
|
||||
|
||||
void
|
||||
vec_print_errs(const unsigned int err);
|
||||
|
||||
#define LOG(msg) printf("LOG: %s\n", msg) /**< Print log message*/
|
||||
#define DEBUG(msg) printf("DEBUB: %s\n", msg) /**< Print debug message*/
|
||||
#define LOGERROR(error_code) \
|
||||
printf("ERROR: %s\n", \
|
||||
error_messages[error_code]) /**< Print log error message*/
|
||||
|
||||
@@ -1,3 +1 @@
|
||||
#include <libvec.h>
|
||||
|
||||
#include "char/char.h"
|
||||
|
||||
+141
@@ -15,11 +15,152 @@
|
||||
#include <libvec.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.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()
|
||||
{
|
||||
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);
|
||||
// for (int i = 0; i < VEC_FAULT_BITS; i++)
|
||||
// {
|
||||
// print_bits(mark);
|
||||
// mark <<= 1;
|
||||
// }
|
||||
// const char* str = vec_get_error_desc(ERR_ALLOC_FAIL);
|
||||
// const unsigned int error = ERR_BUF_NULL;
|
||||
// if (!(ERR_ALLOC_FAIL & SUCCESS))
|
||||
// {
|
||||
// printf("Hello\n");
|
||||
// }
|
||||
// printf("%s", str);
|
||||
// char ar1[] = { '1', '2', '3', '4' };
|
||||
// char ar2[] = { 'a', 'b', 'c', 'd' };
|
||||
// Vec8_t v1 = create(ar2, find_len(ar2));
|
||||
|
||||
Reference in New Issue
Block a user