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