7 #include <msp/core/mspcore_api.h>
22 Stores a filesystem path. Paths are always stored in a normalized form; there
23 are never any "." or ".." components in the middle of the path, and relative
24 paths always begin with a single "." component or a sequence ".." components.
25 As a special case, calling the constructor with no arguments or with an empty
26 string will construct an empty path; this can be usedful as an invalid value.
28 A path can also be treated as an array of components, supporting indexing,
29 iteration and slicing. In this context the root directory is treated as a
32 class MSPCORE_API Path
35 typedef std::vector<std::string::size_type> PositionArray;
38 class MSPCORE_API Iterator
41 typedef PositionArray::difference_type difference_type;
42 typedef const std::string value_type;
43 typedef const std::string *pointer;
44 typedef const std::string &reference;
45 typedef std::input_iterator_tag iterator_category;
49 PositionArray::const_iterator iter;
53 Iterator(const Path &, bool = false);
55 static Iterator at_begin(const Path &p) { return Iterator(p); }
56 static Iterator at_end(const Path &p) { return Iterator(p, true); }
58 Iterator &operator++();
59 Iterator operator++(int) { Iterator i = *this; ++*this; return i; }
60 Iterator &operator--();
61 const std::string &operator*() const { return current; }
62 const std::string *operator->() const { return ¤t; }
63 bool operator==(const Iterator &i) const { return (iter==i.iter && end==i.end); }
64 bool operator!=(const Iterator &i) const { return !(*this==i); }
71 PositionArray separators;
75 Path(const std::string &);
76 Path(const char *p): Path(std::string(p)) { }
78 /// Returns the path as a string.
79 const std::string &str() const { return path; }
81 /// Returns the path as a pointer to a null-terminated string.
82 const char *c_str() const { return path.c_str(); }
84 /// Returns the number of components in the path.
85 unsigned size() const;
87 /** Indicates whether the path is empty. This really means empty; a path
88 pointing to the current directory is not empty. */
89 bool empty() const { return path.empty(); }
91 /// Determines whether the path starts from the root directory.
92 bool is_absolute() const;
94 /// Extracts a range of components from the path.
95 Path subpath(unsigned start, unsigned count = static_cast<unsigned>(-1)) const;
97 /// Concatenates this path with another one, with usual filesystem semantics.
98 Path operator/(const Path &p) const;
99 Path &operator/=(const Path &);
102 /** Adds a component to the path. It must not contain the directory
103 separator character. */
104 void add_component(const std::string &);
107 /** Extracts a single component from the path. Negative indices count from
108 the end of the path. */
109 std::string operator[](int) const;
111 bool operator==(const Path &) const;
112 bool operator<(const Path &) const;
113 bool operator>(const Path &) const;
114 bool operator<=(const Path &other) const { return !(*this>other); }
115 bool operator>=(const Path &other) const { return !(*this<other); }
116 bool operator!=(const Path &other) const { return !(*this==other); }
118 Iterator begin() const { return Iterator::at_begin(*this); }
119 Iterator end() const { return Iterator::at_end(*this); }
122 inline std::ostream &operator<<(std::ostream &o, const Path &p) { o<<p.str(); return o; }