Hermes
file_system.h
Go to the documentation of this file.
1 
32 #ifndef HERMES_FILE_SYSTEM_H
33 #define HERMES_FILE_SYSTEM_H
34 
35 #include <hermes/common/defs.h>
36 #include <hermes/common/str.h>
38 #include <vector>
39 #include <string>
40 #include <ostream>
41 
42 namespace hermes {
43 
44 // *********************************************************************************************************************
45 // Path
46 // *********************************************************************************************************************
48 class Path {
49 public:
50  // *******************************************************************************************************************
51  // CONSTRUCTORS
52  // *******************************************************************************************************************
54  Path() = default;
57  Path(const Str &path);
60  Path(const char *const &&path);
63  Path(std::string path);
66  Path(const Path &other);
68  Path(Path &&other) noexcept;
69  // *******************************************************************************************************************
70  // OPERATORS
71  // *******************************************************************************************************************
72  // casting
75  explicit operator std::string() const { return path_.str(); }
76  // assignment
80  Path &operator=(const Path &path);
81  // boolean
85  bool operator==(const Path &b) const;
86  // arithmetic
90  Path operator+(const Str &b) const;
94  Path operator/(const Path &other) const;
98  Path &operator/=(const Path &other);
102  Path &operator+=(const std::string &other);
106  Path &operator+=(const Path &other);
107  // *******************************************************************************************************************
108  // METHODS
109  // *******************************************************************************************************************
113  Path &join(const Path &path);
116  Path &cd(const std::string &path);
119  [[nodiscard]] bool exists() const;
122  [[nodiscard]] bool isDirectory() const;
125  [[nodiscard]] bool isFile() const;
128  [[nodiscard]] bool hasExtension() 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 &regular_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;
165  // *******************************************************************************************************************
166  // PUBLIC FIELDS
167  // *******************************************************************************************************************
168  std::string separator{"/"};
169 private:
170  Str path_;
171 };
172 
173 // *********************************************************************************************************************
174 // ls_options
175 // *********************************************************************************************************************
177 enum class ls_options {
178  none = 0x0,
179  sort = 0x1,
180  reverse_sort = 0x2,
181  directories = 0x4,
182  files = 0x8,
183  group_directories_first = 0x10,
184  recursive = 0x20,
185 };
187 // *********************************************************************************************************************
188 // find_options
189 // *********************************************************************************************************************
191 enum class find_options {
192  none = 0x0,
193  recursive = 0x1,
194  sort = 0x2,
195 };
196 HERMES_ENABLE_BITMASK_OPERATORS(find_options);
197 
198 // *********************************************************************************************************************
199 // FileSystem
200 // *********************************************************************************************************************
202 class FileSystem {
203 public:
204  // *******************************************************************************************************************
205  // STATIC METHODS
206  // *******************************************************************************************************************
207  // path structure
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);
227  // file read
232  static u64 readFile(const char *filename, char **text);
236  static std::vector<unsigned char> readBinaryFile(const Path &filename);
240  static std::vector<std::string> readLines(const Path &path);
244  static std::string readFile(const Path &filename);
245  // file write
249  static bool touch(const Path &path_to_file);
255  static u64 writeFile(const Path &path,
256  const std::vector<char> &content, bool is_binary = false);
262  static u64 writeFile(const Path &path,
263  const std::string &content, bool is_binary = false);
269  static u64 writeLine(const Path &path, const std::string &line, bool is_binary = false);
270  // file append
276  static u64 appendToFile(const Path &path,
277  const std::vector<char> &content, bool is_binary = false);
283  static u64 appendToFile(const Path &path,
284  const std::string &content, bool is_binary = false);
290  static u64 appendLine(const Path &path, const std::string &line, bool is_binary = false);
291  // queries
295  static bool fileExists(const Path &path);
299  static bool isFile(const Path &path);
303  static bool isDirectory(const Path &dir_name);
304  // directories
316  static std::vector<Path> ls(const Path &path, ls_options options = ls_options::none);
320  static bool mkdir(const Path &path);
325  static bool copyFile(const Path &source, const Path &destination);
326  // search
334  static std::vector<Path> find(const Path &path,
335  const std::string &pattern,
336  find_options options = find_options::none);
337 };
338 
339 // *********************************************************************************************************************
340 // IO
341 // *********************************************************************************************************************
346 std::ostream &operator<<(std::ostream &o, const Path &path);
347 
348 }
349 
350 #endif //HERMES_FILE_SYSTEM_H
351 
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 &regular_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
Data type definitions.
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
@ none
default behaviour
@ files
list only files
@ 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
String utils.