Devel Lib
Path.h
1 #pragma once
2 
3 #include <filesystem>
4 #include <utility>
5 #include <iterator>
6 
9 namespace Devel::IO {
10  namespace fs = std::filesystem;
11 
28  class CPath {
29  public:
31  CPath() = default;
32 
35  explicit CPath(fs::path i_oPath) : m_oPath(std::move(i_oPath)) {}
36 
39  CPath(const CPath &i_oPath) { this->operator=(i_oPath); }
40 
43  CPath(CPath &&i_oPath) noexcept { this->operator=(std::move(i_oPath)); }
44 
45  private:
48  fs::path m_oPath;
49  public:
52  void setPath(fs::path i_oPath) {
53  this->m_oPath = std::move(i_oPath);
54  }
55 
59  fs::path replaceFilename(const fs::path &i_oFilename) {
60  return this->m_oPath.replace_filename(i_oFilename);
61  }
62 
66  fs::path replaceExtension(const fs::path &i_oExtension) {
67  return this->m_oPath.replace_extension(i_oExtension);
68  }
69 
70  public:
73  [[nodiscard]] fs::path getRootName() const {
74  return this->m_oPath.root_name();
75  }
76 
79  [[nodiscard]] fs::path getRootPath() const {
80  return this->m_oPath.root_path();
81  }
82 
85  [[nodiscard]] fs::path getRelativePath() const {
86  return this->m_oPath.relative_path();
87  }
88 
91  [[nodiscard]] fs::path getParentPath() const {
92  return this->m_oPath.parent_path();
93  }
94 
97  [[nodiscard]] fs::path getFileNameWithoutExtension() const {
98  return this->m_oPath.stem();
99  }
100 
103  [[nodiscard]] fs::path getFileExtension() const {
104  return this->m_oPath.extension();
105  }
106 
109  [[nodiscard]] fs::path getAbsolutePath() const {
110  return fs::canonical(this->m_oPath);
111  }
112 
115  fs::path removeFilename() {
116  return this->m_oPath.remove_filename();
117  }
118 
121  [[nodiscard]] const fs::path &getPath() const {
122  return this->m_oPath;
123  }
124 
125  public:
128  [[nodiscard]] std::string toStdString() const {
129  return this->m_oPath.generic_string();
130  }
131 
134  [[nodiscard]] std::wstring toStdWString() const {
135  return this->m_oPath.wstring();
136  }
137 
138  public:
141  [[nodiscard]] bool hasRootName() const {
142  return this->m_oPath.has_root_name();
143  }
144 
147  [[nodiscard]] bool hasRootDirectory() const {
148  return this->m_oPath.has_root_directory();
149  }
150 
153  [[nodiscard]] bool hasRelativePath() const {
154  return this->m_oPath.has_relative_path();
155  }
156 
159  [[nodiscard]] bool hasParentPath() const {
160  return this->m_oPath.has_parent_path();
161  }
162 
165  [[nodiscard]] bool hasFilename() const {
166  return this->m_oPath.has_filename();
167  }
168 
171  [[nodiscard]] bool hasFileNameWithoutExtension() const {
172  return this->m_oPath.has_stem();
173  }
174 
177  [[nodiscard]] bool hasExtension() const {
178  return this->m_oPath.has_extension();
179  }
180 
183  [[nodiscard]] bool isAbsolutePath() const {
184  return this->m_oPath.is_absolute();
185  }
186 
189  [[nodiscard]] bool isRelativePath() const {
190  return this->m_oPath.is_relative();
191  }
192 
193  public:
196  auto begin() {
197  return this->m_oPath.begin();
198  }
199 
202  auto end() {
203  return this->m_oPath.end();
204  }
205 
206  public:
209  [[nodiscard]] bool exists() const {
210  return CPath::exists(this->m_oPath);
211  }
212 
213  public:
217  CPath &operator=(fs::path i_oPath) {
218  this->m_oPath = std::move(i_oPath);
219  return *this;
220  }
221 
225  CPath &operator=(const CPath &i_oPath) {
226  this->m_oPath.operator=(i_oPath.m_oPath);
227  return *this;
228  }
229 
233  CPath &operator=(CPath &&i_oPath) {
234  this->m_oPath = std::move(i_oPath.m_oPath);
235  return *this;
236  }
237 
241  CPath &operator+=(const fs::path &i_oPath) {
242  this->m_oPath.operator+=(i_oPath);
243  return *this;
244  }
245 
249  CPath &operator+=(const CPath &i_oPath) {
250  return this->operator+=(i_oPath.m_oPath);
251  }
252 
253  public:
257  static bool exists(const fs::path &i_oPath) {
258  return fs::exists(i_oPath);
259  }
260  };
261 
262 }
A class that encapsulates the functionality of std::filesystem::path.
Definition: Path.h:28
fs::path getRelativePath() const
Returns the relative path.
Definition: Path.h:85
fs::path removeFilename()
Removes the filename from the path.
Definition: Path.h:115
auto begin()
Returns an iterator to the beginning of the path.
Definition: Path.h:196
bool hasFileNameWithoutExtension() const
Checks if the path has a filename without extension.
Definition: Path.h:171
fs::path getRootName() const
Returns the root name of the path.
Definition: Path.h:73
CPath & operator=(const CPath &i_oPath)
Assigns a new path to the object.
Definition: Path.h:225
static bool exists(const fs::path &i_oPath)
Checks whether a path exists.
Definition: Path.h:257
fs::path getAbsolutePath() const
Returns the absolute path.
Definition: Path.h:109
bool hasRootName() const
Checks if the path has a root name.
Definition: Path.h:141
bool exists() const
Checks whether the path exists.
Definition: Path.h:209
fs::path replaceExtension(const fs::path &i_oExtension)
Replaces the extension in the path.
Definition: Path.h:66
std::string toStdString() const
Converts the path to a standard string.
Definition: Path.h:128
CPath()=default
Default constructor.
fs::path getParentPath() const
Returns the parent path.
Definition: Path.h:91
CPath & operator+=(const fs::path &i_oPath)
Appends a path to the current path.
Definition: Path.h:241
CPath(const CPath &i_oPath)
Copy constructor.
Definition: Path.h:39
fs::path getFileNameWithoutExtension() const
Returns the filename without extension.
Definition: Path.h:97
bool hasFilename() const
Checks if the path has a filename.
Definition: Path.h:165
fs::path getRootPath() const
Returns the root directory of the path.
Definition: Path.h:79
CPath(CPath &&i_oPath) noexcept
Move constructor.
Definition: Path.h:43
CPath & operator=(fs::path i_oPath)
Assigns a new path to the object.
Definition: Path.h:217
bool hasExtension() const
Checks if the path has an extension.
Definition: Path.h:177
CPath & operator+=(const CPath &i_oPath)
Appends a path to the current path.
Definition: Path.h:249
fs::path replaceFilename(const fs::path &i_oFilename)
Replaces the filename in the path.
Definition: Path.h:59
fs::path getFileExtension() const
Returns the file extension.
Definition: Path.h:103
bool isRelativePath() const
Checks if the path is relative.
Definition: Path.h:189
bool hasRelativePath() const
Checks if the path has a relative path.
Definition: Path.h:153
bool hasRootDirectory() const
Checks if the path has a root directory.
Definition: Path.h:147
bool isAbsolutePath() const
Checks if the path is absolute.
Definition: Path.h:183
void setPath(fs::path i_oPath)
Sets the path.
Definition: Path.h:52
auto end()
Returns an iterator to the end of the path.
Definition: Path.h:202
CPath(fs::path i_oPath)
Construct from a std::filesystem::path.
Definition: Path.h:35
const fs::path & getPath() const
Returns the encapsulated fs::path object.
Definition: Path.h:121
CPath & operator=(CPath &&i_oPath)
Assigns a new path to the object.
Definition: Path.h:233
std::wstring toStdWString() const
Converts the path to a standard wide string.
Definition: Path.h:134
bool hasParentPath() const
Checks if the path has a parent path.
Definition: Path.h:159
The namespace encapsulating I/O related classes and functions in the Devel framework.