31 #ifndef HELPER_FILESYSTEM_HPP_ 32 #define HELPER_FILESYSTEM_HPP_ 34 #include "../Main/Exception.hpp" 42 #include <string_view> 55 bool exists(std::string_view path);
65 std::string_view pathToDir,
66 std::string_view fileExtension
68 bool contains(std::string_view pathToDir, std::string_view pathToCheck);
113 inline bool exists(std::string_view path) {
117 catch(
const stdfs::filesystem_error& e) {
118 std::string exceptionString{
"Could not check the existence of the path '"};
120 exceptionString += path;
121 exceptionString +=
"': ";
122 exceptionString += e.what();
139 const stdfs::path dir(path);
144 catch(
const stdfs::filesystem_error& e) {
145 std::string exceptionString{
"Could not check the existence of the directory '"};
147 exceptionString += path;
148 exceptionString +=
"': ";
149 exceptionString += e.what();
166 const stdfs::path file(path);
171 catch(
const stdfs::filesystem_error& e) {
172 std::string exceptionString{
"Could not check the validity of the file '"};
174 exceptionString += path;
175 exceptionString +=
"': ";
176 exceptionString += e.what();
188 return stdfs::path::preferred_separator;
210 std::string_view pathToDir,
211 std::string_view fileExtension
213 std::vector<std::string> result;
216 const stdfs::path path(pathToDir);
219 std::string exceptionString{
"'"};
221 exceptionString += pathToDir;
222 exceptionString +=
"' does not exist";
227 if(!stdfs::is_directory(path)) {
228 std::string exceptionString{
"'"};
230 exceptionString += pathToDir;
231 exceptionString +=
"' is not a directory";
238 for(
const auto& it: stdfs::recursive_directory_iterator(path)) {
239 if(fileExtension.empty() || it.path().extension().string() == fileExtension) {
240 result.emplace_back(it.path().string());
244 catch(
const stdfs::filesystem_error& e) {
245 std::string exceptionString{
"Could not iterate over the files in '"};
247 exceptionString += pathToDir;
248 exceptionString +=
"': ";
249 exceptionString += e.what();
270 inline bool contains(std::string_view pathToDir, std::string_view pathToCheck) {
272 std::string exceptionString{
"'"};
274 exceptionString += pathToDir;
275 exceptionString +=
"' is not a valid directory";
282 const auto absPathToDir{
283 stdfs::canonical(pathToDir)
286 #ifndef INCLUDE_STD_FILESYSTEM_EXPERIMENTAL 289 stdfs::weakly_canonical(pathToCheck)
293 auto completePathToCheck{
294 stdfs::absolute(pathToCheck)
297 stdfs::path absPathToCheck;
299 for(
const auto& segment : completePathToCheck) {
300 if(segment ==
"..") {
301 absPathToCheck = absPathToCheck.parent_path();
303 else if(segment ==
".") {
307 absPathToCheck /= segment;
310 if(stdfs::is_symlink(absPathToCheck)) {
311 std::string exceptionString{
312 "Path contains unsupported symlink: '" 315 exceptionString += absPathToCheck;
316 exceptionString +=
"'";
324 if(absPathToCheck.has_filename()) {
325 absPathToCheck.remove_filename();
330 std::distance(absPathToDir.begin(), absPathToDir.end())
331 > std::distance(absPathToCheck.begin(), absPathToCheck.end())
338 return std::equal(absPathToDir.begin(), absPathToDir.end(), absPathToCheck.begin());
340 catch(
const stdfs::filesystem_error& e) {
341 std::string exceptionString{
342 "Could not make paths absolute: '" 345 exceptionString += pathToDir;
346 exceptionString +=
"' and '";
347 exceptionString += pathToCheck;
348 exceptionString +=
"': ";
349 exceptionString += e.what();
367 stdfs::create_directory(pathToDir);
369 catch(
const stdfs::filesystem_error& e) {
370 std::string exceptionString{
"Could not create directory '"};
372 exceptionString += pathToDir;
373 exceptionString +=
"': ";
374 exceptionString += e.what();
407 std::string exceptionString{
"'"};
409 exceptionString += pathToDir;
410 exceptionString +=
"' is not a valid directory";
415 const stdfs::path dir(pathToDir);
417 for(stdfs::directory_iterator it(pathToDir), endIt; it != endIt; ++it) {
419 stdfs::remove_all(it->path());
421 catch(
const stdfs::filesystem_error& e) {
422 std::string exceptionString{
"Could not remove '"};
424 exceptionString += it->path();
425 exceptionString +=
"' with all its subdirectories: ";
426 exceptionString += e.what();
445 return stdfs::space(path).available;
447 catch(
const stdfs::filesystem_error& e) {
448 std::string exceptionString{
"Could not get the available disk space at '"};
450 exceptionString += path;
451 exceptionString +=
"': ";
452 exceptionString += e.what();
bool isValidFile(std::string_view path)
Checks whether the given path points to a valid file.
Definition: FileSystem.hpp:165
void createDirectory(std::string_view pathToDir)
Creates a directory at the given path.
Definition: FileSystem.hpp:365
bool contains(std::string_view pathToDir, std::string_view pathToCheck)
Checks whether the given path is located inside the given directory.
Definition: FileSystem.hpp:270
Class for file system exceptions.
Definition: FileSystem.hpp:97
#define MAIN_EXCEPTION_CLASS()
Macro used to easily define classes for general exceptions.
Definition: Exception.hpp:50
Namespace for global file system helper functions.
Definition: FileSystem.hpp:46
bool isValidDirectory(std::string_view path)
Checks whether the given path points to a valid directory.
Definition: FileSystem.hpp:138
bool exists(std::string_view path)
Checks whether the specified path exists.
Definition: FileSystem.hpp:113
void clearDirectory(std::string_view pathToDir)
Deletes all files and folders in the given directory.
Definition: FileSystem.hpp:405
char getPathSeparator()
Gets the preferred separator for file paths in the current operating system.
Definition: FileSystem.hpp:187
std::uintmax_t getFreeSpace(std::string_view path)
Gets the available disk space at the given location in bytes.
Definition: FileSystem.hpp:443
std::vector< std::string > listFilesInPath(std::string_view pathToDir, std::string_view fileExtension)
Lists all files with the given extension in the given directory and its subdirectories.
Definition: FileSystem.hpp:209
void createDirectoryIfNotExists(std::string_view pathToDir)
Creates a directory at the given path, if it does not exist already.
Definition: FileSystem.hpp:390