implemented ErrorCodePage

This commit is contained in:
Timerix 2025-11-10 11:42:25 +05:00
parent 6a1067a612
commit 972f244ae5
5 changed files with 82 additions and 5 deletions

17
README.md Normal file
View File

@ -0,0 +1,17 @@
# tlibc
C library with collections, strings, exceptions, io...
## Build
1. Clone the repository.
```
git clone https://timerix.ddns.net/git/Timerix/tlibc.git
```
2. Install [cbuild](https://timerix.ddns.net/git/Timerix/cbuild.git).
3. Build static library
```
cd tlibtoml
cbuild build_static_lib_dbg
```
## Usage

View File

@ -18,15 +18,31 @@ typedef struct ErrorCallPos {
typedef struct Error {
str msg;
bool is_msg_on_heap;
u16 error_code_page;
u32 error_code;
List(ErrorCallPos) call_stack;
} Error;
Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos p);
Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos p,
u16 error_code_page, u32 error_code);
void Error_free(Error* e);
void Error_addCallPos(Error* e, ErrorCallPos p);
str Error_toStr(Error* e);
void Error_printAndExit(Error* e) ATTRIBUTE_NORETURN;
#define ErrorCodePage_name(name) ErrorCodePage_##name
#define ErrorCodePage_declare(name) extern u16 ErrorCodePage_name(name);
#define ErrorCodePage_define(name) u16 ErrorCodePage_name(name) = 0;
void _ErrorCodePage_register(u16* error_code_page_ptr);
#define ErrorCodePage_register(name) _ErrorCodePage_register(&ErrorCodePage_name(name));
typedef enum TLIBC_ERROR {
TLIBC_ERROR_UNKNOWN
} TLIBC_ERROR;
#define ErrorCodePage_NONE 0
ErrorCodePage_declare(TLIBC_ERROR);
ErrorCodePage_declare(LIBC_ERRNO);
typedef struct Result_ {
Error* error;
union {
@ -46,9 +62,21 @@ typedef struct Result_ {
///USAGE: IGNORE_RESULT trySomething();
#define IGNORE_RESULT Result_ __ignored_##__LINE__ ATTRIBUTE_UNUSED =
#define RESULT_ERROR(MSG, IS_MSG_ON_HEAP) (Result_){ .error = Error_create(MSG, IS_MSG_ON_HEAP, ErrorCallPos_here()) }
#define RESULT_ERROR_FMT(FORMAT, ARGS...) RESULT_ERROR(sprintf_malloc(FORMAT ,##ARGS), true)
#define RESULT_ERROR_ERRNO() RESULT_ERROR(strerror(errno), false)
#define RESULT_ERROR_CODE(CODE_PAGE, CODE, MSG, IS_MSG_ON_HEAP) (Result_){ \
.error = Error_create(MSG, IS_MSG_ON_HEAP, ErrorCallPos_here(), \
ErrorCodePage_name(CODE_PAGE), CODE), \
.u = 0 \
}
#define RESULT_ERROR_CODE_FMT(CODE_PAGE, CODE, FORMAT, ARGS...) \
RESULT_ERROR_CODE(CODE_PAGE, CODE, sprintf_malloc(FORMAT ,##ARGS), true)
#define RESULT_ERROR(MSG, IS_MSG_ON_HEAP) \
RESULT_ERROR_CODE(NONE, 0, MSG, IS_MSG_ON_HEAP);
#define RESULT_ERROR_FMT(FORMAT, ARGS...) \
RESULT_ERROR_CODE_FMT(NONE, 0, FORMAT ,##ARGS)
#define RESULT_ERROR_ERRNO() \
RESULT_ERROR_CODE(LIBC_ERRNO, errno, strerror(errno), false)
#define RESULT_VOID (Result_){ .error = NULL, .u = 0 }
#define RESULT_VALUE(FIELD, V) (Result_){ .error = NULL, .FIELD = V }

5
include/tlibc/tlibc.h Normal file
View File

@ -0,0 +1,5 @@
#include "tlibc/errors.h"
/// call once at the start of main()
Result(void) tlibc_init();
void tlibc_deinit();

View File

@ -3,10 +3,14 @@
#define ERRMSG_LENGTH 1024
Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos p){
Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos p,
u16 error_code_page, u32 error_code)
{
Error* e = (Error*)malloc(sizeof(Error));
e->msg = str_construct((char*)(void*)msg, strlen(msg), true);
e->is_msg_on_heap = is_msg_on_heap;
e->error_code_page = error_code_page;
e->error_code = error_code;
e->call_stack = List_alloc(ErrorCallPos, 16);
Error_addCallPos(e, p);
return e;
@ -51,3 +55,12 @@ void Error_printAndExit(Error* e){
Error_free(e);
exit(111);
}
ErrorCodePage_define(TLIBC_ERROR);
ErrorCodePage_define(LIBC_ERRNO);
static u16 _error_code_page_last = 0;
void _ErrorCodePage_register(u16* error_code_page_ptr){
*error_code_page_ptr = ++_error_code_page_last;
}

14
src/tlibc.c Normal file
View File

@ -0,0 +1,14 @@
#include "tlibc/tlibc.h"
Result(void) tlibc_init(){
Deferral(8);
ErrorCodePage_register(TLIBC_ERROR);
ErrorCodePage_register(LIBC_ERRNO);
Return RESULT_VOID;
}
void tlibc_deinit(){
}