Compare commits

...

11 Commits

Author SHA1 Message Date
8eeaff4245 made str_equals faster 2025-08-10 17:21:55 +03:00
a097d5aff9 header fix for linux 2025-08-10 03:22:10 +03:00
d04aac567f added defer macro 2025-08-09 21:42:12 +03:00
fe9e44a660 file_openOrCreateReadWrite 2025-08-08 21:38:34 +03:00
223406d4e4 added bool value return to dir_create 2025-08-04 19:15:30 +03:00
e1dc972b24 implemented file read/write functions 2025-08-04 18:23:46 +03:00
ab55f85160 fixed bugs in path functions 2025-08-04 16:33:58 +03:00
e0f1941c82 added macro try_void 2025-08-04 16:32:23 +03:00
6d959fe8f5 added macro RESULT_ERROR_FMT and some filesystem functions 2025-08-04 16:03:08 +03:00
961d00fdb0 filesystem.h 2025-08-04 13:47:24 +03:00
4c3933f789 renamed v_ptr to p 2025-07-31 18:24:53 +03:00
11 changed files with 484 additions and 24 deletions

81
include/tlibc/defer.h Normal file
View File

@@ -0,0 +1,81 @@
/*
Based on https://github.com/moon-chilled/Defer
Usage:
void foo(){
Deferral(16);
void* p = malloc(8);
Defer(free(p));
Return;
}
*/
#pragma once
#if defined(__GNUC__) || defined(__TINYC__)
#define Deferral(MAX_DEFER_STATEMENTS) \
unsigned char _num_deferrals = 0; \
void *_defer_return_loc = 0, *_deferrals[MAX_DEFER_STATEMENTS] = {0};
# define Defer(block) _Defer(block, __LINE__)
# define Return _Return(__LINE__)
#define _defer_token_cat(a, b) a ## b
#define _Defer(block, n) do { \
_deferrals[_num_deferrals++] = && _defer_token_cat(_defer_ini, n); \
if (0) { \
_defer_token_cat(_defer_ini, n): \
block; \
if (_num_deferrals) { \
goto *_deferrals[--_num_deferrals]; \
} else { \
goto *_defer_return_loc; \
} \
} \
} while (0)
#define _Return(n) \
if (_num_deferrals \
/* this nonsense disables warning Wunused-but-set-variable */ \
&& _defer_return_loc == _defer_return_loc ) \
{ \
_defer_return_loc = && _defer_token_cat(_defer_fini_, n); \
goto *_deferrals[--_num_deferrals]; \
} else _defer_token_cat(_defer_fini_, n): \
return
#else /* !__GNUC__ && !__TINYCC__ */
#include <setjmp.h>
#ifdef _MSC_VER
# pragma message("You are using the unsafe longjmp()-based defer implementation. Expect bugs if you don't know what you're doing.")
#else
# warning You are using the unsafe longjmp()-based defer implementation. Expect bugs if you don't know what you're doing.
#endif
#define Deferral(MAX_DEFER_STATEMENTS) \
volatile unsigned char _num_deferrals = 0; \
jmp_buf _defer_return_loc = {0}, _deferrals[MAX_DEFER_STATEMENTS] = {0};
#define Defer(block) do { \
if (setjmp(_deferrals[_num_deferrals++])) { \
block; \
if (_num_deferrals) { \
longjmp(_deferrals[--_num_deferrals], 1); \
} else { \
longjmp(_defer_return_loc, 1); \
} \
} \
} while (0)
#define Return \
if (!setjmp(_defer_return_loc)) { \
if (_num_deferrals) longjmp(_deferrals[--_num_deferrals], 1); \
} else return
#endif /* __GNUC__ */

View File

