libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
filepath.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <filesystem>
12#include <promeki/core/string.h>
13
15
27class FilePath {
28 public:
30 FilePath() = default;
31
36 FilePath(const String &path) : _path(path.str()) { }
37
42 FilePath(const char *path) : _path(path) { }
43
48 FilePath(const std::filesystem::path &path) : _path(path) { }
49
54 bool isEmpty() const {
55 return _path.empty();
56 }
57
62 String fileName() const {
63 return _path.filename().string();
64 }
65
70 String baseName() const {
71 return _path.stem().string();
72 }
73
78 String suffix() const {
79 auto ext = _path.extension().string();
80 if(ext.size() > 1) return ext.substr(1);
81 return String();
82 }
83
91 auto fn = _path.filename().string();
92 auto pos = fn.find('.');
93 if(pos == std::string::npos || pos == 0) return String();
94 return fn.substr(pos + 1);
95 }
96
101 FilePath parent() const {
102 return FilePath(_path.parent_path());
103 }
104
110 FilePath join(const FilePath &other) const {
111 return FilePath(_path / other._path);
112 }
113
120 return join(other);
121 }
122
129 return FilePath(_path / std::filesystem::path(other.str()));
130 }
131
137 FilePath operator/(const char *other) const {
138 return FilePath(_path / std::filesystem::path(other));
139 }
140
145 bool exists() const {
146 std::error_code ec;
147 return std::filesystem::exists(_path, ec);
148 }
149
154 bool isAbsolute() const {
155 return _path.is_absolute();
156 }
157
162 bool isRelative() const {
163 return _path.is_relative();
164 }
165
171 std::error_code ec;
172 return FilePath(std::filesystem::absolute(_path, ec));
173 }
174
179 String toString() const {
180 return _path.string();
181 }
182
187 const std::filesystem::path &toStdPath() const {
188 return _path;
189 }
190
192 bool operator==(const FilePath &other) const {
193 return _path == other._path;
194 }
195
197 bool operator!=(const FilePath &other) const {
198 return _path != other._path;
199 }
200
202 bool operator<(const FilePath &other) const {
203 return _path < other._path;
204 }
205
206 private:
207 std::filesystem::path _path;
208};
209
Simple value type wrapping std::filesystem::path.
Definition filepath.h:27
FilePath(const String &path)
Constructs a FilePath from a String.
Definition filepath.h:36
FilePath(const std::filesystem::path &path)
Constructs a FilePath from a std::filesystem::path.
Definition filepath.h:48
FilePath operator/(const String &other) const
Joins this path with a string path component.
Definition filepath.h:128
FilePath join(const FilePath &other) const
Joins this path with another path component.
Definition filepath.h:110
bool isAbsolute() const
Returns true if the path is absolute.
Definition filepath.h:154
FilePath operator/(const char *other) const
Joins this path with a C string path component.
Definition filepath.h:137
FilePath()=default
Constructs an empty file path.
bool operator!=(const FilePath &other) const
Inequality comparison.
Definition filepath.h:197
String toString() const
Converts the path to a String.
Definition filepath.h:179
bool exists() const
Returns true if the path exists on the filesystem.
Definition filepath.h:145
bool isEmpty() const
Returns true if the path is empty.
Definition filepath.h:54
String fileName() const
Returns the filename component (including extension).
Definition filepath.h:62
String completeSuffix() const
Returns the complete suffix (all extensions).
Definition filepath.h:90
String suffix() const
Returns the file extension without the leading dot.
Definition filepath.h:78
FilePath(const char *path)
Constructs a FilePath from a C string.
Definition filepath.h:42
bool isRelative() const
Returns true if the path is relative.
Definition filepath.h:162
FilePath operator/(const FilePath &other) const
Joins this path with another path component.
Definition filepath.h:119
FilePath absolutePath() const
Returns the absolute form of this path.
Definition filepath.h:170
bool operator<(const FilePath &other) const
Less-than comparison for ordered containers.
Definition filepath.h:202
String baseName() const
Returns the filename without its extension.
Definition filepath.h:70
const std::filesystem::path & toStdPath() const
Returns the underlying std::filesystem::path.
Definition filepath.h:187
bool operator==(const FilePath &other) const
Equality comparison.
Definition filepath.h:192
FilePath parent() const
Returns the parent directory as a FilePath.
Definition filepath.h:101
Dynamic array container wrapping std::vector.
Definition list.h:40
size_t size() const noexcept
Returns the number of elements in the list.
Definition list.h:301
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19