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