Files CCore/inc/FileSystem.h CCore/src/FileSystem.cpp
The class FileSystem implements the standard list of file system operations on the local file system.
class FileSystem : NoCopy
{
Sys::FileSystem fs;
public:
FileSystem();
~FileSystem();
FileType getFileType(StrLen path);
CmpFileTimeType getFileUpdateTime(StrLen path);
void createFile(StrLen file_name);
void deleteFile(StrLen file_name);
void createDir(StrLen dir_name);
void deleteDir(StrLen dir_name,bool recursive);
void rename(StrLen old_path,StrLen new_path,bool allow_overwrite);
void remove(StrLen path);
void exec(StrLen dir,StrLen program,StrLen arg);
StrLen pathOf(StrLen path,char buf[MaxPathLen+1]);
class DirCursor;
};
Methods throw exceptions in case of errors.
There are extra operations, provided by this class.
exec() runs a program. dir is the working directory, program is the file of the program, arg is the command line.
pathOf() converts path to the full path using the provided buffer and returns the result.
The class DirCursor can list a directory content.
class FileSystem::DirCursor : NoCopy
{
Sys::FileSystem::DirCursor cur;
bool ok;
public:
DirCursor(FileSystem &fs,StrLen dir_name);
~DirCursor();
bool next();
StrLen getFileName() const;
FileType getFileType() const;
template <class FuncInit>
void apply(FuncInit func_init); // func(StrLen file_name,FileType file_type)
};
DirCursor constructor creates a directory enumerator. The first argument is the reference to the FileSystem object, the second is the directory name.
next() moves to the next file. Initially the current file is the "before-the-first" file.
getFileName() is the name of the current file (path is not included).
getFileType() is the type of the current file.
apply() applies the given functor on the file name list.
An example:
FileSystem fs; DirCursor cur(fs,"C:/"); while( cur.next() ) { Printf(Con,"#; #;\n",cur.getFileName(),cur.getFileType()); }
These classes are built upon the target classes Sys::FileSystem and Sys::FileSystem::DirCursor, declared in the header sys/SysFileSystem.h.
struct Sys::FileSystem
{
// private
....
// public
struct TypeResult
{
FileType type;
FileError error;
};
struct CmpTimeResult
{
CmpFileTimeType time;
FileError error;
};
struct PathOfResult
{
StrLen path;
FileError error;
};
struct DirCursor
{
....
// public
char file_name[MaxPathLen]; // file name only
ulen len;
FileType type;
FileError error;
void init(FileSystem *fs,StrLen dir_name) noexcept;
void exit() noexcept;
bool next() noexcept;
};
// public
FileError init() noexcept;
FileError exit() noexcept;
TypeResult getFileType(StrLen path) noexcept;
CmpTimeResult getFileUpdateTime(StrLen path) noexcept;
FileError createFile(StrLen file_name) noexcept;
FileError deleteFile(StrLen file_name) noexcept;
FileError createDir(StrLen dir_name) noexcept;
FileError deleteDir(StrLen dir_name,bool recursive) noexcept;
FileError rename(StrLen old_path,StrLen new_path,bool allow_overwrite) noexcept;
FileError remove(StrLen path) noexcept;
FileError exec(StrLen dir,StrLen program,StrLen arg) noexcept;
PathOfResult pathOf(StrLen path,char buf[MaxPathLen+1]) noexcept;
};