filesystem
This commit is contained in:
parent
451358ad6c
commit
7f62334a4a
1
src/Filesystem/dir.c
Normal file
1
src/Filesystem/dir.c
Normal file
@ -0,0 +1 @@
|
|||||||
|
#include "dir.h"
|
||||||
18
src/Filesystem/dir.h
Normal file
18
src/Filesystem/dir.h
Normal file
@ -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
|
||||||
108
src/Filesystem/file.c
Normal file
108
src/Filesystem/file.c
Normal file
@ -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<length && !rezult; i++)
|
||||||
|
rezult=fputc(buffer[i], file);
|
||||||
|
ioWriteCheck();
|
||||||
|
return MaybeNull;
|
||||||
|
}
|
||||||
|
|
||||||
|
Maybe file_writeCptr(File* file, char* cptr){
|
||||||
|
int rezult=fputs(cptr, file);
|
||||||
|
ioWriteCheck();
|
||||||
|
return MaybeNull;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Maybe file_readChar(File* file){
|
||||||
|
int rezult=fgetc(file);
|
||||||
|
if(feof(file)) safethrow(ERR_IO_EOF,;);
|
||||||
|
if(ferror(file)) safethrow(ERR_IO,;);
|
||||||
|
return SUCCESS(UniUInt64(rezult));
|
||||||
|
}
|
||||||
|
|
||||||
|
Maybe file_readBuffer(File* file, char* buffer, uint64 length){
|
||||||
|
int rezult=0;
|
||||||
|
uint64 i=0;
|
||||||
|
for(; i<length && rezult!=EOF; i++){
|
||||||
|
rezult=fgetc(file);
|
||||||
|
buffer[i]=(char)rezult;
|
||||||
|
}
|
||||||
|
if(ferror(file)) safethrow(ERR_IO,;);
|
||||||
|
return SUCCESS(UniUInt64(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
Maybe file_readAll(File* file, char** allBytes){
|
||||||
|
int rezult=0;
|
||||||
|
char buffer[256];
|
||||||
|
string bufStr={.ptr=buffer, .length=sizeof(buffer)};
|
||||||
|
StringBuilder* sb=StringBuilder_create();
|
||||||
|
uint64 i=0;
|
||||||
|
while(true){
|
||||||
|
rezult=fgetc(file);
|
||||||
|
if(rezult!=EOF){
|
||||||
|
if(ferror(file))
|
||||||
|
safethrow(ERR_IO,; StringBuilder_free(sb));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
buffer[i]=(char)rezult;
|
||||||
|
i++;
|
||||||
|
if(!(i%sizeof(buffer)))
|
||||||
|
StringBuilder_append_string(sb,bufStr);
|
||||||
|
}
|
||||||
|
bufStr.length=i%sizeof(buffer);
|
||||||
|
if(bufStr.length!=0)
|
||||||
|
StringBuilder_append_string(sb,bufStr);
|
||||||
|
string str=StringBuilder_build(sb);
|
||||||
|
*allBytes=str.ptr;
|
||||||
|
// i dont know how can it happen, but if it will have, it will be very dangerous
|
||||||
|
if(i!=str.length)
|
||||||
|
throw(ERR_UNEXPECTEDVAL);
|
||||||
|
return SUCCESS(UniUInt64(i));
|
||||||
|
}
|
||||||
74
src/Filesystem/file.h
Normal file
74
src/Filesystem/file.h
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "../base/base.h"
|
||||||
|
#include "../Array/Array.h"
|
||||||
|
#include "../String/string.h"
|
||||||
|
|
||||||
|
typedef char* FilePath;
|
||||||
|
Array_declare(FilePath);
|
||||||
|
typedef FILE File;
|
||||||
|
ktid_declare(File);
|
||||||
|
|
||||||
|
typedef enum FileOpenMode{
|
||||||
|
// open a file for reading
|
||||||
|
FileOpenMode_Read=1,
|
||||||
|
// (re)create a file for writing
|
||||||
|
FileOpenMode_Write=2,
|
||||||
|
// opens file for writing additional data to the end / creates new file
|
||||||
|
FileOpenMode_Append=4,
|
||||||
|
// (re)creates file for reading/writing
|
||||||
|
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
|
||||||
|
} FileOpenMode;
|
||||||
|
|
||||||
|
|
||||||
|
/// @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);
|
||||||
|
|
||||||
|
/// @brief closes file descriptor
|
||||||
|
/// @return Maybe<void>
|
||||||
|
Maybe file_close(File* file);
|
||||||
|
|
||||||
|
/// @brief closes file descriptor
|
||||||
|
/// @param byte byte to write
|
||||||
|
/// @return Maybe<void>
|
||||||
|
Maybe file_writeChar(File* file, char byte);
|
||||||
|
|
||||||
|
/// @brief closes file descriptor
|
||||||
|
/// @param buffer bytes to write
|
||||||
|
/// @param length buffer length
|
||||||
|
/// @return Maybe<void>
|
||||||
|
Maybe file_writeBuffer(File* file, char* buffer, uint64 length);
|
||||||
|
|
||||||
|
/// @brief writes all cstring array content to file
|
||||||
|
/// @param cptr zero-terminated cstring
|
||||||
|
/// @return Maybe<void>
|
||||||
|
Maybe file_writeCptr(File* file, char* cptr);
|
||||||
|
|
||||||
|
|
||||||
|
/// @brief reads single byte from file
|
||||||
|
/// @return Maybe<char>
|
||||||
|
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<uint64> 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<uint64> total number of successfully read bytes
|
||||||
|
Maybe file_readAll(File* file, char** allBytes);
|
||||||
|
|
||||||
|
#if __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@ -1,22 +1,3 @@
|
|||||||
#pragma once
|
#include "path.h"
|
||||||
|
#include "dir.h"
|
||||||
#if __cplusplus
|
#include "file.h"
|
||||||
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
|
|
||||||
|
|||||||
@ -36,3 +36,7 @@ char* __path_concat(uint16 n, char* prev_part, ...){
|
|||||||
free(lengths);
|
free(lengths);
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char* path_fixSeparators(char* path){
|
||||||
|
|
||||||
|
}
|
||||||
20
src/Filesystem/path.h
Normal file
20
src/Filesystem/path.h
Normal file
@ -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
|
||||||
Loading…
Reference in New Issue
Block a user