32 #ifndef HERMES_FILE_SYSTEM_H
33 #define HERMES_FILE_SYSTEM_H
60 Path(
const char *
const &&path);
63 Path(std::string path);
75 explicit operator std::string()
const {
return path_.
str(); }
116 Path &
cd(
const std::string &path);
119 [[nodiscard]]
bool exists()
const;
125 [[nodiscard]]
bool isFile()
const;
131 [[nodiscard]] std::vector<std::string>
parts()
const;
134 [[nodiscard]] std::string
name()
const;
137 [[nodiscard]]
const std::string &
fullName()
const;
140 [[nodiscard]] std::string
extension()
const;
144 [[nodiscard]]
bool match_r(
const std::string ®ular_expression)
const;
147 [[nodiscard]]
Path cwd()
const;
150 [[nodiscard]]
bool mkdir()
const;
153 [[nodiscard]]
bool touch()
const;
157 [[nodiscard]]
u64 writeTo(
const std::string &content)
const;
161 [[nodiscard]]
u64 appendTo(
const std::string &content)
const;
164 [[nodiscard]] std::string
read()
const;
212 static std::vector<std::string>
basename(
const std::vector<std::string> &paths,
const std::string &suffix =
"");
217 static std::string
basename(
const std::string &path,
const std::string &suffix =
"");
221 static std::string
fileExtension(
const std::string &filename);
226 static std::string
normalizePath(
const std::string &path,
bool with_backslash =
false);
232 static u64 readFile(
const char *filename,
char **text);
249 static bool touch(
const Path &path_to_file);
256 const std::vector<char> &content,
bool is_binary =
false);
263 const std::string &content,
bool is_binary =
false);
269 static u64 writeLine(
const Path &path,
const std::string &line,
bool is_binary =
false);
277 const std::vector<char> &content,
bool is_binary =
false);
284 const std::string &content,
bool is_binary =
false);
290 static u64 appendLine(
const Path &path,
const std::string &line,
bool is_binary =
false);
316 static std::vector<Path>
ls(
const Path &path,
ls_options options = ls_options::none);
334 static std::vector<Path>
find(
const Path &path,
335 const std::string &pattern,
346 std::ostream &operator<<(std::ostream &o,
const Path &path);
Support of bitwise operations for compatible enum classes.
#define HERMES_ENABLE_BITMASK_OPERATORS(x)
Adds bitwise operation support to a given enum class.
Definition: bitmask_operators.h:58
Set of useful functions to manipulate files and directories.
Definition: file_system.h:202
static std::vector< std::string > basename(const std::vector< std::string > &paths, const std::string &suffix="")
Strips directory and suffix from filenames.
Definition: file_system.cpp:218
static bool isFile(const Path &path)
Checks if filename corresponds to a file.
Definition: file_system.cpp:412
static u64 readFile(const char *filename, char **text)
loads contents from file
Definition: file_system.cpp:256
static std::vector< Path > ls(const Path &path, ls_options options=ls_options::none)
Lists files inside a directory.
Definition: file_system.cpp:426
static bool mkdir(const Path &path)
Recursively creates the path of directories.
Definition: file_system.cpp:486
static u64 appendToFile(const Path &path, const std::vector< char > &content, bool is_binary=false)
Appends content to file.
Definition: file_system.cpp:373
static std::vector< std::string > readLines(const Path &path)
Read file's contents separated by line breaks.
Definition: file_system.cpp:308
static std::string fileExtension(const std::string &filename)
Retrieves file's extension.
Definition: file_system.cpp:225
static u64 appendLine(const Path &path, const std::string &line, bool is_binary=false)
Appends line to file.
Definition: file_system.cpp:399
static u64 writeFile(const Path &path, const std::vector< char > &content, bool is_binary=false)
Writes content to file.
Definition: file_system.cpp:334
static std::vector< unsigned char > readBinaryFile(const Path &filename)
loads binary content from file
Definition: file_system.cpp:284
static std::vector< Path > find(const Path &path, const std::string &pattern, find_options options=find_options::none)
Search for files in a directory hierarchy.
Definition: file_system.cpp:580
static bool fileExists(const Path &path)
Checks if file exists.
Definition: file_system.cpp:317
static std::string normalizePath(const std::string &path, bool with_backslash=false)
Fixes path separators and ".." parts.
Definition: file_system.cpp:535
static bool copyFile(const Path &source, const Path &destination)
Copy file's contents to destination file.
Definition: file_system.cpp:559
static bool isDirectory(const Path &dir_name)
Checks if dir_name corresponds to a directory.
Definition: file_system.cpp:419
static u64 writeLine(const Path &path, const std::string &line, bool is_binary=false)
Writes line to path.
Definition: file_system.cpp:360
static bool touch(const Path &path_to_file)
Creates an empty file or access it.
Definition: file_system.cpp:325
Representation of a directory/file in the filesystem.
Definition: file_system.h:48
bool operator==(const Path &b) const
Full comparison.
Definition: file_system.cpp:193
std::string read() const
Reads ascii content from this file.
Definition: file_system.cpp:181
Path & operator+=(const std::string &other)
Joins with separator.
Definition: file_system.cpp:74
Path operator+(const Str &b) const
Generates copy concatenated with separator.
Definition: file_system.cpp:187
Path & cd(const std::string &path)
Jump to path.
Definition: file_system.cpp:84
Path operator/(const Path &other) const
Generates copy concatenated with separator.
Definition: file_system.cpp:196
bool hasExtension() const
Definition: file_system.cpp:130
std::vector< std::string > parts() const
Splits this path into a list.
Definition: file_system.cpp:135
bool exists() const
Check if this path exists in filesystem.
Definition: file_system.cpp:118
Path & operator=(const Path &path)
Copy assignment.
Definition: file_system.cpp:69
bool match_r(const std::string ®ular_expression) const
Checks if this path matches pattern.
Definition: file_system.cpp:151
const std::string & fullName() const
Gets this full path string.
Definition: file_system.cpp:143
Path & operator/=(const Path &other)
Joins with separator.
Definition: file_system.cpp:200
std::string extension() const
Gets file extension.
Definition: file_system.cpp:147
u64 writeTo(const std::string &content) const
Writes content into this file.
Definition: file_system.cpp:171
Path & join(const Path &path)
Joins with separator.
Definition: file_system.cpp:112
Path()=default
Default constructor.
bool touch() const
Creates empty file from this path.
Definition: file_system.cpp:167
std::string name() const
Gets last folder/file name.
Definition: file_system.cpp:139
std::string separator
OS path separator.
Definition: file_system.h:168
bool isDirectory() const
Check if this path exists and represents a folder.
Definition: file_system.cpp:122
u64 appendTo(const std::string &content) const
Appends to this file.
Definition: file_system.cpp:176
bool isFile() const
Check if this path exists and represents a file.
Definition: file_system.cpp:126
Path cwd() const
Gets this path's folder location.
Definition: file_system.cpp:156
bool mkdir() const
Creates folder from this path.
Definition: file_system.cpp:163
String class and set of string functions.
Definition: str.h:53
const std::string & str() const
Get std::string object.
Definition: str.h:381
find_options
list of options for find the method
Definition: file_system.h:191
ls_options
List of options for ls the method.
Definition: file_system.h:177
@ reverse_sort
sorts in reverse order
@ group_directories_first
list directories first
@ directories
list only directories
@ sort
sorts results in lexicographical order
@ recursive
list recursively
uint64_t u64
64 bit size unsigned integer type
Definition: defs.h:89