Compare commits
5 Commits
8a40caaf10
...
ea6c20f430
| Author | SHA1 | Date | |
|---|---|---|---|
| ea6c20f430 | |||
| 77e4f38416 | |||
| f3e4f8f061 | |||
| 83091ac707 | |||
|
|
6a7f0a8715 |
@@ -8,7 +8,10 @@ C library with collections, strings, exceptions, io...
|
||||
git clone https://timerix.ddns.net/git/Timerix/tlibc.git
|
||||
```
|
||||
|
||||
2. Install [cbuild](https://timerix.ddns.net/git/Timerix/cbuild) version specified in `project.config`.
|
||||
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
|
||||
```
|
||||
|
||||
@@ -37,8 +37,18 @@ static inline List(T) List_##T##_copy(const List(T)* src) { \
|
||||
\
|
||||
static inline void List_##T##_destroy(List(T)* self) { _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 = (T*)(_List_expand((void*)self, 1)); \
|
||||
T* empty_cell = List_##T##_expand(self, 1); \
|
||||
*empty_cell = value; \
|
||||
} \
|
||||
\
|
||||
@@ -46,11 +56,6 @@ static inline void List_##T##_pushMany(List(T)* self, T* values_ptr, u32 len) {
|
||||
_List_push((void*)self, values_ptr, len); \
|
||||
} \
|
||||
\
|
||||
/* 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); \
|
||||
} \
|
||||
\
|
||||
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); \
|
||||
|
||||
@@ -133,10 +133,14 @@ static inline Result(void) file_readBytesArrayExactly(FILE* f, Array(u8) dst){
|
||||
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 //
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -13,7 +13,13 @@ typedef struct str {
|
||||
bool isZeroTerminated;
|
||||
} str;
|
||||
|
||||
/*
|
||||
USAGE:
|
||||
str s = STR("something");
|
||||
printf(FMT_str"\n", str_expand(s));
|
||||
*/
|
||||
#define FMT_str "%.*s"
|
||||
#define str_expand(S) (S).len, (S).data
|
||||
|
||||
/// creates str from a string literal
|
||||
#define STR(LITERAL) str_construct(LITERAL, ARRAY_LEN(LITERAL) - 1, true)
|
||||
|
||||
@@ -142,10 +142,28 @@ Result(void) file_readWhole(FILE* f, Array(u8)* out_buf){
|
||||
|
||||
try(i64 f_size, i, file_getSize(f));
|
||||
Array(u8) buf = Array_u8_alloc(f_size);
|
||||
Defer(if(!success) free(buf.data));
|
||||
try_void(file_readBytesArray(f, buf));
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user