Debian Codegraph @

cutl/fs/path.txx

raw
1 // file : cutl/fs/path.txx
2 // copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
3 // license : MIT; see accompanying LICENSE file
4
5 #include <vector>
6
7 namespace cutl
8 {
9 namespace fs
10 {
11 template <typename C>
12 basic_path<C> basic_path<C>::
13 leaf () const
14 {
15 size_type p (traits::rfind_separator (path_));
16
17 return p != string_type::npos
18 ? basic_path (path_.c_str () + p + 1, path_.size () - p - 1)
19 : *this;
20 }
21
22 template <typename C>
23 basic_path<C> basic_path<C>::
24 directory () const
25 {
26 if (root ())
27 return basic_path ();
28
29 size_type p (traits::rfind_separator (path_));
30
31 // Include the trailing slash so that we get correct behavior
32 // if directory is root.
33 //
34 return p != string_type::npos
35 ? basic_path (path_.c_str (), p + 1)
36 : basic_path ();
37 }
38
39 template <typename C>
40 basic_path<C> basic_path<C>::
41 base () const
42 {
43 size_type i (path_.size ());
44
45 for (; i > 0; --i)
46 {
47 if (path_[i - 1] == '.')
48 break;
49
50 if (traits::is_separator (path_[i - 1]))
51 {
52 i = 0;
53 break;
54 }
55 }
56
57 // Weed out paths like ".txt" and "/.txt"
58 //
59 if (i > 1 && !traits::is_separator (path_[i - 2]))
60 {
61 return basic_path (path_.c_str (), i - 1);
62 }
63 else
64 return *this;
65 }
66
67 #ifdef _WIN32
68 template <typename C>
69 typename basic_path<C>::string_type basic_path<C>::
70 posix_string () const
71 {
72 if (absolute ())
73 throw invalid_basic_path<C> (path_);
74
75 string_type r (path_);
76
77 // Translate Windows-style separators to the POSIX ones.
78 //
79 for (size_type i (0), n (r.size ()); i != n; ++i)
80 if (r[i] == '\\')
81 r[i] = '/';
82
83 return r;
84 }
85 #endif
86
87 template <typename C>
88 basic_path<C>& basic_path<C>::
89 operator/= (basic_path<C> const& r)
90 {
91 if (r.absolute ())
92 throw invalid_basic_path<C> (r.path_);
93
94 if (path_.empty () || r.path_.empty ())
95 {
96 path_ += r.path_;
97 return *this;
98 }
99
100 if (!traits::is_separator (path_[path_.size () - 1]))
101 path_ += traits::directory_separator;
102
103 path_ += r.path_;
104
105 return *this;
106 }
107
108 template <typename C>
109 basic_path<C>& basic_path<C>::
110 normalize ()
111 {
112 if (empty ())
113 return *this;
114
115 bool abs (absolute ());
116
117 typedef std::vector<string_type> paths;
118 paths ps;
119
120 for (size_type b (0), e (traits::find_separator (path_)),
121 n (path_.size ());;
122 e = traits::find_separator (path_, b))
123 {
124 string_type s (path_, b, e == string_type::npos ? e : e - b);
125 ps.push_back (s);
126
127 if (e == string_type::npos)
128 break;
129
130 ++e;
131
132 while (e < n && traits::is_separator (path_[e]))
133 ++e;
134
135 if (e == n)
136 break;
137
138 b = e;
139 }
140
141 // First collapse '.' and '..'.
142 //
143 paths r;
144
145 for (typename paths::const_iterator i (ps.begin ()), e (ps.end ());
146 i != e; ++i)
147 {
148 string_type const& s (*i);
149 size_type n (s.size ());
150
151 if (n == 1 && s[0] == '.')
152 continue;
153
154 if (n == 2 && s[0] == '.' && s[1] == '.')
155 {
156 // Pop the last directory from r unless it is '..'.
157 //
158 if (!r.empty ())
159 {
160 string_type const& s1 (r.back ());
161
162 if (!(s1.size () == 2 && s1[0] == '.' && s1[1] == '.'))
163 {
164 // Cannot go past the root directory.
165 //
166 if (abs && r.size () == 1)
167 throw invalid_basic_path<C> (path_);
168
169 r.pop_back ();
170 continue;
171 }
172 }
173 }
174
175 r.push_back (s);
176 }
177
178 // Reassemble the path.
179 //
180 string_type p;
181
182 for (typename paths::const_iterator i (r.begin ()), e (r.end ());
183 i != e;)
184 {
185 p += *i;
186
187 if (++i != e)
188 p += traits::directory_separator;
189 }
190
191 if (p.empty () && !r.empty ())
192 p += traits::directory_separator; // Root directory.
193
194 path_.swap (p);
195 return *this;
196 }
197
198 template <typename C>
199 void basic_path<C>::
200 init ()
201 {
202 // Strip trailing slashes except for the case where the single
203 // slash represents the root directory.
204 //
205 size_type n (path_.size ());
206 for (; n > 1 && traits::is_separator (path_[n - 1]); --n) ;
207 path_.resize (n);
208 }
209 }
210 }