#include "filesystem.h" #include "../String/StringBuilder.h" #include "io_includes.h" void __file_destructMembers(void* _f){ fclose((FileHandle)_f); } kt_define(FileHandle, __file_destructMembers, NULL) bool file_exists(const char* path){ if(path[0]=='.'){ if(path[1]==0 || (path[1]==path_sep && path[2]==0)) return false; // . or ./ is not a file // else if(path[1]=='.' && path[2]==path_sep) //TODO path_resolve because windows doesnt recognize .\ pattern } #if KFS_USE_WINDOWS_H DWORD dwAttrib = GetFileAttributes(path); return (bool)( (dwAttrib != INVALID_FILE_ATTRIBUTES) && // file exists !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); // file is not directory #else struct stat stats; i32 rez=stat(path, &stats); return (bool)( (rez!=-1) && // file exists !(S_ISDIR(stats.st_mode))); // file is not directory #endif } Maybe file_delete(const char* path, bool recursive){ throw(ERR_NOTIMPLEMENTED); return MaybeNull; } char* FileOpenMode_toStr(FileOpenMode m){ char* p; switch(m){ case FileOpenMode_Read: p="rb"; break; case FileOpenMode_Write: p="wb"; break; case FileOpenMode_Append: p="ab"; break; case FileOpenMode_ReadWrite: p="wb+"; break; case FileOpenMode_ReadAppend: p="ab+"; break; default: dbg(m); throw(ERR_UNEXPECTEDVAL); } return p; } 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(al, "can't open file ", (char*)path), LinearAllocator_destruct(&_al)); LinearAllocator_destruct(&_al); return SUCCESS(UniHeapPtr(FileHandle,file)); } Maybe file_close(FileHandle file){ if(!file) safethrow(ERR_NULLPTR,;); if(fclose(file)) safethrow(ERR_IO,;); return MaybeNull; } #define ioWriteCheck() \ if(rezult==EOF) \ safethrow(ERR_IO_EOF,;); \ if(rezult!=0) \ safethrow(ERR_IO,;); Maybe file_writeChar(FileHandle file, char byte){ i32 rezult=fputc(byte, file); ioWriteCheck(); return MaybeNull; } Maybe file_writeBuffer(FileHandle file, char* buffer, u64 length){ i32 rezult=0; for(u64 i=0; i