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 "\\"
#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);

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

@ -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')"

View File

@ -26,12 +26,26 @@ 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",
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){

View File

@ -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;