ADTF  3.18.2
path.h
Go to the documentation of this file.
1 
15 #ifndef _A_UTILS_UTIL_FILESYSTEM_PATH_INCLUDED_
16 #define _A_UTILS_UTIL_FILESYSTEM_PATH_INCLUDED_
17 
19 
20 #include <memory>
21 #include <stdexcept>
22 
23 namespace a_util {
24 namespace filesystem {
25 // forward declarations
26 class Path;
27 Path getWorkingDirectory();
28 
30 class PathException : public std::runtime_error {
31 public:
36  PathException(const std::string& message);
37 };
38 
45 class Path {
46 public:
48  typedef enum {
51  PS_Native = 2
53 
54 public:
56  Path();
57 
62  Path(const Path& path);
63 
69  Path& operator=(const Path& path);
70 
76  Path(const std::string& path);
77 
83  Path(const char* path);
84 
86  ~Path();
87 
93  operator std::string() const;
94 
100  std::string toString(PathSeparator separator = PS_Native) const;
101 
107  bool setPath(const std::string& path);
108 
116  Path& append(const Path& path);
117 
126  DEV_ESSENTIAL_DEPRECATED("Use Path::getRootName() instead.")
127  Path getRoot() const; // expected-note {{'getRoot' has been explicitly marked deprecated here}}
128 
137  Path getRootName() const;
138 
147  Path getRootPath() const;
148 
155  Path getParent() const;
156 
163 
169  std::string getExtension() const;
170 
178  Path& appendToBasename(const std::string& str);
179 
187  Path& replaceExtension(const std::string& extension);
188 
195 
204 
210 
216  Path& makeAbsolute(Path base_path);
217 
223 
230  Path& makeRelative(Path input_path);
231 
232  // Makes the current path relative to a specified parent directory (default: working directory)
233  // @note If the path is already relative, no changes are made!
234  // @param [in] oParent The parent directory
235  // @return Returns *this, to be able to chain additional method calls
236  // Path& MakeRelative(const Path& oParent = filesystem::getWorkingDirectory());
237 
238  // Makes the current path absolute, using a specified parent directory (default: working
239  // directory)
240  // @note If the path is already absolute, no changes are made!
241  // @param [in] oParent The parent directory
242  // @return Returns *this, to be able to chain additional method calls
243  // Path& MakeAbsolute(const Path& oParent = filesystem::getWorkingDirectory());
244 
250  bool isRelative() const;
251 
257  bool isAbsolute() const;
258 
263  bool isEmpty() const;
264 
270 
271 private:
272  class Implementation;
273  std::unique_ptr<Implementation> _impl;
274  friend bool operator==(const Path& lhs, const Path& rhs);
275 };
276 
284 bool operator==(const Path& lhs, const Path& rhs);
285 
293 bool operator!=(const Path& lhs, const Path& rhs);
294 
303 std::ostream& operator<<(std::ostream& os, const Path& p);
304 
311 Path operator+(const Path& a, const Path& b);
312 
313 } // namespace filesystem
314 } // namespace a_util
315 
316 #endif // _A_UTILS_UTIL_FILESYSTEM_PATH_INCLUDED_
Exception class used by Path methods.
Definition: path.h:30
PathException(const std::string &message)
CTOR.
File/Directory path class.
Definition: path.h:45
Path(const std::string &path)
Initializing CTOR.
bool isAbsolute() const
Returns whether the current path is absolute.
Path & operator=(const Path &path)
Assignment Operator.
Path getLastElement() const
Get the name of the directory or file.
Path & append(const Path &path)
Appends a relative file or directory path or another file extension to the current path.
Path(const Path &path)
Copy CTOR.
Path & appendToBasename(const std::string &str)
Append a string to a basename.
Path & Clear()
Clears the path and resets it to the empty state.
Path & makeRelative()
Convert the path to a relative path relative to the current working directory.
Path getRootName() const
Get the root name of the directory/file.
Path getRootPath() const
Get the root path of the directory/file.
Path & replaceExtension(const std::string &extension)
Replace the extension of the path.
std::string toString(PathSeparator separator=PS_Native) const
Get the string representation of the current path using the specified separator.
bool setPath(const std::string &path)
Set the current path of the instance, handling platform aspects or any trailing slashes.
std::string getExtension() const
Get the extension of the file (without the '.
Path()
Default CTOR for an empty path.
Path & makeAbsolute()
Convert the path to an absolute path relative to the current working directory.
Path getParent() const
Get the parent path of the directory or file.
Path & makeCanonical()
Make the current path canonical.
bool isEmpty() const
Returns whether the current path is empty.
Path(const char *path)
Initializing CTOR.
Path & removeLastElement()
Remove the last part of the path, making it a directory path.
bool isRelative() const
Returns whether the current path is relative.
PathSeparator
Path separator types.
Definition: path.h:48
@ PS_ForwardSlash
Unix slash.
Definition: path.h:49
@ PS_BackwardSlash
Windows slash.
Definition: path.h:50
@ PS_Native
Native slash, depending on the current platform.
Definition: path.h:51
Path getRoot() const
Get the root path of the directory/file.
Definition of preprocessor macro DEV_ESSENTIAL_DEPRECATED.
#define DEV_ESSENTIAL_DEPRECATED(msg)
Adds [[deprecated("msg")]] to allowed declarations.
Definition: deprecated.h:28
Path getWorkingDirectory()
Retrieve path to the working directory of the current process.
Serves as the root component, with common functionality documented in core functionality.
Definition: base.h:24