6 #ifndef JLM_UTIL_FILE_HPP
7 #define JLM_UTIL_FILE_HPP
30 :
path_(std::move(other.path_))
49 path_ = std::move(other.path_);
60 [[nodiscard]] std::string
64 auto pos = fn.find_first_of(
".");
65 if (pos == std::string::npos)
68 return fn.substr(0, pos);
80 [[nodiscard]] std::string
83 auto pos =
path_.find_last_of(
"/");
84 if (pos == std::string::npos)
87 return path_.substr(pos + 1,
path_.size() - pos);
97 [[nodiscard]] std::string
101 auto pos = fn.find_first_of(
".");
102 if (pos == std::string::npos)
105 return fn.substr(pos + 1, fn.size() - pos);
115 [[nodiscard]] std::string
119 auto pos = fn.find_last_of(
".");
120 if (pos == std::string::npos)
123 return fn.substr(pos + 1, fn.size() - pos);
156 size_t lastSlash = std::string::npos;
157 if (
path_.size() >= 1)
158 lastSlash =
path_.find_last_of(
'/',
path_.size() - 2);
161 if (lastSlash == std::string::npos)
193 Join(
const std::string & other)
const
195 std::filesystem::path t(
path_ ==
"." ?
"" :
path_.c_str());
224 auto fileStatus = std::filesystem::status(
path_);
225 return std::filesystem::exists(fileStatus);
235 auto fileStatus = std::filesystem::status(
path_);
236 return std::filesystem::is_directory(fileStatus);
246 auto fileStatus = std::filesystem::status(
path_);
247 return std::filesystem::is_regular_file(fileStatus);
265 throw Error(
"parent directory is not a directory: " + baseDir.
to_str());
268 std::filesystem::create_directory(
path_, ec);
271 throw Error(
"could not create directory '" +
path_ +
"': " + ec.message());
274 [[nodiscard]]
const std::string &
283 return path_ == other.path_;
303 const std::string & fileNamePrefix,
304 const std::string & fileNameSuffix)
307 FilePath filePath(directory.
to_str() +
"/" + fileNamePrefix + randomString + fileNameSuffix);
319 return FilePath(std::filesystem::temp_directory_path().
string());
343 path_(std::move(other.path_))
358 path_ = std::move(other.path_);
static FilePath createUniqueFileName(const FilePath &directory, const std::string &fileNamePrefix, const std::string &fileNameSuffix)
Generates a unique file in a given directory with a prefix and suffix.
FilePath Join(const FilePath &other) const
std::string name() const noexcept
Returns the name of the file, excluding the path.
void CreateDirectory() const
bool IsDirectory() const noexcept
Determines whether file path is a directory.
static FilePath TempDirectoryPath()
std::string complete_suffix() const noexcept
Returns the complete suffix (extension) of the file.
const std::string & to_str() const noexcept
FilePath(const FilePath &other)
FilePath(const std::string &path)
FilePath Join(const std::string &other) const
bool operator==(const std::string &f) const noexcept
bool Exists() const noexcept
Determines whether the file path exists.
std::string base() const noexcept
Returns the base name of the file without the path.
FilePath & operator=(const FilePath &other)
FilePath(FilePath &&other) noexcept
bool isFile() const noexcept
Determines whether file path is a file.
FilePath Dirname() const noexcept
Returns the path to the file or directory's parent directory. Emulates the behavior of the GNU coreut...
std::string suffix() const noexcept
Returns the suffix (extension) of the file.
FilePath WithSuffix(const std::string &suffix) const
bool operator==(const FilePath &other) const noexcept
FilePath & operator=(FilePath &&other) noexcept
File(const FilePath &path)
File(File &&other) noexcept
FILE * fd() const noexcept
bool is_open() const noexcept
void open(const char *mode)
File & operator=(const File &)=delete
const FilePath & path() const noexcept
File(const File &)=delete
File & operator=(File &&other) noexcept
std::string CreateRandomAlphanumericString(std::size_t length)