file_openOrCreateReadWrite

This commit is contained in:
Timerix 2025-08-08 21:38:34 +03:00
parent 223406d4e4
commit fe9e44a660
5 changed files with 38 additions and 19 deletions

View File

@ -30,12 +30,12 @@
#define path_notseps "\\" #define path_notseps "\\"
#endif #endif
/// @brief removes part of path after path_sep /// @brief removes part of path after path_sep (including path_sep)
/// @return str with data = path.data /// @return pointer to a segment of path.data or "." if no path_sep has been found
str path_dirname(str path); str path_dirname(str path);
/// @brief removes part of path before path_sep /// @brief removes part of path before path_sep (including path_sep)
/// @return str with data = path.data /// @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); str path_basename(str path, bool remove_ext);
@ -43,19 +43,23 @@ str path_basename(str path, bool remove_ext);
// FILE // // FILE //
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
/// open a file for reading /// open file for reading
#define FO_Read "rb" #define FO_ReadExisting "rb"
/// (re)create a file for writing /// (re)create file for writing
#define FO_Write "wb" #define FO_WriteNew "wb"
/// opens file for writing additional data to the end / creates new file /// open or create file for writing data to the end
#define FO_Append "ab" #define FO_AppendOrCreate "ab"
/// (re)creates file for reading/writing /// open file for reading and writing
#define FO_ReadWrite "w+b" #define FO_ReadWriteExisting "rb+"
/// opens file for readng/writing additional data to the end / creates new file /// (re)create file for reading/writing
#define FO_ReadAppend "a+b" #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); 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); bool file_exists(cstr path);
Result(i64) file_getSize(FILE* f); Result(i64) file_getSize(FILE* f);

View File

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

View File

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

View File

@ -26,8 +26,22 @@ bool file_exists(cstr path){
Result(FILE*) file_open(cstr file_name, cstr fopen_mode){ Result(FILE*) file_open(cstr file_name, cstr fopen_mode){
FILE* f = fopen(file_name, fopen_mode); FILE* f = fopen(file_name, fopen_mode);
if(f == NULL){ if(f == NULL){
return RESULT_ERROR_FMT("Can't open (%s) file '%s': %s", return RESULT_ERROR_FMT(
fopen_mode, file_name, strerror(errno)); "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); return RESULT_VALUE(p, f);
} }

View File

@ -11,7 +11,7 @@ str path_dirname(str path){
i32 sepIndex = str_seekCharReverse(path, path_sep, -1); i32 sepIndex = str_seekCharReverse(path, path_sep, -1);
if(sepIndex < 0) if(sepIndex < 0)
return path; return STR(".");
path.size = sepIndex; path.size = sepIndex;
path.isZeroTerminated = false; path.isZeroTerminated = false;