1 //===- llvm/Support/PathV2.h - Path Operating System Concept ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
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
25 //===----------------------------------------------------------------------===//
27 #ifndef LLVM_SUPPORT_PATHV2_H
28 #define LLVM_SUPPORT_PATHV2_H
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"
40 /// @name Lexical Component Iterator
43 /// @brief Path iterator.
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.
53 /// Iteration examples. Each component is separated by ',':
57 /// /foo/bar => /,foo,bar
59 /// C:\foo\bar => C:,/,foo,bar
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.
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);
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;
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;
86 /// @brief Difference in bytes between this and RHS.
87 ptrdiff_t operator-(const const_iterator &RHS) const;
90 typedef std::reverse_iterator<const_iterator> reverse_iterator;
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);
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);
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));
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));
117 /// @name Lexical Modifiers
120 /// @brief Make \a path an absolute path.
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.
125 /// /absolute/path => /absolute/path
126 /// relative/../path => <current-directory>/path
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);
133 /// @brief Remove the last component from \a path if it exists.
135 /// directory/filename.cpp => directory/
136 /// directory/ => directory
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);
143 /// @brief Replace the file extension of \a path with \a extension.
145 /// ./filename.cpp => ./filename.extension
146 /// ./filename => ./filename.extension
147 /// ./ => ? TODO: decide what semantics this has.
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
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);
158 /// @brief Append to path.
160 /// /foo + bar/f => /foo/bar/f
161 /// /foo/ + bar/f => /foo/bar/f
162 /// foo + bar/f => foo/bar/f
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,
171 const Twine &d = "");
173 /// @brief Append to path.
175 /// /foo + [bar,f] => /foo/bar/f
176 /// /foo/ + [bar,f] => /foo/bar/f
177 /// foo + [bar,f] => foo/bar/f
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);
188 /// @name Transforms (or some other better name)
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 '\'.
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);
202 /// @name Lexical Observers
205 /// @brief Get root name.
207 /// //net/hello => //net
208 /// c:/hello => c: (on Windows, on other platforms nothing)
209 /// /hello => <empty>
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);
217 /// @brief Get root directory.
221 /// d/file.txt => <empty>
223 /// @param path Input path.
224 /// @param result Set to the root directory of \a path if it has one, otherwise
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);
230 /// @brief Get root path.
232 /// Equivalent to root_name + root_directory.
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);
240 /// @brief Get relative path.
242 /// C:\hello\world => hello\world
243 /// foo/bar => foo/bar
244 /// /foo/bar => foo/bar
246 /// @param path Input path.
247 /// @param result Set to the path starting after root_path if one exists,
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);
253 /// @brief Get parent path.
257 /// foo/../bar => foo/..
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);
265 /// @brief Get filename.
267 /// /foo.txt => foo.txt
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);
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
285 /// /foo/bar.txt => bar
287 /// /foo/.txt => <empty>
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);
297 /// @brief Get extension.
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 "".
303 /// /foo/bar.txt => .txt
304 /// /foo/bar => <empty>
305 /// /foo/.txt => .txt
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);
313 /// @brief Has root name?
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);
323 /// @brief Has root directory?
325 /// root_directory != ""
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);
333 /// @brief Has root path?
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);
343 /// @brief Has relative path?
345 /// relative_path != ""
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);
353 /// @brief Has parent path?
355 /// parent_path != ""
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);
363 /// @brief Has filename?
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);
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);
383 /// @brief Has extension?
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);
393 /// @brief Is path absolute?
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);
401 /// @brief Is path relative?
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.
410 } // end namespace path
411 } // end namespace sys
412 } // end namespace llvm