]> git.tdb.fi Git - libs/core.git/blob - source/path.cpp
Add files
[libs/core.git] / source / path.cpp
1 #include <msp/strutils.h>
2 #include "path.h"
3
4 using namespace std;
5 #include <iostream>
6
7 namespace Msp {
8 namespace Path {
9
10 bool is_windows_drive(const std::string &p)
11 {
12         return (p.size()==2 && ((p[0]>='A' && p[0]<='Z') || (p[0]>='a' && p[0]<='z')) && p[1]==':');
13 }
14
15 /**
16 Returns the number of components in the path.
17 */
18 unsigned Path::size() const
19 {
20         if(!path.size()) return 0;
21         if(path.size()==1 && path[0]==DIRCHAR) return 1;
22         unsigned count=1;
23         for(string::const_iterator i=path.begin(); i!=path.end(); ++i)
24                 if(*i==DIRCHAR) ++count;
25         return count;
26 }
27
28 bool Path::is_absolute() const
29 {
30 #ifdef WIN32
31         if(is_windows_drive((*this)[0])) return true;
32 #endif
33         if(path[0]==DIRCHAR) return true;
34         return false;
35 }
36
37 Path Path::subpath(unsigned start, unsigned count) const
38 {
39         Path result;
40         iterator i=begin();
41         for(unsigned j=0; (j<start && i!=end()); ++j)
42                 ++i;
43         for(unsigned j=0; (j<count && i!=end()); ++j)
44         {
45                 result/=*i;
46                 ++i;
47         }
48         return result;
49 }
50
51 /**
52 Attaches another path to the end of this one.  An absolute path replaces the
53 existing data.  ".." elements annihilate the last component and "." elements
54 are ignored.
55 */
56 Path &Path::operator/=(const Path &p)
57 {
58         if(p.is_absolute())
59                 path=p.path;
60         else
61         {
62                 for(iterator i=p.begin(); i!=p.end(); ++i)
63                 {
64                         if(*i=="..")
65                         {
66                                 unsigned slash=path.rfind(DIRCHAR);
67 #ifdef WIN32
68                                 if(is_windows_drive(path.substr(0,slash))) ++slash;
69 #endif
70                                 if(slash==0) ++slash;
71                                 if(slash==string::npos)
72                                         path="";
73                                 else
74                                         path.erase(slash);
75                         }
76                         else if(*i!=".")
77                         {
78                                 if(path.size()>1 || (path.size()==1 && path[0]!=DIRCHAR))
79                                         path+=DIRCHAR;
80                                 path+=*i;
81                         }
82                 }
83         }
84         return *this;
85 }
86
87 /**
88 Returns the path component at the specified index.  Negative indices count from
89 the end of the path.
90 */
91 string Path::operator[](int n) const
92 {
93         if(n>=0)
94         {
95                 for(iterator i=begin(); i!=end(); ++i,--n)
96                         if(!n) return *i;
97         }
98         else
99         {
100                 for(iterator i=--end();; --i)
101                 {
102                         if(!++n) return *i;
103                         if(i==begin()) break;
104                 }
105         }
106
107         return "";
108 }
109
110 bool Path::operator==(const Path &p) const
111 {
112 #ifdef WIN32
113         return !strcasecmp(path, p.path);
114 #else
115         return path==p.path;
116 #endif
117 }
118
119 void Path::init(const std::string &p)
120 {
121         unsigned start=0;
122         bool absolute=false;
123         if(p[0]==DIRCHAR)
124                 absolute=true;
125         while(1)
126         {
127                 unsigned slash=p.find_first_of("/\\",start);
128                 if(slash>start)
129                 {
130                         if(path.size() || absolute)
131                                 path+=DIRCHAR;
132                         path+=p.substr(start,slash-start);
133                 }
134                 if(slash==string::npos)
135                         break;
136                 start=slash+1;
137         }
138 }
139
140 Path::iterator::iterator(const Path &p):
141         path(p),
142         start(0)
143 {
144         if(path.path[0]==DIRCHAR)
145                 end=1;
146 #ifdef WIN32
147         else if(path.path[2]==DIRCHAR && is_windows_drive(path.path.substr(0,2)))
148                 end=2;
149 #endif
150         else
151                 end=path.path.find(DIRCHAR);
152 }
153
154 Path::iterator &Path::iterator::operator++()
155 {
156         start=end;
157         if(start>=path.path.size()) return *this;
158         if(path.path[start]==DIRCHAR) ++start;
159         end=path.path.find(DIRCHAR,start);
160         return *this;
161 }
162
163 Path::iterator &Path::iterator::operator--()
164 {
165         end=start;
166         if(end==0) return *this;
167         if(end>1 && end<path.path.size() && path.path[end]!=DIRCHAR) --end;
168         start=path.path.rfind(DIRCHAR,end-1);
169         if(start==string::npos)
170                 start=0;
171         else if(start<end-1)
172                 ++start;
173         return *this;
174 }
175
176 string Path::iterator::operator*() const
177 {
178         if(start>=path.path.size()) return "";
179         if(start==end) return "";
180         return path.path.substr(start,end-start);
181 }
182
183 } // namespace Path
184 } // namespace Msp