]> git.tdb.fi Git - libs/core.git/blob - source/path.cpp
Add a missing ! to make .. components to behave correctly
[libs/core.git] / source / path.cpp
1 /*
2 This file is part of libmsppath
3 Copyright © 2006  Mikko Rasa, Mikkosoft Productions
4 Distributed under the LGPL
5 */
6 #include <msp/strutils.h>
7 #include "path.h"
8 #include "utils.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace Path {
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 /**
38 Extracts the given component range from the path.
39 */
40 Path Path::subpath(unsigned start, unsigned count) const
41 {
42         Path result;
43         iterator i=begin();
44         for(unsigned j=0; (j<start && i!=end()); ++j)
45                 ++i;
46         for(unsigned j=0; (j<count && i!=end()); ++j)
47         {
48                 result/=*i;
49                 ++i;
50         }
51         return result;
52 }
53
54 /**
55 Attaches another path to the end of this one.  An absolute path replaces the
56 existing data.  ".." elements annihilate the last component and "." elements
57 are ignored.
58 */
59 Path &Path::operator/=(const Path &p)
60 {
61         if(p.is_absolute())
62                 path=p.path;
63         else
64         {
65                 for(iterator i=p.begin(); i!=p.end(); ++i)
66                         add_component(*i);
67         }
68         return *this;
69 }
70
71 /**
72 Returns the path component at the specified index.  Negative indices count from
73 the end of the path.
74 */
75 string Path::operator[](int n) const
76 {
77         if(n>=0)
78         {
79                 for(iterator i=begin(); i!=end(); ++i,--n)
80                         if(!n) return *i;
81         }
82         else
83         {
84                 for(iterator i=--end();; --i)
85                 {
86                         if(!++n) return *i;
87                         if(i==begin()) break;
88                 }
89         }
90
91         return "";
92 }
93
94 bool Path::operator==(const Path &p) const
95 {
96 #ifdef WIN32
97         return !strcasecmp(path, p.path);
98 #else
99         return path==p.path;
100 #endif
101 }
102
103 void Path::init(const string &p)
104 {
105         unsigned start=0;
106         if(p[0]=='/' || p[0]=='\\')
107                 add_component(string(1, DIRCHAR));
108         while(1)
109         {
110                 unsigned slash=p.find_first_of("/\\",start);
111                 if(slash>start)
112                 {
113                         add_component(p.substr(start, slash-start));
114                 }
115                 if(slash==string::npos)
116                         break;
117                 start=slash+1;
118         }
119 }
120
121 /**
122 Adds a single component to the path, emulating the cd command.
123 */
124 void Path::add_component(const string &comp)
125 {
126         if(comp.empty())
127                 ;
128         else if(comp.size()==1 && comp[0]==DIRCHAR)
129         {
130                 // Replace the path with the root directory
131 #ifdef WIN32
132                 unsigned slash=path.find(DIRCHAR);
133                 if(is_windows_drive(path.substr(0, slash)))
134                         path=path.substr(0,2);
135                 else
136 #endif
137                 path=comp;
138         }
139 #ifdef WIN32
140         else if(is_windows_drive(comp))
141                 path=comp;
142 #endif
143         else if(comp=="..")
144         {
145                 if(path.empty())
146                         path=comp;
147                 // .. in root directory is a no-op
148                 else if(path.size()==1 && path[0]==DIRCHAR)
149                         ;
150 #ifdef WIN32
151                 else if(is_windows_drive(path))
152                         ;
153 #endif
154                 else
155                 {
156                         unsigned slash=path.rfind(DIRCHAR);
157                         unsigned start=(slash==string::npos)?0:slash+1;
158                         if(!path.compare(start, string::npos, ".."))
159                         {
160                                 // If the last component already is a .., add another
161                                 path+=DIRCHAR;
162                                 path+=comp;
163                         }
164                         else
165                                 // Otherwise, erase the last component
166                                 path.erase(slash, string::npos);
167                 }
168         }
169         else if(comp!=".")
170         {
171                 if(path.size()>1 || (path.size()==1 && path[0]!=DIRCHAR))
172                         path+=DIRCHAR;
173                 path+=comp;
174         }
175 }
176
177 Path::iterator::iterator(const Path &p):
178         path(p),
179         start(0)
180 {
181         if(path.path.empty())
182                 start=end=string::npos;
183         else if(path.path[0]==DIRCHAR)
184                 end=1;
185 #ifdef WIN32
186         else if(path.path.size()>2 && path.path[2]==DIRCHAR && is_windows_drive(path.path.substr(0,2)))
187                 end=2;
188 #endif
189         else
190                 end=path.path.find(DIRCHAR);
191 }
192
193 Path::iterator &Path::iterator::operator++()
194 {
195         start=end;
196         if(start>=path.path.size()) return *this;
197         if(path.path[start]==DIRCHAR) ++start;
198         end=path.path.find(DIRCHAR,start);
199         return *this;
200 }
201
202 Path::iterator &Path::iterator::operator--()
203 {
204         end=start;
205         if(end==0) return *this;
206         if(end>1 && end<path.path.size() && path.path[end]!=DIRCHAR) --end;
207         start=path.path.rfind(DIRCHAR,end-1);
208         if(start==string::npos)
209                 start=0;
210         else if(start<end-1)
211                 ++start;
212         return *this;
213 }
214
215 string Path::iterator::operator*() const
216 {
217         if(start>=path.path.size()) return "";
218         if(start==end) return "";
219         return path.path.substr(start,end-start);
220 }
221
222 } // namespace Path
223 } // namespace Msp