X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Ffs%2Fpath.h;h=210b5d93d14c88bf136d07f25a5ddc78c7fc89e2;hp=ed9cc7985b62d863f86a6bc19726de692791d077;hb=6c958cd2d8806063580f836e4c3d486832af9635;hpb=e1452710edb6b1d0ebceb4b9273dae4b2f899630 diff --git a/source/fs/path.h b/source/fs/path.h index ed9cc79..210b5d9 100644 --- a/source/fs/path.h +++ b/source/fs/path.h @@ -3,6 +3,7 @@ #include #include +#include namespace Msp { namespace FS { @@ -20,64 +21,89 @@ enum Stores a filesystem path. Paths are always stored in a normalized form; there are never any "." or ".." components in the middle of the path, and relative paths always begin with a single "." component or a sequence ".." components. +As a special case, calling the constructor with no arguments or with an empty +string will construct an empty path; this can be usedful as an invalid value. + +A path can also be treated as an array of components, supporting indexing, +iteration and slicing. In this context the root directory is treated as a +component of its own. */ class Path { +private: + typedef std::vector PositionArray; + public: class Iterator { - friend class Path; - private: - const Path &path; - std::string::size_type start,end; + const Path *path; + PositionArray::const_iterator iter; + bool end; - Iterator(const Path &); + Iterator(const Path &, bool = false); public: + static Iterator at_begin(const Path &p) { return Iterator(p); } + static Iterator at_end(const Path &p) { return Iterator(p, true); } + Iterator &operator++(); Iterator &operator--(); std::string operator*() const; - bool operator==(const Iterator &i) const { return (start==i.start && end==i.end); } + bool operator==(const Iterator &i) const { return (iter==i.iter && end==i.end); } bool operator!=(const Iterator &i) const { return !(*this==i); } }; private: std::string path; + std::vector separators; public: Path(); Path(const std::string &); Path(const char *); +private: + void init(const std::string &); +public: + /// Returns the path as a string. const std::string &str() const { return path; } /// Returns the number of components in the path. unsigned size() const; + /** Indicates whether the path is empty. This really means empty; a path + pointing to the current directory is not empty. */ bool empty() const { return path.empty(); } - /// Determines whether the path starts from the root directory + /// Determines whether the path starts from the root directory. bool is_absolute() const; /// Extracts a range of components from the path. Path subpath(unsigned start, unsigned count = static_cast(-1)) const; - /// Concatenates this path with another one, with usual filesystem semantics + /// Concatenates this path with another one, with usual filesystem semantics. Path operator/(const Path &p) const; Path &operator/=(const Path &); - /** - Extracts a single component from the path. Negative indices count from the - end of the path. - */ +private: + /** Adds a component to the path. It must not contain the directory + separator character. */ + void add_component(const std::string &); + +public: + /** Extracts a single component from the path. Negative indices count from + the end of the path. */ std::string operator[](int) const; bool operator==(const Path &) const; - Iterator begin() const; - Iterator end() const; -private: - void init(const std::string &); - void add_component(const std::string &); + bool operator<(const Path &) const; + bool operator>(const Path &) const; + bool operator<=(const Path &other) const { return !(*this>other); } + bool operator>=(const Path &other) const { return !(*this