diff --git a/include/tlibc/errors.h b/include/tlibc/errors.h new file mode 100755 index 0000000..9dc67e8 --- /dev/null +++ b/include/tlibc/errors.h @@ -0,0 +1,26 @@ +#pragma once +#include "std.h" +#include "string/cstr.h" + +typedef struct Result_ { + cstr error; + union { + u64 v_u64; + i64 v_i64; + f32 v_f32; + f64 v_f64; + void* v_ptr; + }; +} Result_; + +#define Result(T) Result_ + +char* _genErrorMessage(cstr file, i32 line, cstr func, char* msg, bool free_msg); + + +#define _genErrorMessageHere(MSG, FREE_MSG) \ + _genErrorMessage(__FILE__, __LINE__, __func__, MSG, FREE_MSG) + +#define RESULT_ERROR(MSG, FREE_MSG) (Result_){ .error = _genErrorMessageHere(MSG, FREE_MSG) } +#define RESULT_VOID (Result_){ .error = NULL } +#define RESULT_VALUE(TYPE, V) (Result_){ .error = NULL, .v_##TYPE = V } diff --git a/src/errors.c b/src/errors.c new file mode 100755 index 0000000..846b57c --- /dev/null +++ b/src/errors.c @@ -0,0 +1,10 @@ +#include "tlibc/errors.h" + +#define ERRMSG_LENGTH 1024 + +char* _genErrorMessage(cstr file, i32 line, cstr func, char* msg, bool free_msg){ + char* result = sprintf_malloc(1024, "[%s:%i] %s() throwed error: %s", file, line, func, msg); + if(free_msg) + free(msg); + return result; +}