1 //===- llvm/Support/Path.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 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_SUPPORT_PATH_H
17 #define LLVM_SUPPORT_PATH_H
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/ADT/iterator.h"
21 #include "llvm/Support/DataTypes.h"
23 #include <system_error>
29 enum class Style { windows, posix, native };
31 /// @name Lexical Component Iterator
36 /// This is an input iterator that iterates over the individual components in
37 /// \a path. The traversal order is as follows:
38 /// * The root-name element, if present.
39 /// * The root-directory element, if present.
40 /// * Each successive filename element, if present.
41 /// * Dot, if one or more trailing non-root slash characters are present.
42 /// Traversing backwards is possible with \a reverse_iterator
44 /// Iteration examples. Each component is separated by ',':
49 /// /foo/bar => /,foo,bar
51 /// C:\foo\bar => C:,/,foo,bar
54 : public iterator_facade_base<const_iterator, std::input_iterator_tag,
56 StringRef Path; ///< The entire path.
57 StringRef Component; ///< The current component. Not necessarily in Path.
58 size_t Position; ///< The iterators current position within Path.
59 Style S; ///< The path style to use.
61 // An end iterator has Position = Path.size() + 1.
62 friend const_iterator begin(StringRef path, Style style);
63 friend const_iterator end(StringRef path);
66 reference operator*() const { return Component; }
67 const_iterator &operator++(); // preincrement
68 bool operator==(const const_iterator &RHS) const;
70 /// Difference in bytes between this and RHS.
71 ptrdiff_t operator-(const const_iterator &RHS) const;
74 /// Reverse path iterator.
76 /// This is an input iterator that iterates over the individual components in
77 /// \a path in reverse order. The traversal order is exactly reversed from that
78 /// of \a const_iterator
79 class reverse_iterator
80 : public iterator_facade_base<reverse_iterator, std::input_iterator_tag,
82 StringRef Path; ///< The entire path.
83 StringRef Component; ///< The current component. Not necessarily in Path.
84 size_t Position; ///< The iterators current position within Path.
85 Style S; ///< The path style to use.
87 friend reverse_iterator rbegin(StringRef path, Style style);
88 friend reverse_iterator rend(StringRef path);
91 reference operator*() const { return Component; }
92 reverse_iterator &operator++(); // preincrement
93 bool operator==(const reverse_iterator &RHS) const;
95 /// Difference in bytes between this and RHS.
96 ptrdiff_t operator-(const reverse_iterator &RHS) const;
99 /// Get begin iterator over \a path.
100 /// @param path Input path.
101 /// @returns Iterator initialized with the first component of \a path.
102 const_iterator begin(StringRef path, Style style = Style::native);
104 /// Get end iterator over \a path.
105 /// @param path Input path.
106 /// @returns Iterator initialized to the end of \a path.
107 const_iterator end(StringRef path);
109 /// Get reverse begin iterator over \a path.
110 /// @param path Input path.
111 /// @returns Iterator initialized with the first reverse component of \a path.
112 reverse_iterator rbegin(StringRef path, Style style = Style::native);
114 /// Get reverse end iterator over \a path.
115 /// @param path Input path.
116 /// @returns Iterator initialized to the reverse end of \a path.
117 reverse_iterator rend(StringRef path);
120 /// @name Lexical Modifiers
123 /// Remove the last component from \a path unless it is the root dir.
126 /// directory/filename.cpp => directory/
127 /// directory/ => directory
128 /// filename.cpp => <empty>
132 /// @param path A path that is modified to not have a file component.
133 void remove_filename(SmallVectorImpl<char> &path, Style style = Style::native);
135 /// Replace the file extension of \a path with \a extension.
138 /// ./filename.cpp => ./filename.extension
139 /// ./filename => ./filename.extension
140 /// ./ => ./.extension
143 /// @param path A path that has its extension replaced with \a extension.
144 /// @param extension The extension to be added. It may be empty. It may also
145 /// optionally start with a '.', if it does not, one will be
147 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension,
148 Style style = Style::native);
150 /// Replace matching path prefix with another path.
153 /// /foo, /old, /new => /foo
154 /// /old/foo, /old, /new => /new/foo
155 /// /foo, <empty>, /new => /new/foo
156 /// /old/foo, /old, <empty> => /foo
159 /// @param Path If \a Path starts with \a OldPrefix modify to instead
160 /// start with \a NewPrefix.
161 /// @param OldPrefix The path prefix to strip from \a Path.
162 /// @param NewPrefix The path prefix to replace \a NewPrefix with.
163 void replace_path_prefix(SmallVectorImpl<char> &Path,
164 const StringRef &OldPrefix, const StringRef &NewPrefix,
165 Style style = Style::native);
170 /// /foo + bar/f => /foo/bar/f
171 /// /foo/ + bar/f => /foo/bar/f
172 /// foo + bar/f => foo/bar/f
175 /// @param path Set to \a path + \a component.
176 /// @param a The component to be appended to \a path.
177 void append(SmallVectorImpl<char> &path, const Twine &a,
180 const Twine &d = "");
182 void append(SmallVectorImpl<char> &path, Style style, const Twine &a,
183 const Twine &b = "", const Twine &c = "", const Twine &d = "");
188 /// /foo + [bar,f] => /foo/bar/f
189 /// /foo/ + [bar,f] => /foo/bar/f
190 /// foo + [bar,f] => foo/bar/f
193 /// @param path Set to \a path + [\a begin, \a end).
194 /// @param begin Start of components to append.
195 /// @param end One past the end of components to append.
196 void append(SmallVectorImpl<char> &path, const_iterator begin,
197 const_iterator end, Style style = Style::native);
200 /// @name Transforms (or some other better name)
203 /// Convert path to the native form. This is used to give paths to users and
204 /// operating system calls in the platform's normal way. For example, on Windows
205 /// all '/' are converted to '\'.
207 /// @param path A path that is transformed to native format.
208 /// @param result Holds the result of the transformation.
209 void native(const Twine &path, SmallVectorImpl<char> &result,
210 Style style = Style::native);
212 /// Convert path to the native form in place. This is used to give paths to
213 /// users and operating system calls in the platform's normal way. For example,
214 /// on Windows all '/' are converted to '\'.
216 /// @param path A path that is transformed to native format.
217 void native(SmallVectorImpl<char> &path, Style style = Style::native);
219 /// Replaces backslashes with slashes if Windows.
221 /// @param path processed path
222 /// @result The result of replacing backslashes with forward slashes if Windows.
223 /// On Unix, this function is a no-op because backslashes are valid path
225 std::string convert_to_slash(StringRef path, Style style = Style::native);
228 /// @name Lexical Observers
234 /// //net/hello => //net
235 /// c:/hello => c: (on Windows, on other platforms nothing)
236 /// /hello => <empty>
239 /// @param path Input path.
240 /// @result The root name of \a path if it has one, otherwise "".
241 StringRef root_name(StringRef path, Style style = Style::native);
243 /// Get root directory.
248 /// d/file.txt => <empty>
251 /// @param path Input path.
252 /// @result The root directory of \a path if it has one, otherwise
254 StringRef root_directory(StringRef path, Style style = Style::native);
258 /// Equivalent to root_name + root_directory.
260 /// @param path Input path.
261 /// @result The root path of \a path if it has one, otherwise "".
262 StringRef root_path(StringRef path, Style style = Style::native);
264 /// Get relative path.
267 /// C:\hello\world => hello\world
268 /// foo/bar => foo/bar
269 /// /foo/bar => foo/bar
272 /// @param path Input path.
273 /// @result The path starting after root_path if one exists, otherwise "".
274 StringRef relative_path(StringRef path, Style style = Style::native);
281 /// foo/../bar => foo/..
284 /// @param path Input path.
285 /// @result The parent path of \a path if one exists, otherwise "".
286 StringRef parent_path(StringRef path, Style style = Style::native);
291 /// /foo.txt => foo.txt
297 /// @param path Input path.
298 /// @result The filename part of \a path. This is defined as the last component
300 StringRef filename(StringRef path, Style style = Style::native);
304 /// If filename contains a dot but not solely one or two dots, result is the
305 /// substring of filename ending at (but not including) the last dot. Otherwise
309 /// /foo/bar.txt => bar
311 /// /foo/.txt => <empty>
316 /// @param path Input path.
317 /// @result The stem of \a path.
318 StringRef stem(StringRef path, Style style = Style::native);
322 /// If filename contains a dot but not solely one or two dots, result is the
323 /// substring of filename starting at (and including) the last dot, and ending
324 /// at the end of \a path. Otherwise "".
327 /// /foo/bar.txt => .txt
328 /// /foo/bar => <empty>
329 /// /foo/.txt => .txt
332 /// @param path Input path.
333 /// @result The extension of \a path.
334 StringRef extension(StringRef path, Style style = Style::native);
336 /// Check whether the given char is a path separator on the host OS.
338 /// @param value a character
339 /// @result true if \a value is a path separator character on the host OS
340 bool is_separator(char value, Style style = Style::native);
342 /// Return the preferred separator for this platform.
344 /// @result StringRef of the preferred separator, null-terminated.
345 StringRef get_separator(Style style = Style::native);
347 /// Get the typical temporary directory for the system, e.g.,
348 /// "/var/tmp" or "C:/TEMP"
350 /// @param erasedOnReboot Whether to favor a path that is erased on reboot
351 /// rather than one that potentially persists longer. This parameter will be
352 /// ignored if the user or system has set the typical environment variable
353 /// (e.g., TEMP on Windows, TMPDIR on *nix) to specify a temporary directory.
355 /// @param result Holds the resulting path name.
356 void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result);
358 /// Get the user's home directory.
360 /// @param result Holds the resulting path name.
361 /// @result True if a home directory is set, false otherwise.
362 bool home_directory(SmallVectorImpl<char> &result);
364 /// Get the user's cache directory.
366 /// Expect the resulting path to be a directory shared with other
367 /// applications/services used by the user. Params \p Path1 to \p Path3 can be
368 /// used to append additional directory names to the resulting path. Recommended
369 /// pattern is <user_cache_directory>/<vendor>/<application>.
371 /// @param Result Holds the resulting path.
372 /// @param Path1 Additional path to be appended to the user's cache directory
373 /// path. "" can be used to append nothing.
374 /// @param Path2 Second additional path to be appended.
375 /// @param Path3 Third additional path to be appended.
376 /// @result True if a cache directory path is set, false otherwise.
377 bool user_cache_directory(SmallVectorImpl<char> &Result, const Twine &Path1,
378 const Twine &Path2 = "", const Twine &Path3 = "");
384 /// @param path Input path.
385 /// @result True if the path has a root name, false otherwise.
386 bool has_root_name(const Twine &path, Style style = Style::native);
388 /// Has root directory?
390 /// root_directory != ""
392 /// @param path Input path.
393 /// @result True if the path has a root directory, false otherwise.
394 bool has_root_directory(const Twine &path, Style style = Style::native);
400 /// @param path Input path.
401 /// @result True if the path has a root path, false otherwise.
402 bool has_root_path(const Twine &path, Style style = Style::native);
404 /// Has relative path?
406 /// relative_path != ""
408 /// @param path Input path.
409 /// @result True if the path has a relative path, false otherwise.
410 bool has_relative_path(const Twine &path, Style style = Style::native);
414 /// parent_path != ""
416 /// @param path Input path.
417 /// @result True if the path has a parent path, false otherwise.
418 bool has_parent_path(const Twine &path, Style style = Style::native);
424 /// @param path Input path.
425 /// @result True if the path has a filename, false otherwise.
426 bool has_filename(const Twine &path, Style style = Style::native);
432 /// @param path Input path.
433 /// @result True if the path has a stem, false otherwise.
434 bool has_stem(const Twine &path, Style style = Style::native);
440 /// @param path Input path.
441 /// @result True if the path has a extension, false otherwise.
442 bool has_extension(const Twine &path, Style style = Style::native);
444 /// Is path absolute?
446 /// @param path Input path.
447 /// @result True if the path is absolute, false if it is not.
448 bool is_absolute(const Twine &path, Style style = Style::native);
450 /// Is path relative?
452 /// @param path Input path.
453 /// @result True if the path is relative, false if it is not.
454 bool is_relative(const Twine &path, Style style = Style::native);
456 /// Remove redundant leading "./" pieces and consecutive separators.
458 /// @param path Input path.
459 /// @result The cleaned-up \a path.
460 StringRef remove_leading_dotslash(StringRef path, Style style = Style::native);
462 /// In-place remove any './' and optionally '../' components from a path.
464 /// @param path processed path
465 /// @param remove_dot_dot specify if '../' (except for leading "../") should be
467 /// @result True if path was changed
468 bool remove_dots(SmallVectorImpl<char> &path, bool remove_dot_dot = false,
469 Style style = Style::native);
472 std::error_code widenPath(const Twine &Path8, SmallVectorImpl<wchar_t> &Path16);
475 } // end namespace path
476 } // end namespace sys
477 } // end namespace llvm