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