Compare commits

...

5 Commits

Author SHA1 Message Date
82a6293f21 added macro try_handle() 2026-01-13 18:20:01 +05:00
c4fd22542d fixed IGNORE_RESULT macro bug 2026-01-09 09:47:41 +05:00
4cfed24ec3 fixed cpp guard 2026-01-09 05:52:38 +05:00
de88e9ff16 added List_T_destroyWithElements 2025-12-21 20:22:35 +05:00
26de01c3e7 added path_getUserDir, dir_createParent 2025-12-21 20:00:10 +05:00
11 changed files with 124 additions and 56 deletions

View File

@@ -19,6 +19,8 @@ C library with collections, strings, exceptions, io...
cbuild build_static_lib_dbg
```
6. If you use tlibc as static library, add `LINKER_LIBS` from tlibc `project.config` to your project.
## Usage
```c

View File

@@ -39,6 +39,13 @@ static inline List(T) List_##T##_copy(const List(T)* src) { \
\
static inline void List_##T##_destroy(List(T)* self) { _List_destroy((void*)self); } \
\
static inline void List_##T##_destroyWithElements(List(T)* self, void (*elem_destructor)(T*)) { \
for(u32 i = 0; i < self->len; i++){ \
elem_destructor(self->data + i);\
} \
_List_destroy((void*)self); \
} \
\
/* alloc bigger buffer if size + len_to_add won't fit in current */ \
static inline void List_##T##_increaseCapacity(List(T)* self, u32 len_to_add){ \
_List_increaseCapacity((void*)self, len_to_add); \

View File

@@ -73,8 +73,11 @@ typedef struct Result_ {
/// Warning can be suppressed by IGNORE_RESULT
#define Result(T) Result_ ATTRIBUTE_WARN_UNUSED_RESULT
#define ResultVar(T) Result_
// for some stupid reason gcc requires more than 3 levels of macros to concat token with line number
#define _ignore_var_name(N) CAT2(__ignored_, N)
///USAGE: IGNORE_RESULT trySomething();
#define IGNORE_RESULT Result_ __ignored_##__LINE__ ATTRIBUTE_UNUSED =
#define IGNORE_RESULT Result_ _ignore_var_name(__LINE__) ATTRIBUTE_UNUSED =
#define RESULT_ERROR_CODE(CODE_PAGE, CODE, MSG, IS_MSG_ON_HEAP) (Result_){ \
@@ -97,55 +100,61 @@ typedef struct Result_ {
#define RESULT_VOID (Result_){ .error = NULL, .u = 0 }
#define RESULT_VALUE(FIELD, V) (Result_){ .error = NULL, .FIELD = V }
#define try(VAR, RESULT_FIELD, RSLT_CALL) \
_try(VAR, RESULT_FIELD, RSLT_CALL, __LINE__)
#define try_void(RSLT_CALL) \
_try_void(RSLT_CALL, __LINE__)
#define try_fatal(VAR, RESULT_FIELD, RSLT_CALL) \
_try_fatal(VAR, RESULT_FIELD, RSLT_CALL, __LINE__)
#define try_fatal_void(RSLT_CALL) \
_try_fatal_void(RSLT_CALL, __LINE__)
#define try_handle(VAR, RESULT_FIELD, RSLT_CALL, HANDLER) \
_try_handle(VAR, RESULT_FIELD, RSLT_CALL, HANDLER, __LINE__)
#define try_handle_void(RSLT_CALL, HANDLER) \
_try_handle_void(RSLT_CALL, HANDLER, __LINE__)
#define try_stderrcode(RSLT_CALL) \
_try_stderrcode(RSLT_CALL, __LINE__)
#define try_fatal_stderrcode(RSLT_CALL) \
_try_fatal_stderrcode(RSLT_CALL, __LINE__)
#define _rname(N) __r_##N
#define try(VAR, RESULT_FIELD, RSLT_CALL) _try(VAR, RESULT_FIELD, RSLT_CALL, __LINE__)
#define try_fatal(VAR, RESULT_FIELD, RSLT_CALL) _try_fatal(VAR, RESULT_FIELD, RSLT_CALL, __LINE__)
#define try_void(RSLT_CALL) _try_void(RSLT_CALL, __LINE__)
#define try_fatal_void(RSLT_CALL) _try_fatal_void(RSLT_CALL, __LINE__)
#define try_stderrcode(RSLT_CALL) _try_stderrcode(RSLT_CALL, __LINE__)
#define try_fatal_stderrcode(RSLT_CALL) _try_fatal_stderrcode(RSLT_CALL, __LINE__)
#define _try_handle(VAR, RESULT_FIELD, RSLT_CALL, HANDLER, N) \
Result_ _rname(N) = RSLT_CALL;\
if(_rname(N).error){\
Error_addCallPos(_rname(N).error, ErrorCallPos_here());\
HANDLER(_rname(N));\
}\
VAR = _rname(N).RESULT_FIELD;
#define _try_handle_void(RSLT_CALL, HANDLER, N) \
Result_ _rname(N) = RSLT_CALL;\
if(_rname(N).error){\
Error_addCallPos(_rname(N).error, ErrorCallPos_here());\
HANDLER(_rname(N));\
}\
#define _try__handler(R) Return R
#define _try(VAR, RESULT_FIELD, RSLT_CALL, N) \
Result_ _rname(N) = RSLT_CALL;\
if(_rname(N).error){\
Error_addCallPos(_rname(N).error, ErrorCallPos_here());\
Return _rname(N);\
};\
VAR = _rname(N).RESULT_FIELD;
#define _try_void(RSLT_CALL, N) do {\
Result_ _rname(N) = RSLT_CALL;\
if(_rname(N).error){\
Error_addCallPos(_rname(N).error, ErrorCallPos_here());\
Return _rname(N);\
};\
} while(0)
_try_handle(VAR, RESULT_FIELD, RSLT_CALL, _try__handler, N)
#define _try_void(RSLT_CALL, N) \
_try_handle_void(RSLT_CALL, _try__handler, N)
#define _try_fatal__handler(R) Error_printAndExit(R.error)
#define _try_fatal(VAR, RESULT_FIELD, RSLT_CALL, N) \
Result_ _rname(N) = RSLT_CALL;\
if(_rname(N).error){\
Error_addCallPos(_rname(N).error, ErrorCallPos_here());\
Error_printAndExit(_rname(N).error);\
};\
VAR = _rname(N).RESULT_FIELD;
_try_handle(VAR, RESULT_FIELD, RSLT_CALL, _try_fatal__handler, N)
#define _try_fatal_void(RSLT_CALL, N) \
_try_handle_void(RSLT_CALL, _try_fatal__handler, N)
#define _try_fatal_void(RSLT_CALL, N) do {\
Result_ _rname(N) = RSLT_CALL;\
if(_rname(N).error){\
Error_addCallPos(_rname(N).error, ErrorCallPos_here());\
Error_printAndExit(_rname(N).error);\
};\
} while(0)
#define _try_stderrcode(CALL, N) do {\
#define _try_stderrcode(CALL, N) \
int _rname(N) = CALL;\
if(_rname(N) != 0){\
Return RESULT_ERROR_CODE(LIBC_ERRNO, _rname(N), str_from_cstr(strerror_malloc(_rname(N))), true);\
}\
} while(0)
#define _try_fatal_stderrcode(CALL, N) do {\
#define _try_fatal_stderrcode(CALL, N) \
int _rname(N) = CALL;\
if(_rname(N) != 0){\
Error_printAndExit(Error_create(\
@@ -154,6 +163,5 @@ typedef struct Result_ {
ErrorCodePage_name(LIBC_ERRNO), _rname(N)\
));\
}\
} while(0)
#define try_assert(EXPR) if(!(EXPR)) { Return RESULT_ERROR_LITERAL("assertion must be true: " #EXPR); }

