11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
19PROMEKI_NAMESPACE_BEGIN
43 FilePath(
const String &path) : _path(path.str()) {}
49 FilePath(
const char *path) : _path(path) {}
55 FilePath(
const std::filesystem::path &path) : _path(path) {}
61 bool isEmpty()
const {
return _path.empty(); }
67 String fileName()
const {
return _path.filename().string(); }
73 String baseName()
const {
return _path.stem().string(); }
79 String suffix()
const {
80 auto ext = _path.extension().string();
81 if (ext.size() > 1)
return ext.substr(1);
91 String completeSuffix()
const {
92 String fn = _path.filename().string();
93 auto pos = fn.find(
'.');
94 if (pos == String::npos || pos == 0)
return String();
95 return fn.substr(pos + 1);
102 FilePath parent()
const {
return FilePath(_path.parent_path()); }
109 FilePath join(
const FilePath &other)
const {
return FilePath(_path / other._path); }
116 FilePath operator/(
const FilePath &other)
const {
return join(other); }
123 FilePath operator/(
const String &other)
const {
124 return FilePath(_path / std::filesystem::path(other.str()));
132 FilePath operator/(
const char *other)
const {
return FilePath(_path / std::filesystem::path(other)); }
138 bool exists()
const {
140 return std::filesystem::exists(_path, ec);
147 bool isAbsolute()
const {
return _path.is_absolute(); }
153 bool isRelative()
const {
return _path.is_relative(); }
159 FilePath absolutePath()
const {
161 return FilePath(std::filesystem::absolute(_path, ec));
176 Result<FilePath> canonicalPath()
const {
178 auto r = std::filesystem::canonical(_path, ec);
179 if (ec)
return Result<FilePath>(*
this, Error::Invalid);
180 return Result<FilePath>(FilePath(r), Error::Ok);
196 Result<FilePath> relativeTo(
const FilePath &base)
const {
198 auto r = std::filesystem::relative(_path, base._path, ec);
199 if (ec || r.empty())
return Result<FilePath>(*
this, Error::Invalid);
200 return Result<FilePath>(FilePath(r), Error::Ok);
207 String toString()
const {
return _path.string(); }
213 const std::filesystem::path &toStdPath()
const {
return _path; }
216 bool operator==(
const FilePath &other)
const {
return _path == other._path; }
219 bool operator!=(
const FilePath &other)
const {
return _path != other._path; }
222 bool operator<(
const FilePath &other)
const {
return _path < other._path; }
225 std::filesystem::path _path;
230PROMEKI_FORMAT_VIA_TOSTRING(promeki::FilePath);