@@ -2,6 +2,7 @@
#include "std.h"
#include "string/str.h"
#include "collections/List.h"
#include "defer.h"
typedef struct ErrorCallPos {
i32 line;
@@ -33,7 +34,7 @@ typedef struct Result_ {
i64 i;
f32 f;
f64 d;
void* v_ptr;
void* p;
};
} Result_;
@@ -41,21 +42,46 @@ typedef struct Result_ {
#define RESULT_ERROR(MSG, IS_MSG_ON_HEAP) (Result_){ .error = Error_create(MSG, IS_MSG_ON_HEAP, ErrorCallPos_here()) }
#define RESULT_VOID (Result_){ .error = NULL }
#define RESULT_ERROR_FMT(FORMAT, ARGS...) RESULT_ERROR(sprintf_malloc(4096, FORMAT ,##ARGS), true)
#define RESULT_ERROR_ERRNO() RESULT_ERROR(strerror(errno), false)
#define RESULT_VOID (Result_){ .error = NULL, .u = 0 }
#define RESULT_VALUE(FIELD, V) (Result_){ .error = NULL, .FIELD = V }
#define try(VAR, RSLT_CALL, DEFER_CODE) \
Result_ VAR = RSLT_CALL;\
if(VAR.error){\
Error_addCallPos(VAR.error, ErrorCallPos_here());\
DEFER_CODE;\
return VAR;\
};
#define _rname(N) __r_##N
#define try_fatal(VAR, RSLT_CALL, DEFER_CODE) \
Result_ VAR = RSLT_CALL;\
if(VAR.error){\
Error_addCallPos(VAR.error, ErrorCallPos_here());\
DEFER_CODE;\
Error_printAndExit(VAR.error);\
};
#define try(VAR, RESULT_FIELD, RSLT_CALL) _try(VAR, RESULT_FIELD, RSLT_CALL, __LINE__)
#define try_void(RSLT_CALL) _try_void(RSLT_CALL, __LINE__)
#define try_fatal(VAR, RESULT_FIELD, RSLT_CALL) _try_fatal(VAR, RESULT_FIELD, RSLT_CALL, __LINE__)
#define try_fatal_void(RSLT_CALL) _try_fatal_void(RSLT_CALL, __LINE__)
#define _try(VAR, RESULT_FIELD, RSLT_CALL, N) \
Result_ _rname(N) = RSLT_CALL;\
if(_rname(N).error){\
Error_addCallPos(_rname(N).error, ErrorCallPos_here());\
Return _rname(N);\
};\
VAR = _rname(N).RESULT_FIELD;
#define _try_void(RSLT_CALL, N) do {\
Result_ _rname(N) = RSLT_CALL;\
if(_rname(N).error){\
Error_addCallPos(_rname(N).error, ErrorCallPos_here());\
Return _rname(N);\
};\
} while(0)
#define _try_fatal(VAR, RESULT_FIELD, RSLT_CALL, N) \
Result_ _rname(N) = RSLT_CALL;\
if(_rname(N).error){\
Error_addCallPos(_rname(N).error, ErrorCallPos_here());\
Error_printAndExit(_rname(N).error);\
};\
VAR = _rname(N).RESULT_FIELD;
#define _try_fatal_void(RSLT_CALL, N) do {\
Result_ _rname(N) = RSLT_CALL;\
if(_rname(N).error){\
Error_addCallPos(_rname(N).error, ErrorCallPos_here());\
Error_printAndExit(_rname(N).error);\
};\
} while(0)

132
include/tlibc/filesystem.h Normal file
View File

