Compare commits

...

3 Commits

Author SHA1 Message Date
c68e4e87b3 fixed DateTime_get bugs and added FMT_DateTime 2025-11-18 16:00:50 +05:00
bc41577248 file_readWhole 2025-11-18 14:18:09 +05:00
89aab2b5bf Array_free 2025-11-18 12:16:46 +05:00
7 changed files with 55 additions and 16 deletions

View File

@@ -37,6 +37,10 @@ static inline Array_ Array_copy(const Array_ self){
return copy; return copy;
} }
static inline void Array_free(Array_ self){
free(self.data);
}
#define Array_len(SELF, T) (SELF.size / sizeof(T)) #define Array_len(SELF, T) (SELF.size / sizeof(T))
#define Array_memset(SELF, VAL) memset(SELF.data, VAL, SELF.size) #define Array_memset(SELF, VAL) memset(SELF.data, VAL, SELF.size)

View File

@@ -20,6 +20,10 @@ static inline List_ List_construct_size(void* data_ptr, u32 occupied_size, u32 a
#define List_alloc(T, INITIAL_COUNT) List_alloc_size((INITIAL_COUNT) * sizeof(T)) #define List_alloc(T, INITIAL_COUNT) List_alloc_size((INITIAL_COUNT) * sizeof(T))
List_ List_alloc_size(u32 initial_size); List_ List_alloc_size(u32 initial_size);
static inline void List_destroy(List_ self){
free(self.data);
}
List_ List_copy(List_ src); List_ List_copy(List_ src);
// alloc bigger buffer if size + size_to_add won't fit in current // alloc bigger buffer if size + size_to_add won't fit in current

View File

@@ -130,6 +130,10 @@ 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.size);
} }
/// @brief allocates array of size equal `file_getSize()` and reads whole file
/// @param out_buf output array allocated on heap
Result(void) file_readWhole(FILE* f, Array(u8)* out_buf);
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
// DIRECTORY // // DIRECTORY //
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////

View File

@@ -22,7 +22,7 @@ static inline str str_from_cstr(cstr s_ptr){
return str_construct((void*)s_ptr, strlen(s_ptr), true); return str_construct((void*)s_ptr, strlen(s_ptr), true);
} }
static inline void str_destroy(str s){ static inline void str_free(str s){
free(s.data); free(s.data);
} }

View File

@@ -29,18 +29,18 @@ void sleepMsec(msec_t time);
typedef struct Time { typedef struct Time {
i32 nsec; i32 nsec; /* 0..999_999_999 */
i8 sec; i8 sec; /* 0..60 leap second??? */
i8 min; i8 min; /* 0..59 */
i8 hour; i8 hour; /* 0..23 */
} Time; } Time;
typedef struct Date { typedef struct Date {
i8 month_day; i8 month_day; /* 1..31 */
i8 month; i8 month; /* 1..12 */
i16 year; i16 year; /* 1900-xxxx */
i8 week_day; i8 week_day; /* 1..7 */
i16 year_day; i16 year_day; /* 1..366 */
} Date; } Date;
typedef struct DateTime { typedef struct DateTime {
@@ -52,6 +52,19 @@ void DateTime_get(DateTime* dt, bool utc_time);
static inline void DateTime_getLocal(DateTime* dt) { DateTime_get(dt, false); } static inline void DateTime_getLocal(DateTime* dt) { DateTime_get(dt, false); }
static inline void DateTime_getUTC(DateTime* dt) { DateTime_get(dt, true); } static inline void DateTime_getUTC(DateTime* dt) { DateTime_get(dt, true); }
// 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"
/*
USAGE:
DateTime dt;
DateTime_getLocal(&dt);
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
#if __cplusplus #if __cplusplus
} }
#endif #endif

View File

@@ -124,3 +124,17 @@ Result(void) file_readStructsExactly(FILE* f, void* dst, u64 struct_size, u64 ex
} }
return RESULT_VOID; return RESULT_VOID;
} }
Result(void) file_readWhole(FILE* f, Array(u8)* out_buf){
Deferral(1);
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));
*out_buf = buf;
success = true;
Return RESULT_VOID;
}

View File

@@ -51,21 +51,21 @@ void sleepMsec(msec_t time){
void DateTime_get(DateTime* dt, bool utc_time){ void DateTime_get(DateTime* dt, bool utc_time){
time_t c_time = time(NULL); time_t c_time = time(NULL);
struct tm c_tm; struct tm c_tm;
struct timespec c_timespec;
if(utc_time){ if(utc_time){
portable_gmtime_s(&c_time, &c_tm); portable_gmtime_s(&c_time, &c_tm);
} }
else { else {
portable_localtime_s(&c_time, &c_tm); portable_localtime_s(&c_time, &c_tm);
} }
struct timespec c_timespec;
clock_gettime(CLOCK_REALTIME, &c_timespec); clock_gettime(CLOCK_REALTIME, &c_timespec);
dt->t.nsec = c_timespec.tv_nsec; dt->t.nsec = c_timespec.tv_nsec;
dt->t.sec = c_timespec.tv_sec; dt->t.sec = c_tm.tm_sec;
dt->t.min = c_tm.tm_min; dt->t.min = c_tm.tm_min;
dt->t.hour = c_tm.tm_hour; dt->t.hour = c_tm.tm_hour;
dt->d.month_day = c_tm.tm_mday; dt->d.month_day = c_tm.tm_mday;
dt->d.month = c_tm.tm_mon; dt->d.month = c_tm.tm_mon + 1;
dt->d.year = c_tm.tm_year; dt->d.year = c_tm.tm_year + 1900;
dt->d.week_day = c_tm.tm_wday; dt->d.week_day = c_tm.tm_wday + 1;
dt->d.year_day = c_tm.tm_yday; dt->d.year_day = c_tm.tm_yday + 1;
} }