Debian Codegraph @

cutl/fs/path.hxx

raw
1 // file : cutl/fs/path.hxx
2 // copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
3 // license : MIT; see accompanying LICENSE file
4
5 #ifndef CUTL_FS_PATH_HXX
6 #define CUTL_FS_PATH_HXX
7
8 #include <string>
9 #include <ostream>
10
11 #include <cutl/exception.hxx>
12
13 #include <cutl/details/export.hxx>
14
15 namespace cutl
16 {
17 namespace fs
18 {
19 template <typename C>
20 class basic_path;
21
22 template <typename C>
23 struct path_traits
24 {
25 typedef std::basic_string<C> string_type;
26 typedef typename string_type::size_type size_type;
27
28 // Canonical directory and path seperators.
29 //
30 #ifdef _WIN32
31 static C const directory_separator = '\\';
32 static C const path_separator = ';';
33 #else
34 static C const directory_separator = '/';
35 static C const path_separator = ':';
36 #endif
37
38 // Directory separator tests. On some platforms there
39 // could be multiple seperators. For example, on Windows
40 // we check for both '/' and '\'.
41 //
42 static bool
43 is_separator (C c)
44 {
45 #ifdef _WIN32
46 return c == '\\' || c == '/';
47 #else
48 return c == '/';
49 #endif
50 }
51
52 static size_type
53 find_separator (string_type const& s, size_type pos = 0)
54 {
55 for (size_type n (s.size ()); pos < n; ++pos)
56 {
57 if (is_separator (s[pos]))
58 return pos;
59 }
60
61 return string_type::npos;
62 }
63
64 static size_type
65 rfind_separator (string_type const& s, size_type pos = string_type::npos)
66 {
67 if (pos == string_type::npos)
68 pos = s.size ();
69 else
70 pos++;
71
72 for (; pos > 0; --pos)
73 {
74 if (is_separator (s[pos - 1]))
75 return pos - 1;
76 }
77
78 return string_type::npos;
79 }
80
81 static int
82 compare (string_type const& l, string_type const& r)
83 {
84 size_type ln (l.size ()), rn (r.size ()), n (ln < rn ? ln : rn);
85 for (size_type i (0); i != n; ++i)
86 {
87 #ifdef _WIN32
88 C lc (tolower (l[i])), rc (tolower (r[i]));
89 #else
90 C lc (l[i]), rc (r[i]);
91 #endif
92 if (is_separator (lc) && is_separator (rc))
93 continue;
94
95 if (lc < rc) return -1;
96 if (lc > rc) return 1;
97 }
98
99 return ln < rn ? -1 : (ln > rn ? 1 : 0);
100 }
101
102 private:
103 #ifdef _WIN32
104 static C
105 tolower (C);
106 #endif
107 };
108
109 template <typename C>
110 class invalid_basic_path;
111
112 typedef basic_path<char> path;
113 typedef invalid_basic_path<char> invalid_path;
114
115 typedef basic_path<wchar_t> wpath;
116 typedef invalid_basic_path<wchar_t> invalid_wpath;
117
118 //
119 //
120 class LIBCUTL_EXPORT invalid_path_base: exception
121 {
122 public:
123 virtual char const*
124 what () const throw ();
125 };
126
127 template <typename C>
128 class invalid_basic_path: public invalid_path_base
129 {
130 public:
131 typedef std::basic_string<C> string_type;
132
133 invalid_basic_path (C const* p): path_ (p) {}
134 invalid_basic_path (string_type const& p): path_ (p) {}
135 ~invalid_basic_path () throw () {}
136
137 string_type const&
138 path () const
139 {
140 return path_;
141 }
142
143 private:
144 string_type path_;
145 };
146
147 template <typename C>
148 class basic_path
149 {
150 public:
151 typedef std::basic_string<C> string_type;
152 typedef typename string_type::size_type size_type;
153
154 typedef path_traits<C> traits;
155
156 // Construct special empty path.
157 //
158 basic_path ()
159 {
160 }
161
162 explicit
163 basic_path (C const* s)
164 : path_ (s)
165 {
166 init ();
167 }
168
169 basic_path (C const* s, size_type n)
170 : path_ (s, n)
171 {
172 init ();
173 }
174
175 explicit
176 basic_path (string_type const& s)
177 : path_ (s)
178 {
179 init ();
180 }
181
182 void
183 swap (basic_path& p)
184 {
185 path_.swap (p.path_);
186 }
187
188 void
189 clear ()
190 {
191 path_.clear ();
192 }
193
194 static basic_path
195 current ();
196
197 static void
198 current (basic_path const&);
199
200 public:
201 bool
202 empty () const
203 {
204 return path_.empty ();
205 }
206
207 bool
208 absolute () const;
209
210 bool
211 relative () const
212 {
213 return !absolute ();
214 }
215
216 bool
217 root () const;
218
219 public:
220 // Return the path without the directory part.
221 //
222 basic_path
223 leaf () const;
224
225 // Return the directory part of the path or empty path if
226 // there is no directory.
227 //
228 basic_path
229 directory () const;
230
231 // Return the path without the extension, if any.
232 //
233 basic_path
234 base () const;
235
236 public:
237 // Normalize the path. This includes collapsing the '.' and '..'
238 // directories if possible, collapsing multiple directory
239 // separators, and converting all directory separators to the
240 // canonical form. Returns *this.
241 //
242 basic_path&
243 normalize ();
244
245 // Make the path absolute using the current directory unless
246 // it is already absolute.
247 //
248 basic_path&
249 complete ();
250
251 public:
252 basic_path
253 operator/ (basic_path const& x) const
254 {
255 basic_path r (*this);
256 r /= x;
257 return r;
258 }
259
260 basic_path&
261 operator/= (basic_path const&);
262
263 basic_path
264 operator+ (string_type const& s) const
265 {
266 return basic_path (path_ + s);
267 }
268
269 basic_path&
270 operator+= (string_type const& s)
271 {
272 path_ += s;
273 return *this;
274 }
275
276 // Note that comparison is case-insensitive if the filesystem is
277 // not case-sensitive (e.g., Windows).
278 //
279 bool
280 operator== (basic_path const& x) const
281 {
282 return traits::compare (path_, x.path_) == 0;
283 }
284
285 bool
286 operator!= (basic_path const& x) const
287 {
288 return !(*this == x);
289 }
290
291 bool
292 operator< (basic_path const& x) const
293 {
294 return traits::compare (path_, x.path_) < 0;
295 }
296
297 public:
298 const string_type&
299 string () const
300 {
301 return path_;
302 }
303
304 // If possible, return a POSIX representation of the path. For example,
305 // for a Windows path in the form foo\bar this function will return
306 // foo/bar. If it is not possible to create a POSIX representation for
307 // this path (e.g., c:\foo), this function will throw the invalid_path
308 // exception.
309 //
310 string_type
311 posix_string () const;
312
313 private:
314 void
315 init ();
316
317 private:
318 string_type path_;
319 };
320
321 template <typename C>
322 inline std::basic_ostream<C>&
323 operator<< (std::basic_ostream<C>& os, basic_path<C> const& p)
324 {
325 return os << p.string ();
326 }
327 }
328 }
329
330 #include <cutl/fs/path.ixx>
331 #include <cutl/fs/path.txx>
332
333 #endif // CUTL_FS_PATH_HXX