]> git.tdb.fi Git - libs/core.git/blob - source/path.h
082a1bae97c1f0e4a0b7d1e3e083ea74cf0da157
[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         DIRCHAR='\\'
21 #else
22         DIRCHAR='/'
23 #endif
24 };
25
26 class Path
27 {
28 public:
29         class Iterator
30         {
31         public:
32                 Iterator    &operator++();
33                 Iterator    &operator--();
34                 std::string operator*() const;
35                 bool        operator==(const Iterator &i) const { return (start==i.start && end==i.end); }
36                 bool        operator!=(const Iterator &i) const { return !(*this==i); }
37         private:
38                 const Path &path;
39                 unsigned   start,end;
40
41                 Iterator(const Path &);
42
43                 friend class Path;
44         };
45
46         Path() { }
47         Path(const std::string &p)     { init(p); }
48         Path(const char *p)            { init(p); }
49         const std::string &str() const { return path; }
50         unsigned    size() const;
51         bool        empty() const { return path.empty(); }
52         bool        is_absolute() const;
53         Path        subpath(unsigned, unsigned =static_cast<unsigned>(-1)) const;
54         Path        operator/(const Path &p) const { Path a=*this; a/=p; return a; }
55         Path        &operator/=(const Path &);
56         std::string operator[](int) const;
57         bool        operator==(const Path &) const;
58         Iterator    begin() const { return Iterator(*this); }
59         Iterator    end() const   { Iterator i(*this); i.start=i.end=std::string::npos; return i; }
60 private:
61         std::string path;
62
63         void init(const std::string &);
64         void add_component(const std::string &);
65 };
66
67 inline std::ostream &operator<<(std::ostream &o, const Path &p) { o<<p.str(); return o; }
68
69 } // namespace FS
70 } // namespace Msp
71
72 #endif