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"
28 enum class Style { windows, posix, native };
30 /// @name Lexical Component Iterator
33 /// @brief Path iterator.
35 /// This is an input iterator that iterates over the individual components in
36 /// \a path. The traversal order is as follows:
37 /// * The root-name element, if present.
38 /// * The root-directory element, if present.
39 /// * Each successive filename element, if present.
40 /// * Dot, if one or more trailing non-root slash characters are present.
41 /// Traversing backwards is possible with \a reverse_iterator
43 /// Iteration examples. Each component is separated by ',':
48 /// /foo/bar => /,foo,bar
50 /// C:\foo\bar => C:,/,foo,bar
53 : public iterator_facade_base<const_iterator, std::input_iterator_tag,
55 StringRef Path; ///< The entire path.
56 StringRef Component; ///< The current component. Not necessarily in Path.
57 size_t Position; ///< The iterators current position within Path.
58 Style S; ///< The path style to use.
60 // An end iterator has Position = Path.size() + 1.
61 friend const_iterator begin(StringRef path, Style style);
62 friend const_iterator end(StringRef path);
65 reference operator*() const { return Component; }
66 const_iterator &operator++(); // preincrement
67 bool operator==(const const_iterator &RHS) const;
69 /// @brief Difference in bytes between this and RHS.
70 ptrdiff_t operator-(const const_iterator &RHS) const;
73 /// @brief Reverse path iterator.
75 /// This is an input iterator that iterates over the individual components in
76 /// \a path in reverse order. The traversal order is exactly reversed from that
77 /// of \a const_iterator
78 class reverse_iterator
79 : public iterator_facade_base<reverse_iterator, std::input_iterator_tag,
81 StringRef Path; ///< The entire path.
82 StringRef Component; ///< The current component. Not necessarily in Path.
83 size_t Position; ///< The iterators current position within Path.
84 Style S; ///< The path style to use.
86 friend reverse_iterator rbegin(StringRef path, Style style);
87 friend reverse_iterator rend(StringRef path);
90 reference operator*() const { return Component; }
91 reverse_iterator &operator++(); // preincrement
92 bool operator==(const reverse_iterator &RHS) const;
94 /// @brief Difference in bytes between this and RHS.
95 ptrdiff_t operator-(const reverse_iterator &RHS) const;
98 /// @brief Get begin iterator over \a path.
99 /// @param path Input path.
100 /// @returns Iterator initialized with the first component of \a path.
101 const_iterator begin(StringRef path, Style style = Style::native);
103 /// @brief Get end iterator over \a path.
104 /// @param path Input path.
105 /// @returns Iterator initialized to the end of \a path.
106 const_iterator end(StringRef path);
108 /// @brief Get reverse begin iterator over \a path.
109 /// @param path Input path.
110 /// @returns Iterator initialized with the first reverse component of \a path.
111 reverse_iterator rbegin(StringRef path, Style style = Style::native);
113 /// @brief Get reverse end iterator over \a path.
114 /// @param path Input path.
115 /// @returns Iterator initialized to the reverse end of \a path.
116 reverse_iterator rend(StringRef path);
119 /// @name Lexical Modifiers
122 /// @brief Remove the last component from \a path unless it is the root dir.
125 /// directory/filename.cpp => directory/
126 /// directory/ => directory
127 /// filename.cpp => <empty>
131 /// @param path A path that is modified to not have a file component.
132 void remove_filename(SmallVectorImpl<char> &path, Style style = Style::native);
134 /// @brief Replace the file extension of \a path with \a extension.
137 /// ./filename.cpp => ./filename.extension
138 /// ./filename => ./filename.extension
139 /// ./ => ./.extension
142 /// @param path A path that has its extension replaced with \a extension.
143 /// @param extension The extension to be added. It may be empty. It may also
144 /// optionally start with a '.', if it does not, one will be
146 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension,
147 Style style = Style::native);
149 /// @brief Replace matching path prefix with another path.
152 /// /foo, /old, /new => /foo
153 /// /old/foo, /old, /new => /new/foo
154 /// /foo, <empty>, /new => /new/foo
155 /// /old/foo, /old, <empty> => /foo
158 /// @param Path If \a Path starts with \a OldPrefix modify to instead
159 /// start with \a NewPrefix.
160 /// @param OldPrefix The path prefix to strip from \a Path.
161 /// @param NewPrefix The path prefix to replace \a NewPrefix with.
162 void replace_path_prefix(SmallVectorImpl<char> &Path,
163 const StringRef &OldPrefix, const StringRef &NewPrefix,
164 Style style = Style::native);
166 /// @brief Append to path.
169 /// /foo + bar/f => /foo/bar/f
170 /// /foo/ + bar/f => /foo/bar/f
171 /// foo + bar/f => foo/bar/f
174 /// @param path Set to \a path + \a component.
175 /// @param a The component to be appended to \a path.
176 void append(SmallVectorImpl<char> &path, const Twine &a,
179 const Twine &d = "");
181 void append(SmallVectorImpl<char> &path, Style style, const Twine &a,
182 const Twine &b = "", const Twine &c = "", const Twine &d = "");
184 /// @brief Append to path.
187 /// /foo + [bar,f] => /foo/bar/f
188 /// /foo/ + [bar,f] => /foo/bar/f
189 /// foo + [bar,f] => foo/bar/f
192 /// @param path Set to \a path + [\a begin, \a end).
193 /// @param begin Start of components to append.
194 /// @param end One past the end of components to append.
195 void append(SmallVectorImpl<char> &path, const_iterator begin,
196 const_iterator end, Style style = Style::native);
199 /// @name Transforms (or some other better name)
202 /// Convert path to the native form. This is used to give paths to users and
203 /// operating system calls in the platform's normal way. For example, on Windows
204 /// all '/' are converted to '\'.
206 /// @param path A path that is transformed to native format.
207 /// @param result Holds the result of the transformation.
208 void native(const Twine &path, SmallVectorImpl<char> &result,
209 Style style = Style::native);
211 /// Convert path to the native form in place. This is used to give paths to
212 /// users and operating system calls in the platform's normal way. For example,
213 /// on Windows all '/' are converted to '\'.
215 /// @param path A path that is transformed to native format.
216 void native(SmallVectorImpl<char> &path, Style style = Style::native);
218 /// @brief Replaces backslashes with slashes if Windows.
220 /// @param path processed path
221 /// @result The result of replacing backslashes with forward slashes if Windows.
222 /// On Unix, this function is a no-op because backslashes are valid path
224 std::string convert_to_slash(StringRef path, Style style = Style::native);
227 /// @name Lexical Observers
230 /// @brief Get root name.
233 /// //net/hello => //net
234 /// c:/hello => c: (on Windows, on other platforms nothing)
235 /// /hello => <empty>
238 /// @param path Input path.
239 /// @result The root name of \a path if it has one, otherwise "".
240 StringRef root_name(StringRef path, Style style = Style::native);
242 /// @brief Get root directory.
247 /// d/file.txt => <empty>
250 /// @param path Input path.
251 /// @result The root directory of \a path if it has one, otherwise
253 StringRef root_directory(StringRef path, Style style = Style::native);
255 /// @brief Get root path.
257 /// Equivalent to root_name + root_directory.
259 /// @param path Input path.
260 /// @result The root path of \a path if it has one, otherwise "".
261 StringRef root_path(StringRef path, Style style = Style::native);
263 /// @brief Get relative path.
266 /// C:\hello\world => hello\world
267 /// foo/bar => foo/bar
268 /// /foo/bar => foo/bar
271 /// @param path Input path.
272 /// @result The path starting after root_path if one exists, otherwise "".
273 StringRef relative_path(StringRef path, Style style = Style::native);
275 /// @brief Get parent path.
280 /// foo/../bar => foo/..
283 /// @param path Input path.
284 /// @result The parent path of \a path if one exists, otherwise "".
285 StringRef parent_path(StringRef path, Style style = Style::native);
287 /// @brief Get filename.
290 /// /foo.txt => foo.txt
296 /// @param path Input path.
297 /// @result The filename part of \a path. This is defined as the last component
299 StringRef filename(StringRef path, Style style = Style::native);
303 /// If filename contains a dot but not solely one or two dots, result is the
304 /// substring of filename ending at (but not including) the last dot. Otherwise
308 /// /foo/bar.txt => bar
310 /// /foo/.txt => <empty>
315 /// @param path Input path.
316 /// @result The stem of \a path.
317 StringRef stem(StringRef path, Style style = Style::native);
319 /// @brief Get extension.
321 /// If filename contains a dot but not solely one or two dots, result is the
322 /// substring of filename starting at (and including) the last dot, and ending
323 /// at the end of \a path. Otherwise "".
326 /// /foo/bar.txt => .txt
327 /// /foo/bar => <empty>
328 /// /foo/.txt => .txt
331 /// @param path Input path.
332 /// @result The extension of \a path.
333 StringRef extension(StringRef path, Style style = Style::native);
335 /// @brief Check whether the given char is a path separator on the host OS.
337 /// @param value a character
338 /// @result true if \a value is a path separator character on the host OS
339 bool is_separator(char value, Style style = Style::native);
341 /// @brief Return the preferred separator for this platform.
343 /// @result StringRef of the preferred separator, null-terminated.
344 StringRef get_separator(Style style = Style::native);
346 /// @brief Get the typical temporary directory for the system, e.g.,
347 /// "/var/tmp" or "C:/TEMP"
349 /// @param erasedOnReboot Whether to favor a path that is erased on reboot
350 /// rather than one that potentially persists longer. This parameter will be
351 /// ignored if the user or system has set the typical environment variable
352 /// (e.g., TEMP on Windows, TMPDIR on *nix) to specify a temporary directory.
354 /// @param result Holds the resulting path name.
355 void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result);
357 /// @brief Get the user's home directory.
359 /// @param result Holds the resulting path name.
360 /// @result True if a home directory is set, false otherwise.
361 bool home_directory(SmallVectorImpl<char> &result);
363 /// @brief Get the user's cache directory.
365 /// Expect the resulting path to be a directory shared with other
366 /// applications/services used by the user. Params \p Path1 to \p Path3 can be
367 /// used to append additional directory names to the resulting path. Recommended
368 /// pattern is <user_cache_directory>/<vendor>/<application>.
370 /// @param Result Holds the resulting path.
371 /// @param Path1 Additional path to be appended to the user's cache directory
372 /// path. "" can be used to append nothing.
373 /// @param Path2 Second additional path to be appended.
374 /// @param Path3 Third additional path to be appended.
375 /// @result True if a cache directory path is set, false otherwise.
376 bool user_cache_directory(SmallVectorImpl<char> &Result, const Twine &Path1,
377 const Twine &Path2 = "", const Twine &Path3 = "");
379 /// @brief Has root name?
383 /// @param path Input path.
384 /// @result True if the path has a root name, false otherwise.
385 bool has_root_name(const Twine &path, Style style = Style::native);
387 /// @brief Has root directory?
389 /// root_directory != ""
391 /// @param path Input path.
392 /// @result True if the path has a root directory, false otherwise.
393 bool has_root_directory(const Twine &path, Style style = Style::native);
395 /// @brief Has root path?
399 /// @param path Input path.
400 /// @result True if the path has a root path, false otherwise.
401 bool has_root_path(const Twine &path, Style style = Style::native);
403 /// @brief Has relative path?
405 /// relative_path != ""
407 /// @param path Input path.
408 /// @result True if the path has a relative path, false otherwise.
409 bool has_relative_path(const Twine &path, Style style = Style::native);
411 /// @brief Has parent path?
413 /// parent_path != ""
415 /// @param path Input path.
416 /// @result True if the path has a parent path, false otherwise.
417 bool has_parent_path(const Twine &path, Style style = Style::native);
419 /// @brief Has filename?
423 /// @param path Input path.
424 /// @result True if the path has a filename, false otherwise.
425 bool has_filename(const Twine &path, Style style = Style::native);
431 /// @param path Input path.
432 /// @result True if the path has a stem, false otherwise.
433 bool has_stem(const Twine &path, Style style = Style::native);
435 /// @brief Has extension?
439 /// @param path Input path.
440 /// @result True if the path has a extension, false otherwise.
441 bool has_extension(const Twine &path, Style style = Style::native);
443 /// @brief Is path absolute?
445 /// @param path Input path.
446 /// @result True if the path is absolute, false if it is not.
447 bool is_absolute(const Twine &path, Style style = Style::native);
449 /// @brief Is path relative?
451 /// @param path Input path.
452 /// @result True if the path is relative, false if it is not.
453 bool is_relative(const Twine &path, Style style = Style::native);
455 /// @brief Remove redundant leading "./" pieces and consecutive separators.
457 /// @param path Input path.
458 /// @result The cleaned-up \a path.
459 StringRef remove_leading_dotslash(StringRef path, Style style = Style::native);
461 /// @brief In-place remove any './' and optionally '../' components from a path.
463 /// @param path processed path
464 /// @param remove_dot_dot specify if '../' (except for leading "../") should be
466 /// @result True if path was changed
467 bool remove_dots(SmallVectorImpl<char> &path, bool remove_dot_dot = false,
468 Style style = Style::native);
470 } // end namespace path
471 } // end namespace sys
472 } // end namespace llvm