@@ -0,0 +1,132 @@
#pragma once
#include <stdio.h>
#include "std.h"
#include "errors.h"
#include "string/str.h"
#include "collections/Array.h"
#if !defined(TLIBC_FS_USE_WINDOWS_H)
#if defined(_WIN64) || defined(_WIN32)
#define TLIBC_FS_USE_WINDOWS_H 1
#else
#define TLIBC_FS_USE_WINDOWS_H 0
#endif
#endif
//////////////////////////////////////////////////////////////////////////////
// PATH //
//////////////////////////////////////////////////////////////////////////////
#if defined(_WIN64) || defined(_WIN32)
#define path_sep '\\'
#define path_seps "\\"
#define path_notsep '/'
#define path_notseps "/"
#else
#define path_sep '/'
#define path_seps "/"
#define path_notsep '\\'
#define path_notseps "\\"
#endif
/// @brief removes part of path after path_sep (including path_sep)
/// @return pointer to a segment of path.data or "." if no path_sep has been found
str path_dirname(str path);
/// @brief removes part of path before path_sep (including path_sep)
/// @return pointer to a segment of path.data or path itself if no path_sep has been found
str path_basename(str path, bool remove_ext);
//////////////////////////////////////////////////////////////////////////////
// FILE //
//////////////////////////////////////////////////////////////////////////////
/// open file for reading
#define FO_ReadExisting "rb"
/// (re)create file for writing
#define FO_WriteNew "wb"
/// open or create file for writing data to the end
#define FO_AppendOrCreate "ab"
/// open file for reading and writing
#define FO_ReadWriteExisting "rb+"
/// (re)create file for reading/writing
#define FO_ReadWriteNew "wb+"
/// open or create file for readng and writing data to the end
#define FO_ReadAppend "ab+"
Result(FILE*) file_open(cstr file_name, cstr fopen_mode);
/// if file exists, opens it with "rb+", else creates it with "wb+"
Result(FILE*) file_openOrCreateReadWrite(cstr file_name);
bool file_exists(cstr path);
Result(i64) file_getSize(FILE* f);
typedef enum SeekOrigin {
SeekOrigin_Start = SEEK_SET,
SeekOrigin_Current = SEEK_CUR,
SeekOrigin_End = SEEK_END,
} SeekOrigin;
Result(void) file_seek(FILE* f, i64 offset, SeekOrigin origin);
Result(void) file_writeByte(FILE* f, u8 b);
Result(void) file_writeStructs(FILE* f, const void* src, u64 struct_size, u64 count);
static inline Result(void) file_writeBytes(FILE* f, const void* src, u64 size){
return file_writeStructs(f, src, size, 1);
}
static inline Result(void) file_writeBytesArray(FILE* f, Array(u8) src){
return file_writeStructs(f, src.data, src.size, 1);
}
/// @param out_byte is not set on error or end of file
/// @return true if byte was read, false if end of file was reached
Result(bool) file_readByte(FILE* f, u8* out_byte);
/// @param max_count maximum number of structs to read
/// @return number of structs that were read (<=max_count)
Result(u64) file_readStructs(FILE* f, void* dst, u64 struct_size, u64 max_count);
/// @param max_count maximum number of bytes to read
/// @return number of bytes that were read (<=max_count)
static inline Result(u64) file_readBytes(FILE* f, void* dst, u64 max_count){
return file_readStructs(f, dst, 1, max_count);
}
/// @param dst array where .size is the maximum number of bytes to read
/// @return number of bytes that were read (<=max_count)
static inline Result(u64) file_readBytesArray(FILE* f, Array(u8) dst){
return file_readStructs(f, dst.data, 1, dst.size);
}
/// @param max_count exact number of structs to read
Result(void) file_readStructsExactly(FILE* f, void* dst, u64 struct_size, u64 exact_count);
/// @param exact_count exact number of bytes to read
static inline Result(void) file_readBytesExactly(FILE* f, void* dst, u64 exact_count){
return file_readStructsExactly(f, dst, 1, exact_count);
}
/// @param dst array where .size is the exact number of bytes to read
static inline Result(void) file_readBytesArrayExactly(FILE* f, Array(u8) dst){
return file_readStructsExactly(f, dst.data, 1, dst.size);
}
//////////////////////////////////////////////////////////////////////////////
// DIRECTORY //
//////////////////////////////////////////////////////////////////////////////
bool dir_exists(cstr path);
/// @brief creates directories specified in path recursively
/// @return false if directory was present already, true if it has been created
Result(bool) dir_create(cstr path);

View File

