diff --git a/cbuild b/cbuild index 15a9661..1b38b43 160000 --- a/cbuild +++ b/cbuild @@ -1 +1 @@ -Subproject commit 15a9661c467facf65b2155eab3fad1ecf5f0c945 +Subproject commit 1b38b43c547246ca358a676cee5eda85598ab949 diff --git a/src/Filesystem/dir.c b/src/Filesystem/dir.c index a46164d..78f9cb0 100644 --- a/src/Filesystem/dir.c +++ b/src/Filesystem/dir.c @@ -1,7 +1,14 @@ #include "filesystem.h" #include "io_includes.h" +#include "../kprint/kprint.h" -bool dir_exists(char* path){ +bool dir_exists(const char* path){ + if(path[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 + } #if KFS_USE_WINDOWS_H DWORD dwAttrib = GetFileAttributes(path); return (bool)( @@ -16,35 +23,47 @@ bool dir_exists(char* path){ #endif } -Maybe dir_create(char* path){ +Maybe dir_create(const char* path){ if (dir_exists(path)) return MaybeNull; char* parentDir=path_parentDir(path); dir_create(parentDir); free(parentDir); +#if KFS_USE_WINDOWS_H + if(!CreateDirectory(path, NULL)) +#else + if(mkdir(path, 0777) == -1) +#endif + { + char err[512]; + IFWIN( + sprintf_s(err, 512, "can't create dicectory <%s>", path), + sprintf(err, "can't create dicectory <%s>", path)); + safethrow(err,;); + } + + return MaybeNull; +} + +Maybe dir_delete(const char* path){ throw(ERR_NOTIMPLEMENTED); return MaybeNull; } -Maybe dir_delete(char* path){ +Maybe dir_getFiles(const char* path, bool recursive){ + throw(ERR_NOTIMPLEMENTED); + return MaybeNull; +} +Maybe dir_getDirs(const char* path, bool recursive){ throw(ERR_NOTIMPLEMENTED); return MaybeNull; } -Maybe dir_getFiles(char* path, bool recursive){ +Maybe dir_findFiles(const char* path, char* searchPattern, bool recursive){ throw(ERR_NOTIMPLEMENTED); return MaybeNull; } -Maybe dir_getDirs(char* path, bool recursive){ - throw(ERR_NOTIMPLEMENTED); - return MaybeNull; -} - -Maybe dir_findFiles(char* path, char* searchPattern, bool recursive){ - throw(ERR_NOTIMPLEMENTED); - return MaybeNull; -} -Maybe dir_findDirs(char* path, char* searchPattern, bool recursive){ +Maybe dir_findDirs(const char* path, char* searchPattern, bool recursive){ throw(ERR_NOTIMPLEMENTED); return MaybeNull; } diff --git a/src/Filesystem/dir.h b/src/Filesystem/dir.h index df9a664..3821229 100644 --- a/src/Filesystem/dir.h +++ b/src/Filesystem/dir.h @@ -7,21 +7,21 @@ extern "C" { #include "../base/base.h" #include "file.h" -bool dir_exists(char* path); +bool dir_exists(const char* path); ///@return Maybe -Maybe dir_create(char* path); +Maybe dir_create(const char* path); ///@return Maybe -Maybe dir_delete(char* path); +Maybe dir_delete(const char* path); ///@return Maybe -Maybe dir_getFiles(char* path, bool recursive); +Maybe dir_getFiles(const char* path, bool recursive); ///@return Maybe -Maybe dir_getDirs(char* path, bool recursive); +Maybe dir_getDirs(const char* path, bool recursive); ///@return Maybe -Maybe dir_findFiles(char* path, char* searchPattern, bool recursive); +Maybe dir_findFiles(const char* path, char* searchPattern, bool recursive); ///@return Maybe -Maybe dir_findDirs(char* path, char* searchPattern, bool recursive); +Maybe dir_findDirs(const char* path, char* searchPattern, bool recursive); #if __cplusplus } diff --git a/src/Filesystem/file.c b/src/Filesystem/file.c index e368d85..cbedced 100644 --- a/src/Filesystem/file.c +++ b/src/Filesystem/file.c @@ -1,12 +1,16 @@ -#include "file.h" +#include "filesystem.h" #include "../String/StringBuilder.h" #include "io_includes.h" ktid_define(File); -bool file_exists(char* path){ - - // tryLast(path_throwIfEscapes(path)); +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); @@ -22,7 +26,7 @@ bool file_exists(char* path){ #endif } -Maybe file_delete(char* path, bool recursive){ +Maybe file_delete(const char* path, bool recursive){ throw(ERR_NOTIMPLEMENTED); return MaybeNull; } @@ -42,7 +46,7 @@ char* FileOpenMode_toStr(FileOpenMode m){ return p; } -Maybe file_open(char* path, FileOpenMode mode){ +Maybe file_open(const char* path, FileOpenMode mode){ File* file=fopen(path, FileOpenMode_toStr(mode)); if(!file) safethrow(cptr_concat("can't open file ", (char*)path),;); diff --git a/src/Filesystem/file.h b/src/Filesystem/file.h index 8498b52..febdbb0 100644 --- a/src/Filesystem/file.h +++ b/src/Filesystem/file.h @@ -11,10 +11,10 @@ extern "C" { typedef FILE File; ktid_declare(File); -bool file_exists(char* path); +bool file_exists(const char* path); ///@return Maybe -Maybe file_delete(char* path, bool recursive); +Maybe file_delete(const char* path, bool recursive); PACK_ENUM(FileOpenMode, // open a file for reading @@ -33,7 +33,7 @@ PACK_ENUM(FileOpenMode, /// @param path path to file /// @param mode Read/Write/Append/ReadWrite/ReadAppend /// @return Maybe -Maybe file_open(char* path, FileOpenMode mode); +Maybe file_open(const char* path, FileOpenMode mode); /// @brief closes file descriptor /// @return Maybe diff --git a/src/Filesystem/path.c b/src/Filesystem/path.c index 0ebf386..5cd2d6e 100644 --- a/src/Filesystem/path.c +++ b/src/Filesystem/path.c @@ -37,7 +37,7 @@ char* __path_concat(uint16 n, ...){ return output; } -char* path_fixSeparators(char* path){ +char* path_fixSeparators(const char* path){ char* pathCopy=cptr_copy(path); char c; while((c=*pathCopy)){ @@ -48,14 +48,13 @@ char* path_fixSeparators(char* path){ return pathCopy; } -Maybe path_throwIfEscapes(char* path){ +Maybe path_throwIfEscapes(const char* path){ if(cptr_contains(path,"..")) safethrow(cptr_concat("path <",path,"> uses <..>, that's not allowed"),); return MaybeNull; } char* path_parentDir(char* dir){ - throw(ERR_NOTIMPLEMENTED); char* copy=cptr_copy(dir); uint32 length=cptr_length(copy); int i=cptr_lastIndexOfChar(copy,path_sep); @@ -65,10 +64,9 @@ char* path_parentDir(char* dir){ } if(i==-1){ free(copy); - copy=malloc(3); + copy=malloc(2); copy[0]='.'; - copy[1]=path_sep; - copy[2]=0; + copy[1]=0; } return copy; } diff --git a/src/Filesystem/path.h b/src/Filesystem/path.h index d39c97e..b9ab2ab 100644 --- a/src/Filesystem/path.h +++ b/src/Filesystem/path.h @@ -22,14 +22,14 @@ char* __path_concat(uint16 n, ...); /// @brief fixes path separators /// @param cstr where can be /// @return new cstr with correct separators -char* path_fixSeparators(char* path); +char* path_fixSeparators(const char* path); #define path_resolve(PATH_PARTS...) path_fixSeparators(path_concat(PATH_PARTS)) /// @brief calls safethrow() if finds escape sequense in path /// @param path cstr where can be <..> /// @return Maybe -Maybe path_throwIfEscapes(char* path); +Maybe path_throwIfEscapes(const char* path); char* path_parentDir(char* dir); diff --git a/src/base/errors.h b/src/base/errors.h index 7c46733..90d9751 100644 --- a/src/base/errors.h +++ b/src/base/errors.h @@ -50,24 +50,40 @@ char* __unknownErr( ); )(E) #if __cplusplus - #define throw_id(E) __EXIT(((char*)__genErrMsg(errname(E), __FILE__,__LINE__,__func__))) - #define throw_msg(E) __EXIT(((char*)__genErrMsg(E, __FILE__,__LINE__,__func__))) +#define throw_id(E) __EXIT(((char*)__genErrMsg(errname(E), __FILE__,__LINE__,__func__))) +#define throw_msg(E) __EXIT(((char*)__genErrMsg(E, __FILE__,__LINE__,__func__))) + +#define safethrow_id(E, FREEMEM) { FREEMEM;\ + __RETURN_EXCEPTION(((char*)__genErrMsg(errname(E), __FILE__,__LINE__,__func__)));\ +} +#define safethrow_msg(E, FREEMEM) { FREEMEM;\ + __RETURN_EXCEPTION(((char*)__genErrMsg(E, __FILE__,__LINE__,__func__)));\ +} + +#define try_cpp(_funcCall, _rezult, freeMem) Maybe _rezult=_funcCall; if(_rezult.errmsg){\ + freeMem;\ + _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\ + return _rezult;\ +} + #else - #define throw(E) __EXIT(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__))) - #define safethrow(E, FREEMEM) { FREEMEM; __RETURN_EXCEPTION(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__))); } +#define throw(E) __EXIT(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__))) + +#define safethrow(E, FREEMEM) { FREEMEM;\ + __RETURN_EXCEPTION(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__)));\ +} #define try(_funcCall, _rezult, freeMem) Maybe _rezult=_funcCall; if(_rezult.errmsg){\ - freeMem;\ - _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\ - return _rezult;\ - } + freeMem;\ + _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\ + return _rezult;\ +} +#endif #define tryLast(_funcCall, _rezult) Maybe _rezult=_funcCall; if(_rezult.errmsg){\ - _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\ - __EXIT(_rezult.errmsg);\ - } - -#endif + _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\ + __EXIT(_rezult.errmsg);\ +} #if __cplusplus }