Compare commits
3 Commits
6d959fe8f5
...
e1dc972b24
| Author | SHA1 | Date | |
|---|---|---|---|
| e1dc972b24 | |||
| ab55f85160 | |||
| e0f1941c82 |
@ -41,7 +41,8 @@ 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_ERROR_FMT(FORMAT, ARGS...) RESULT_ERROR(sprintf_malloc(4096, FORMAT, ARGS), true)
|
#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 }
|
#define RESULT_VOID (Result_){ .error = NULL }
|
||||||
#define RESULT_VALUE(FIELD, V) (Result_){ .error = NULL, .FIELD = V }
|
#define RESULT_VALUE(FIELD, V) (Result_){ .error = NULL, .FIELD = V }
|
||||||
|
|
||||||
@ -53,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){\
|
||||||
@ -60,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) };
|
||||||
|
|||||||
@ -1,17 +1,24 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <stdio.h>
|
||||||
#include "std.h"
|
#include "std.h"
|
||||||
#include "errors.h"
|
#include "errors.h"
|
||||||
#include "string/str.h"
|
#include "string/str.h"
|
||||||
|
#include "collections/Array.h"
|
||||||
|
|
||||||
#if !defined(KFS_USE_WINDOWS_H)
|
#if !defined(TLIBC_FS_USE_WINDOWS_H)
|
||||||
#if defined(_WIN64) || defined(_WIN32)
|
#if defined(_WIN64) || defined(_WIN32)
|
||||||
#define KFS_USE_WINDOWS_H 1
|
#define TLIBC_FS_USE_WINDOWS_H 1
|
||||||
#else
|
#else
|
||||||
#define KFS_USE_WINDOWS_H 0
|
#define TLIBC_FS_USE_WINDOWS_H 0
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if KFS_USE_WINDOWS_H
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
// PATH //
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#if defined(_WIN64) || defined(_WIN32)
|
||||||
#define path_sep '\\'
|
#define path_sep '\\'
|
||||||
#define path_seps "\\"
|
#define path_seps "\\"
|
||||||
#define path_notsep '/'
|
#define path_notsep '/'
|
||||||
@ -32,6 +39,10 @@ str path_dirname(str path);
|
|||||||
str path_basename(str path, bool remove_ext);
|
str path_basename(str path, bool remove_ext);
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
// FILE //
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
/// open a file for reading
|
/// open a file for reading
|
||||||
#define FO_Read "rb"
|
#define FO_Read "rb"
|
||||||
/// (re)create a file for writing
|
/// (re)create a file for writing
|
||||||
@ -45,10 +56,11 @@ str path_basename(str path, bool remove_ext);
|
|||||||
|
|
||||||
Result(FILE*) file_open(cstr file_name, cstr fopen_mode);
|
Result(FILE*) file_open(cstr file_name, cstr fopen_mode);
|
||||||
|
|
||||||
bool file_exists(cstr path);
|
|
||||||
|
|
||||||
|
bool file_exists(cstr path);
|
||||||
Result(i64) file_getSize(FILE* f);
|
Result(i64) file_getSize(FILE* f);
|
||||||
|
|
||||||
|
|
||||||
typedef enum SeekOrigin {
|
typedef enum SeekOrigin {
|
||||||
SeekOrigin_Start = SEEK_SET,
|
SeekOrigin_Start = SEEK_SET,
|
||||||
SeekOrigin_Current = SEEK_CUR,
|
SeekOrigin_Current = SEEK_CUR,
|
||||||
@ -58,6 +70,58 @@ typedef enum SeekOrigin {
|
|||||||
Result(void) file_seek(FILE* f, i64 offset, SeekOrigin origin);
|
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);
|
bool dir_exists(cstr path);
|
||||||
|
|
||||||
|
/// @brief creates directories specified in path recursively if they don't exist
|
||||||
Result(void) dir_create(cstr path);
|
Result(void) dir_create(cstr path);
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
#include "tlibc/filesystem.h"
|
#include "tlibc/filesystem.h"
|
||||||
#include "io_includes.h"
|
#include "internal.h"
|
||||||
|
|
||||||
bool dir_exists(cstr path){
|
bool dir_exists(cstr path){
|
||||||
if(path[0]=='.'){
|
if(path[0]=='.'){
|
||||||
@ -8,7 +8,7 @@ bool dir_exists(cstr path){
|
|||||||
// else if(path[1]=='.' && path[2]==path_sep)
|
// else if(path[1]=='.' && path[2]==path_sep)
|
||||||
//TODO path_resolve because windows doesnt recognize .\ pattern
|
//TODO path_resolve because windows doesnt recognize .\ pattern
|
||||||
}
|
}
|
||||||
#if KFS_USE_WINDOWS_H
|
#if TLIBC_FS_USE_WINDOWS_H
|
||||||
DWORD dwAttrib = GetFileAttributes(path);
|
DWORD dwAttrib = GetFileAttributes(path);
|
||||||
return (bool)(
|
return (bool)(
|
||||||
(dwAttrib != INVALID_FILE_ATTRIBUTES) && // file exists
|
(dwAttrib != INVALID_FILE_ATTRIBUTES) && // file exists
|
||||||
@ -27,9 +27,9 @@ Result(void) dir_create(cstr path){
|
|||||||
return RESULT_VOID;
|
return RESULT_VOID;
|
||||||
|
|
||||||
char* parentDir= str_copy(path_dirname(str_from_cstr((void*)path))).data;
|
char* parentDir= str_copy(path_dirname(str_from_cstr((void*)path))).data;
|
||||||
try(_1, dir_create(parentDir), free(parentDir));
|
try_void(dir_create(parentDir), free(parentDir));
|
||||||
free(parentDir);
|
free(parentDir);
|
||||||
#if KFS_USE_WINDOWS_H
|
#if TLIBC_FS_USE_WINDOWS_H
|
||||||
if(!CreateDirectory(path, NULL))
|
if(!CreateDirectory(path, NULL))
|
||||||
#else
|
#else
|
||||||
if(mkdir(path, 0777) == -1)
|
if(mkdir(path, 0777) == -1)
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
#include "tlibc/filesystem.h"
|
#include "tlibc/filesystem.h"
|
||||||
#include "io_includes.h"
|
#include "internal.h"
|
||||||
|
|
||||||
bool file_exists(cstr path){
|
bool file_exists(cstr path){
|
||||||
if(path[0]=='.'){
|
if(path[0]=='.'){
|
||||||
@ -9,7 +9,7 @@ bool file_exists(cstr path){
|
|||||||
//TODO path_resolve because windows doesnt recognize .\ pattern
|
//TODO path_resolve because windows doesnt recognize .\ pattern
|
||||||
}
|
}
|
||||||
|
|
||||||
#if KFS_USE_WINDOWS_H
|
#if TLIBC_FS_USE_WINDOWS_H
|
||||||
DWORD dwAttrib = GetFileAttributes(path);
|
DWORD dwAttrib = GetFileAttributes(path);
|
||||||
return (bool)(
|
return (bool)(
|
||||||
(dwAttrib != INVALID_FILE_ATTRIBUTES) && // file exists
|
(dwAttrib != INVALID_FILE_ATTRIBUTES) && // file exists
|
||||||
@ -35,7 +35,7 @@ Result(FILE*) file_open(cstr file_name, cstr fopen_mode){
|
|||||||
Result(i64) file_getSize(FILE* f){
|
Result(i64) file_getSize(FILE* f){
|
||||||
i64 r = IFWIN(_ftelli64, ftello64)(f);
|
i64 r = IFWIN(_ftelli64, ftello64)(f);
|
||||||
if(r < 0){
|
if(r < 0){
|
||||||
return RESULT_ERROR(strerror(errno), false);
|
return RESULT_ERROR_ERRNO();
|
||||||
}
|
}
|
||||||
return RESULT_VALUE(i, r);
|
return RESULT_VALUE(i, r);
|
||||||
}
|
}
|
||||||
@ -46,4 +46,54 @@ Result(void) file_seek(FILE* f, i64 offset, SeekOrigin origin){
|
|||||||
"Can't seek (offset: " IFWIN("%lli", "%li") ", origin: %i) in file: %s",
|
"Can't seek (offset: " IFWIN("%lli", "%li") ", origin: %i) in file: %s",
|
||||||
offset, origin, strerror(errno));
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#if KFS_USE_WINDOWS_H
|
#include "tlibc/filesystem.h"
|
||||||
|
#if TLIBC_FS_USE_WINDOWS_H
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#else
|
#else
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
@ -1,8 +1,16 @@
|
|||||||
#include "tlibc/filesystem.h"
|
#include "tlibc/filesystem.h"
|
||||||
|
|
||||||
str path_dirname(str path){
|
str path_dirname(str path){
|
||||||
i32 sepIndex = str_seekCharReverse(path, path_sep, 0);
|
if(path.size == 0)
|
||||||
if(sepIndex <= 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;
|
return path;
|
||||||
|
|
||||||
path.size = sepIndex;
|
path.size = sepIndex;
|
||||||
@ -11,16 +19,24 @@ str path_dirname(str path){
|
|||||||
}
|
}
|
||||||
|
|
||||||
str path_basename(str path, bool remove_ext){
|
str path_basename(str path, bool remove_ext){
|
||||||
i32 nameIndex = str_seekCharReverse(path, path_sep, 0) + 1;
|
if(path.size == 0)
|
||||||
if(nameIndex == path.size)
|
|
||||||
return path;
|
return path;
|
||||||
|
|
||||||
path.data += nameIndex;
|
// remove trailing slash (name/)
|
||||||
path.size -= nameIndex;
|
if(path.data[path.size - 1] == path_sep){
|
||||||
if(remove_ext)
|
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;
|
return path;
|
||||||
|
|
||||||
i32 extIndex = str_seekCharReverse(path, '.', -1);
|
i32 extIndex = str_seekCharReverse(path, '.', -1);
|
||||||
if(extIndex > 0){
|
if(extIndex > 0){
|
||||||
path.size = extIndex;
|
path.size = extIndex;
|
||||||
path.isZeroTerminated = false;
|
path.isZeroTerminated = false;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user