Compare commits

...

7 Commits

Author SHA1 Message Date
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
7 changed files with 336 additions and 2 deletions

View File

@@ -33,7 +33,7 @@ typedef struct Result_ {
i64 i; i64 i;
f32 f; f32 f;
f64 d; f64 d;
void* v_ptr; void* p;
}; };
} Result_; } Result_;
@@ -41,7 +41,9 @@ typedef struct Result_ {
#define RESULT_ERROR(MSG, IS_MSG_ON_HEAP) (Result_){ .error = Error_create(MSG, IS_MSG_ON_HEAP, ErrorCallPos_here()) } #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 RESULT_VALUE(FIELD, V) (Result_){ .error = NULL, .FIELD = V }
#define try(VAR, RSLT_CALL, DEFER_CODE) \ #define try(VAR, RSLT_CALL, DEFER_CODE) \
@@ -52,6 +54,8 @@ typedef struct Result_ {
return VAR;\ return VAR;\
}; };
#define try_void(RSLT_CALL, DEFER_CODE) { try(__result_void, RSLT_CALL, DEFER_CODE) };
#define try_fatal(VAR, RSLT_CALL, DEFER_CODE) \ #define try_fatal(VAR, RSLT_CALL, DEFER_CODE) \
Result_ VAR = RSLT_CALL;\ Result_ VAR = RSLT_CALL;\
if(VAR.error){\ if(VAR.error){\
@@ -59,3 +63,5 @@ typedef struct Result_ {
DEFER_CODE;\ DEFER_CODE;\
Error_printAndExit(VAR.error);\ Error_printAndExit(VAR.error);\
}; };
#define try_fatal_void(RSLT_CALL, DEFER_CODE) { try_fatal(__result_void, RSLT_CALL, DEFER_CODE) };

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

@@ -0,0 +1,128 @@
#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
/// @return str with data = path.data
str path_dirname(str path);
/// @brief removes part of path before path_sep
/// @return str with data = path.data
str path_basename(str path, bool remove_ext);
//////////////////////////////////////////////////////////////////////////////
// FILE //
//////////////////////////////////////////////////////////////////////////////
/// open a file for reading
#define FO_Read "rb"
/// (re)create a file for writing
#define FO_Write "wb"
/// opens file for writing additional data to the end / creates new file
#define FO_Append "ab"
/// (re)creates file for reading/writing
#define FO_ReadWrite "w+b"
/// opens file for readng/writing additional data to the end / creates new file
#define FO_ReadAppend "a+b"
Result(FILE*) file_open(cstr file_name, cstr fopen_mode);
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

@@ -16,6 +16,10 @@ typedef struct str {
#define str_construct(DATA, LEN, ZERO_TERMINATED) ((str){ .data = DATA, .size = LEN, .isZeroTerminated = ZERO_TERMINATED }) #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); static const str str_null = str_construct(NULL, 0, 0);
/// copies src content to new string and adds \0 at the end /// copies src content to new string and adds \0 at the end

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

@@ -0,0 +1,42 @@
#include "tlibc/filesystem.h"
#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){
if (dir_exists(path))
return RESULT_VALUE(i, false);
char* parentDir= str_copy(path_dirname(str_from_cstr((void*)path))).data;
try_void(dir_create(parentDir), free(parentDir));
free(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);
}

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

@@ -0,0 +1,99 @@
#include "tlibc/filesystem.h"
#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(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;
}

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

@@ -0,0 +1,10 @@
#pragma once
#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 path;
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;
}