Compare commits

...

2 Commits

Author SHA1 Message Date
5fb2db2380 implemented str_slice 2025-11-01 19:49:31 +05:00
c4cb696721 fixed file_getSize 2025-11-01 19:49:17 +05:00
5 changed files with 32 additions and 3 deletions

View File

@ -38,11 +38,12 @@ static inline Array_ Array_copy(Array_ src){
#define struct_castTo_Array(STRUCT_PTR) Array_construct_size((STRUCT_PTR), sizeof(*STRUCT_PTR)) #define struct_castTo_Array(STRUCT_PTR) Array_construct_size((STRUCT_PTR), sizeof(*STRUCT_PTR))
///@return a[0..n]
static inline Array(u8) Array_sliceBefore(Array(u8) a, u32 n){ static inline Array(u8) Array_sliceBefore(Array(u8) a, u32 n){
return Array_construct_size(a.data, n); return Array_construct_size(a.data, n);
} }
///@return a[n...]
static inline Array(u8) Array_sliceAfter(Array(u8) a, u32 n){ static inline Array(u8) Array_sliceAfter(Array(u8) a, u32 n){
return Array_construct_size((u8*)a.data + n, a.size - n); return Array_construct_size((u8*)a.data + n, a.size - n);
} }

View File

@ -93,4 +93,4 @@ typedef struct Result_ {
}\ }\
} while(0) } while(0)
#define try_assert(EXPR) if(!(EXPR)) { Return RESULT_ERROR((#EXPR), false); } #define try_assert(EXPR) if(!(EXPR)) { Return RESULT_ERROR(("try_assert(" #EXPR ")"), false); }

View File

@ -62,6 +62,10 @@ Result(FILE*) file_open(cstr file_name, cstr fopen_mode);
Result(FILE*) file_openOrCreateReadWrite(cstr file_name); Result(FILE*) file_openOrCreateReadWrite(cstr file_name);
bool file_exists(cstr path); bool file_exists(cstr path);
///@return current position in file
Result(i64) file_tellPos(FILE* f);
///@return total size of file
Result(i64) file_getSize(FILE* f); Result(i64) file_getSize(FILE* f);
@ -71,6 +75,7 @@ typedef enum SeekOrigin {
SeekOrigin_End = SEEK_END, SeekOrigin_End = SEEK_END,
} SeekOrigin; } SeekOrigin;
/// @brief changes current position in file
Result(void) file_seek(FILE* f, i64 offset, SeekOrigin origin); Result(void) file_seek(FILE* f, i64 offset, SeekOrigin origin);

View File

@ -59,3 +59,14 @@ str hex_to_str(Array(u8) buf, bool uppercase);
/// @brief removes blank characters from start and end of the line /// @brief removes blank characters from start and end of the line
void str_trim(str* line, bool set_zero_at_end); void str_trim(str* line, bool set_zero_at_end);
///@return s[0..n]
static inline str str_sliceBefore(str s, u32 n){
return str_construct(s.data, n, false);
}
///@return s[n...]
static inline str str_sliceAfter(str s, u32 n){
return str_construct(s.data + n, s.size - n, s.isZeroTerminated);
}

View File

@ -47,7 +47,7 @@ Result(FILE*) file_openOrCreateReadWrite(cstr file_name){
Result(i64) file_getSize(FILE* f){ Result(i64) file_tellPos(FILE* f){
i64 r = IFWIN(_ftelli64, ftello64)(f); i64 r = IFWIN(_ftelli64, ftello64)(f);
if(r < 0){ if(r < 0){
return RESULT_ERROR_ERRNO(); return RESULT_ERROR_ERRNO();
@ -64,6 +64,18 @@ Result(void) file_seek(FILE* f, i64 offset, SeekOrigin origin){
return RESULT_VOID; return RESULT_VOID;
} }
Result(i64) file_getSize(FILE* f){
Deferral(4);
// get current position
try(i64 original_pos, i, file_tellPos(f));
// seek to end
try_void(file_seek(f, 0, SeekOrigin_End));
try(i64 size, i, file_tellPos(f));
// restore original position
try_void(file_seek(f, original_pos, SeekOrigin_Start));
Return RESULT_VALUE(i, size);
}
Result(void) file_writeStructs(FILE* f, const void* src, u64 struct_size, u64 count){ Result(void) file_writeStructs(FILE* f, const void* src, u64 struct_size, u64 count){
u64 r = fwrite(src, struct_size, count, f); u64 r = fwrite(src, struct_size, count, f);