e2b87510452a878a74cd4716e240f6411dfe6cd1
[oota-llvm.git] / include / llvm / Support / PathV2.h
1 //===- llvm/Support/PathV2.h - Path Operating System Concept ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the llvm::sys::path namespace. It is designed after
11 // TR2/boost filesystem (v3), but modified to remove exception handling and the
12 // path class.
13 //
14 // All functions return an error_code and their actual work via the last out
15 // argument. The out argument is defined if and only if errc::success is
16 // returned. A function may return any error code in the generic or system
17 // category. However, they shall be equivalent to any error conditions listed
18 // in each functions respective documentation if the condition applies. [ note:
19 // this does not guarantee that error_code will be in the set of explicitly
20 // listed codes, but it does guarantee that if any of the explicitly listed
21 // errors occur, the correct error_code will be used ]. All functions may
22 // return errc::not_enough_memory if there is not enough memory to complete the
23 // operation.
24 //
25 //===----------------------------------------------------------------------===//
26
27 #ifndef LLVM_SUPPORT_PATHV2_H
28 #define LLVM_SUPPORT_PATHV2_H
29
30 #include "llvm/ADT/SmallString.h"
31 #include "llvm/ADT/Twine.h"
32 #include "llvm/Support/DataTypes.h"
33 #include "llvm/Support/system_error.h"
34 #include <iterator>
35
36 namespace llvm {
37 namespace sys {
38 namespace path {
39
40 /// @name Lexical Component Iterator
41 /// @{
42
43 /// @brief Path iterator.
44 ///
45 /// This is a bidirectional iterator that iterates over the individual
46 /// components in \a path. The forward traversal order is as follows:
47 /// * The root-name element, if present.
48 /// * The root-directory element, if present.
49 /// * Each successive filename element, if present.
50 /// * Dot, if one or more trailing non-root slash characters are present.
51 /// The backwards traversal order is the reverse of forward traversal.
52 ///
53 /// Iteration examples. Each component is separated by ',':
54 /// /          => /
55 /// /foo       => /,foo
56 /// foo/       => foo,.
57 /// /foo/bar   => /,foo,bar
58 /// ../        => ..,.
59 /// C:\foo\bar => C:,/,foo,bar
60 ///
61 class const_iterator {
62   StringRef Path;      //< The entire path.
63   StringRef Component; //< The current component. Not necessarily in Path.
64   size_t    Position;  //< The iterators current position within Path.
65
66   // An end iterator has Position = Path.size() + 1.
67   friend const_iterator begin(const StringRef &path);
68   friend const_iterator end(const StringRef &path);
69
70 public:
71   typedef const StringRef value_type;
72   typedef ptrdiff_t difference_type;
73   typedef value_type &reference;
74   typedef value_type *pointer;
75   typedef std::bidirectional_iterator_tag iterator_category;
76
77   reference operator*() const { return Component; }
78   pointer   operator->() const { return &Component; }
79   const_iterator &operator++();    // preincrement
80   const_iterator &operator++(int); // postincrement
81   const_iterator &operator--();    // predecrement
82   const_iterator &operator--(int); // postdecrement
83   bool operator==(const const_iterator &RHS) const;
84   bool operator!=(const const_iterator &RHS) const;
85
86   /// @brief Difference in bytes between this and RHS.
87   ptrdiff_t operator-(const const_iterator &RHS) const;
88 };
89
90 typedef std::reverse_iterator<const_iterator> reverse_iterator;
91
92 /// @brief Get begin iterator over \a path.
93 /// @param path Input path.
94 /// @returns Iterator initialized with the first component of \a path.
95 const_iterator begin(const StringRef &path);
96
97 /// @brief Get end iterator over \a path.
98 /// @param path Input path.
99 /// @returns Iterator initialized to the end of \a path.
100 const_iterator end(const StringRef &path);
101
102 /// @brief Get reverse begin iterator over \a path.
103 /// @param path Input path.
104 /// @returns Iterator initialized with the first reverse component of \a path.
105 inline reverse_iterator rbegin(const StringRef &path) {
106   return reverse_iterator(end(path));
107 }
108
109 /// @brief Get reverse end iterator over \a path.
110 /// @param path Input path.
111 /// @returns Iterator initialized to the reverse end of \a path.
112 inline reverse_iterator rend(const StringRef &path) {
113   return reverse_iterator(begin(path));
114 }
115
116 /// @}
117 /// @name Lexical Modifiers
118 /// @{
119
120 /// @brief Make \a path an absolute path.
121 ///
122 /// Makes \a path absolute using the current directory if it is not already. An
123 /// empty \a path will result in the current directory.
124 ///
125 /// /absolute/path   => /absolute/path
126 /// relative/../path => <current-directory>/path
127 ///
128 /// @param path A path that is modified to be an absolute path.
129 /// @returns errc::success if \a path has been made absolute, otherwise a
130 ///          platform specific error_code.
131 error_code make_absolute(SmallVectorImpl<char> &path);
132
133 /// @brief Remove the last component from \a path if it exists.
134 ///
135 /// directory/filename.cpp => directory/
136 /// directory/             => directory
137 ///
138 /// @param path A path that is modified to not have a file component.
139 /// @returns errc::success if \a path's file name has been removed (or there was
140 ///          not one to begin with), otherwise a platform specific error_code.
141 error_code remove_filename(SmallVectorImpl<char> &path);
142
143 /// @brief Replace the file extension of \a path with \a extension.
144 ///
145 /// ./filename.cpp => ./filename.extension
146 /// ./filename     => ./filename.extension
147 /// ./             => ? TODO: decide what semantics this has.
148 ///
149 /// @param path A path that has its extension replaced with \a extension.
150 /// @param extension The extension to be added. It may be empty. It may also
151 ///                  optionally start with a '.', if it does not, one will be
152 ///                  prepended.
153 /// @returns errc::success if \a path's extension has been replaced, otherwise a
154 ///          platform specific error_code.
155 error_code replace_extension(SmallVectorImpl<char> &path,
156                              const Twine &extension);
157
158 /// @brief Append to path.
159 ///
160 /// /foo  + bar/f => /foo/bar/f
161 /// /foo/ + bar/f => /foo/bar/f
162 /// foo   + bar/f => foo/bar/f
163 ///
164 /// @param path Set to \a path + \a component.
165 /// @param component The component to be appended to \a path.
166 /// @returns errc::success if \a component has been appended to \a path,
167 ///          otherwise a platform specific error_code.
168 error_code append(SmallVectorImpl<char> &path, const Twine &a,
169                                                const Twine &b = "",
170                                                const Twine &c = "",
171                                                const Twine &d = "");
172
173 /// @brief Append to path.
174 ///
175 /// /foo  + [bar,f] => /foo/bar/f
176 /// /foo/ + [bar,f] => /foo/bar/f
177 /// foo   + [bar,f] => foo/bar/f
178 ///
179 /// @param path Set to \a path + [\a begin, \a end).
180 /// @param begin Start of components to append.
181 /// @param end One past the end of components to append.
182 /// @returns errc::success if [\a begin, \a end) has been appended to \a path,
183 ///          otherwise a platform specific error_code.
184 error_code append(SmallVectorImpl<char> &path,
185                   const_iterator begin, const_iterator end);
186
187 /// @}
188 /// @name Transforms (or some other better name)
189 /// @{
190
191 /// Convert path to the native form. This is used to give paths to users and
192 /// operating system calls in the platform's normal way. For example, on Windows
193 /// all '/' are converted to '\'.
194 ///
195 /// @param path A path that is transformed to native format.
196 /// @param result Holds the result of the transformation.
197 /// @returns errc::success if \a path has been transformed and stored in result,
198 ///          otherwise a platform specific error_code.
199 error_code native(const Twine &path, SmallVectorImpl<char> &result);
200
201 /// @}
202 /// @name Lexical Observers
203 /// @{
204
205 /// @brief Get root name.
206 ///
207 /// //net/hello => //net
208 /// c:/hello    => c: (on Windows, on other platforms nothing)
209 /// /hello      => <empty>
210 ///
211 /// @param path Input path.
212 /// @param result Set to the root name of \a path if it has one, otherwise "".
213 /// @results errc::success if result has been successfully set, otherwise a
214 ///          platform specific error_code.
215 error_code root_name(const StringRef &path, StringRef &result);
216
217 /// @brief Get root directory.
218 ///
219 /// /goo/hello => /
220 /// c:/hello   => /
221 /// d/file.txt => <empty>
222 ///
223 /// @param path Input path.
224 /// @param result Set to the root directory of \a path if it has one, otherwise
225 ///               "".
226 /// @results errc::success if result has been successfully set, otherwise a
227 ///          platform specific error_code.
228 error_code root_directory(const StringRef &path, StringRef &result);
229
230 /// @brief Get root path.
231 ///
232 /// Equivalent to root_name + root_directory.
233 ///
234 /// @param path Input path.
235 /// @param result Set to the root path of \a path if it has one, otherwise "".
236 /// @results errc::success if result has been successfully set, otherwise a
237 ///          platform specific error_code.
238 error_code root_path(const StringRef &path, StringRef &result);
239
240 /// @brief Get relative path.
241 ///
242 /// C:\hello\world => hello\world
243 /// foo/bar        => foo/bar
244 /// /foo/bar       => foo/bar
245 ///
246 /// @param path Input path.
247 /// @param result Set to the path starting after root_path if one exists,
248 ///               otherwise "".
249 /// @results errc::success if result has been successfully set, otherwise a
250 ///          platform specific error_code.
251 error_code relative_path(const StringRef &path, StringRef &result);
252
253 /// @brief Get parent path.
254 ///
255 /// /          => <empty>
256 /// /foo       => /
257 /// foo/../bar => foo/..
258 ///
259 /// @param path Input path.
260 /// @param result Set to the parent path of \a path if one exists, otherwise "".
261 /// @results errc::success if result has been successfully set, otherwise a
262 ///          platform specific error_code.
263 error_code parent_path(const StringRef &path, StringRef &result);
264
265 /// @brief Get filename.
266 ///
267 /// /foo.txt    => foo.txt
268 /// .          => .
269 /// ..         => ..
270 /// /          => /
271 ///
272 /// @param path Input path.
273 /// @param result Set to the filename part of \a path. This is defined as the
274 ///               last component of \a path.
275 /// @results errc::success if result has been successfully set, otherwise a
276 ///          platform specific error_code.
277 error_code filename(const StringRef &path, StringRef &result);
278
279 /// @brief Get stem.
280 ///
281 /// If filename contains a dot but not solely one or two dots, result is the
282 /// substring of filename ending at (but not including) the last dot. Otherwise
283 /// it is filename.
284 ///
285 /// /foo/bar.txt => bar
286 /// /foo/bar     => bar
287 /// /foo/.txt    => <empty>
288 /// /foo/.       => .
289 /// /foo/..      => ..
290 ///
291 /// @param path Input path.
292 /// @param result Set to the stem of \a path.
293 /// @results errc::success if result has been successfully set, otherwise a
294 ///          platform specific error_code.
295 error_code stem(const StringRef &path, StringRef &result);
296
297 /// @brief Get extension.
298 ///
299 /// If filename contains a dot but not solely one or two dots, result is the
300 /// substring of filename starting at (and including) the last dot, and ending
301 /// at the end of \a path. Otherwise "".
302 ///
303 /// /foo/bar.txt => .txt
304 /// /foo/bar     => <empty>
305 /// /foo/.txt    => .txt
306 ///
307 /// @param path Input path.
308 /// @param result Set to the extension of \a path.
309 /// @results errc::success if result has been successfully set, otherwise a
310 ///          platform specific error_code.
311 error_code extension(const StringRef &path, StringRef &result);
312
313 /// @brief Has root name?
314 ///
315 /// root_name != ""
316 ///
317 /// @param path Input path.
318 /// @param result Set to true if the path has a root name, false otherwise.
319 /// @results errc::success if result has been successfully set, otherwise a
320 ///          platform specific error_code.
321 error_code has_root_name(const Twine &path, bool &result);
322
323 /// @brief Has root directory?
324 ///
325 /// root_directory != ""
326 ///
327 /// @param path Input path.
328 /// @param result Set to true if the path has a root directory, false otherwise.
329 /// @results errc::success if result has been successfully set, otherwise a
330 ///          platform specific error_code.
331 error_code has_root_directory(const Twine &path, bool &result);
332
333 /// @brief Has root path?
334 ///
335 /// root_path != ""
336 ///
337 /// @param path Input path.
338 /// @param result Set to true if the path has a root path, false otherwise.
339 /// @results errc::success if result has been successfully set, otherwise a
340 ///          platform specific error_code.
341 error_code has_root_path(const Twine &path, bool &result);
342
343 /// @brief Has relative path?
344 ///
345 /// relative_path != ""
346 ///
347 /// @param path Input path.
348 /// @param result Set to true if the path has a relative path, false otherwise.
349 /// @results errc::success if result has been successfully set, otherwise a
350 ///          platform specific error_code.
351 error_code has_relative_path(const Twine &path, bool &result);
352
353 /// @brief Has parent path?
354 ///
355 /// parent_path != ""
356 ///
357 /// @param path Input path.
358 /// @param result Set to true if the path has a parent path, false otherwise.
359 /// @results errc::success if result has been successfully set, otherwise a
360 ///          platform specific error_code.
361 error_code has_parent_path(const Twine &path, bool &result);
362
363 /// @brief Has filename?
364 ///
365 /// filename != ""
366 ///
367 /// @param path Input path.
368 /// @param result Set to true if the path has a filename, false otherwise.
369 /// @results errc::success if result has been successfully set, otherwise a
370 ///          platform specific error_code.
371 error_code has_filename(const Twine &path, bool &result);
372
373 /// @brief Has stem?
374 ///
375 /// stem != ""
376 ///
377 /// @param path Input path.
378 /// @param result Set to true if the path has a stem, false otherwise.
379 /// @results errc::success if result has been successfully set, otherwise a
380 ///          platform specific error_code.
381 error_code has_stem(const Twine &path, bool &result);
382
383 /// @brief Has extension?
384 ///
385 /// extension != ""
386 ///
387 /// @param path Input path.
388 /// @param result Set to true if the path has a extension, false otherwise.
389 /// @results errc::success if result has been successfully set, otherwise a
390 ///          platform specific error_code.
391 error_code has_extension(const Twine &path, bool &result);
392
393 /// @brief Is path absolute?
394 ///
395 /// @param path Input path.
396 /// @param result Set to true if the path is absolute, false if it is not.
397 /// @results errc::success if result has been successfully set, otherwise a
398 ///          platform specific error_code.
399 error_code is_absolute(const Twine &path, bool &result);
400
401 /// @brief Is path relative?
402 ///
403 /// @param path Input path.
404 /// @param result Set to true if the path is relative, false if it is not.
405 /// @results errc::success if result has been successfully set, otherwise a
406 ///          platform specific error_code.
407 error_code is_relative(const Twine &path, bool &result);
408 // end purely lexical.
409
410 } // end namespace path
411 } // end namespace sys
412 } // end namespace llvm
413
414 #endif