From f87a2fa4fa1d1257d3bf8f50835338d7a56e8919 Mon Sep 17 00:00:00 2001 From: ahmedsamyh Date: Fri, 11 Apr 2025 19:15:50 +0500 Subject: [PATCH] [commonlib.h] Update --- include/commonlib.h | 87 ++++++++++++++++++++++++++++----------------- 1 file changed, 54 insertions(+), 33 deletions(-) diff --git a/include/commonlib.h b/include/commonlib.h index 3eb80c8..326fd56 100644 --- a/include/commonlib.h +++ b/include/commonlib.h @@ -8,6 +8,7 @@ #include #include #include +#include // Remove Prefix #ifdef COMMONLIB_REMOVE_PREFIX @@ -27,7 +28,7 @@ #define log_info c_log_info #define log_warning c_log_warning -#define slurp_file c_slurp_file +#define read_file c_read_file #define touch_file_if_doesnt_exist c_touch_file_if_doesnt_exist #define Arena c_Arena @@ -66,7 +67,7 @@ #define sv_trim c_sv_trim #define sv_to_cstr c_sv_to_cstr #define sv_to_int c_sv_to_int -#define sv_to_uint64 c_sv_to_uint64 +#define sv_to_uint c_sv_to_uint #define sv_to_uint8_hex c_sv_to_uint8_hex #define sv_to_ptr c_sv_to_ptr #define sv_to_float c_sv_to_float @@ -208,8 +209,8 @@ bool c_os_file_exists(cstr filename); // File // -// reads entire file and gives back the string holding the contents. (caller must be responsible for freeing the string!) -const char* c_slurp_file(const char* filename, bool* success); +// reads entire file and gives back the file content and filesize in bytes. (caller must be responsible for freeing the string!) +const char* c_read_file(const char* filename, int *file_size); void c_touch_file_if_doesnt_exist(cstr file); // @@ -281,11 +282,10 @@ void c_sv_ltrim(c_String_view* sv); void c_sv_rtrim(c_String_view* sv); void c_sv_trim(c_String_view* sv); char* c_sv_to_cstr(c_String_view sv); -int32 c_sv_to_int(c_String_view sv); -uint64 c_sv_to_uint64(c_String_view sv); -uint8 c_sv_to_uint8_hex(c_String_view sv); -void* c_sv_to_ptr(c_String_view sv); -float32 c_sv_to_float(c_String_view sv); +int64 c_sv_to_int(c_String_view sv, int *count, int base); +uint64 c_sv_to_uint(c_String_view sv, int *count, int base); +void* c_sv_to_ptr(c_String_view sv, int *count, int base); +float64 c_sv_to_float(c_String_view sv, int *count); bool c_sv_contains_char(c_String_view sv, char ch); bool c_sv_is_hex_numbers(c_String_view sv); bool c_sv_equals(c_String_view sv1, c_String_view sv2); @@ -334,8 +334,8 @@ bool c_os_file_exists(cstr filename) { result = ret_val;\ goto defer -const char *c_slurp_file(const char* filename, bool* success) { - FILE* f = fopen(filename, "rb"); +const char *c_read_file(const char* filename, int *file_size) { + FILE* f = fopen(filename, "r"); char* result = NULL; if (f == NULL){ @@ -367,17 +367,16 @@ const char *c_slurp_file(const char* filename, bool* success) { defer(NULL); } - if (fread((char*)result, sizeof(char), fsize, f) != fsize){ - c_log_error("'%s': %s", filename, strerror(errno)); - defer(NULL); - } + size_t read = fread((char*)result, sizeof(char), fsize, f); + + *file_size = (int)read; // set null-terminator - result[fsize] = '\0'; + result[read] = '\0'; defer: if (f) fclose(f); - *success = result != NULL; + if (result == NULL) *file_size = -1; return result; } @@ -616,37 +615,59 @@ char* c_sv_to_cstr(c_String_view sv){ return res; } -// TODO: check for failure of conversion. returns 0/0.0 on failure, so just check if the str contains zero. -int32 c_sv_to_int(c_String_view sv) { - char* str = c_sv_to_cstr(sv); - int32 res = atoi(str); - C_FREE(str); - return res; -} +int64 c_sv_to_int(c_String_view sv, int *count_out, int base) { + char *str = c_sv_to_cstr(sv); + + char *endptr = NULL; + + int64 res = strtol(str, &endptr, base); + + if (endptr == str) { + *count_out = -1; + return res; + } + + *count_out = (int)(endptr - str); -uint64 c_sv_to_uint64(c_String_view sv) { - char* str = c_sv_to_cstr(sv); - uint64 res = (uint64)atoll(str); C_FREE(str); return res; } -uint8 c_sv_to_uint8_hex(c_String_view sv) { +uint64 c_sv_to_uint(c_String_view sv, int *count, int base) { char* str = c_sv_to_cstr(sv); - char* end = str + sv.count; - uint8 res = (uint8)strtol(str, &end, 16); + + char *endptr = NULL; + uint64 res = strtoul(str, &endptr, base); + + if (endptr == str) { + *count = -1; + return res; + } + + *count = (int)(endptr - str); + C_FREE(str); return res; } -float32 c_sv_to_float(c_String_view sv) { +float64 c_sv_to_float(c_String_view sv, int *count) { char* str = c_sv_to_cstr(sv); - float32 res = (float32)atof(str); + + char *endptr = NULL; + float64 res = strtod(str, &endptr); + + if (endptr == str) { + *count = -1; + return res; + } + + *count = (int)(endptr - str); + C_FREE(str); return res; } -void* c_sv_to_ptr(c_String_view sv) { +void* c_sv_to_ptr(c_String_view sv, int *count, int base) { char* str = c_sv_to_cstr(sv); char* end = NULL; void* res = (void*)strtoull(str, &end, 16); -- 2.39.5