rcpputils
master
C++ API providing common utilities and data structures.
|
Go to the documentation of this file.
44 #ifndef RCPPUTILS__FILESYSTEM_HELPER_HPP_
45 #define RCPPUTILS__FILESYSTEM_HELPER_HPP_
62 # define RCPPUTILS_IMPL_OS_DIRSEP '\\'
64 # define RCPPUTILS_IMPL_OS_DIRSEP '/'
72 # define access _access_s
75 # include <sys/types.h>
110 : path_(p), path_as_vector_(
split(p, kPreferredSeparator))
138 return access(path_.
c_str(), 0) == 0;
148 struct stat stat_buffer;
149 const auto rc = stat(path_.
c_str(), &stat_buffer);
156 return (stat_buffer.st_mode & S_IFDIR) == S_IFDIR;
158 return S_ISDIR(stat_buffer.st_mode);
169 struct stat stat_buffer;
170 const auto rc = stat(path_.
c_str(), &stat_buffer);
177 return (stat_buffer.st_mode & S_IFREG) == S_IFREG;
179 return S_ISREG(stat_buffer.st_mode);
192 auto ec = std::make_error_code(std::errc::is_a_directory);
196 struct stat stat_buffer;
197 const auto rc = stat(path_.
c_str(), &stat_buffer);
204 return static_cast<uint64_t
>(stat_buffer.st_size);
215 return path_.
empty();
225 return path_.
size() > 0 &&
227 this->is_absolute_with_drive_letter());
237 return path_as_vector_.
cbegin();
247 return path_as_vector_.
cend();
264 if (1u == path_as_vector_.
size()) {
267 if (this->is_absolute_with_drive_letter()) {
268 return path(path_as_vector_[0] + kPreferredSeparator);
277 if (2u == path_as_vector_.
size() && this->is_absolute_with_drive_letter()) {
278 return path(path_as_vector_[0] + kPreferredSeparator);
282 for (
auto it = this->
cbegin(); it != --this->
cend(); ++it) {
283 if (!parent.
empty() || it->empty()) {
311 const char * delimiter =
".";
312 auto split_fname =
split(this->
string(), *delimiter);
313 return split_fname.size() == 1 ?
path(
"") :
path(
"." + split_fname.back());
347 return path(*this).operator/=(other);
359 this->path_ = other.path_;
360 this->path_as_vector_ = other.path_as_vector_;
362 this->path_ += kPreferredSeparator + other.
string();
363 this->path_as_vector_.
insert(
372 bool is_absolute_with_drive_letter()
const
378 return 0 == path_.
compare(1, 2,
":\\");
396 return p.is_regular_file();
407 return p.is_directory();
431 return path_to_check.
exists();
444 #error "rcpputils::fs does not support Unicode paths"
446 TCHAR temp_path[MAX_PATH];
447 DWORD size = GetTempPathA(MAX_PATH, temp_path);
448 if (size > MAX_PATH || size == 0) {
452 temp_path[size] =
'\0';
454 const char * temp_path = getenv(
"TMPDIR");
459 return path(temp_path);
473 #error "rcpputils::fs does not support Unicode paths"
476 if (
nullptr == _getcwd(cwd, MAX_PATH)) {
479 if (
nullptr == getcwd(cwd, PATH_MAX)) {
500 for (
auto it = p.
cbegin(); it != p.
cend() && status == 0; ++it) {
501 if (!p_built.
empty() || it->empty()) {
510 status = mkdir(p_built.
string().
c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
528 if (s.st_mode & S_IFDIR) {
531 if (s.st_mode & S_IFREG) {
556 TCHAR * temp_dir =
new TCHAR[length + 2];
558 temp_dir[length] =
'\0';
559 temp_dir[length + 1] =
'\0';
561 SHFILEOPSTRUCT file_options;
562 file_options.hwnd =
nullptr;
563 file_options.wFunc = FO_DELETE;
564 file_options.pFrom = temp_dir;
565 file_options.pTo =
nullptr;
566 file_options.fFlags = FOF_NOCONFIRMATION | FOF_SILENT;
567 file_options.fAnyOperationsAborted = FALSE;
568 file_options.lpszProgressTitle =
nullptr;
569 file_options.hNameMappings =
nullptr;
571 auto ret = SHFileOperation(&file_options);
574 return 0 == ret &&
false == file_options.fAnyOperationsAborted;
577 struct dirent * directory_entry;
578 while ((directory_entry = readdir(dir)) !=
nullptr) {
580 if (strcmp(directory_entry->d_name,
".") != 0 && strcmp(directory_entry->d_name,
"..") != 0) {
583 if (sub_path.is_directory() && !
remove_all(sub_path)) {
586 }
else if (!
remove(sub_path)) {
609 path new_path(file_path);
610 for (
int i = 0; i < n_times; i++) {
611 const auto new_path_str = new_path.
string();
613 if (last_dot == std::string::npos) {
616 new_path =
path(new_path_str.substr(0, last_dot));
621 #undef RCPPUTILS_IMPL_OS_DIRSEP
626 #endif // RCPPUTILS__FILESYSTEM_HELPER_HPP_
bool exists() const
Check if this path exists.
Definition: filesystem_helper.hpp:136
bool is_regular_file(const path &p) noexcept
Check if the path is a regular file.
Definition: filesystem_helper.hpp:394
path operator/(const path &other)
Concatenate two paths together.
Definition: filesystem_helper.hpp:345
bool remove(const path &p)
Remove the file or directory at the path p.
Definition: filesystem_helper.hpp:523
path temp_directory_path()
Get a path to a location in the temporary directory, if it's available.
Definition: filesystem_helper.hpp:440
uint64_t file_size(const path &p)
Get the file size of the path.
Definition: filesystem_helper.hpp:418
Definition: asserts.hpp:37
#define RCPPUTILS_IMPL_OS_DIRSEP
Definition: filesystem_helper.hpp:64
path current_path()
Return current working directory.
Definition: filesystem_helper.hpp:469
Split string by provided delimiter.
path filename() const
Get the last element in this path.
Definition: filesystem_helper.hpp:299
path parent_path() const
Get the parent directory of this path.
Definition: filesystem_helper.hpp:255
path & operator/=(const path &other)
Append a string component to this path.
Definition: filesystem_helper.hpp:356
std::vector< std::string >::const_iterator cend() const
Definition: filesystem_helper.hpp:245
bool empty() const
Check if the path is empty.
Definition: filesystem_helper.hpp:213
T system_category(T... args)
bool is_absolute() const
Check if the path is an absolute path.
Definition: filesystem_helper.hpp:223
path remove_extension(const path &file_path, int n_times=1)
Remove extension(s) from a path.
Definition: filesystem_helper.hpp:607
path(const std::string &p)
Conversion constructor from a std::string path.
Definition: filesystem_helper.hpp:109
uint64_t file_size() const
Return the size of the file in bytes.
Definition: filesystem_helper.hpp:189
void split(const std::string &input, char delim, InsertIterator &it, bool skip_empty=false)
Split a specified input into tokens using a delimiter and a type erased insert iterator.
Definition: split.hpp:68
T find_last_of(T... args)
bool is_directory(const path &p) noexcept
Check if the path is a directory.
Definition: filesystem_helper.hpp:405
std::string string() const
Get the path delimited using this system's path separator.
Definition: filesystem_helper.hpp:126
path operator/(const std::string &other)
Concatenate a path and a string into a single path.
Definition: filesystem_helper.hpp:322
Drop-in replacement for std::filesystem::path.
Definition: filesystem_helper.hpp:94
std::vector< std::string >::const_iterator cbegin() const
Const iterator to first element of this path.
Definition: filesystem_helper.hpp:235
path extension() const
Get a relative path to the component including and following the last '.'.
Definition: filesystem_helper.hpp:309
bool exists(const path &path_to_check)
Check if a path exists.
Definition: filesystem_helper.hpp:429
bool create_directories(const path &p)
Create a directory with the given path p.
Definition: filesystem_helper.hpp:495
bool is_directory() const noexcept
Check if the path exists and it is a directory.
Definition: filesystem_helper.hpp:146
path()
Constructs an empty path.
Definition: filesystem_helper.hpp:100
bool remove_all(const path &p)
Remove the directory at the path p and its content.
Definition: filesystem_helper.hpp:549
path & operator/=(const std::string &other)
Append a string component to this path.
Definition: filesystem_helper.hpp:333
bool is_regular_file() const noexcept
Check if the path is a regular file.
Definition: filesystem_helper.hpp:167