SPH
Path.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "common/Assert.h"
9 #include <unistd.h>
10 
12 
17 class Path {
18 private:
19  std::string path;
20 
21  static constexpr char SEPARATOR = '/';
22 
23 public:
25  Path() = default;
26 
28  explicit Path(const std::string& path);
29 
30 
32 
34  bool empty() const;
35 
37  bool isHidden() const;
38 
41  bool isAbsolute() const;
42 
44  bool isRelative() const;
45 
47  bool isRoot() const;
48 
50  bool hasExtension() const;
51 
52 
54 
56  Path parentPath() const;
57 
61  Path fileName() const;
62 
72  Path extension() const;
73 
74 
76 
78  std::string native() const;
79 
80 
82 
89  Path& replaceExtension(const std::string& newExtension);
90 
95 
98 
102  Path& makeAbsolute();
103 
107  Path& makeRelative();
108 
109 
111 
113  static Path currentPath();
114 
115 
117 
119  Path operator/(const Path& other) const;
120 
122  Path& operator/=(const Path& other);
123 
128  bool operator==(const Path& other) const;
129 
131  bool operator!=(const Path& other) const;
132 
136  bool operator<(const Path& other) const;
137 
139  friend std::ostream& operator<<(std::ostream& stream, const Path& path);
140 
141 private:
143  void convert();
144 
146  std::size_t findFolder(const std::string& folder);
147 };
148 
152 Path operator"" _path(const char* nativePath, const std::size_t size);
153 
Custom assertions.
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
#define NAMESPACE_SPH_END
Definition: Object.h:12
Object representing a path on a filesystem.
Definition: Path.h:17
bool isRelative() const
Checks if the path is relative. Empty path is not considered relative.
Definition: Path.cpp:23
Path operator/(const Path &other) const
Appends two paths together.
Definition: Path.cpp:177
static Path currentPath()
Returns the current working directory, or empty path if the function failed.
Definition: Path.cpp:166
Path & replaceExtension(const std::string &newExtension)
Changes the extension of the file.
Definition: Path.cpp:76
Path & removeExtension()
Removes the extension from the path.
Definition: Path.cpp:100
friend std::ostream & operator<<(std::ostream &stream, const Path &path)
Prints the path into the stream.
Definition: Path.cpp:204
std::string native() const
Returns the native version of the path.
Definition: Path.cpp:71
bool operator<(const Path &other) const
Does lexicographical comparison of two paths.
Definition: Path.cpp:200
bool isRoot() const
Checks if the object holds root path.
Definition: Path.cpp:27
Path & makeAbsolute()
Turns the path into an absolute path.
Definition: Path.cpp:130
Path()=default
Constructs an empty path.
Path & removeSpecialDirs()
Removes . and .. directories from the path.
Definition: Path.cpp:115
bool isHidden() const
Checks if the file is hidden, i.e. starts with a dot (makes only sense on Unit based systems).
Definition: Path.cpp:14
bool empty() const
Checks if the path is empty.
Definition: Path.cpp:10
Path & operator/=(const Path &other)
Appends another path to this one.
Definition: Path.cpp:187
Path fileName() const
Returns the filename of the path.
Definition: Path.cpp:47
Path parentPath() const
Returns the parent directory. If the path is empty or root, return empty path.
Definition: Path.cpp:35
bool isAbsolute() const
Definition: Path.cpp:19
Path extension() const
Returns the extension of the filename.
Definition: Path.cpp:59
bool operator==(const Path &other) const
Checks if two objects represent the same path.
Definition: Path.cpp:192
bool hasExtension() const
Checks if the file has an extension.
Definition: Path.cpp:31
bool operator!=(const Path &other) const
Checks if two objects represent different paths.
Definition: Path.cpp:196
Path & makeRelative()
Turns the path into a relative path.
Definition: Path.cpp:138