View File

@@ -39,6 +39,8 @@ str path_dirname(str path);
/// @return pointer to a segment of path.data or path itself if no path_sep has been found
str path_basename(str path, bool remove_ext);
/// @return heap-allocated string
Result(char*) path_getUserDir();
//////////////////////////////////////////////////////////////////////////////
// FILE //
@@ -145,8 +147,15 @@ Result(void) file_readWholeText(FILE* f, str* out_str);
// DIRECTORY //
//////////////////////////////////////////////////////////////////////////////
/// @return true if directory exists or `path` is null or empty or '.' or './'
bool dir_exists(cstr path);
/// @brief creates directories specified in path recursively
/// EXAMPLE: dir_createParent("a/b/c") -> creates "a", "a/b", "a/b/c"
/// @return false if directory was present already, true if it has been created
Result(bool) dir_create(cstr path);
/// @brief creates directories except the last part of path
/// EXAMPLE: dir_createParent("a/b/c") -> creates "a", "a/b"
/// @return false if directory was present already, true if it has been created
Result(bool) dir_createParent(cstr path);

View File

@@ -1,6 +1,6 @@
#pragma once
#if __cplusplus
#ifdef __cplusplus
extern "C" {
#endif
@@ -142,6 +142,6 @@ typedef void (*Destructor_t)(void* self);
CODE; \
PRAGMA_WARNING_POP
#if __cplusplus
#ifdef __cplusplus
}
#endif

View File

