diff --git a/src/Filesystem/dir.c b/src/Filesystem/dir.c new file mode 100644 index 0000000..840a3f3 --- /dev/null +++ b/src/Filesystem/dir.c @@ -0,0 +1 @@ +#include "dir.h" \ No newline at end of file diff --git a/src/Filesystem/dir.h b/src/Filesystem/dir.h new file mode 100644 index 0000000..111e4ca --- /dev/null +++ b/src/Filesystem/dir.h @@ -0,0 +1,18 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +#include "../base/base.h" +#include "file.h" + +typedef char* DirPath; +Array_declare(DirPath); + +Array_FilePath dir_getFiles(DirPath path); +Array_FilePath dir_findFiles(DirPath path, FilePath searchPattern); + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/src/Filesystem/file.c b/src/Filesystem/file.c new file mode 100644 index 0000000..08e53fb --- /dev/null +++ b/src/Filesystem/file.c @@ -0,0 +1,108 @@ +#include "file.h" +#include "../String/StringBuilder.h" + +ktid_define(File); + +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(FilePath path, FileOpenMode mode){ + File* file=fopen(path, FileOpenMode_toStr(mode)); + if(!file) + safethrow(cptr_concat("can't open file ", (char*)path),;); + return SUCCESS(UniHeapPtr(File,file)); +} + +Maybe file_close(File* 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(File* file, char byte){ + int rezult=fputc(byte, file); + ioWriteCheck(); + return MaybeNull; +} + +Maybe file_writeBuffer(File* file, char* buffer, uint64 length){ + int rezult=0; + for(uint64 i=0; i +Maybe file_open(FilePath path, FileOpenMode mode); + +/// @brief closes file descriptor +/// @return Maybe +Maybe file_close(File* file); + +/// @brief closes file descriptor +/// @param byte byte to write +/// @return Maybe +Maybe file_writeChar(File* file, char byte); + +/// @brief closes file descriptor +/// @param buffer bytes to write +/// @param length buffer length +/// @return Maybe +Maybe file_writeBuffer(File* file, char* buffer, uint64 length); + +/// @brief writes all cstring array content to file +/// @param cptr zero-terminated cstring +/// @return Maybe +Maybe file_writeCptr(File* file, char* cptr); + + +/// @brief reads single byte from file +/// @return Maybe +Maybe file_readChar(File* file); + +/// @brief reads byte array of specofied length +/// @param buffer buffer that will be filled with file bytes +/// @param length buffer length +/// @return Maybe total number of successfully read bytes (<=length) +Maybe file_readBuffer(File* file, char* buffer, uint64 length); + +/// @brief reads all bytes from file +/// @param allBytes ptr to the file's content will be pushed there +/// @return Maybe total number of successfully read bytes +Maybe file_readAll(File* file, char** allBytes); + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/src/Filesystem/filesystem.h b/src/Filesystem/filesystem.h index 2bdd4cb..9e68038 100644 --- a/src/Filesystem/filesystem.h +++ b/src/Filesystem/filesystem.h @@ -1,22 +1,3 @@ -#pragma once - -#if __cplusplus -extern "C" { -#endif - -#include "std.h" - -#if defined(_WIN32) || defined (_WIN64) -static const char path_sep='\\'; -#else -static const char path_sep='/'; -#endif - -char* __path_concat(uint16 n, char* path_start, ...); -#define path_concat(PATH_PARTS) __path_concat(count_args(PATH_PARTS), PATH_PARTS) - - - -#if __cplusplus -} -#endif \ No newline at end of file +#include "path.h" +#include "dir.h" +#include "file.h" diff --git a/src/Filesystem/filesystem.c b/src/Filesystem/path.c similarity index 95% rename from src/Filesystem/filesystem.c rename to src/Filesystem/path.c index fe535da..6b52991 100644 --- a/src/Filesystem/filesystem.c +++ b/src/Filesystem/path.c @@ -36,3 +36,7 @@ char* __path_concat(uint16 n, char* prev_part, ...){ free(lengths); return output; } + +char* path_fixSeparators(char* path){ + +} \ No newline at end of file diff --git a/src/Filesystem/path.h b/src/Filesystem/path.h new file mode 100644 index 0000000..1b9ec52 --- /dev/null +++ b/src/Filesystem/path.h @@ -0,0 +1,20 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +#include "../base/base.h" + +#if defined(_WIN32) || defined (_WIN64) +static const char path_sep='\\'; +#else +static const char path_sep='/'; +#endif + +char* __path_concat(uint16 n, char* path_start, ...); +#define path_concat(PATH_PARTS) __path_concat(count_args(PATH_PARTS), PATH_PARTS) + +#if __cplusplus +} +#endif \ No newline at end of file