Compare commits

...

24 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
7dc80c2fdf insertion sort fix 2025-12-19 08:15:28 +05:00
bdbe959e23 DateTime_parse 2025-12-15 23:21:59 +05:00
94b051e852 defer simplified 2025-12-15 13:38:03 +05:00
08d45faa83 fixed some errors 2025-12-13 03:24:46 +05:00
af0931c727 changed Error_create msg argument type to str 2025-12-13 02:30:45 +05:00
0d422cd7e5 HashMapKeyValue.value -> value_ptr 2025-12-02 20:06:02 +05:00
17d2d1c38d added inline insertion sort and binary search 2025-12-02 19:24:46 +05:00
a57f05cfeb zeroStruct 2025-12-02 17:17:39 +05:00
ea6c20f430 str_expand fix 2025-11-27 01:44:49 +05:00
77e4f38416 updated README.md 2025-11-27 01:38:50 +05:00
f3e4f8f061 added str_expand 2025-11-27 01:23:41 +05:00
83091ac707 added file_readWholeText 2025-11-26 23:52:18 +05:00
Timerix
6a7f0a8715 added List<T>_expand 2025-11-26 20:56:09 +05:00
8a40caaf10 str_destroy and str_free 2025-11-26 17:03:54 +05:00
4c97787c27 moved term.h to tlibc 2025-11-25 18:22:14 +05:00
82bd234d08 rewritten Array and List, changed str.size -> .len 2025-11-25 16:31:04 +05:00
b557881168 LList fix 2025-11-24 23:48:38 +05:00
f2ce18b16d implemented doubly linked list 2025-11-24 23:41:58 +05:00
3a7f09bb49 fixed file_close bug when file is NULL 2025-11-21 21:21:02 +05:00
59 changed files with 1095 additions and 322 deletions

View File

