filesystem rework
This commit is contained in:
parent
29a5b22286
commit
835bc5e834
@ -16,7 +16,7 @@ Array_define(uint64)
|
||||
Array_define(Unitype)
|
||||
|
||||
void Array_Unitype_free_(Array_Unitype* array, bool freeMembers){
|
||||
if(freeMembers) for (int32 i; i<array->length; i++)
|
||||
if(freeMembers) for (int32 i=0; i<array->length; i++)
|
||||
Unitype_free(array->values[i]);
|
||||
if(array->allocatedOnHeap)
|
||||
free(array->values);
|
||||
|
||||
@ -1 +1,50 @@
|
||||
#include "dir.h"
|
||||
#include "filesystem.h"
|
||||
#include "io_includes.h"
|
||||
|
||||
bool dir_exists(char* path){
|
||||
#if KFS_USE_WINDOWS_H
|
||||
DWORD dwAttrib = GetFileAttributes(path);
|
||||
return (bool)(
|
||||
(dwAttrib != INVALID_FILE_ATTRIBUTES) & // file exists
|
||||
(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); // file is a directory
|
||||
#else
|
||||
struct stat stats;
|
||||
int rez=stat(path, &stats);
|
||||
return (bool)(
|
||||
(rez!=-1) & // file exists
|
||||
(S_ISDIR(stats.st_mode))); // file is a directory
|
||||
#endif
|
||||
}
|
||||
|
||||
Maybe dir_create(char* path){
|
||||
if (dir_exists(path))
|
||||
return MaybeNull;
|
||||
char* parentDir=path_parentDir(path);
|
||||
dir_create(parentDir);
|
||||
free(parentDir);
|
||||
throw(ERR_NOTIMPLEMENTED);
|
||||
return MaybeNull;
|
||||
}
|
||||
|
||||
Maybe dir_delete(char* path){
|
||||
throw(ERR_NOTIMPLEMENTED);
|
||||
return MaybeNull;
|
||||
}
|
||||
|
||||
Maybe dir_getFiles(char* path, 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){
|
||||
throw(ERR_NOTIMPLEMENTED);
|
||||
return MaybeNull;
|
||||
}
|
||||
|
||||
@ -7,11 +7,21 @@ extern "C" {
|
||||
#include "../base/base.h"
|
||||
#include "file.h"
|
||||
|
||||
typedef char* DirPath;
|
||||
Array_declare(DirPath);
|
||||
bool dir_exists(char* path);
|
||||
///@return Maybe<void>
|
||||
Maybe dir_create(char* path);
|
||||
///@return Maybe<void>
|
||||
Maybe dir_delete(char* path);
|
||||
|
||||
Array_FilePath dir_getFiles(DirPath path);
|
||||
Array_FilePath dir_findFiles(DirPath path, FilePath searchPattern);
|
||||
///@return Maybe<Array_string>
|
||||
Maybe dir_getFiles(char* path, bool recursive);
|
||||
///@return Maybe<Array_string>
|
||||
Maybe dir_getDirs(char* path, bool recursive);
|
||||
|
||||
///@return Maybe<Array_string>
|
||||
Maybe dir_findFiles(char* path, char* searchPattern, bool recursive);
|
||||
///@return Maybe<Array_string>
|
||||
Maybe dir_findDirs(char* path, char* searchPattern, bool recursive);
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
|
||||
@ -1,8 +1,32 @@
|
||||
#include "file.h"
|
||||
#include "../String/StringBuilder.h"
|
||||
#include "io_includes.h"
|
||||
|
||||
ktid_define(File);
|
||||
|
||||
bool file_exists(char* path){
|
||||
|
||||
// tryLast(path_throwIfEscapes(path));
|
||||
|
||||
#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;
|
||||
int rez=stat(path, &stats);
|
||||
return (bool)(
|
||||
(rez!=-1) & // file exists
|
||||
!(S_ISDIR(stats.st_mode))); // file is not directory
|
||||
#endif
|
||||
}
|
||||
|
||||
Maybe file_delete(char* path, bool recursive){
|
||||
throw(ERR_NOTIMPLEMENTED);
|
||||
return MaybeNull;
|
||||
}
|
||||
|
||||
char* FileOpenMode_toStr(FileOpenMode m){
|
||||
char* p;
|
||||
switch(m){
|
||||
@ -18,7 +42,7 @@ char* FileOpenMode_toStr(FileOpenMode m){
|
||||
return p;
|
||||
}
|
||||
|
||||
Maybe file_open(FilePath path, FileOpenMode mode){
|
||||
Maybe file_open(char* path, FileOpenMode mode){
|
||||
File* file=fopen(path, FileOpenMode_toStr(mode));
|
||||
if(!file)
|
||||
safethrow(cptr_concat("can't open file ", (char*)path),;);
|
||||
|
||||
@ -8,11 +8,14 @@ extern "C" {
|
||||
#include "../Array/Array.h"
|
||||
#include "../String/string.h"
|
||||
|
||||
typedef char* FilePath;
|
||||
Array_declare(FilePath);
|
||||
typedef FILE File;
|
||||
ktid_declare(File);
|
||||
|
||||
bool file_exists(char* path);
|
||||
|
||||
///@return Maybe<void>
|
||||
Maybe file_delete(char* path, bool recursive);
|
||||
|
||||
PACK_ENUM(FileOpenMode,
|
||||
// open a file for reading
|
||||
FileOpenMode_Read=1,
|
||||
@ -24,14 +27,13 @@ PACK_ENUM(FileOpenMode,
|
||||
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
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
/// @brief opens file
|
||||
/// @param path path to file
|
||||
/// @param mode Read/Write/Append/ReadWrite/ReadAppend
|
||||
/// @return Maybe<File*>
|
||||
Maybe file_open(FilePath path, FileOpenMode mode);
|
||||
Maybe file_open(char* path, FileOpenMode mode);
|
||||
|
||||
/// @brief closes file descriptor
|
||||
/// @return Maybe<void>
|
||||
|
||||
15
src/Filesystem/io_includes.h
Normal file
15
src/Filesystem/io_includes.h
Normal file
@ -0,0 +1,15 @@
|
||||
#include "../base/std.h"
|
||||
|
||||
#if defined(_WIN64) || defined(_WIN32)
|
||||
#define KFS_USE_WINDOWS_H 1
|
||||
#else
|
||||
#define KFS_USE_WINDOWS_H 0
|
||||
#endif
|
||||
|
||||
#if KFS_USE_WINDOWS_H
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
@ -53,3 +53,22 @@ Maybe path_throwIfEscapes(char* 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);
|
||||
if(i==length-1){
|
||||
copy[length-1]=0;
|
||||
i=cptr_lastIndexOfChar(copy,path_sep);
|
||||
}
|
||||
if(i==-1){
|
||||
free(copy);
|
||||
copy=malloc(3);
|
||||
copy[0]='.';
|
||||
copy[1]=path_sep;
|
||||
copy[2]=0;
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
|
||||
@ -31,6 +31,8 @@ char* path_fixSeparators(char* path);
|
||||
/// @return Maybe<void>
|
||||
Maybe path_throwIfEscapes(char* path);
|
||||
|
||||
char* path_parentDir(char* dir);
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
@ -57,6 +57,41 @@ uint32 cptr_indexOf(char* ptr, char* fragment){
|
||||
if(fragment[fi]==0)
|
||||
return si-fi+1;
|
||||
}
|
||||
else fi=0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
uint32 cptr_indexOfChar(char* ptr, char fragment){
|
||||
char sc=*ptr;
|
||||
for(int si=0; sc!=0; si++){
|
||||
sc=ptr[si];
|
||||
if(sc==fragment){
|
||||
return si;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
uint32 cptr_lastIndexOf(char* ptr, char* fragment){
|
||||
char sc=*ptr;
|
||||
int fi_last=cptr_length(fragment)-1;
|
||||
for(int si=cptr_length(ptr)-1, fi=fi_last; si>=0; si--){
|
||||
sc=ptr[si];
|
||||
if(sc==fragment[fi]){
|
||||
if(fi==0)
|
||||
return si;
|
||||
fi--;
|
||||
}
|
||||
else fi=fi_last;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
uint32 cptr_lastIndexOfChar(char* ptr, char fragment){
|
||||
char sc=*ptr;
|
||||
for(int si=cptr_length(ptr)-1; si>=0; si--){
|
||||
sc=ptr[si];
|
||||
if(sc==fragment){
|
||||
return si;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -25,6 +25,15 @@ bool cptr_endsWith(char* ptr, char* fragment);
|
||||
/// @brief search for <fragment> in <ptr>
|
||||
/// @return index of first <fragment> inclusion or -1 if not found
|
||||
uint32 cptr_indexOf(char* ptr, char* fragment);
|
||||
/// @brief search for <fragment> in <ptr>
|
||||
/// @return index of first <fragment> inclusion or -1 if not found
|
||||
uint32 cptr_indexOfChar(char* ptr, char fragment);
|
||||
/// @brief search for <fragment> in <ptr>
|
||||
/// @return index of last <fragment> inclusion or -1 if not found
|
||||
uint32 cptr_lastIndexOf(char* ptr, char* fragment);
|
||||
/// @brief search for <fragment> in <ptr>
|
||||
/// @return index of last <fragment> inclusion or -1 if not found
|
||||
uint32 cptr_lastIndexOfChar(char* ptr, char fragment);
|
||||
|
||||
static inline bool cptr_contains(char* ptr, char* fragment){
|
||||
// if(cptr_indexOf(ptr, fragment)==-1)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user