]> git.tdb.fi Git - libs/core.git/blob - source/path.h
Add Path::empty()
[libs/core.git] / source / path.h
1 /*
2 This file is part of libmsppath
3 Copyright © 2006  Mikko Rasa, Mikkosoft Productions
4 Distributed under the LGPL
5 */
6 #ifndef MSP_PATH_PATH_H_
7 #define MSP_PATH_PATH_H_
8
9 #include <ostream>
10 #include <string>
11
12 namespace Msp {
13 namespace Path {
14
15 enum
16 {
17 #ifdef WIN32
18         DIRCHAR='\\'
19 #else
20         DIRCHAR='/'
21 #endif
22 };
23
24 class Path
25 {
26 public:
27         class iterator
28         {
29         public:
30                 iterator    &operator++();
31                 iterator    &operator--();
32                 std::string operator*() const;
33                 bool        operator==(const iterator &i) const { return (start==i.start && end==i.end); }
34                 bool        operator!=(const iterator &i) const { return !(*this==i); }
35         private:
36                 const Path &path;
37                 unsigned   start,end;
38
39                 iterator(const Path &);
40
41                 friend class Path;
42         };
43
44         Path() { }
45         Path(const std::string &p)     { init(p); }
46         Path(const char *p)            { init(p); }
47         const std::string &str() const { return path; }
48         unsigned    size() const;
49         bool        empty() const { return path.empty(); }
50         bool        is_absolute() const;
51         Path        subpath(unsigned, unsigned =(unsigned)-1) const;
52         Path        operator/(const Path &p) const { Path a=*this; a/=p; return a; }
53         Path        &operator/=(const Path &);
54         std::string operator[](int) const;
55         bool        operator==(const Path &) const;
56         iterator    begin() const { return iterator(*this); }
57         iterator    end() const   { iterator i(*this); i.start=i.end=std::string::npos; return i; }
58 private:
59         std::string path;
60
61         void init(const std::string &);
62 };
63
64 inline std::ostream &operator<<(std::ostream &o, const Path &p) { o<<p.str(); return o; }
65
66 } // namespace Path
67 } // namespace Msp
68
69 #endif