Compare commits

...

5 Commits

Author SHA1 Message Date
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
5 changed files with 46 additions and 10 deletions

View File

@@ -8,7 +8,10 @@ C library with collections, strings, exceptions, io...
git clone https://timerix.ddns.net/git/Timerix/tlibc.git 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 3. Build static library
``` ```

View File

@@ -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); } \ 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) { \ 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; \ *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); \ _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) ATTRIBUTE_WARN_UNUSED_RESULT; \
static inline bool List_##T##_tryRemoveAt(List(T)* self, u32 i, u32 remove_len) { \ static inline bool List_##T##_tryRemoveAt(List(T)* self, u32 i, u32 remove_len) { \
return _List_tryRemoveAt((void*)self, i, remove_len); \ return _List_tryRemoveAt((void*)self, i, remove_len); \

View File

@@ -133,10 +133,14 @@ static inline Result(void) file_readBytesArrayExactly(FILE* f, Array(u8) dst){
return file_readStructsExactly(f, dst.data, 1, dst.len); 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 /// @param out_buf output array allocated on heap
Result(void) file_readWhole(FILE* f, Array(u8)* out_buf); 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 // // DIRECTORY //
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////

View File

@@ -13,7 +13,13 @@ typedef struct str {
bool isZeroTerminated; bool isZeroTerminated;
} str; } str;
/*
USAGE:
str s = STR("something");
printf(FMT_str"\n", str_expand(s));
*/
#define FMT_str "%.*s" #define FMT_str "%.*s"
#define str_expand(S) (S).len, (S).data
/// creates str from a string literal /// creates str from a string literal
#define STR(LITERAL) str_construct(LITERAL, ARRAY_LEN(LITERAL) - 1, true) #define STR(LITERAL) str_construct(LITERAL, ARRAY_LEN(LITERAL) - 1, true)

View File

@@ -142,10 +142,28 @@ Result(void) file_readWhole(FILE* f, Array(u8)* out_buf){
try(i64 f_size, i, file_getSize(f)); try(i64 f_size, i, file_getSize(f));
Array(u8) buf = Array_u8_alloc(f_size); Array(u8) buf = Array_u8_alloc(f_size);
Defer(if(!success) free(buf.data)); Defer(if(!success) Array_u8_destroy(&buf));
try_void(file_readBytesArray(f, buf));
try_void(file_readBytesArrayExactly(f, buf));
*out_buf = buf; *out_buf = buf;
success = true; success = true;
Return RESULT_VOID; 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;
}