From fe9e44a660e23c28255ba27522d15ab94044f55b Mon Sep 17 00:00:00 2001 From: Timerix Date: Fri, 8 Aug 2025 21:38:34 +0300 Subject: [PATCH] file_openOrCreateReadWrite --- include/tlibc/filesystem.h | 32 ++++++++++++++++++-------------- include/tlibc/std.h | 1 + project.config | 4 ++-- src/filesystem/file.c | 18 ++++++++++++++++-- src/filesystem/path.c | 2 +- 5 files changed, 38 insertions(+), 19 deletions(-) diff --git a/include/tlibc/filesystem.h b/include/tlibc/filesystem.h index 3a0c14c..e14d9c7 100644 --- a/include/tlibc/filesystem.h +++ b/include/tlibc/filesystem.h @@ -30,12 +30,12 @@ #define path_notseps "\\" #endif -/// @brief removes part of path after path_sep -/// @return str with data = path.data +/// @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 -/// @return str with data = path.data +/// @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); @@ -43,19 +43,23 @@ 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" +/// 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); diff --git a/include/tlibc/std.h b/include/tlibc/std.h index 6b930b6..5d7c09f 100755 --- a/include/tlibc/std.h +++ b/include/tlibc/std.h @@ -10,6 +10,7 @@ extern "C" { #include #include #include +#include typedef int8_t i8; typedef uint8_t u8; diff --git a/project.config b/project.config index 7ec4191..dc4170b 100644 --- a/project.config +++ b/project.config @@ -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')" diff --git a/src/filesystem/file.c b/src/filesystem/file.c index 313de5a..e1d2281 100644 --- a/src/filesystem/file.c +++ b/src/filesystem/file.c @@ -26,8 +26,22 @@ bool file_exists(cstr path){ 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_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); } diff --git a/src/filesystem/path.c b/src/filesystem/path.c index 9691111..ebd90f4 100644 --- a/src/filesystem/path.c +++ b/src/filesystem/path.c @@ -11,7 +11,7 @@ str path_dirname(str path){ i32 sepIndex = str_seekCharReverse(path, path_sep, -1); if(sepIndex < 0) - return path; + return STR("."); path.size = sepIndex; path.isZeroTerminated = false;