filesystem.h
This commit is contained in:
parent
4c3933f789
commit
961d00fdb0
54
include/tlibc/filesystem.h
Normal file
54
include/tlibc/filesystem.h
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "std.h"
|
||||||
|
#include "errors.h"
|
||||||
|
#include "string/str.h"
|
||||||
|
|
||||||
|
#if !defined(KFS_USE_WINDOWS_H)
|
||||||
|
#if defined(_WIN64) || defined(_WIN32)
|
||||||
|
#define KFS_USE_WINDOWS_H 1
|
||||||
|
#else
|
||||||
|
#define KFS_USE_WINDOWS_H 0
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if KFS_USE_WINDOWS_H
|
||||||
|
#define path_sep '\\'
|
||||||
|
#define path_seps "\\"
|
||||||
|
#define path_notsep '/'
|
||||||
|
#define path_notseps "/"
|
||||||
|
#else
|
||||||
|
#define path_sep '/'
|
||||||
|
#define path_seps "/"
|
||||||
|
#define path_notsep '\\'
|
||||||
|
#define path_notseps "\\"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/// @brief removes part of path after path_sep
|
||||||
|
/// @return str with data = path.data
|
||||||
|
str path_dirname(str path);
|
||||||
|
|
||||||
|
/// @brief removes part of path before path_sep
|
||||||
|
/// @return str with data = path.data
|
||||||
|
str path_basename(str path, bool remove_ext);
|
||||||
|
|
||||||
|
|
||||||
|
/// open a file for reading
|
||||||
|
#define FO_Read "rb"
|
||||||
|
/// (re)create a file for writing
|
||||||
|
#define FO_Write "wb"
|
||||||
|
/// opens file for writing additional data to the end / creates new file
|
||||||
|
#define FO_Append "ab"
|
||||||
|
/// (re)creates file for reading/writing
|
||||||
|
#define FO_ReadWrite "w+b"
|
||||||
|
/// opens file for readng/writing additional data to the end / creates new file
|
||||||
|
#define FO_ReadAppend "a+b"
|
||||||
|
|
||||||
|
Result(FILE*) file_open(cstr file_name, cstr fopen_mode);
|
||||||
|
|
||||||
|
bool file_exists(cstr path);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
bool dir_exists(cstr path);
|
||||||
|
|
||||||
|
Result(void) dir_create(cstr path);
|
||||||
@ -16,6 +16,10 @@ typedef struct str {
|
|||||||
|
|
||||||
#define str_construct(DATA, LEN, ZERO_TERMINATED) ((str){ .data = DATA, .size = LEN, .isZeroTerminated = ZERO_TERMINATED })
|
#define str_construct(DATA, LEN, ZERO_TERMINATED) ((str){ .data = DATA, .size = LEN, .isZeroTerminated = ZERO_TERMINATED })
|
||||||
|
|
||||||
|
static inline str str_from_cstr(cstr s_ptr){
|
||||||
|
return str_construct((void*)s_ptr, strlen(s_ptr), true);
|
||||||
|
}
|
||||||
|
|
||||||
static const str str_null = str_construct(NULL, 0, 0);
|
static const str str_null = str_construct(NULL, 0, 0);
|
||||||
|
|
||||||
/// copies src content to new string and adds \0 at the end
|
/// copies src content to new string and adds \0 at the end
|
||||||
|
|||||||
42
src/filesystem/dir.c
Normal file
42
src/filesystem/dir.c
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#include "tlibc/filesystem.h"
|
||||||
|
#include "io_includes.h"
|
||||||
|
|
||||||
|
bool dir_exists(cstr 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)(
|
||||||
|
(dwAttrib != INVALID_FILE_ATTRIBUTES) && // file exists
|
||||||
|
(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); // file is a directory
|
||||||
|
#else
|
||||||
|
struct stat stats;
|
||||||
|
i32 rez=stat(path, &stats);
|
||||||
|
return (bool)(
|
||||||
|
(rez!=-1) && // file exists
|
||||||
|
(S_ISDIR(stats.st_mode))); // file is a directory
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
Result(void) dir_create(cstr path){
|
||||||
|
if (dir_exists(path))
|
||||||
|
return RESULT_VOID;
|
||||||
|
|
||||||
|
char* parentDir= str_copy(path_dirname(str_from_cstr((void*)path))).data;
|
||||||
|
try(_1, dir_create(parentDir), free(parentDir));
|
||||||
|
free(parentDir);
|
||||||
|
#if KFS_USE_WINDOWS_H
|
||||||
|
if(!CreateDirectory(path, NULL))
|
||||||
|
#else
|
||||||
|
if(mkdir(path, 0777) == -1)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
return RESULT_ERROR(sprintf_malloc(512, "can't create dicectory '%s'", path), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
return RESULT_VOID;
|
||||||
|
}
|
||||||
34
src/filesystem/file.c
Normal file
34
src/filesystem/file.c
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#include "tlibc/filesystem.h"
|
||||||
|
#include "io_includes.h"
|
||||||
|
|
||||||
|
bool file_exists(cstr 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
|
||||||
|
}
|
||||||
|
|
||||||
|
Result(FILE*) file_open(cstr file_name, cstr fopen_mode){
|
||||||
|
FILE* f = fopen(file_name, fopen_mode);
|
||||||
|
if(f == NULL){
|
||||||
|
char* errmsg = sprintf_malloc(256, "can't open (%s) file '%s': %s",
|
||||||
|
fopen_mode, file_name, strerror(errno));
|
||||||
|
return RESULT_ERROR(errmsg, true);
|
||||||
|
}
|
||||||
|
return RESULT_VALUE(p, f);
|
||||||
|
}
|
||||||
9
src/filesystem/io_includes.h
Normal file
9
src/filesystem/io_includes.h
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if KFS_USE_WINDOWS_H
|
||||||
|
#include <windows.h>
|
||||||
|
#else
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
29
src/filesystem/path.c
Normal file
29
src/filesystem/path.c
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#include "tlibc/filesystem.h"
|
||||||
|
|
||||||
|
str path_dirname(str path){
|
||||||
|
i32 sepIndex = str_seekCharReverse(path, path_sep, 0);
|
||||||
|
if(sepIndex <= 0)
|
||||||
|
return path;
|
||||||
|
|
||||||
|
path.size = sepIndex;
|
||||||
|
path.isZeroTerminated = false;
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
str path_basename(str path, bool remove_ext){
|
||||||
|
i32 nameIndex = str_seekCharReverse(path, path_sep, 0) + 1;
|
||||||
|
if(nameIndex == path.size)
|
||||||
|
return path;
|
||||||
|
|
||||||
|
path.data += nameIndex;
|
||||||
|
path.size -= nameIndex;
|
||||||
|
if(remove_ext)
|
||||||
|
return path;
|
||||||
|
|
||||||
|
i32 extIndex = str_seekCharReverse(path, '.', -1);
|
||||||
|
if(extIndex > 0){
|
||||||
|
path.size = extIndex;
|
||||||
|
path.isZeroTerminated = false;
|
||||||
|
}
|
||||||
|
return path;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user