]> git.tdb.fi Git - libs/core.git/blob - source/fs/path.h
Add other comparison operators to FS::Path
[libs/core.git] / source / fs / path.h
1 #ifndef MSP_FS_PATH_H_
2 #define MSP_FS_PATH_H_
3
4 #include <ostream>
5 #include <string>
6
7 namespace Msp {
8 namespace FS {
9
10 enum
11 {
12 #ifdef WIN32
13         DIRSEP = '\\'
14 #else
15         DIRSEP = '/'
16 #endif
17 };
18
19 /**
20 Stores a filesystem path.  Paths are always stored in a normalized form; there
21 are never any "." or ".." components in the middle of the path, and relative
22 paths always begin with a single "." component or a sequence ".." components.
23 */
24 class Path
25 {
26 public:
27         class Iterator
28         {
29                 friend class Path;
30
31         private:
32                 const Path &path;
33                 std::string::size_type start, end;
34
35                 Iterator(const Path &);
36         public:
37                 Iterator &operator++();
38                 Iterator &operator--();
39                 std::string operator*() const;
40                 bool operator==(const Iterator &i) const { return (start==i.start && end==i.end); }
41                 bool operator!=(const Iterator &i) const { return !(*this==i); }
42         };
43
44 private:
45         std::string path;
46
47 public:
48         Path();
49         Path(const std::string &);
50         Path(const char *);
51 private:
52         void init(const std::string &);
53
54 public:
55         const std::string &str() const { return path; }
56
57         /// Returns the number of components in the path.
58         unsigned size() const;
59
60         bool empty() const { return path.empty(); }
61
62         /// Determines whether the path starts from the root directory
63         bool is_absolute() const;
64
65         /// Extracts a range of components from the path.
66         Path subpath(unsigned start, unsigned count = static_cast<unsigned>(-1)) const;
67
68         /// Concatenates this path with another one, with usual filesystem semantics
69         Path operator/(const Path &p) const;
70         Path &operator/=(const Path &);
71
72 private:
73         /** Adds a component to the path.  It must not contain the directory
74         separator character. */
75         void add_component(const std::string &);
76
77 public:
78         /** Extracts a single component from the path.  Negative indices count from
79         the end of the path. */
80         std::string operator[](int) const;
81
82         bool operator==(const Path &) const;
83         bool operator<(const Path &) const;
84         bool operator>(const Path &) const;
85         bool operator<=(const Path &other) const { return !(*this>other); }
86         bool operator>=(const Path &other) const { return !(*this<other); }
87         bool operator!=(const Path &other) const { return !(*this==other); }
88
89         Iterator begin() const;
90         Iterator end() const;
91 };
92
93 inline std::ostream &operator<<(std::ostream &o, const Path &p) { o<<p.str(); return o; }
94
95 } // namespace FS
96 } // namespace Msp
97
98 #endif