refactoring

This commit is contained in:
2023-06-08 15:15:38 +06:00
parent a983df1ac6
commit c5585bbb0c
88 changed files with 922 additions and 1071 deletions

View File

@@ -4,7 +4,7 @@
bool dir_exists(const char* path){
if(path[0]=='.'){
if(path[1]==0 || (path[1]==path_sep && path[1]==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
@@ -26,9 +26,10 @@ bool dir_exists(const char* path){
Maybe dir_create(const char* path){
if (dir_exists(path))
return MaybeNull;
char* parentDir=path_parentDir(path);
LinearAllocator _al; LinearAllocator_construct(&_al, 128);
allocator_ptr al=&_al.base;
char* parentDir=path_parentDir(al, path);
dir_create(parentDir);
free(parentDir);
#if KFS_USE_WINDOWS_H
if(!CreateDirectory(path, NULL))
#else
@@ -36,12 +37,10 @@ Maybe dir_create(const char* path){
#endif
{
char err[512];
IFWIN(
sprintf_s(err, 512, "can't create dicectory <%s>", path),
sprintf(err, "can't create dicectory <%s>", path));
sprintf_s(err, sizeof(err), "can't create dicectory <%s>", path);
safethrow(err,;);
}
LinearAllocator_destruct(&_al);
return MaybeNull;
}

View File

@@ -25,4 +25,4 @@ Maybe dir_findDirs(const char* path, char* searchPattern, bool recursive);
#if __cplusplus
}
#endif
#endif

View File

@@ -2,9 +2,9 @@
#include "../String/StringBuilder.h"
#include "io_includes.h"
void __file_freeMembers(void* _f){ fclose((FileHandle)_f); }
void __file_destructMembers(void* _f){ fclose((FileHandle)_f); }
kt_define(FileHandle, __file_freeMembers, NULL)
kt_define(FileHandle, __file_destructMembers, NULL)
bool file_exists(const char* path){
if(path[0]=='.'){
@@ -50,8 +50,11 @@ char* FileOpenMode_toStr(FileOpenMode m){
Maybe file_open(const char* path, FileOpenMode mode){
FileHandle file=fopen(path, FileOpenMode_toStr(mode));
LinearAllocator _al; LinearAllocator_construct(&_al, 128);
allocator_ptr al=&_al.base;
if(!file)
safethrow(cptr_concat("can't open file ", (char*)path),;);
safethrow(cptr_concat(al, "can't open file ", (char*)path),;);
LinearAllocator_destruct(&_al);
return SUCCESS(UniHeapPtr(FileHandle,file));
}
@@ -117,8 +120,8 @@ Maybe file_readAll(FileHandle file, char** allBytes){
while(true){
rezult=fgetc(file);
if(rezult==EOF){
if(ferror(file))
safethrow(ERR_IO, StringBuilder_free(sb));
if(ferror(file))
safethrow(ERR_IO, StringBuilder_destruct(sb));
break;
}
buffer[i%sizeof(buffer)]=(char)rezult;
@@ -132,7 +135,7 @@ Maybe file_readAll(FileHandle file, char** allBytes){
string str=StringBuilder_build(sb);
*allBytes=str.ptr;
// i dont know how can it happen, but if it will have, it will be very dangerous
if(i!=str.length)
if(i!=str.length)
throw(ERR_UNEXPECTEDVAL);
return SUCCESS(UniUInt64(i));
}

View File

@@ -5,7 +5,6 @@ extern "C" {
#endif
#include "../base/base.h"
#include "../Array/Array.h"
#include "../String/string.h"
typedef FILE* FileHandle;
@@ -17,15 +16,15 @@ bool file_exists(const char* path);
Maybe file_delete(const char* path, bool recursive);
PACKED_ENUM(FileOpenMode,
// open a file for reading
FileOpenMode_Read=1,
// open a file for reading
FileOpenMode_Read=1,
// (re)create a file for writing
FileOpenMode_Write=2,
// opens file for writing additional data to the end / creates new file
FileOpenMode_Write=2,
// opens file for writing additional data to the end / creates new file
FileOpenMode_Append=4,
// (re)creates file for reading/writing
FileOpenMode_ReadWrite=FileOpenMode_Read|FileOpenMode_Write,
// opens file for readng/writing additional data to the end / creates new file
FileOpenMode_ReadWrite=FileOpenMode_Read|FileOpenMode_Write,
// opens file for readng/writing additional data to the end / creates new file
FileOpenMode_ReadAppend=FileOpenMode_Read|FileOpenMode_Append
)
@@ -67,10 +66,10 @@ Maybe file_readChar(FileHandle file);
Maybe file_readBuffer(FileHandle file, char* buffer, u64 length);
/// @brief reads all bytes from file
/// @param allBytes ptr to the file's content will be pushed there
/// @param allBytes ptr to the file's content will be pushed there
/// @return Maybe<u64> total number of successfully read bytes
Maybe file_readAll(FileHandle file, char** allBytes);
#if __cplusplus
}
#endif
#endif

View File

@@ -2,7 +2,7 @@
#if defined(_WIN64) || defined(_WIN32)
#define KFS_USE_WINDOWS_H 1
#else
#else
#define KFS_USE_WINDOWS_H 0
#endif

View File

@@ -1,8 +1,8 @@
#include "filesystem.h"
char* __path_concat(u32 n, ...){
char** parts=(char**)malloc(n*sizeof(char*));
u32* lengths=malloc(n*sizeof(u32));
char* __path_concat(allocator_ptr al, u32 n, ...){
char** parts=(char**)allocator_alloc(al, n*sizeof(char*));
u32* lengths=allocator_alloc(al, n*sizeof(u32));
u32 totalLength=0;
// reading args from va_list
@@ -18,7 +18,7 @@ char* __path_concat(u32 n, ...){
va_end(vl);
// allocating memory for output value
char* totality=malloc(totalLength+1);
char* totality=allocator_alloc(al, totalLength+1);
const char* output=totality;
totality[totalLength]=0;
@@ -32,13 +32,13 @@ char* __path_concat(u32 n, ...){
}
memcpy(totality, parts[k], lengths[k]);
free(parts);
free(lengths);
allocator_free(al, lengths);
allocator_free(al, parts);
return output;
}
char* path_fixSeparators(const char* path){
char* pathCopy=cptr_copy(path);
char* path_fixSeparators(allocator_ptr al, const char* path){
char* pathCopy=cptr_copy(al, path);
char c;
while((c=*pathCopy)){
if(c==path_notSep)
@@ -49,13 +49,16 @@ char* path_fixSeparators(const char* path){
}
Maybe path_throwIfEscapes(const char* path){
LinearAllocator _al; LinearAllocator_construct(&_al, 128);
allocator_ptr al=&_al.base;
if(cptr_contains(path,".."))
safethrow(cptr_concat("path <",path,"> uses <..>, that's not allowed"),);
safethrow(cptr_concat(al, "path <",path,"> uses <..>, that's not allowed"),);
LinearAllocator_destruct(&_al);
return MaybeNull;
}
char* path_parentDir(char* dir){
char* copy=cptr_copy(dir);
char* path_parentDir(allocator_ptr al, char* dir){
char* copy=cptr_copy(al, dir);
i32 length=cptr_length(copy);
i32 i=cptr_lastIndexOfChar(copy,path_sep);
if(i!=-1 && i==length-1){
@@ -63,8 +66,8 @@ char* path_parentDir(char* dir){
i=cptr_lastIndexOfChar(copy,path_sep);
}
if(i==-1){
free(copy);
copy=malloc(2);
allocator_free(al, copy);
copy=allocator_alloc(al, 2);
copy[0]='.';
copy[1]=0;
}
@@ -72,7 +75,7 @@ char* path_parentDir(char* dir){
}
char* path_basename(char* path, bool with_extension){
char* path_basename(allocator_ptr al, char* path, bool with_extension){
i32 nameIndex=cptr_lastIndexOfChar(path, path_sep)+1;
string rezult=string_fromCptr(path+nameIndex);
if(!with_extension){

View File

@@ -14,17 +14,17 @@ static const char path_sep='/';
static const char path_notSep='\\';
#endif
char* __path_concat(u32 n, ...);
char* __path_concat(allocator_ptr al, u32 n, ...);
/// @brief merges path parts together and puts <path_sep> between them
/// @return new cstr
#define path_concat(PATH_PARTS...) __path_concat(count_args(PATH_PARTS), PATH_PARTS)
#define path_concat(ALLOCATOR, PATH_PARTS...) __path_concat(ALLOCATOR, count_args(PATH_PARTS), PATH_PARTS)
/// @brief fixes path separators
/// @param cstr where can be <path_notSep>
/// @return new cstr with correct separators
char* path_fixSeparators(const char* path);
/// @return new cstr with correct separators
char* path_fixSeparators(allocator_ptr al, const char* path);
#define path_resolve(PATH_PARTS...) path_fixSeparators(path_concat(PATH_PARTS))
#define path_resolve(ALLOCATOR, PATH_PARTS...) path_fixSeparators(ALLOCATOR, path_concat(ALLOCATOR, PATH_PARTS))
/// @brief calls safethrow() if finds escape sequense in path
/// @param path cstr where can be <..>
@@ -32,11 +32,11 @@ char* path_fixSeparators(const char* path);
Maybe path_throwIfEscapes(const char* path);
///@return path of parent dir
char* path_parentDir(char* path);
char* path_parentDir(allocator_ptr al, char* path);
///@return file name
char* path_basename(char* path, bool with_extension);
char* path_basename(allocator_ptr al, char* path, bool with_extension);
#if __cplusplus
}
#endif
#endif