@@ -3,21 +3,34 @@ C library with collections, strings, exceptions, io...
## Build
1. Clone the repository.
1. Clone this repository.
```
git clone https://timerix.ddns.net/git/Timerix/tlibc.git
```
2. Install [cbuild](https://timerix.ddns.net/git/Timerix/cbuild).
2. Install [cbuild](https://timerix.ddns.net/git/Timerix/cbuild/releases).
Select latest version compatible with the one in `project.config`.
Example: For `2.3.0` download latest `2.3.x`.
3. Build static library
```
cd tlibc
cbuild build_static_lib_dbg
```
6. If you use tlibc as static library, add `LINKER_LIBS` from tlibc `project.config` to your project.
## Usage
Include `tlibc/tlibc.h` and put this code at the beginning of `main()`.
```
Deferral(32);
```c
#include "tlibc/tlibc.h"
int main(){
Deferral(32); // reserve memory for 32 defers
try_fatal_void(tlibc_init());
Defer(tlibc_deinit());
Return 0; // call defers
}
```

View File

@@ -0,0 +1,32 @@
#pragma once
#include "std.h"
/// USAGE insertionSort(list.data, list.len, .id)
#define insertionSort_inline(arr, n, field) \
for(i32 i = 1, j; i < (i32)n; i++) { \
j = i; \
typeof(arr[i]) t = arr[i];\
while(j > 0 && arr[j - 1]field > t##field){\
arr[j] = arr[j - 1]; \
j--; \
} \
arr[j] = t; \
} \
#define binarySearch_inline(arr, n, key, field, out_index) {\
i32 low = 0; \
i32 high = n - 1; \
while (low <= high) { \
i32 mid = low + (high - low) / 2; \
if (arr[mid]field == key) { \
out_index = mid; \
break; \
} \
/* choose left or right half */ \
if (arr[mid]field < key) \
low = mid + 1; \
else high = mid - 1; \
} \
out_index = -1; \
}

View File

@@ -1,57 +1,62 @@
#pragma once
#include "../std.h"
#define Array(T) Array_
/*
Pointer and length.
Can be initialized with {0}.
*/
typedef struct Array_ {
void* data;
u32 size;
} Array_;
#define Array(T) Array_##T
/// creates Array_ from self const array
#define ARRAY(T, A...) Array_construct_size(((T[])A), sizeof((T[])A))
/// create Array(T) from array initializer list ({ a, b, c })
#define ARRAY(T, A...) ((Array(T)){ .data = ((T[])A), .len = sizeof((T[])A) / sizeof(T) })
#define Array_construct(DATA, T, COUNT) Array_construct_size(DATA, (COUNT) * sizeof(T))
#define Array_construct_size(DATA, LEN) ((Array_){ .data = (DATA), .size = (LEN) })
#define Array_declare(T) \
typedef struct Array(T) { \
T* data; \
u32 len; \
} Array(T); \
\
static inline Array(T) Array_##T##_construct(T* data, u32 len) { \
return (Array(T)){ .data = data, .len = len }; \
} \
\
static inline Array(T) Array_##T##_alloc(u32 len){ \
if(len == 0) \
return Array_##T##_construct(NULL, 0); \
return Array_##T##_construct(malloc(len * sizeof(T)), len); \
} \
\
static inline void Array_##T##_realloc(Array(T)* self, u32 new_len){ \
self->data = (T*)realloc(self->data, new_len * sizeof(T)); \
self->len = new_len; \
} \
\
static inline Array(T) Array_##T##_copy(const Array(T) src){ \
Array(T) copy = Array_##T##_alloc(src.len); \
if(copy.data != NULL) \
memcpy(copy.data, src.data, src.len * sizeof(T)); \
return copy; \
} \
\
static inline void Array_##T##_destroy(Array(T)* self){ \
if(!self) \
return; \
free(self->data); \
} \
\
static inline void Array_##T##_memset(Array(T)* self, u32 value){ \
memset(self->data, value, self->len * sizeof(T)); \
} \
\
/* @return self[0..i-1] */ \
static inline Array(T) Array_##T##_sliceTo(const Array(T) src, u32 i){ \
return Array_##T##_construct(src.data, i); \
} \
\
/* @return self[i...] */ \
static inline Array(T) Array_##T##_sliceFrom(const Array(T) src, u32 i){ \
return Array_##T##_construct(src.data + i, src.len - i); \
} \
#define Array_null Array_construct_size(NULL, 0)
#define Array_alloc(T, COUNT) Array_alloc_size((COUNT) * sizeof(T))
static inline Array_ Array_alloc_size(u32 size){
if(size == 0)
return Array_null;
return Array_construct_size(malloc(size), size);
}
#define Array_realloc(SELF, T, COUNT) Array_realloc_size(SELF, (COUNT) * sizeof(T))
static inline void Array_realloc_size(Array_* self, u32 new_size){
self->data = realloc(self->data, new_size);
self->size = new_size;
}
static inline Array_ Array_copy(const Array_ self){
Array_ copy = Array_alloc_size(self.size);
if(copy.data != NULL)
memcpy(copy.data, self.data, self.size);
return copy;
}
static inline void Array_free(Array_ self){
free(self.data);
}
#define Array_len(SELF, T) (SELF.size / sizeof(T))
#define Array_memset(SELF, VAL) memset(SELF.data, VAL, SELF.size)
#define struct_castTo_Array(STRUCT_PTR) Array_construct_size((STRUCT_PTR), sizeof(*STRUCT_PTR))
///@return self[0..i-1]
static inline Array(u8) Array_sliceTo(const Array(u8) self, u32 i){
return Array_construct_size(self.data, i);
}
///@return self[i...]
static inline Array(u8) Array_sliceFrom(const Array(u8) self, u32 i){
return Array_construct_size((u8*)self.data + i, self.size - i);
}
#define struct_castTo_Array_u8(STRUCT_PTR) Array_u8_construct((void*)(STRUCT_PTR), sizeof(*STRUCT_PTR))

View File

@@ -0,0 +1,2 @@
#pragma once
Array_declare(char);

View File

@@ -0,0 +1,2 @@
#pragma once
Array_declare(f32);

View File

@@ -0,0 +1,2 @@
#pragma once
Array_declare(f64);

View File

@@ -0,0 +1,2 @@
#pragma once
Array_declare(i16);

View File

@@ -0,0 +1,2 @@
#pragma once
Array_declare(i32);

View File

@@ -0,0 +1,2 @@
#pragma once
Array_declare(i64);

View File

@@ -0,0 +1,2 @@
#pragma once
Array_declare(i8);

View File

@@ -0,0 +1,2 @@
#pragma once
Array_declare(u16);

View File

@@ -0,0 +1,2 @@
#pragma once
Array_declare(u32);

View File

@@ -0,0 +1,2 @@
#pragma once
Array_declare(u64);

View File

@@ -0,0 +1,2 @@
#pragma once
Array_declare(u8);

View File

@@ -1,33 +1,32 @@
#pragma once
#include "../std.h"
#include "../string/str.h"
#include "Array.h"
#include "List.h"
typedef void (*FreeFunction)(void*);
typedef struct HashMapKeyHash {
str key;
u32 hash;
} HashMapKeyHash;
List_declare(HashMapKeyHash);
typedef struct HashMapBucket {
List(HashMapKeyHash) key_hash_list;
List(T) value_list;
List_ value_list;
} HashMapBucket;
#define HashMap(T) HashMap_
typedef struct HashMap_ {
NULLABLE(HashMapBucket*) table;
NULLABLE(FreeFunction) value_destructor;
NULLABLE(Destructor_t) value_destructor;
u32 value_t_size;
u32 height;
u16 height_n;
} HashMap_;
#define HashMap_construct(SELF, T, VALUE_DESTRUCTOR) HashMap_construct_size(SELF, sizeof(T), VALUE_DESTRUCTOR)
void HashMap_construct_size(HashMap_* self, u32 value_t_size, FreeFunction NULLABLE(value_destructor));
void HashMap_construct_size(HashMap_* self, u32 value_t_size, Destructor_t NULLABLE(value_destructor));
void HashMap_destroy(HashMap_* self);
bool HashMap_tryPush(HashMap_* self, const str key, void* value_ptr);
@@ -36,7 +35,7 @@ NULLABLE(void*) HashMap_tryGetPtr(const HashMap_* self, const str key);
bool HashMap_tryDelete(HashMap_* self, const str key);
typedef struct {
typedef struct HashMapIter {
const HashMap_* map;
i32 bucket_n;
i32 elem_n;
@@ -44,7 +43,7 @@ typedef struct {
typedef struct HashMapKeyValue {
str key;
void* value;
void* value_ptr;
} HashMapKeyValue;
static inline HashMapIter HashMapIter_create(const HashMap_* table){

View File

@@ -0,0 +1,92 @@
#pragma once
#include "../std.h"
/*
Doubly linked list.
Can be initialized with {0}.
*/
#define LLNode(T) LLNode_##T
#define LList(T) LList_##T
#define LList_declare(T) \
typedef struct LLNode(T) LLNode(T);\
typedef struct LLNode(T) { \
LLNode(T)* prev; \
LLNode(T)* next; \
T value; \
} LLNode(T); \
\
static inline LLNode(T)* LLNode_##T##_createZero(){ \
LLNode(T)* n = (LLNode(T)*)malloc(sizeof(LLNode(T))); \
n->prev = NULL; \
n->next = NULL; \
zeroStruct(&n->value); \
return n; \
} \
\
typedef struct LList(T) { \
LLNode(T)* first; \
LLNode(T)* last; \
u32 count; \
NULLABLE(void (*value_destructor)(T*)); \
} LList(T); \
\
/* Peek node from list. Detatched nodes can be inserted in different place. */ \
static inline void LList_##T##_detatch(LList(T)* l, LLNode(T)* n) \
{ _LList_detatch((void*)l, (void*)n); } \
\
/* @param detatched must have null .next and .prev */ \
/* @param target can be null only if it is l->first or l->last */ \
static inline void LList_##T##_insertAfter( \
LList(T)* l, LLNode(T)* target, LLNode(T)* detatched) \
{ _LList_insertAfter((void*)l, (void*)target, (void*)detatched); } \
\
/* @param detatched must have null .next and .prev */ \
/* @param target can be null only if it is l->first or l->last */ \
static inline void LList_##T##_insertBefore( \
LList(T)* l, LLNode(T)* target, LLNode(T)* detatched) \
{ _LList_insertBefore((void*)l, (void*)target, (void*)detatched); } \
\
static inline void LList_##T##_destroy(LList(T)* l){ \
if(!l) \
return; \
LLNode(T)* n = l->first; \
while(n){ \
if(l->value_destructor) \
l->value_destructor(&n->value); \
LLNode(T)* next = n->next; \
free(n); \
n = next; \
} \
} \
#define LList_construct(T, VALUE_DESTRUCTOR) ((LList(T)){ \
.first = NULL, .last = NULL, .count = 0, .value_destructor = VALUE_DESTRUCTOR })
typedef struct LLNode_ LLNode_;
typedef struct LLNode_ {
LLNode_* prev;
LLNode_* next;
/* value of unknown type */
} LLNode_;
typedef struct LList_ {
LLNode_* first;
LLNode_* last;
u32 count;
NULLABLE(Destructor_t) value_destructor;
} LList_;
/* Peek node from list. Detatched nodes can be inserted in different place. */
void _LList_detatch(LList_* l, LLNode_* n);
/* @param detatched must have null .next and .prev */
/* @param target can be null only if it is l->first or l->last */
void _LList_insertAfter(LList_* l, NULLABLE(LLNode_*) target, LLNode_* detatched);
/* @param detatched must have null .next and .prev */
/* @param target can be null only if it is l->first or l->last */
void _LList_insertBefore(LList_* l, NULLABLE(LLNode_*) target, LLNode_* detatched);

View File

@@ -0,0 +1,2 @@
#pragma once
LList_declare(char);

View File

@@ -0,0 +1,2 @@
#pragma once
LList_declare(f32);

View File

@@ -0,0 +1,2 @@
#pragma once
LList_declare(f64);

View File

@@ -0,0 +1,2 @@
#pragma once
LList_declare(i16);

View File

@@ -0,0 +1,2 @@
#pragma once
LList_declare(i32);

View File

@@ -0,0 +1,2 @@
#pragma once
LList_declare(i64);

View File

@@ -0,0 +1,2 @@
#pragma once
LList_declare(i8);

View File

@@ -0,0 +1,2 @@
#pragma once
LList_declare(u16);

View File

@@ -0,0 +1,2 @@
#pragma once
LList_declare(u32);

View File

@@ -0,0 +1,2 @@
#pragma once
LList_declare(u64);

View File

@@ -0,0 +1,2 @@
#pragma once
LList_declare(u8);

View File

@@ -1,42 +1,103 @@
#pragma once
#include "../std.h"
#define List(T) List_
/*
Dynamic array.
CAN NOT be initialized with {0}.
*/
#define List(T) List_##T
#define List_declare(T) \
typedef struct List(T) { \
T* data; \
u32 len; \
u32 capacity; \
u32 elem_t_size; \
} List(T); \
\
static inline List(T) List_##T##_construct(T* data_ptr, u32 occupied_len, u32 capacity) { \
return (List(T)){ \
.data = data_ptr, \
.len = occupied_len, \
.capacity = capacity, \
.elem_t_size = sizeof(T) \
}; \
} \
\
static inline List(T) List_##T##_alloc(u32 initial_capacity) { \
List_ l = _List_alloc(initial_capacity, sizeof(T)); \
void* l_ptr = &l; \
return *(List(T)*)l_ptr; \
} \
\
static inline List(T) List_##T##_copy(const List(T)* src) { \
List_ l = _List_copy((void*)src); \
void* l_ptr = &l; \
return *(List(T)*)l_ptr; \
} \
\
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); \
} \
\
/* call increaseCapacity and return pointer to next empty cell */ \
static inline T* List_##T##_expand(List(T)* self, u32 len_to_add){ \
return (T*)_List_expand((void*)self, len_to_add); \
} \
\
static inline void List_##T##_push(List(T)* self, T value) { \
T* empty_cell = List_##T##_expand(self, 1); \
*empty_cell = value; \
} \
\
static inline void List_##T##_pushMany(List(T)* self, T* values_ptr, u32 len) { \
_List_push((void*)self, values_ptr, len); \
} \
\
static inline bool List_##T##_tryRemoveAt(List(T)* self, u32 i, u32 remove_len) ATTRIBUTE_WARN_UNUSED_RESULT; \
static inline bool List_##T##_tryRemoveAt(List(T)* self, u32 i, u32 remove_len) { \
return _List_tryRemoveAt((void*)self, i, remove_len); \
} \
typedef struct List_ {
void* data;
u32 size;
u32 allocated_size;
u32 len;
u32 capacity;
u32 elem_t_size;
} List_;
#define List_construct(T, DATA_PTR, OCCUPIED_COUNT, ALLOCATED_COUNT) \
List_construct_size(DATA_PTR, (OCCUPIED_COUNT) * sizeof(T), (ALLOCATED_COUNT) * sizeof(T))
static inline List_ List_construct_size(void* data_ptr, u32 occupied_size, u32 allocated_size) {
return (List_){ .data = data_ptr, .size = occupied_size, .allocated_size = allocated_size };
}
#define List_null List_construct_size(NULL, 0, 0)
#define List_alloc(T, INITIAL_COUNT) List_alloc_size((INITIAL_COUNT) * sizeof(T))
List_ List_alloc_size(u32 initial_size);
static inline void List_destroy(List_ self){
free(self.data);
static inline List_ _List_construct(void* data_ptr, u32 len, u32 capacity, u32 elem_t_size) {
return (List_){
.data = data_ptr,
.len = len,
.capacity = capacity,
.elem_t_size = elem_t_size
};
}
List_ List_copy(List_ src);
static inline void _List_destroy(List_* self){
if(!self)
return;
free(self->data);
}
// alloc bigger buffer if size + size_to_add won't fit in current
void List_increaseCapacity_size(List_* self, u32 size_to_add);
void* List_expand_size(List_* self, u32 size_to_add);
#define List_push(SELF, T, VALUE) *(T*)(List_expand_size(SELF, sizeof(T))) = VALUE
#define List_pushMany(SELF, T, VALUES_PTR, COUNT) List_push_size(SELF, VALUES_PTR, (COUNT) * sizeof(T))
void List_push_size(List_* self, void* values, u32 size);
#define List_removeAt(SELF, T, I, COUNT) List_removeAt_size(SELF, (I)*sizeof(T), (COUNT) * sizeof(T))
bool List_removeAt_size(List_* self, u32 i, u32 remove_size);
#define List_len(SELF, T) (SELF.size / sizeof(T))
#define List_index(SELF, T, I) (((T*)SELF.data)[I])
#define List_castTo_Array(SELF) Array_construct_size(SELF.data, SELF.size)
List_ _List_alloc(u32 initial_capacity, u32 elem_t_size);
List_ _List_copy(const List_* src);
/* alloc bigger buffer if size + len_to_add won't fit in current */
void _List_increaseCapacity(List_* self, u32 len_to_add);
void* _List_expand(List_* self, u32 len_to_add);
void _List_push(List_* self, void* values, u32 len);
bool _List_tryRemoveAt(List_* self, u32 i, u32 remove_len) ATTRIBUTE_WARN_UNUSED_RESULT;

View File

@@ -0,0 +1,2 @@
#pragma once
List_declare(char);

View File

@@ -0,0 +1,2 @@
#pragma once
List_declare(f32);

View File

@@ -0,0 +1,2 @@
#pragma once
List_declare(f64);

View File

@@ -0,0 +1,2 @@
#pragma once
List_declare(i16);

View File

@@ -0,0 +1,2 @@
#pragma once
List_declare(i32);

View File

@@ -0,0 +1,2 @@
#pragma once
List_declare(i64);

View File

@@ -0,0 +1,2 @@
#pragma once
List_declare(i8);

View File

@@ -0,0 +1,2 @@
#pragma once
List_declare(u16);

View File

@@ -0,0 +1,2 @@
#pragma once
List_declare(u32);

View File

@@ -0,0 +1,2 @@
#pragma once
List_declare(u64);

View File

@@ -0,0 +1,2 @@
#pragma once
List_declare(u8);

View File

@@ -17,18 +17,21 @@ void foo(){
#if defined(__GNUC__) || defined(__TINYC__)
#define Deferral(MAX_DEFER_STATEMENTS) \
unsigned char _num_deferrals = 0; \
void *_defer_return_loc = 0, *_deferrals[MAX_DEFER_STATEMENTS] = {0};
int _num_deferrals = 0; \
ATTRIBUTE_UNUSED void *_defer_return_loc = 0;\
ATTRIBUTE_UNUSED void *_deferrals[MAX_DEFER_STATEMENTS] = {0};
# define Defer(block) _Defer(block, __LINE__)
# define Return _Return(__LINE__)
#define _defer_token_cat(a, b) a ## b
#ifndef CAT2
#define CAT2(a, b) a ## b
#endif
#define _Defer(block, n) do { \
_deferrals[_num_deferrals++] = && _defer_token_cat(_defer_ini, n); \
_deferrals[_num_deferrals++] = && CAT2(_defer_ini, n); \
if (0) { \
_defer_token_cat(_defer_ini, n): \
CAT2(_defer_ini, n): \
block; \
if (_num_deferrals) { \
goto *_deferrals[--_num_deferrals]; \
@@ -39,13 +42,11 @@ void foo(){
} while (0)
#define _Return(n) \
if (_num_deferrals \
/* this nonsense disables warning Wunused-but-set-variable */ \
&& _defer_return_loc == _defer_return_loc ) \
if (_num_deferrals) \
{ \
_defer_return_loc = && _defer_token_cat(_defer_fini_, n); \
_defer_return_loc = && CAT2(_defer_fini_, n); \
goto *_deferrals[--_num_deferrals]; \
} else _defer_token_cat(_defer_fini_, n): \
} else CAT2(_defer_fini_, n): \
return
#else /* !__GNUC__ && !__TINYCC__ */
@@ -59,7 +60,7 @@ void foo(){
#endif
#define Deferral(MAX_DEFER_STATEMENTS) \
volatile unsigned char _num_deferrals = 0; \
volatile int _num_deferrals = 0; \
jmp_buf _defer_return_loc = {0}, _deferrals[MAX_DEFER_STATEMENTS] = {0};
#define Defer(block) do { \

View File

@@ -9,12 +9,15 @@ typedef struct ErrorCallPos {
cstr file;
cstr func;
} ErrorCallPos;
#define ErrorCallPos_here() (ErrorCallPos){\
.line = __LINE__,\
.file = __FILE__,\
.func = __func__\
}
List_declare(ErrorCallPos);
typedef struct Error {
str msg;
bool is_msg_on_heap;
@@ -23,13 +26,23 @@ typedef struct Error {
List(ErrorCallPos) call_stack;
} Error;
Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos p,
Error* Error_create(str 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 in header, declare somewhere in source.
Create a function like
```
void init_your_lib() {
ErrorCodePage_register(YOUR_PAGE);
}
```
and call it in main().
*/
#define ErrorCodePage_name(name) ErrorCodePage_##name
#define ErrorCodePage_declare(name) extern u16 ErrorCodePage_name(name);
#define ErrorCodePage_define(name) u16 ErrorCodePage_name(name) = 0;
@@ -43,6 +56,7 @@ typedef enum TlibcError {
ErrorCodePage_declare(TLIBC);
ErrorCodePage_declare(LIBC_ERRNO);
typedef struct Result_ {
Error* error;
union {
@@ -59,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_){ \
@@ -69,62 +86,82 @@ typedef struct Result_ {
.u = 0 \
}
#define RESULT_ERROR_CODE_FMT(CODE_PAGE, CODE, FORMAT, ARGS...) \
RESULT_ERROR_CODE(CODE_PAGE, CODE, sprintf_malloc(FORMAT ,##ARGS), true)
RESULT_ERROR_CODE(CODE_PAGE, CODE, str_from_cstr(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_LITERAL(MSG) \
RESULT_ERROR(STR((MSG)), false)
#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_malloc(errno), true)
RESULT_ERROR_CODE(LIBC_ERRNO, errno, str_from_cstr(strerror_malloc(errno)), true)
#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_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(VAR, RESULT_FIELD, RSLT_CALL, N) \
#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());\
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)
#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;
#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) do {\
int r = CALL;\
if(r != 0){\
Return RESULT_ERROR_CODE(LIBC_ERRNO, r, strerror_malloc(r), true);\
HANDLER(_rname(N));\
}\
} while(0)
VAR = _rname(N).RESULT_FIELD;
#define try_assert(EXPR) if(!(EXPR)) { Return RESULT_ERROR(("try_assert(" #EXPR ")"), false); }
#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) \
_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) \
_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_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);\
}\
#define _try_fatal_stderrcode(CALL, N) \
int _rname(N) = CALL;\
if(_rname(N) != 0){\
Error_printAndExit(Error_create(\
str_from_cstr(strerror_malloc(_rname(N))), true, \
ErrorCallPos_here(), \
ErrorCodePage_name(LIBC_ERRNO), _rname(N)\
));\
}\
#define try_assert(EXPR) if(!(EXPR)) { Return RESULT_ERROR_LITERAL("assertion must be true: " #EXPR); }

View File

@@ -4,6 +4,7 @@
#include "errors.h"
#include "string/str.h"
#include "collections/Array.h"
#include "collections/Array_impl/Array_u8.h"
#if !defined(TLIBC_FS_USE_WINDOWS_H)
#if defined(_WIN64) || defined(_WIN32)
@@ -38,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 //
@@ -59,6 +62,8 @@ str path_basename(str path, bool remove_ext);
Result(FILE*) file_open(cstr file_name, cstr fopen_mode);
static inline void file_close(FILE* f){
if(f == NULL)
return;
fclose(f);
}
@@ -91,7 +96,7 @@ static inline Result(void) file_writeBytes(FILE* f, const void* src, u64 size){
}
static inline Result(void) file_writeBytesArray(FILE* f, Array(u8) src){
return file_writeStructs(f, src.data, src.size, 1);
return file_writeStructs(f, src.data, src.len, 1);
}
@@ -110,10 +115,10 @@ static inline Result(u64) file_readBytes(FILE* f, void* dst, u64 max_count){
return file_readStructs(f, dst, 1, max_count);
}
/// @param dst array where .size is the maximum number of bytes to read
/// @param dst array where .len is the maximum number of bytes to read
/// @return number of bytes that were read (<=max_count)
static inline Result(u64) file_readBytesArray(FILE* f, Array(u8) dst){
return file_readStructs(f, dst.data, 1, dst.size);
return file_readStructs(f, dst.data, 1, dst.len);
}
@@ -125,21 +130,32 @@ static inline Result(void) file_readBytesExactly(FILE* f, void* dst, u64 exact_c
return file_readStructsExactly(f, dst, 1, exact_count);
}
/// @param dst array where .size is the exact number of bytes to read
/// @param dst array where .len is the exact number of bytes to read
static inline Result(void) file_readBytesArrayExactly(FILE* f, Array(u8) dst){
return file_readStructsExactly(f, dst.data, 1, dst.size);
return file_readStructsExactly(f, dst.data, 1, dst.len);
}
/// @brief allocates array of size equal `file_getSize()` and reads whole file
/// @brief allocates buffer of size `file_getSize(f)` and reads whole file
/// @param out_buf output array allocated on heap
Result(void) file_readWhole(FILE* f, Array(u8)* out_buf);
/// @brief allocates buffer of size `file_getSize(f) + 1` and reads whole file
/// @param out_str output str allocated on heap, null-terminated
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
@@ -31,6 +31,7 @@ typedef u8 bool;
#define false 0
#endif
typedef void (*Destructor_t)(void* self);
#define FMT_i8 "%"PRIi8
#define FMT_i16 "%"PRIi16
@@ -58,6 +59,8 @@ typedef u8 bool;
#define dbg(N) printf("\e[95m%d\n",N)
#define nameof(V) #V
#define CAT2(a, b) a ## b
#define CAT3(a, b, c) a ## b ## c
#define ARRAY_LEN(A) (sizeof(A)/sizeof(A[0]))
#define ALIGN_TO(_SIZE,_ALIGN) (((_SIZE) + ((_ALIGN) - 1)) & ~((_ALIGN) - 1))
@@ -75,7 +78,7 @@ typedef u8 bool;
a48,a49,a50,a51,a52,a53,a54,a55, a56,a57,a58,a59,a60,a61,a62,a63, \
a64,...) a64
// Macro for counting variadic arguments (max 64)
// (see usage in kprint.h)
// (see usage in cptr.h)
#define count_args(ARGS...) __count_args(ARGS, \
64,63,62,61,60,59,58,57, 56,55,54,53,52,51,50,49, \
48,47,46,45,44,43,42,41, 40,39,38,37,36,35,34,33, \
@@ -84,6 +87,8 @@ typedef u8 bool;
#define printfe(FORMAT, ...) fprintf(stderr, FORMAT ,##__VA_ARGS__)
#define zeroStruct(STRUCT_PTR) memset((STRUCT_PTR), 0, sizeof(*STRUCT_PTR));
/// @warning pointer can be null
#define NULLABLE(NAME) NAME
@@ -119,6 +124,24 @@ typedef u8 bool;
#define ATTRIBUTE_THREAD_LOCAL __thread
#endif
#if __cplusplus
#ifdef _MSC_VER
#define PRAGMA_WARNING_PUSH __pragma(warning( push ))
#define PRAGMA_WARNING_POP __pragma(warning( pop ))
#define PRAGMA_WARNING_DISABLE(wNumber) __pragma(warning( disable : wNumber ))
#define W_RETURN_TYPE
#else
#define _PRAGMA(P) _Pragma(#P)
#define PRAGMA_WARNING_PUSH _PRAGMA(GCC diagnostic push)
#define PRAGMA_WARNING_POP _PRAGMA(GCC diagnostic pop)
#define PRAGMA_WARNING_DISABLE(wName) _PRAGMA(GCC diagnostic ignored wName)
#define W_RETURN_TYPE "-Wreturn-type"
#endif
#define WARNING_DISABLE(WARNING, CODE...) \
PRAGMA_WARNING_PUSH \
PRAGMA_WARNING_DISABLE(WARNING) \
CODE; \
PRAGMA_WARNING_POP
#ifdef __cplusplus
}
#endif

View File

@@ -1,7 +1,7 @@
#pragma once
#include "../collections/List.h"
#include "../collections/Array.h"
#include "../collections/List_impl/List_char.h"
#include "str.h"
typedef struct StringBuilder {
@@ -9,17 +9,17 @@ typedef struct StringBuilder {
} StringBuilder;
static inline StringBuilder StringBuilder_alloc(u32 initial_size) {
return (StringBuilder){ .buffer = List_alloc_size(initial_size) };
return (StringBuilder){ .buffer = List_char_alloc(initial_size) };
}
void StringBuilder_destroy(StringBuilder* b);
static inline StringBuilder StringBuilder_copy(const StringBuilder* b){
return (StringBuilder) { .buffer = List_copy(b->buffer) };
return (StringBuilder) { .buffer = List_char_copy(&b->buffer) };
}
// alloc bigger buffer if size + size_to_add won't fit in current
static inline void StringBuilder_increaseCapacity(StringBuilder* b, u32 size_to_add){
List_increaseCapacity_size(&b->buffer, size_to_add);
// alloc bigger buffer if size + len_to_add won't fit in current
static inline void StringBuilder_increaseCapacity(StringBuilder* b, u32 len_to_add){
List_char_increaseCapacity(&b->buffer, len_to_add);
}

View File

@@ -4,39 +4,62 @@
#include "char.h"
#include "cstr.h"
#include "../collections/Array.h"
#include "../collections/Array_impl/Array_char.h"
#include "../collections/Array_impl/Array_u8.h"
typedef struct str {
char* data;
u32 size; // size of data in bytes without \0
u32 len; // size of data in bytes without \0
bool isZeroTerminated;
} str;
/*
USAGE:
str s = STR("something");
printf(FMT_str"\n", str_unwrap(s));
*/
#define FMT_str "%.*s"
#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)
#define str_construct(DATA, LEN, ZERO_TERMINATED) ((str){ .data = DATA, .size = LEN, .isZeroTerminated = ZERO_TERMINATED })
#define str_construct(DATA, LEN, ZERO_TERMINATED) ((str){ .data = DATA, .len = LEN, .isZeroTerminated = ZERO_TERMINATED })
static inline str str_from_cstr(cstr s_ptr){
return str_construct((void*)s_ptr, strlen(s_ptr), true);
}
static inline void str_free(str s){
free(s.data);
/// destroy str with .data allocated on heap
static inline void str_destroy(str self){
free(self.data);
}
static inline Array_ str_castTo_Array(str s) {
return Array_construct_size(s.data, s.size);
/// destroy str allocated on heap with .data allocated on heap
static inline void str_free(str* self){
if(!self)
return;
free(self->data);
free(self);
}
static inline str Array_castTo_str(Array_ a, bool isZeroTerminated) {
return str_construct(a.data, a.size, isZeroTerminated);
static inline Array(char) str_castTo_Array_char(str s) {
return Array_char_construct(s.data, s.len);
}
static inline Array(u8) str_castTo_Array_u8(str s) {
return Array_u8_construct((void*)s.data, s.len);
}
static inline str Array_char_castTo_str(Array(char) a, bool isZeroTerminated) {
return str_construct(a.data, a.len, isZeroTerminated);
}
static inline str Array_u8_castTo_str(Array(u8) a, bool isZeroTerminated) {
return str_construct((void*)a.data, a.len, 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
@@ -74,5 +97,5 @@ static inline str str_sliceBefore(str s, u32 n){
///@return s[n...]
static inline str str_sliceAfter(str s, u32 n){
return str_construct(s.data + n, s.size - n, s.isZeroTerminated);
return str_construct(s.data + n, s.len - n, s.isZeroTerminated);
}

47
include/tlibc/term.h Normal file
View File

@@ -0,0 +1,47 @@
#pragma once
#include "tlibc/errors.h"
typedef struct TerminalSize {
i16 cols;
i16 rows;
} TerminalSize;
typedef enum Color16 {
Color16_Black = 30,
Color16_DarkRed = 31,
Color16_DarkGreen = 32,
Color16_DarkYellow = 33,
Color16_DarkBlue = 34,
Color16_DarkMagenta = 35,
Color16_DarkCyan = 36,
Color16_Gray = 37,
Color16_DarkGray = 90,
Color16_Red = 91,
Color16_Green = 92,
Color16_Yellow = 93,
Color16_Blue = 94,
Color16_Magenta = 95,
Color16_Cyan = 96,
Color16_White = 97
} Color16;
Result(void) term_init();
Result(void) term_getSize(TerminalSize* out);
Result(void) term_readLine(char* buf, u32 bufsize);
Result(void) term_readLineHidden(char *buf, u32 bufsize);
void term_setFgColor16(Color16 c);
void term_setBgColor16(Color16 c);
void term_bold();
void term_italic();
void term_underline();
void term_strikethrough();
void term_resetColors();
void term_clear();
void term_eraseRow();
void term_resetCursor();
void term_cursorMove(u16 row, u16 column);
void term_cursorHide();
void term_cursorShow();

View File

@@ -1,10 +1,10 @@
#pragma once
#if __cplusplus
#ifdef __cplusplus
extern "C" {
#endif
#include "std.h"
#include "errors.h"
/// nanoseconds
typedef u64 nsec_t;
@@ -52,10 +52,15 @@ void DateTime_get(DateTime* dt, bool utc_time);
static inline void DateTime_getLocal(DateTime* dt) { DateTime_get(dt, false); }
static inline void DateTime_getUTC(DateTime* dt) { DateTime_get(dt, true); }
// yyyy.MM.dd_HH-mm-ss
/// yyyy.MM.dd HH:mm:ss_float
Result(void) DateTime_parse(cstr src, DateTime* dt);
/// yyyy.MM.dd_HH-mm-ss
#define FMT_DateTime_fileName "%04i.%02i.%02i_%02i-%02i-%02i"
// yyyy.MM.dd HH:mm:ss
#define FMT_DateTime_text "%04i.%02i.%02i %02i:%02i:%02i"
/// yyyy.MM.dd HH:mm:ss
/// yyyy.MM.dd HH:mm:ss_float
#define FMT_DateTime_text "%04i.%02i.%02i-%02i:%02i:%02i"
#define FMT_DateTime_text_subsec FMT_DateTime_text".%09i"
/*
USAGE:
DateTime dt;
@@ -63,8 +68,8 @@ USAGE:
printf(FMT_DateTime_text, DT_expand(dt));
*/
#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,4 +1,6 @@
#include "tlibc/collections/HashMap.h"
#include "tlibc/collections/Array.h"
#include "tlibc/collections/Array_impl/Array_u32.h"
#include <assert.h>
//TODO: sort bucket keys for binary search
@@ -9,11 +11,16 @@
static const Array(u32) __HashMap_heights = ARRAY(u32, {
0, 17, 31, 61, 127, 257, 521, 1021, 2053, 4099, 8191, 16381,
32771, 65521, 131071, 262147, 524287, 1048583, 2097169, 4194319,
8388617, 16777213, 33554467, 67108859, 134217757, 268435493
8388617, 16777213, 33554467, 67108859, 134217757, 268435493,
536871001, 1073742037, 2147484061
});
void HashMapBucket_construct(HashMapBucket* self, u32 value_t_size){
self->key_hash_list = List_HashMapKeyHash_construct(NULL, 0, 0);
self->value_list = _List_construct(NULL, 0, 0, value_t_size);
}
void HashMap_construct_size(HashMap_* self, u32 value_t_size, FreeFunction NULLABLE(value_destructor)){
void HashMap_construct_size(HashMap_* self, u32 value_t_size, Destructor_t NULLABLE(value_destructor)){
self->value_t_size = value_t_size;
self->value_destructor = value_destructor;
self->height_n = 0;
@@ -27,9 +34,9 @@ void HashMap_destroy(HashMap_* self){
for(u32 i = 0; i < self->height; i++){
HashMapBucket* bu = &self->table[i];
u32 len = List_len(bu->key_hash_list, HashMapKeyHash);
// free key strings
u32 len = bu->key_hash_list.len;
for(u32 j = 0; j < len; j++){
HashMapKeyHash* kh = (HashMapKeyHash*)bu->key_hash_list.data + j;
free(kh->key.data);
@@ -38,7 +45,7 @@ void HashMap_destroy(HashMap_* self){
// destroy values
if(self->value_destructor){
u8* value_ptr = (u8*)bu->value_list.data;
u8* end = value_ptr + bu->value_list.size;
u8* end = value_ptr + bu->value_list.len * bu->value_list.elem_t_size;
while(value_ptr < end){
self->value_destructor(value_ptr);
value_ptr += self->value_t_size;
@@ -68,7 +75,8 @@ static BucketAndIndex __HashMap_search(const HashMap_* self, const str key, u32
BucketAndIndex r;
r.bu = &self->table[hash % self->height];
for(r.i = 0; r.i < (i32)List_len(r.bu->key_hash_list, HashMapKeyHash); r.i++){
i32 len = r.bu->key_hash_list.len;
for(r.i = 0; r.i < len; r.i++){
HashMapKeyHash* kh = (HashMapKeyHash*)r.bu->key_hash_list.data + r.i;
if(kh->hash == hash && str_equals(kh->key, key)){
return r;
@@ -91,29 +99,33 @@ void* HashMap_tryGetPtr(const HashMap_* self, const str key){
static void __HashMap_expand(HashMap_* self){
u32 height_expanded_n = self->height_n + 1;
assert(height_expanded_n < Array_len(__HashMap_heights, u32) && "HashMap IS FULL! Fix your code.");
assert(height_expanded_n < __HashMap_heights.len && "HashMap IS FULL! Fix your code.");
// alloc new HashMapBucket array
u32 height_expanded = ((u32*)__HashMap_heights.data)[height_expanded_n];
u32 height_expanded = __HashMap_heights.data[height_expanded_n];
u32 table_expanded_size = height_expanded * sizeof(HashMapBucket);
HashMapBucket* table_expanded = (HashMapBucket*)malloc(table_expanded_size);
memset(table_expanded, 0, table_expanded_size);
// init buckets
for(u32 i = 0; i < height_expanded; i++){
HashMapBucket_construct(table_expanded + i, self->value_t_size);
}
// copy values from old buckets to new
for(u32 i = 0; i < self->height; i++){
HashMapBucket* old_bucket = &self->table[i];
u32 len = List_len(old_bucket->key_hash_list, HashMapKeyHash);
u32 len = old_bucket->key_hash_list.len;
for(u32 j = 0; j < len; j++){
HashMapKeyHash kh = ((HashMapKeyHash*)old_bucket->key_hash_list.data)[j];
HashMapBucket* new_bucket = &table_expanded[kh.hash % height_expanded];
List_push(&new_bucket->key_hash_list, HashMapKeyHash, kh);
HashMapKeyHash* kh = old_bucket->key_hash_list.data + j;
HashMapBucket* new_bucket = &table_expanded[kh->hash % height_expanded];
List_HashMapKeyHash_pushMany(&new_bucket->key_hash_list, kh, 1);
void* old_value_ptr = (u8*)old_bucket->value_list.data + j * self->value_t_size;
List_push_size(&new_bucket->value_list, old_value_ptr, self->value_t_size);
_List_push(&new_bucket->value_list, old_value_ptr, 1);
}
free(old_bucket->key_hash_list.data);
free(old_bucket->value_list.data);
List_HashMapKeyHash_destroy(&old_bucket->key_hash_list);
_List_destroy(&old_bucket->value_list);
}
free(self->table);
self->table = table_expanded;
@@ -129,14 +141,14 @@ bool HashMap_tryPush(HashMap_* self, const str key, void* value_ptr){
return false;
HashMapBucket* bu = r.bu;
if(bu == NULL || List_len(bu->key_hash_list, HashMapKeyHash) >= __HashMapBucket_MAX_LEN){
if(bu == NULL || r.bu->key_hash_list.len >= __HashMapBucket_MAX_LEN){
__HashMap_expand(self);
bu = &self->table[hash % self->height];
}
HashMapKeyHash kh = { .key = str_copy(key), .hash = hash };
List_push(&bu->key_hash_list, HashMapKeyHash, kh);
List_push_size(&bu->value_list, value_ptr, self->value_t_size);
List_HashMapKeyHash_push(&bu->key_hash_list, kh);
_List_push(&bu->value_list, value_ptr, 1);
return true;
}
@@ -151,14 +163,14 @@ void HashMap_pushOrUpdate(HashMap_* self, const str key, void* value_ptr){
}
HashMapBucket* bu = r.bu;
if(bu == NULL || List_len(bu->key_hash_list, HashMapKeyHash) >= __HashMapBucket_MAX_LEN){
if(bu == NULL || r.bu->key_hash_list.len >= __HashMapBucket_MAX_LEN){
__HashMap_expand(self);
bu = &self->table[hash % self->height];
}
HashMapKeyHash kh = { .key = str_copy(key), .hash = hash };
List_push(&bu->key_hash_list, HashMapKeyHash, kh);
List_push_size(&bu->value_list, value_ptr, self->value_t_size);
List_HashMapKeyHash_push(&bu->key_hash_list, kh);
_List_push(&bu->value_list, value_ptr, 1);
}
bool HashMap_tryDelete(HashMap_* self, const str key){
@@ -168,8 +180,11 @@ bool HashMap_tryDelete(HashMap_* self, const str key){
if(r.i == -1)
return false;
List_removeAt(&r.bu->key_hash_list, HashMapKeyHash, r.i, 1);
List_removeAt_size(&r.bu->value_list, r.i, self->value_t_size);
if(!List_HashMapKeyHash_tryRemoveAt(&r.bu->key_hash_list, r.i, 1))
return false;
if(!_List_tryRemoveAt(&r.bu->value_list, r.i, 1))
return false;
return true;
}
@@ -182,7 +197,7 @@ bool HashMapIter_moveNext(HashMapIter* self){
}
// move to next element in current bucket
const HashMapBucket* bu = &self->map->table[self->bucket_n];
i32 bu_elem_len = List_len(bu->key_hash_list, HashMapKeyHash);
i32 bu_elem_len = bu->key_hash_list.len;
if(self->elem_n + 1 < bu_elem_len){
self->elem_n++;
return true;
@@ -193,7 +208,7 @@ bool HashMapIter_moveNext(HashMapIter* self){
self->bucket_n++;
self->elem_n = 0;
bu = &self->map->table[self->bucket_n];
if(bu->key_hash_list.size != 0)
if(bu->key_hash_list.len != 0)
return true;
}
}
@@ -206,10 +221,10 @@ bool HashMapIter_getCurrent(HashMapIter* self, HashMapKeyValue* kv){
const HashMapBucket* bu = &self->map->table[self->bucket_n];
// table is empty
if(bu->key_hash_list.size == 0)
if(bu->key_hash_list.len == 0)
return false;
kv->key = List_index(bu->key_hash_list, HashMapKeyHash, self->elem_n).key;
kv->value = (u8*)bu->value_list.data + self->map->value_t_size * self->elem_n;
kv->key = bu->key_hash_list.data[self->elem_n].key;
kv->value_ptr = (u8*)bu->value_list.data + self->map->value_t_size * self->elem_n;
return true;
}

58
src/collections/LList.c Normal file
View File

@@ -0,0 +1,58 @@
#include "tlibc/collections/LList.h"
#include <assert.h>
void _LList_detatch(LList_* l, LLNode_* n){
if(n == l->first){
l->first = n->next;
}
else {
assert(n->prev != NULL);
n->prev->next = n->next;
}
if(n == l->last){
l->last = n->prev;
}
else {
assert(n->next != NULL);
n->next->prev = n->prev;
}
l->count--;
n->prev = NULL;
n->next = NULL;
}
void _LList_insertAfter(LList_* l, NULLABLE(LLNode_*) target, LLNode_* detatched)
{
if(target == NULL){
assert(l->first == NULL && l->last == NULL);
l->first = detatched;
l->last = detatched;
return;
}
detatched->prev = target;
detatched->next = target->next;
target->next = detatched;
if(detatched->next){
detatched->next->prev = detatched;
}
}
void _LList_insertBefore(LList_* l, NULLABLE(LLNode_*) target, LLNode_* detatched)
{
if(target == NULL){
assert(l->first == NULL && l->last == NULL);
l->first = detatched;
l->last = detatched;
return;
}
detatched->prev = target->prev;
detatched->next = target;
target->prev = detatched;
if(detatched->prev){
detatched->prev->next = detatched;
}
}

View File

@@ -1,55 +1,73 @@
#include "tlibc/collections/List.h"
#include <assert.h>
List_ List_alloc_size(u32 initial_size){
if(initial_size == 0)
return List_null;
u32 allocated_size = ALIGN_TO(initial_size, sizeof(void*));
return List_construct_size(malloc(allocated_size), 0, allocated_size);
List_ _List_alloc(u32 initial_capacity, u32 elem_t_size){
assert(elem_t_size != 0);
if(initial_capacity == 0)
return _List_construct(NULL, 0, 0, elem_t_size);
u32 initial_size = initial_capacity * elem_t_size;
u32 aligned_capacity = ALIGN_TO(initial_size, sizeof(void*));
return _List_construct(malloc(aligned_capacity), 0, aligned_capacity, elem_t_size);
}
List_ List_copy(const List_ src){
List_ copy = List_alloc_size(src.allocated_size);
if(copy.data != NULL)
memcpy(copy.data, src.data, src.size);
List_ _List_copy(const List_* src){
assert(src->elem_t_size != 0);
List_ copy = _List_alloc(src->capacity, src->elem_t_size);
if(copy.data != NULL){
memcpy(copy.data, src->data, src->len * src->elem_t_size);
}
return copy;
}
void List_increaseCapacity_size(List_* self, u32 size_to_add){
u32 occupied_size = self->size;
u32 expanded_size = occupied_size + size_to_add;
void _List_increaseCapacity(List_* self, u32 len_to_add){
assert(self->elem_t_size != 0);
if(self->allocated_size < expanded_size) {
u32 expanded_alloc_size = self->allocated_size;
if(expanded_alloc_size == 0)
expanded_alloc_size = 32;
while(expanded_alloc_size < expanded_size){
expanded_alloc_size *= 2;
u32 expanded_len = self->len + len_to_add;
if(self->capacity > expanded_len)
return;
u32 expanded_capacity = self->capacity;
if(expanded_capacity == 0)
expanded_capacity = 32;
while(expanded_capacity < expanded_len){
expanded_capacity *= 2;
}
// if self->data is null, realloc acts like malloc
self->data = realloc(self->data, expanded_alloc_size);
self->allocated_size = expanded_alloc_size;
}
self->data = realloc(self->data, expanded_capacity * self->elem_t_size);
self->capacity = expanded_capacity;
}
void* List_expand_size(List_* self, u32 size_to_add){
List_increaseCapacity_size(self, size_to_add);
u8* empty_cell_ptr = (u8*)self->data + self->size;
self->size += size_to_add;
void* _List_expand(List_* self, u32 len_to_add){
assert(self->elem_t_size != 0);
_List_increaseCapacity(self, len_to_add);
u8* empty_cell_ptr = (u8*)self->data + self->len * self->elem_t_size;
self->len += len_to_add;
return empty_cell_ptr;
}
void List_push_size(List_* self, void* values, u32 size){
void* empty_cell_ptr = List_expand_size(self, size);
memcpy(empty_cell_ptr, values, size);
void _List_push(List_* self, void* values, u32 len){
assert(self->elem_t_size != 0);
void* empty_cell_ptr = _List_expand(self, len);
memcpy(empty_cell_ptr, values, len * self->elem_t_size);
}
bool List_removeAt_size(List_* self, u32 i, u32 remove_size){
if(i + remove_size >= self->size)
bool _List_tryRemoveAt(List_* self, u32 i, u32 remove_len){
assert(self->elem_t_size != 0);
if(i >= self->len)
return false;
self->size -= remove_size;
u8* src = (u8*)self->data + i + remove_size;
u8* dst = (u8*)self->data + i;
memmove(dst, src, self->size - i - remove_size);
if(remove_len + i > self->len)
remove_len = self->len - i;
u32 move_back_n_elements = self->len - i - remove_len;
if(move_back_n_elements > 0){
u32 move_back_size = move_back_n_elements * self->elem_t_size;
u8* dst = (u8*)self->data + i * self->elem_t_size;
u8* src = (u8*)self->data + (i + remove_len) * self->elem_t_size;
memmove(dst, src, move_back_size);
}
self->len -= remove_len;
return true;
}

View File

@@ -3,15 +3,15 @@
#define ERRMSG_LENGTH 1024
Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos p,
Error* Error_create(str 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->msg = msg;
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);
e->call_stack = List_ErrorCallPos_alloc(16);
Error_addCallPos(e, p);
return e;
}
@@ -24,12 +24,12 @@ void Error_free(Error* e){
}
void Error_addCallPos(Error* e, ErrorCallPos p){
List_push(&e->call_stack, ErrorCallPos, p);
List_ErrorCallPos_push(&e->call_stack, p);
}
str Error_toStr(Error* e){
u32 len = List_len(e->call_stack, ErrorCallPos);
StringBuilder b = StringBuilder_alloc(e->msg.size + 80 * len);
u32 len = e->call_stack.len;
StringBuilder b = StringBuilder_alloc(e->msg.len + 80 * len);
StringBuilder_append_str(&b, STR("Catched Error: "));
StringBuilder_append_str(&b, e->msg);
@@ -50,7 +50,7 @@ str Error_toStr(Error* e){
void Error_printAndExit(Error* e){
str e_str = Error_toStr(e);
printfe("\n"FMT_str"\n", e_str.size, e_str.data);
printfe("\n"FMT_str"\n", e_str.len, e_str.data);
free(e_str.data);
Error_free(e);
exit(111);

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

@@ -141,11 +141,29 @@ Result(void) file_readWhole(FILE* f, Array(u8)* out_buf){
bool success = false;
try(i64 f_size, i, file_getSize(f));
Array(u8) buf = Array_alloc(u8, f_size);
Defer(if(!success) free(buf.data));
try_void(file_readBytesArray(f, buf));
Array(u8) buf = Array_u8_alloc(f_size);
Defer(if(!success) Array_u8_destroy(&buf));
try_void(file_readBytesArrayExactly(f, buf));
*out_buf = buf;
success = true;
Return RESULT_VOID;
}
Result(void) file_readWholeText(FILE* f, str* out_str){
Deferral(1);
bool success = false;
try(i64 f_size, i, file_getSize(f));
Array(u8) buf = Array_u8_alloc(f_size + 1);
Defer(if(!success) Array_u8_destroy(&buf));
buf.len--;
try_void(file_readBytesArrayExactly(f, buf));
buf.data[buf.len] = '\0';
*out_str = Array_u8_castTo_str(buf, true);
success = true;
Return RESULT_VOID;
}

View File

@@ -1,36 +1,42 @@
#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.size == 0)
if(path.len == 0)
return path;
// remove trailing slash (name/)
if(path.data[path.size - 1] == path_sep){
path.size -= 1;
if(path.data[path.len - 1] == path_sep){
path.len -= 1;
}
i32 sepIndex = str_seekCharReverse(path, path_sep, -1);
if(sepIndex < 0)
return STR(".");
path.size = sepIndex;
path.len = sepIndex;
path.isZeroTerminated = false;
return path;
}
str path_basename(str path, bool remove_ext){
if(path.size == 0)
if(path.len == 0)
return path;
// remove trailing slash (name/)
if(path.data[path.size - 1] == path_sep){
path.size -= 1;
if(path.data[path.len - 1] == path_sep){
path.len -= 1;
}
i32 nameIndex = str_seekCharReverse(path, path_sep, -1) + 1;
if((u32)nameIndex != 0){
path.data += nameIndex;
path.size -= nameIndex;
path.len -= nameIndex;
}
if(!remove_ext)
@@ -38,8 +44,28 @@ str path_basename(str path, bool remove_ext){
i32 extIndex = str_seekCharReverse(path, '.', -1);
if(extIndex > 0){
path.size = extIndex;
path.len = extIndex;
path.isZeroTerminated = false;
}
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

@@ -8,41 +8,40 @@ void StringBuilder_destroy(StringBuilder* b){
}
str StringBuilder_getStr(StringBuilder* b){
if(b->buffer.size == 0 || ((char*)b->buffer.data)[b->buffer.size - 1] != '\0'){
List_push(&b->buffer, u8, '\0');
// '\0' at the end doesn't increase buffer.size
b->buffer.size -= 1;
if(b->buffer.len == 0 || b->buffer.data[b->buffer.len - 1] != '\0'){
List_char_push(&b->buffer, '\0');
// '\0' at the end doesn't increase buffer.len
b->buffer.len -= 1;
}
str result = str_construct((char*)b->buffer.data, b->buffer.size, true);
str result = str_construct(b->buffer.data, b->buffer.len, true);
return result;
}
bool StringBuilder_equals(const StringBuilder* a, const StringBuilder* b){
str a_str = str_construct((char*)a->buffer.data, a->buffer.size, false);
str b_str = str_construct((char*)b->buffer.data, b->buffer.size, false);
str a_str = str_construct(a->buffer.data, a->buffer.len, false);
str b_str = str_construct(b->buffer.data, b->buffer.len, false);
return str_equals(a_str, b_str);
}
void StringBuilder_removeFromEnd(StringBuilder* b, u32 count){
if(count < b->buffer.size){
b->buffer.size -= count;
if(count < b->buffer.len){
b->buffer.len -= count;
}
else{
b->buffer.size = 0;
b->buffer.len = 0;
}
}
void StringBuilder_append_char(StringBuilder* b, char c){
List_push(&b->buffer, u8, c);
List_char_push(&b->buffer, c);
}
void StringBuilder_append_str(StringBuilder* b, str s){
if(s.data == NULL)
return;
List_push_size(&b->buffer, s.data, s.size);
List_char_pushMany(&b->buffer, s.data, s.len);
}
void StringBuilder_append_cstr(StringBuilder* b, cstr s){
@@ -78,7 +77,7 @@ void StringBuilder_append_memory(StringBuilder* b, Array(u8) mem, bool uppercase
return;
char buf[8];
for (u32 i=0; i < mem.size; i++) {
for (u32 i=0; i < mem.len; i++) {
sprintf(buf, uppercase ? "%02X" : "%02x", ((u8*)mem.data)[i]);
StringBuilder_append_str(b, str_construct(buf, 2, true));
}

View File

@@ -2,17 +2,16 @@
#include "tlibc/string/StringBuilder.h"
str str_copy(const str self){
if(self.data == NULL || self.size == 0)
return self;
str copy = str_construct((char*)malloc(self.size + 1), self.size, true);
memcpy(copy.data, self.data, self.size);
copy.data[copy.size] = '\0';
str copy = str_construct((char*)malloc(self.len + 1), self.len, true);
if(self.len != 0){
memcpy(copy.data, self.data, self.len);
}
copy.data[copy.len] = '\0';
return copy;
}
bool str_equals(const str self, const str other){
if(self.size != other.size)
if(self.len != other.len)
return false;
if(self.data == other.data)
return true;
@@ -23,26 +22,26 @@ bool str_equals(const str self, const str other){
strncmp: 1.611s
memcmp: 0.710s
*/
return memcmp(self.data, other.data, self.size) == 0;
return memcmp(self.data, other.data, self.len) == 0;
}
str str_reverse(str s){
if(s.data == NULL || s.size == 0)
if(s.data == NULL || s.len == 0)
return s;
str r = str_construct(malloc(s.size), s.size, s.isZeroTerminated);
for(u32 i = 0; i < s.size; i++ )
r.data[i] = s.data[s.size - i - 1];
str r = str_construct(malloc(s.len), s.len, s.isZeroTerminated);
for(u32 i = 0; i < s.len; i++ )
r.data[i] = s.data[s.len - i - 1];
return r;
}
i32 str_seek(const str src, const str fragment, u32 startIndex){
if(src.size == 0 || fragment.size == 0)
if(src.len == 0 || fragment.len == 0)
return -1;
for(u32 i = startIndex; i < src.size - fragment.size + 1; i++){
for(u32 i = startIndex; i < src.len - fragment.len + 1; i++){
for(u32 j = 0;; j++){
if(j == fragment.size)
if(j == fragment.len)
return i;
if(src.data[i + j] != fragment.data[j])
break;
@@ -52,16 +51,16 @@ i32 str_seek(const str src, const str fragment, u32 startIndex){
}
i32 str_seekReverse(const str src, const str fragment, u32 startIndex){
if(src.size == 0 || fragment.size == 0)
if(src.len == 0 || fragment.len == 0)
return -1;
if(startIndex > src.size - 1)
startIndex = src.size - 1;
for(u32 i = startIndex; i >= fragment.size - 1; i--){
if(startIndex > src.len - 1)
startIndex = src.len - 1;
for(u32 i = startIndex; i >= fragment.len - 1; i--){
for(u32 j = 0;; j++){
if(j == fragment.size)
if(j == fragment.len)
return i - j + 1;
if(src.data[i - j] != fragment.data[fragment.size - 1 - j])
if(src.data[i - j] != fragment.data[fragment.len - 1 - j])
break;
}
}
@@ -69,7 +68,7 @@ i32 str_seekReverse(const str src, const str fragment, u32 startIndex){
}
i32 str_seekChar(const str src, char c, u32 startIndex){
for(u32 i = startIndex; i < src.size; i++){
for(u32 i = startIndex; i < src.len; i++){
if(src.data[i] == c)
return i;
}
@@ -77,8 +76,8 @@ i32 str_seekChar(const str src, char c, u32 startIndex){
}
i32 str_seekCharReverse(const str src, char c, u32 startIndex){
if(startIndex > src.size - 1)
startIndex = src.size - 1;
if(startIndex > src.len - 1)
startIndex = src.len - 1;
for(u32 i = startIndex; i != (u32)-1; i--){
if(src.data[i] == c)
return i;
@@ -87,36 +86,36 @@ i32 str_seekCharReverse(const str src, char c, u32 startIndex){
}
bool str_startsWith(const str src, const str fragment){
if(src.size < fragment.size)
if(src.len < fragment.len)
return false;
str src_fragment = str_null;
src_fragment.data = src.data;
src_fragment.size = fragment.size;
src_fragment.len = fragment.len;
return str_equals(src_fragment, fragment);
}
bool str_endsWith(const str src, const str fragment){
if(src.size < fragment.size)
if(src.len < fragment.len)
return false;
str src_fragment = str_null;
src_fragment.data = (char*)(src.data + src.size - fragment.size);
src_fragment.size = fragment.size;
src_fragment.data = (char*)(src.data + src.len - fragment.len);
src_fragment.len = fragment.len;
return str_equals(src_fragment, fragment);
}
u32 str_hash32(const str s){
u8* ubuf = (u8*)s.data;
u32 hash=0;
for (u32 i = 0; i < s.size; i++)
for (u32 i = 0; i < s.len; i++)
hash = (hash<<6) + (hash<<16) - hash + ubuf[i];
return hash;
}
str str_toUpper(const str src){
str r = str_copy(src);
for (u32 i = 0; i < r.size; i++){
for (u32 i = 0; i < r.len; i++){
if(char_isLatinLower(r.data[i]))
r.data[i] = r.data[i] - 'a' + 'A';
}
@@ -125,7 +124,7 @@ str str_toUpper(const str src){
str str_toLower(const str src){
str r = str_copy(src);
for (u32 i = 0; i < r.size; i++){
for (u32 i = 0; i < r.len; i++){
if(char_isLatinUpper(r.data[i]))
r.data[i] = r.data[i] - 'A' + 'a';
}
@@ -133,7 +132,7 @@ str str_toLower(const str src){
}
str hex_to_str(Array(u8) buf, bool uppercase){
StringBuilder sb = StringBuilder_alloc(buf.size * 2 + 1);
StringBuilder sb = StringBuilder_alloc(buf.len * 2 + 1);
StringBuilder_append_memory(&sb, buf, uppercase);
return StringBuilder_getStr(&sb);
}
@@ -141,13 +140,13 @@ str hex_to_str(Array(u8) buf, bool uppercase){
void str_trim(str* line, bool set_zero_at_end){
// loop forward
bool stop = false;
while(line->size > 0 && !stop){
while(line->len > 0 && !stop){
char first_char = line->data[0];
switch(first_char){
case '\0': case '\r': case '\n':
case '\t': case ' ':
line->data++;
line->size--;
line->len--;
break;
default:
stop = true;
@@ -157,13 +156,13 @@ void str_trim(str* line, bool set_zero_at_end){
// loop backward
stop = false;
while(line->size > 0 && !stop)
while(line->len > 0 && !stop)
{
char last_char = line->data[line->size - 1];
char last_char = line->data[line->len - 1];
switch(last_char){
case '\0': case '\r': case '\n':
case '\t': case ' ':
line->size--;
line->len--;
break;
default:
stop = true;
@@ -172,7 +171,7 @@ void str_trim(str* line, bool set_zero_at_end){
}
if(set_zero_at_end){
line->data[line->size] = '\0';
line->data[line->len] = '\0';
line->isZeroTerminated = true;
}
}

189
src/term.c Normal file
View File

@@ -0,0 +1,189 @@
#include "tlibc/term.h"
#if defined(_WIN64) || defined(_WIN32)
#include <windows.h>
#define try_win_bool(EXPR) if(!(EXPR)) { Return RESULT_ERROR_FMT(#EXPR " failed with WindowsError %lu", GetLastError()); }
#else
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>
#define try_zero_errno(EXPR) if((EXPR) != 0) { \
char* errno_s = strerror_malloc(errno); \
ResultVar(void) err = RESULT_ERROR_FMT(#EXPR " failed with errno: %s", strerror(errno)); \
free(errno_s); \
Return err; \
}
#endif
Result(void) term_init(){
Deferral(8);
#if defined(_WIN64) || defined(_WIN32)
DWORD mode = 0;
HANDLE console_handle;
// configure stdout
console_handle = GetStdHandle(STD_OUTPUT_HANDLE);
try_assert(console_handle != INVALID_HANDLE_VALUE);
GetConsoleMode(console_handle, &mode);
// https://learn.microsoft.com/en-us/windows/console/setconsolemode
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
mode |= ENABLE_PROCESSED_OUTPUT;
mode |= DISABLE_NEWLINE_AUTO_RETURN;
SetConsoleMode(console_handle, mode);
// configure stderr
console_handle = GetStdHandle(STD_ERROR_HANDLE);
try_assert(console_handle != INVALID_HANDLE_VALUE);
GetConsoleMode(console_handle, &mode);
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
mode |= ENABLE_PROCESSED_OUTPUT;
mode |= DISABLE_NEWLINE_AUTO_RETURN;
SetConsoleMode(console_handle, mode);
// configure stdin
console_handle = GetStdHandle(STD_INPUT_HANDLE);
try_assert(console_handle != INVALID_HANDLE_VALUE);
GetConsoleMode(console_handle, &mode);
mode |= ENABLE_VIRTUAL_TERMINAL_INPUT;
SetConsoleMode(console_handle, mode);
#endif
Return RESULT_VOID;
}
i64 getenv_int(const char* var_name){
char* s = getenv(var_name);
if(s == NULL)
return -1;
return strtoll(s, NULL, 0);
}
Result(void) term_getSize(TerminalSize* out) {
Deferral(4);
#if defined(_WIN64) || defined(_WIN32)
// helps when STD_OUT is redirected to a file
HANDLE console_handle_stderr = GetStdHandle(STD_ERROR_HANDLE);
try_assert(console_handle_stderr != INVALID_HANDLE_VALUE)
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
try_win_bool(GetConsoleScreenBufferInfo(console_handle_stderr, &consoleInfo));
out->cols = consoleInfo.srWindow.Right - consoleInfo.srWindow.Left + 1;
out->rows = consoleInfo.srWindow.Bottom - consoleInfo.srWindow.Top + 1;
#else
struct winsize ws = {0};
// try to get terminal size from stdin, stdout, stderr
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == 0 ||
ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0 ||
ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == 0 ){
out->cols = ws.ws_col;
out->rows = ws.ws_row;
}
// try to get size from environtent variables
else {
out->cols = getenv_int("COLUMNS");
out->rows = getenv_int("LINES");
}
#endif
try_assert(out->cols > 0);
try_assert(out->rows > 0);
Return RESULT_VOID;
}
Result(void) term_readLine(char* buf, u32 bufsize) {
Deferral(1);
if(fgets(buf, bufsize, stdin) == NULL){
try_stderrcode(ferror(stdin));
}
Return RESULT_VOID;
}
Result(void) term_readLineHidden(char *buf, u32 bufsize) {
Deferral(4);
#if defined(_WIN64) || defined(_WIN32)
HANDLE console_handle_stdin = GetStdHandle(STD_INPUT_HANDLE);
try_assert(console_handle_stdin != INVALID_HANDLE_VALUE);
DWORD old_mode;
GetConsoleMode(console_handle_stdin, &old_mode);
// turn off echo
DWORD new_mode = old_mode & ~(ENABLE_ECHO_INPUT);
SetConsoleMode(console_handle_stdin, new_mode);
// restore echo
Defer(SetConsoleMode(console_handle_stdin, old_mode));
#else
struct termios old_mode, new_mode;
try_zero_errno(tcgetattr(STDIN_FILENO, &old_mode));
new_mode = old_mode;
// turn off echo
new_mode.c_lflag &= ~(ECHO);
try_zero_errno(tcsetattr(STDIN_FILENO, TCSAFLUSH, &new_mode));
// restore echo
Defer(tcsetattr(STDIN_FILENO, TCSAFLUSH, &old_mode));
#endif
// read line
try_void(term_readLine(buf, bufsize));
fputc('\n', stdout);
Return RESULT_VOID;
}
/*
Most of escape sequences can be found there
https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
*/
#define ESC "\x1b"
#define CSI ESC"["
void term_setFgColor16(Color16 c){
printf(CSI"%um", c);
}
void term_setBgColor16(Color16 c){
printf(CSI"%um", c + 10);
}
void term_bold(){
printf(CSI"1m");
}
void term_italic(){
printf(CSI"3m");
}
void term_underline(){
printf(CSI"4m");
}
void term_strikethrough(){
printf(CSI"9m");
}
void term_resetCursor() {
printf(CSI"H");
}
void term_resetColors() {
printf(CSI"0m");
}
void term_clear() {
printf(CSI"0m" CSI"H" CSI"2J");
}
void term_eraseRow(){
printf(CSI"2K\r");
}
void term_cursorMove(u16 row, u16 column) {
printf(CSI"%u;%uH", row, column);
}
void term_cursorHide() {
printf(CSI"?25l");
}
void term_cursorShow() {
printf(CSI"?25h");
}

View File

@@ -71,3 +71,17 @@ void DateTime_get(DateTime* dt, bool utc_time){
dt->d.week_day = c_tm.tm_wday + 1;
dt->d.year_day = c_tm.tm_yday + 1;
}
Result(void) DateTime_parse(cstr src, DateTime* dt){
zeroStruct(dt);
f64 sec_f = 0;
i32 r = sscanf(src, "%"SCNi16".%"SCNi8".%"SCNi8"-%"SCNi8":%"SCNi8":%lf",
&dt->d.year, &dt->d.month, &dt->d.month_day,
&dt->t.hour, &dt->t.min, &sec_f);
if(r != 6){
return RESULT_ERROR_FMT("attepmted to parse DateTime, got %i fields out of 6", r);
}
dt->t.sec = (i32)sec_f;
dt->t.nsec = (sec_f - (i32)sec_f) * 1e9;
return RESULT_VOID;
}