]> git.tdb.fi Git - libs/core.git/blob - source/path.h
Refactor the API
[libs/core.git] / source / 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 class Path
27 {
28 public:
29         class Iterator
30         {
31                 friend class Path;
32
33         private:
34                 const Path &path;
35                 unsigned   start,end;
36
37                 Iterator(const Path &);
38         public:
39                 Iterator    &operator++();
40                 Iterator    &operator--();
41                 std::string operator*() const;
42                 bool        operator==(const Iterator &i) const { return (start==i.start && end==i.end); }
43                 bool        operator!=(const Iterator &i) const { return !(*this==i); }
44         };
45
46 private:
47         std::string path;
48
49 public:
50         Path();
51         Path(const std::string &);
52         Path(const char *);
53
54         const std::string &str() const { return path; }
55
56         /// Returns the number of components in the path.
57         unsigned    size() const;
58
59         bool        empty() const { return path.empty(); }
60
61         /// Determines whether the path starts from the root directory
62         bool is_absolute() const;
63
64         /// Extracts a range of components from the path.
65         Path subpath(unsigned start, unsigned count=static_cast<unsigned>(-1)) const;
66
67         /// Concatenates this path with another one, with usual filesystem semantics
68         Path operator/(const Path &p) const;
69         Path &operator/=(const Path &);
70
71         /**
72         Extracts a single component from the path.  Negative indices count from the
73         end of the path.
74         */
75         std::string operator[](int) const;
76
77         bool        operator==(const Path &) const;
78         Iterator    begin() const;
79         Iterator    end() const;
80 private:
81         void init(const std::string &);
82         void add_component(const std::string &);
83 };
84
85 inline std::ostream &operator<<(std::ostream &o, const Path &p) { o<<p.str(); return o; }
86
87 } // namespace FS
88 } // namespace Msp
89
90 #endif