@@ -10,6 +10,7 @@ extern "C" {
#include <stddef.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
typedef int8_t i8;
typedef uint8_t u8;

View File

@@ -16,6 +16,10 @@ typedef struct str {
#define str_construct(DATA, LEN, ZERO_TERMINATED) ((str){ .data = DATA, .size = LEN, .isZeroTerminated = ZERO_TERMINATED })
static inline str str_from_cstr(cstr s_ptr){
return str_construct((void*)s_ptr, strlen(s_ptr), true);
}
static const str str_null = str_construct(NULL, 0, 0);
/// copies src content to new string and adds \0 at the end

View File

@@ -6,8 +6,8 @@ CMP_C="gcc"
CMP_CPP="g++"
STD_C="c99"
STD_CPP="c++11"
WARN_C="-Wall -Wextra"
WARN_CPP="-Wall -Wextra"
WARN_C="-Wall -Wextra -Werror=return-type -Werror=pointer-arith"
WARN_CPP="-Wall -Wextra -Werror=return-type -Werror=pointer-arith"
SRC_C="$(find src -name '*.c')"
SRC_CPP="$(find src -name '*.cpp')"

44
src/filesystem/dir.c Normal file
View File

@@ -0,0 +1,44 @@
#include "internal.h"
bool dir_exists(cstr path){
if(path[0]=='.'){
if(path[1]==0 || (path[1]==path_sep && path[1]==0))
return true; // dir . or ./ always exists
// else if(path[1]=='.' && path[2]==path_sep)
//TODO path_resolve because windows doesnt recognize .\ pattern
}
#if TLIBC_FS_USE_WINDOWS_H
DWORD dwAttrib = GetFileAttributes(path);
return (bool)(
(dwAttrib != INVALID_FILE_ATTRIBUTES) && // file exists
(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); // file is a directory
#else
struct stat stats;
i32 rez=stat(path, &stats);
return (bool)(
(rez!=-1) && // file exists
(S_ISDIR(stats.st_mode))); // file is a directory
#endif
}
Result(bool) dir_create(cstr path){
Deferral(4);
if (dir_exists(path)){
Return RESULT_VALUE(i, false);
}
char* parentDir= str_copy(path_dirname(str_from_cstr((void*)path))).data;
Defer(free(parentDir));
try_void(dir_create(parentDir));
#if TLIBC_FS_USE_WINDOWS_H
if(!CreateDirectory(path, NULL))
#else
if(mkdir(path, 0777) == -1)
#endif
{
Return RESULT_ERROR_FMT("Can't create dicectory '%s': %s", path, strerror(errno));
}
Return RESULT_VALUE(i, true);
}

114
src/filesystem/file.c Normal file
View File

@@ -0,0 +1,114 @@
#include "internal.h"
bool file_exists(cstr path){
if(path[0]=='.'){
if(path[1]==0 || (path[1]==path_sep && path[2]==0))
return false; // . or ./ is not a file
// else if(path[1]=='.' && path[2]==path_sep)
//TODO path_resolve because windows doesnt recognize .\ pattern
}
#if TLIBC_FS_USE_WINDOWS_H
DWORD dwAttrib = GetFileAttributes(path);
return (bool)(
(dwAttrib != INVALID_FILE_ATTRIBUTES) && // file exists
!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); // file is not directory
#else
struct stat stats;
i32 rez=stat(path, &stats);
return (bool)(
(rez!=-1) && // file exists
!(S_ISDIR(stats.st_mode))); // file is not directory
#endif
}
Result(FILE*) file_open(cstr file_name, cstr fopen_mode){
FILE* f = fopen(file_name, fopen_mode);
if(f == NULL){
return RESULT_ERROR_FMT(
"Can't open (%s) file '%s': %s",
fopen_mode, file_name, strerror(errno));
}
return RESULT_VALUE(p, f);
}
Result(FILE*) file_openOrCreateReadWrite(cstr file_name){
FILE* f = fopen(file_name, "rb+");
if(f == NULL){
f = fopen(file_name, "wb+");
if(f == NULL){
return RESULT_ERROR_FMT(
"Can't create (%s) file: %s",
file_name, strerror(errno));
}
}
return RESULT_VALUE(p, f);
}
Result(i64) file_getSize(FILE* f){
i64 r = IFWIN(_ftelli64, ftello64)(f);
if(r < 0){
return RESULT_ERROR_ERRNO();
}
return RESULT_VALUE(i, r);
}
Result(void) file_seek(FILE* f, i64 offset, SeekOrigin origin){
if(IFWIN(_fseeki64, fseeko64)(f, offset, (int)origin) != 0){
return RESULT_ERROR_FMT(
"Can't seek (offset: " IFWIN("%lli", "%li") ", origin: %i) in file: %s",
offset, origin, strerror(errno));
}
return RESULT_VOID;
}
Result(void) file_writeStructs(FILE* f, const void* src, u64 struct_size, u64 count){
u64 r = fwrite(src, struct_size, count, f);
if(r != count){
return RESULT_ERROR_ERRNO();
}
return RESULT_VOID;
}
Result(void) file_writeByte(FILE* f, u8 b){
if(fputc(b, f) != 0){
return RESULT_ERROR_ERRNO();
}
return RESULT_VOID;
}
Result(bool) file_readByte(FILE* f, u8* out_byte){
int r = fgetc(f);
if(ferror(f)){
return RESULT_ERROR_ERRNO();
}
if(feof(f)){
return RESULT_VALUE(u, false);
}
*out_byte = r;
return RESULT_VALUE(u, true);
}
Result(u64) file_readStructs(FILE* f, void* dst, u64 struct_size, u64 max_count){
u64 r = fread(dst, struct_size, max_count, f);
if(ferror(f)){
return RESULT_ERROR_ERRNO();
}
return RESULT_VALUE(u, r);
}
Result(void) file_readStructsExactly(FILE* f, void* dst, u64 struct_size, u64 exact_count){
u64 r = fread(dst, struct_size, exact_count, f);
if(ferror(f)){
return RESULT_ERROR_ERRNO();
}
if(r != exact_count){
return RESULT_ERROR_FMT(
"read " IFWIN("%llu", "%lu") " structures out of " IFWIN("%llu", "%lu"),
r, exact_count);
}
return RESULT_VOID;
}

11
src/filesystem/internal.h Normal file
View File

@@ -0,0 +1,11 @@
#pragma once
#define _LARGEFILE64_SOURCE 1
#include "tlibc/filesystem.h"
#if TLIBC_FS_USE_WINDOWS_H
#include <windows.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif

45
src/filesystem/path.c Normal file
View File

@@ -0,0 +1,45 @@
#include "tlibc/filesystem.h"
str path_dirname(str path){
if(path.size == 0)
return path;
// remove trailing slash (name/)
if(path.data[path.size - 1] == path_sep){
path.size -= 1;
}
i32 sepIndex = str_seekCharReverse(path, path_sep, -1);
if(sepIndex < 0)
return STR(".");
path.size = sepIndex;
path.isZeroTerminated = false;
return path;
}
str path_basename(str path, bool remove_ext){
if(path.size == 0)
return path;
// remove trailing slash (name/)
if(path.data[path.size - 1] == path_sep){
path.size -= 1;
}
i32 nameIndex = str_seekCharReverse(path, path_sep, -1) + 1;
if((u32)nameIndex != 0){
path.data += nameIndex;
path.size -= nameIndex;
}
if(!remove_ext)
return path;
i32 extIndex = str_seekCharReverse(path, '.', -1);
if(extIndex > 0){
path.size = extIndex;
path.isZeroTerminated = false;
}
return path;
}

View File

@@ -14,12 +14,14 @@ str str_copy(str src){
bool str_equals(str s0, str s1){
if(s0.size != s1.size)
return false;
for(u32 i = 0; i < s0.size; i++)
if(s0.data[i] != s1.data[i])
return false;
return true;
/*
BENCHMARK:
str_equals64: 2.967s
strcmp: 4.143s
strncmp: 1.611s
memcmp: 0.710s
*/
return memcmp(s0.data, s1.data, s0.size) == 0;
}
str str_reverse(str s){