Veethree D7 SDK
Loading...
Searching...
No Matches
file_utils.h
Go to the documentation of this file.
1
5
6#ifndef FILE_UTILS_H
7#define FILE_UTILS_H
8
9#include <string>
10#include <string_view>
11#include <sys/stat.h>
12#include <system_error>
13
14namespace vsdk::utils {
15
20
24template<typename TString>
25concept StringLike = std::is_convertible_v<TString, std::string>
26 || std::is_convertible_v<TString, std::string_view>;
27
34std::string path_separator(const StringLike auto& base)
35{
36#ifdef _WIN32
37 return "\\" + std::string(base);
38#else
39 return "/" + std::string(base);
40#endif
41}
42
43// NOLINTBEGIN
54template<StringLike... Components>
55std::string path_join(const StringLike auto& base, Components... comp)
56{
57 return std::string(base) + (path_separator(comp) + ...);
58}
59// NOLINTEND
60
67std::errc file_remove(const std::string& path);
68
78std::errc mkdir(const std::string& path);
79
88std::errc rmdir(const std::string& path);
89
97std::string canonical_path(const std::string& path);
98
104
113inline bool file_exists(const std::string& path)
114{
115 struct stat st{};
116 return stat(path.c_str(), &st) == 0;
117}
118
120
121} // namespace vsdk::utils
122
123#endif // FILE_UTILS_H
Type constraint for string-like types.
Definition file_utils.h:25
std::string canonical_path(const std::string &path)
Get the canonical path. The canonical path will not contain dot, dotdot or symlinks.
std::errc mkdir(const std::string &path)
Make a directory. This behaves as mkdir -p. It will create intermediary path components automatically...
std::errc rmdir(const std::string &path)
Delete a directory recursively. Behaves as rm -rf.
std::string temp_directory_path()
Get the path for the temp directory on the OS. Similar semantics to std::filesystem::temp_directory_p...
std::errc file_remove(const std::string &path)
Remove a file.
std::string path_separator(const StringLike auto &base)
Utility for prepending OS-specific path separator. Needed for the path_join function.
Definition file_utils.h:34
std::string path_join(const StringLike auto &base, Components... comp)
Utility to construct OS-aware paths. Similar to Python's os.path.join(). On Windows the path componen...
Definition file_utils.h:55
bool file_exists(const std::string &path)
Utility to check if a path exists. Same semantics as std::filesystem::exists, but won't do character ...
Definition file_utils.h:113
Any entities that belong to the utils library.