@@ -16,10 +16,10 @@ typedef struct str {
/*
USAGE:
str s = STR("something");
printf(FMT_str"\n", str_expand(s));
printf(FMT_str"\n", str_unwrap(s));
*/
#define FMT_str "%.*s"
#define str_expand(S) (S).len, (S).data
#define str_unwrap(S) (S).len, (S).data
/// creates str from a string literal
#define STR(LITERAL) str_construct(LITERAL, ARRAY_LEN(LITERAL) - 1, true)
@@ -59,7 +59,7 @@ static inline str Array_u8_castTo_str(Array(u8) a, bool isZeroTerminated) {
static const str str_null = str_construct(NULL, 0, 0);
/// copies src content to new string and adds \0 at the end
/// copy str data to new str and add \0 at the end
str str_copy(const str self);
/// compares two strings, NullPtr-friendly

View File

@@ -1,6 +1,6 @@
#pragma once
#if __cplusplus
#ifdef __cplusplus
extern "C" {
#endif
@@ -70,6 +70,6 @@ USAGE:
#define DT_expand(dt) dt.d.year, dt.d.month, dt.d.month_day, dt.t.hour, dt.t.min, dt.t.sec
#define DT_expand_subsec(dt) dt.d.year, dt.d.month, dt.d.month_day, dt.t.hour, dt.t.min, dt.t.sec, dt.t.nsec
#if __cplusplus
#ifdef __cplusplus
}
#endif

View File

@@ -43,7 +43,7 @@ case "$OS" in
EXEC_FILE="$PROJECT.exe"
SHARED_LIB_FILE="$PROJECT.dll"
INCLUDE="$INCLUDE "
LINKER_LIBS=""
LINKER_LIBS="-luuid"
;;
LINUX)
EXEC_FILE="$PROJECT"

View File

@@ -1,6 +1,9 @@
#include "internal.h"
bool dir_exists(cstr path){
if(path == NULL || path[0] == 0)
return true;
if(path[0]=='.'){
if(path[1]==0 || (path[1]==path_sep && path[1]==0))
return true; // dir . or ./ always exists
@@ -23,13 +26,12 @@ bool dir_exists(cstr path){
Result(bool) dir_create(cstr path){
Deferral(4);
if (dir_exists(path)){
if (path == NULL || path[0] == 0 || dir_exists(path)){
Return RESULT_VALUE(i, false);
}
char* parentDir= str_copy(path_dirname(str_from_cstr((void*)path))).data;
Defer(free(parentDir));
try_void(dir_create(parentDir));
try_void(dir_createParent(path));
#if TLIBC_FS_USE_WINDOWS_H
if(!CreateDirectory(path, NULL))
@@ -47,3 +49,18 @@ Result(bool) dir_create(cstr path){
Return RESULT_VALUE(i, true);
}
Result(bool) dir_createParent(cstr path){
Deferral(4);
str parent_dir_str = path_dirname(str_from_cstr((void*)path));
if(parent_dir_str.len == 0){
Return RESULT_VALUE(i, false);
}
char* parent_dir_cstr = str_copy(parent_dir_str).data;
Defer(free(parent_dir_cstr));
try(bool result, i, dir_create(parent_dir_cstr));
Return RESULT_VALUE(i, result);
}

View File

@@ -1,4 +1,10 @@
#include "tlibc/filesystem.h"
#include "internal.h"
#if TLIBC_FS_USE_WINDOWS_H
#include <knownfolders.h>
#include <shlobj.h>
#else
#endif
str path_dirname(str path){
if(path.len == 0)
@@ -43,3 +49,23 @@ str path_basename(str path, bool remove_ext){
}
return path;
}
Result(char*) path_getUserDir(){
#if TLIBC_FS_USE_WINDOWS_H
PWSTR wpath = NULL;
HRESULT reslut = SHGetKnownFolderPath(&FOLDERID_Profile, 0, NULL, &wpath);
if(!SUCCEEDED(reslut)){
return RESULT_ERROR_LITERAL("can't get user directory by SHGetKnownFolderPath()");
}
size_t char_len = wcslen(wpath) * 4 + 1;
char* path = (char*)malloc(char_len);
wcstombs(path, wpath, char_len);
return RESULT_VALUE(p, path);
#else
const char *home = getenv("HOME");
if(home == NULL){
return RESULT_ERROR_LITERAL("can't get user directory by getenv(\"HOME\")");
}
return RESULT_VALUE(p, cstr_copy(home));
#endif
}

View File

@@ -2,11 +2,10 @@
#include "tlibc/string/StringBuilder.h"
str str_copy(const str self){
if(self.data == NULL || self.len == 0)
return self;
str copy = str_construct((char*)malloc(self.len + 1), self.len, true);
memcpy(copy.data, self.data, self.len);
if(self.len != 0){
memcpy(copy.data, self.data, self.len);
}
copy.data[copy.len] = '\0';
return copy;
}