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/SmallString.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/DataTypes.h"
28 /// @name Lexical Component Iterator
31 /// @brief Path iterator.
33 /// This is an input iterator that iterates over the individual components in
34 /// \a path. The traversal order is as follows:
35 /// * The root-name element, if present.
36 /// * The root-directory element, if present.
37 /// * Each successive filename element, if present.
38 /// * Dot, if one or more trailing non-root slash characters are present.
39 /// Traversing backwards is possible with \a reverse_iterator
41 /// Iteration examples. Each component is separated by ',':
46 /// /foo/bar => /,foo,bar
48 /// C:\foo\bar => C:,/,foo,bar
51 : public std::iterator<std::input_iterator_tag, const StringRef> {
52 StringRef Path; ///< The entire path.
53 StringRef Component; ///< The current component. Not necessarily in Path.
54 size_t Position; ///< The iterators current position within Path.
56 // An end iterator has Position = Path.size() + 1.
57 friend const_iterator begin(StringRef path);
58 friend const_iterator end(StringRef path);
61 reference operator*() const { return Component; }
62 pointer operator->() const { return &Component; }
63 const_iterator &operator++(); // preincrement
64 bool operator==(const const_iterator &RHS) const;
65 bool operator!=(const const_iterator &RHS) const { return !(*this == RHS); }
67 /// @brief Difference in bytes between this and RHS.
68 ptrdiff_t operator-(const const_iterator &RHS) const;
71 /// @brief Reverse path iterator.
73 /// This is an input iterator that iterates over the individual components in
74 /// \a path in reverse order. The traversal order is exactly reversed from that
75 /// of \a const_iterator
76 class reverse_iterator
77 : public std::iterator<std::input_iterator_tag, const StringRef> {
78 StringRef Path; ///< The entire path.
79 StringRef Component; ///< The current component. Not necessarily in Path.
80 size_t Position; ///< The iterators current position within Path.
82 friend reverse_iterator rbegin(StringRef path);
83 friend reverse_iterator rend(StringRef path);
86 reference operator*() const { return Component; }
87 pointer operator->() const { return &Component; }
88 reverse_iterator &operator++(); // preincrement
89 bool operator==(const reverse_iterator &RHS) const;
90 bool operator!=(const reverse_iterator &RHS) const { return !(*this == RHS); }
93 /// @brief Get begin iterator over \a path.
94 /// @param path Input path.
95 /// @returns Iterator initialized with the first component of \a path.
96 const_iterator begin(StringRef path);
98 /// @brief Get end iterator over \a path.
99 /// @param path Input path.
100 /// @returns Iterator initialized to the end of \a path.
101 const_iterator end(StringRef path);
103 /// @brief Get reverse begin iterator over \a path.
104 /// @param path Input path.
105 /// @returns Iterator initialized with the first reverse component of \a path.
106 reverse_iterator rbegin(StringRef path);
108 /// @brief Get reverse end iterator over \a path.
109 /// @param path Input path.
110 /// @returns Iterator initialized to the reverse end of \a path.
111 reverse_iterator rend(StringRef path);
114 /// @name Lexical Modifiers
117 /// @brief Remove the last component from \a path unless it is the root dir.
120 /// directory/filename.cpp => directory/
121 /// directory/ => directory
122 /// filename.cpp => <empty>
126 /// @param path A path that is modified to not have a file component.
127 void remove_filename(SmallVectorImpl<char> &path);
129 /// @brief Replace the file extension of \a path with \a extension.
132 /// ./filename.cpp => ./filename.extension
133 /// ./filename => ./filename.extension
134 /// ./ => ./.extension
137 /// @param path A path that has its extension replaced with \a extension.
138 /// @param extension The extension to be added. It may be empty. It may also
139 /// optionally start with a '.', if it does not, one will be
141 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension);
143 /// @brief Append to path.
146 /// /foo + bar/f => /foo/bar/f
147 /// /foo/ + bar/f => /foo/bar/f
148 /// foo + bar/f => foo/bar/f
151 /// @param path Set to \a path + \a component.
152 /// @param a The component to be appended to \a path.
153 void append(SmallVectorImpl<char> &path, const Twine &a,
156 const Twine &d = "");
158 /// @brief Append to path.
161 /// /foo + [bar,f] => /foo/bar/f
162 /// /foo/ + [bar,f] => /foo/bar/f
163 /// foo + [bar,f] => foo/bar/f
166 /// @param path Set to \a path + [\a begin, \a end).
167 /// @param begin Start of components to append.
168 /// @param end One past the end of components to append.
169 void append(SmallVectorImpl<char> &path,
170 const_iterator begin, const_iterator end);
173 /// @name Transforms (or some other better name)
176 /// Convert path to the native form. This is used to give paths to users and
177 /// operating system calls in the platform's normal way. For example, on Windows
178 /// all '/' are converted to '\'.
180 /// @param path A path that is transformed to native format.
181 /// @param result Holds the result of the transformation.
182 void native(const Twine &path, SmallVectorImpl<char> &result);
184 /// Convert path to the native form in place. This is used to give paths to
185 /// users and operating system calls in the platform's normal way. For example,
186 /// on Windows all '/' are converted to '\'.
188 /// @param path A path that is transformed to native format.
189 void native(SmallVectorImpl<char> &path);
192 /// @name Lexical Observers
195 /// @brief Get root name.
198 /// //net/hello => //net
199 /// c:/hello => c: (on Windows, on other platforms nothing)
200 /// /hello => <empty>
203 /// @param path Input path.
204 /// @result The root name of \a path if it has one, otherwise "".
205 StringRef root_name(StringRef path);
207 /// @brief Get root directory.
212 /// d/file.txt => <empty>
215 /// @param path Input path.
216 /// @result The root directory of \a path if it has one, otherwise
218 StringRef root_directory(StringRef path);
220 /// @brief Get root path.
222 /// Equivalent to root_name + root_directory.
224 /// @param path Input path.
225 /// @result The root path of \a path if it has one, otherwise "".
226 StringRef root_path(StringRef path);
228 /// @brief Get relative path.
231 /// C:\hello\world => hello\world
232 /// foo/bar => foo/bar
233 /// /foo/bar => foo/bar
236 /// @param path Input path.
237 /// @result The path starting after root_path if one exists, otherwise "".
238 StringRef relative_path(StringRef path);
240 /// @brief Get parent path.
245 /// foo/../bar => foo/..
248 /// @param path Input path.
249 /// @result The parent path of \a path if one exists, otherwise "".
250 StringRef parent_path(StringRef path);
252 /// @brief Get filename.
255 /// /foo.txt => foo.txt
261 /// @param path Input path.
262 /// @result The filename part of \a path. This is defined as the last component
264 StringRef filename(StringRef path);
268 /// If filename contains a dot but not solely one or two dots, result is the
269 /// substring of filename ending at (but not including) the last dot. Otherwise
273 /// /foo/bar.txt => bar
275 /// /foo/.txt => <empty>
280 /// @param path Input path.
281 /// @result The stem of \a path.
282 StringRef stem(StringRef path);
284 /// @brief Get extension.
286 /// If filename contains a dot but not solely one or two dots, result is the
287 /// substring of filename starting at (and including) the last dot, and ending
288 /// at the end of \a path. Otherwise "".
291 /// /foo/bar.txt => .txt
292 /// /foo/bar => <empty>
293 /// /foo/.txt => .txt
296 /// @param path Input path.
297 /// @result The extension of \a path.
298 StringRef extension(StringRef path);
300 /// @brief Check whether the given char is a path separator on the host OS.
302 /// @param value a character
303 /// @result true if \a value is a path separator character on the host OS
304 bool is_separator(char value);
306 /// @brief Return the preferred separator for this platform.
308 /// @result StringRef of the preferred separator, null-terminated.
309 StringRef get_separator();
311 /// @brief Get the typical temporary directory for the system, e.g.,
312 /// "/var/tmp" or "C:/TEMP"
314 /// @param erasedOnReboot Whether to favor a path that is erased on reboot
315 /// rather than one that potentially persists longer. This parameter will be
316 /// ignored if the user or system has set the typical environment variable
317 /// (e.g., TEMP on Windows, TMPDIR on *nix) to specify a temporary directory.
319 /// @param result Holds the resulting path name.
320 void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result);
322 /// @brief Get the user's home directory.
324 /// @param result Holds the resulting path name.
325 /// @result True if a home directory is set, false otherwise.
326 bool home_directory(SmallVectorImpl<char> &result);
328 /// @brief Has root name?
332 /// @param path Input path.
333 /// @result True if the path has a root name, false otherwise.
334 bool has_root_name(const Twine &path);
336 /// @brief Has root directory?
338 /// root_directory != ""
340 /// @param path Input path.
341 /// @result True if the path has a root directory, false otherwise.
342 bool has_root_directory(const Twine &path);
344 /// @brief Has root path?
348 /// @param path Input path.
349 /// @result True if the path has a root path, false otherwise.
350 bool has_root_path(const Twine &path);
352 /// @brief Has relative path?
354 /// relative_path != ""
356 /// @param path Input path.
357 /// @result True if the path has a relative path, false otherwise.
358 bool has_relative_path(const Twine &path);
360 /// @brief Has parent path?
362 /// parent_path != ""
364 /// @param path Input path.
365 /// @result True if the path has a parent path, false otherwise.
366 bool has_parent_path(const Twine &path);
368 /// @brief Has filename?
372 /// @param path Input path.
373 /// @result True if the path has a filename, false otherwise.
374 bool has_filename(const Twine &path);
380 /// @param path Input path.
381 /// @result True if the path has a stem, false otherwise.
382 bool has_stem(const Twine &path);
384 /// @brief Has extension?
388 /// @param path Input path.
389 /// @result True if the path has a extension, false otherwise.
390 bool has_extension(const Twine &path);
392 /// @brief Is path absolute?
394 /// @param path Input path.
395 /// @result True if the path is absolute, false if it is not.
396 bool is_absolute(const Twine &path);
398 /// @brief Is path relative?
400 /// @param path Input path.
401 /// @result True if the path is relative, false if it is not.
402 bool is_relative(const Twine &path);
404 /// @brief Remove redundant leading "./" pieces and consecutive separators.
406 /// @param path Input path.
407 /// @result The cleaned-up \a path.
408 StringRef remove_leading_dotslash(StringRef path);
410 } // end namespace path
411 } // end namespace sys
412 } // end namespace llvm