Enzo
Loading...
Searching...
No Matches
Path.h
1
2#pragma once
3
4#include <ostream>
5#include <string>
6#include <string_view>
7#include <vector>
8
9namespace enzo {
13class Path
14{
15 public:
17 Path();
19 Path(const std::string& path);
21 Path(const char* path);
22
24 static std::string strip(const std::string& str);
25
27 bool isEmpty() const;
28
30 bool isRoot() const;
31
33 bool isAbsolute() const;
34
36 bool isRelative() const;
37
54 virtual bool isValid() const;
55
57 std::string getName() const;
58
60 Path getParent() const;
61
71 std::vector<Path> getPrefixes() const;
72
74 std::vector<std::string> split() const;
75
77 const std::string& getString() const;
78
88 static bool isValidFormatting(const std::string& pathString);
89
91 static bool isValidComponent(const std::string& component);
92
102 static bool isValidName(const std::string& name);
103
115 Path append(const enzo::Path& path) const;
116
127 virtual Path increment(int increment = 1) const;
128
134 Path makeRelative() const;
135
141 Path makeAbsolute() const;
142
151 Path makeRelativeTo(const Path& anchor) const;
152
171 Path makeAbsoluteFrom(const Path& anchor) const;
172
178 bool hasPrefix(const Path& prefix) const;
179
182 operator std::string_view() const;
183
184 bool operator==(std::string_view other) const;
185 bool operator!=(std::string_view other) const;
186
187 protected:
188 std::string path_;
189};
190
191std::ostream& operator<<(std::ostream& os, const enzo::Path& other);
192} // namespace enzo
A default path class for creation and manipulation of tree-based paths.
Definition Path.h:14
Path makeRelativeTo(const Path &anchor) const
Makes a path relative to a given anchor path.
Definition Path.cpp:213
Path makeRelative() const
Converts an absolute path to relative, stripping the root character.
Definition Path.cpp:199
static std::string strip(const std::string &str)
Returns the string with surrounding whitespace removed.
Definition Path.cpp:25
std::string getName() const
Returns the name of the Path, ie, the last component of the path.
Definition Path.cpp:94
bool hasPrefix(const Path &prefix) const
Checks if the Path has any additional prefixes.
Definition Path.cpp:249
bool isAbsolute() const
Returns true if the Path is an absolute path.
Definition Path.cpp:40
static bool isValidName(const std::string &name)
Checks the given name to make sure it fits the alphanumeric guidelines.
Definition Path.cpp:44
std::vector< Path > getPrefixes() const
Returns every ancestor path leading up to this path, ordered from shortest to longest....
Definition Path.cpp:141
Path makeAbsolute() const
Converts a relative path to absolute, adding a root character.
Definition Path.cpp:206
bool isEmpty() const
Checks if the Path has no characters.
Definition Path.cpp:36
Path()
Constructs an empty enzo::Path object.
Definition Path.cpp:6
virtual bool isValid() const
Runs every validation check and returns true if the path is fully valid.
Definition Path.cpp:92
Path append(const enzo::Path &path) const
Returns a new path formed by appending the given path to this one.
Definition Path.cpp:163
bool isRoot() const
Returns true if the Path is "/".
Definition Path.cpp:38
std::vector< std::string > split() const
Splits all of the components of a Path and returns them as a vector of strings.
Definition Path.cpp:118
static bool isValidComponent(const std::string &component)
Checks whether a single path component is a name or the ".." parent step.
Definition Path.cpp:57
Path getParent() const
Returns the parent path as a Path if it exists.
Definition Path.cpp:105
static bool isValidFormatting(const std::string &pathString)
Checks the path to make sure it fits the alphanumeric guidelines.
Definition Path.cpp:62
bool isRelative() const
Returns true if the Path is a relative path.
Definition Path.cpp:42
Path makeAbsoluteFrom(const Path &anchor) const
Returns the absolute path this one names when read from an anchor.
Definition Path.cpp:227
const std::string & getString() const
Return the path of the Path as an std::string.
Definition Path.cpp:161
virtual Path increment(int increment=1) const
Increments the numerical suffix of the path's name by a given amount.
Definition Path.cpp:178