dir_create
This commit is contained in:
parent
d2eb241d0d
commit
609dfcf3ed
2
cbuild
2
cbuild
@ -1 +1 @@
|
|||||||
Subproject commit 15a9661c467facf65b2155eab3fad1ecf5f0c945
|
Subproject commit 1b38b43c547246ca358a676cee5eda85598ab949
|
||||||
@ -1,7 +1,14 @@
|
|||||||
#include "filesystem.h"
|
#include "filesystem.h"
|
||||||
#include "io_includes.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
|
#if KFS_USE_WINDOWS_H
|
||||||
DWORD dwAttrib = GetFileAttributes(path);
|
DWORD dwAttrib = GetFileAttributes(path);
|
||||||
return (bool)(
|
return (bool)(
|
||||||
@ -16,35 +23,47 @@ bool dir_exists(char* path){
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
Maybe dir_create(char* path){
|
Maybe dir_create(const char* path){
|
||||||
if (dir_exists(path))
|
if (dir_exists(path))
|
||||||
return MaybeNull;
|
return MaybeNull;
|
||||||
char* parentDir=path_parentDir(path);
|
char* parentDir=path_parentDir(path);
|
||||||
dir_create(parentDir);
|
dir_create(parentDir);
|
||||||
free(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);
|
throw(ERR_NOTIMPLEMENTED);
|
||||||
return MaybeNull;
|
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);
|
throw(ERR_NOTIMPLEMENTED);
|
||||||
return MaybeNull;
|
return MaybeNull;
|
||||||
}
|
}
|
||||||
|
|
||||||
Maybe dir_getFiles(char* path, bool recursive){
|
Maybe dir_findFiles(const char* path, char* searchPattern, bool recursive){
|
||||||
throw(ERR_NOTIMPLEMENTED);
|
throw(ERR_NOTIMPLEMENTED);
|
||||||
return MaybeNull;
|
return MaybeNull;
|
||||||
}
|
}
|
||||||
Maybe dir_getDirs(char* path, bool recursive){
|
Maybe dir_findDirs(const char* path, char* searchPattern, 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){
|
|
||||||
throw(ERR_NOTIMPLEMENTED);
|
throw(ERR_NOTIMPLEMENTED);
|
||||||
return MaybeNull;
|
return MaybeNull;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,21 +7,21 @@ extern "C" {
|
|||||||
#include "../base/base.h"
|
#include "../base/base.h"
|
||||||
#include "file.h"
|
#include "file.h"
|
||||||
|
|
||||||
bool dir_exists(char* path);
|
bool dir_exists(const char* path);
|
||||||
///@return Maybe<void>
|
///@return Maybe<void>
|
||||||
Maybe dir_create(char* path);
|
Maybe dir_create(const char* path);
|
||||||
///@return Maybe<void>
|
///@return Maybe<void>
|
||||||
Maybe dir_delete(char* path);
|
Maybe dir_delete(const char* path);
|
||||||
|
|
||||||
///@return Maybe<Array_string>
|
///@return Maybe<Array_string>
|
||||||
Maybe dir_getFiles(char* path, bool recursive);
|
Maybe dir_getFiles(const char* path, bool recursive);
|
||||||
///@return Maybe<Array_string>
|
///@return Maybe<Array_string>
|
||||||
Maybe dir_getDirs(char* path, bool recursive);
|
Maybe dir_getDirs(const char* path, bool recursive);
|
||||||
|
|
||||||
///@return Maybe<Array_string>
|
///@return Maybe<Array_string>
|
||||||
Maybe dir_findFiles(char* path, char* searchPattern, bool recursive);
|
Maybe dir_findFiles(const char* path, char* searchPattern, bool recursive);
|
||||||
///@return Maybe<Array_string>
|
///@return Maybe<Array_string>
|
||||||
Maybe dir_findDirs(char* path, char* searchPattern, bool recursive);
|
Maybe dir_findDirs(const char* path, char* searchPattern, bool recursive);
|
||||||
|
|
||||||
#if __cplusplus
|
#if __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,16 @@
|
|||||||
#include "file.h"
|
#include "filesystem.h"
|
||||||
#include "../String/StringBuilder.h"
|
#include "../String/StringBuilder.h"
|
||||||
#include "io_includes.h"
|
#include "io_includes.h"
|
||||||
|
|
||||||
ktid_define(File);
|
ktid_define(File);
|
||||||
|
|
||||||
bool file_exists(char* path){
|
bool file_exists(const char* path){
|
||||||
|
if(path[0]=='.'){
|
||||||
// tryLast(path_throwIfEscapes(path));
|
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
|
#if KFS_USE_WINDOWS_H
|
||||||
DWORD dwAttrib = GetFileAttributes(path);
|
DWORD dwAttrib = GetFileAttributes(path);
|
||||||
@ -22,7 +26,7 @@ bool file_exists(char* path){
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
Maybe file_delete(char* path, bool recursive){
|
Maybe file_delete(const char* path, bool recursive){
|
||||||
throw(ERR_NOTIMPLEMENTED);
|
throw(ERR_NOTIMPLEMENTED);
|
||||||
return MaybeNull;
|
return MaybeNull;
|
||||||
}
|
}
|
||||||
@ -42,7 +46,7 @@ char* FileOpenMode_toStr(FileOpenMode m){
|
|||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
Maybe file_open(char* path, FileOpenMode mode){
|
Maybe file_open(const char* path, FileOpenMode mode){
|
||||||
File* file=fopen(path, FileOpenMode_toStr(mode));
|
File* file=fopen(path, FileOpenMode_toStr(mode));
|
||||||
if(!file)
|
if(!file)
|
||||||
safethrow(cptr_concat("can't open file ", (char*)path),;);
|
safethrow(cptr_concat("can't open file ", (char*)path),;);
|
||||||
|
|||||||
@ -11,10 +11,10 @@ extern "C" {
|
|||||||
typedef FILE File;
|
typedef FILE File;
|
||||||
ktid_declare(File);
|
ktid_declare(File);
|
||||||
|
|
||||||
bool file_exists(char* path);
|
bool file_exists(const char* path);
|
||||||
|
|
||||||
///@return Maybe<void>
|
///@return Maybe<void>
|
||||||
Maybe file_delete(char* path, bool recursive);
|
Maybe file_delete(const char* path, bool recursive);
|
||||||
|
|
||||||
PACK_ENUM(FileOpenMode,
|
PACK_ENUM(FileOpenMode,
|
||||||
// open a file for reading
|
// open a file for reading
|
||||||
@ -33,7 +33,7 @@ PACK_ENUM(FileOpenMode,
|
|||||||
/// @param path path to file
|
/// @param path path to file
|
||||||
/// @param mode Read/Write/Append/ReadWrite/ReadAppend
|
/// @param mode Read/Write/Append/ReadWrite/ReadAppend
|
||||||
/// @return Maybe<File*>
|
/// @return Maybe<File*>
|
||||||
Maybe file_open(char* path, FileOpenMode mode);
|
Maybe file_open(const char* path, FileOpenMode mode);
|
||||||
|
|
||||||
/// @brief closes file descriptor
|
/// @brief closes file descriptor
|
||||||
/// @return Maybe<void>
|
/// @return Maybe<void>
|
||||||
|
|||||||
@ -37,7 +37,7 @@ char* __path_concat(uint16 n, ...){
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* path_fixSeparators(char* path){
|
char* path_fixSeparators(const char* path){
|
||||||
char* pathCopy=cptr_copy(path);
|
char* pathCopy=cptr_copy(path);
|
||||||
char c;
|
char c;
|
||||||
while((c=*pathCopy)){
|
while((c=*pathCopy)){
|
||||||
@ -48,14 +48,13 @@ char* path_fixSeparators(char* path){
|
|||||||
return pathCopy;
|
return pathCopy;
|
||||||
}
|
}
|
||||||
|
|
||||||
Maybe path_throwIfEscapes(char* path){
|
Maybe path_throwIfEscapes(const char* path){
|
||||||
if(cptr_contains(path,".."))
|
if(cptr_contains(path,".."))
|
||||||
safethrow(cptr_concat("path <",path,"> uses <..>, that's not allowed"),);
|
safethrow(cptr_concat("path <",path,"> uses <..>, that's not allowed"),);
|
||||||
return MaybeNull;
|
return MaybeNull;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* path_parentDir(char* dir){
|
char* path_parentDir(char* dir){
|
||||||
throw(ERR_NOTIMPLEMENTED);
|
|
||||||
char* copy=cptr_copy(dir);
|
char* copy=cptr_copy(dir);
|
||||||
uint32 length=cptr_length(copy);
|
uint32 length=cptr_length(copy);
|
||||||
int i=cptr_lastIndexOfChar(copy,path_sep);
|
int i=cptr_lastIndexOfChar(copy,path_sep);
|
||||||
@ -65,10 +64,9 @@ char* path_parentDir(char* dir){
|
|||||||
}
|
}
|
||||||
if(i==-1){
|
if(i==-1){
|
||||||
free(copy);
|
free(copy);
|
||||||
copy=malloc(3);
|
copy=malloc(2);
|
||||||
copy[0]='.';
|
copy[0]='.';
|
||||||
copy[1]=path_sep;
|
copy[1]=0;
|
||||||
copy[2]=0;
|
|
||||||
}
|
}
|
||||||
return copy;
|
return copy;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,14 +22,14 @@ char* __path_concat(uint16 n, ...);
|
|||||||
/// @brief fixes path separators
|
/// @brief fixes path separators
|
||||||
/// @param cstr where can be <path_notSep>
|
/// @param cstr where can be <path_notSep>
|
||||||
/// @return new cstr with correct separators
|
/// @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))
|
#define path_resolve(PATH_PARTS...) path_fixSeparators(path_concat(PATH_PARTS))
|
||||||
|
|
||||||
/// @brief calls safethrow() if finds escape sequense in path
|
/// @brief calls safethrow() if finds escape sequense in path
|
||||||
/// @param path cstr where can be <..>
|
/// @param path cstr where can be <..>
|
||||||
/// @return Maybe<void>
|
/// @return Maybe<void>
|
||||||
Maybe path_throwIfEscapes(char* path);
|
Maybe path_throwIfEscapes(const char* path);
|
||||||
|
|
||||||
char* path_parentDir(char* dir);
|
char* path_parentDir(char* dir);
|
||||||
|
|
||||||
|
|||||||
@ -52,23 +52,39 @@ char* __unknownErr( );
|
|||||||
#if __cplusplus
|
#if __cplusplus
|
||||||
#define throw_id(E) __EXIT(((char*)__genErrMsg(errname(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 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
|
#else
|
||||||
#define throw(E) __EXIT(((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 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){\
|
#define try(_funcCall, _rezult, freeMem) Maybe _rezult=_funcCall; if(_rezult.errmsg){\
|
||||||
freeMem;\
|
freeMem;\
|
||||||
_rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\
|
_rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\
|
||||||
return _rezult;\
|
return _rezult;\
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#define tryLast(_funcCall, _rezult) Maybe _rezult=_funcCall; if(_rezult.errmsg){\
|
#define tryLast(_funcCall, _rezult) Maybe _rezult=_funcCall; if(_rezult.errmsg){\
|
||||||
_rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\
|
_rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\
|
||||||
__EXIT(_rezult.errmsg);\
|
__EXIT(_rezult.errmsg);\
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if __cplusplus
|
#if __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
Loading…
Reference in New Issue
Block a user