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 const_iterator &operator++(int); // postincrement
65 bool operator==(const const_iterator &RHS) const;
66 bool operator!=(const const_iterator &RHS) const { return !(*this == RHS); }
68 /// @brief Difference in bytes between this and RHS.
69 ptrdiff_t operator-(const const_iterator &RHS) const;
72 /// @brief Reverse path iterator.
74 /// This is an input iterator that iterates over the individual components in
75 /// \a path in reverse order. The traversal order is exactly reversed from that
76 /// of \a const_iterator
77 class reverse_iterator
78 : public std::iterator<std::input_iterator_tag, const StringRef> {
79 StringRef Path; ///< The entire path.
80 StringRef Component; ///< The current component. Not necessarily in Path.
81 size_t Position; ///< The iterators current position within Path.
83 friend reverse_iterator rbegin(StringRef path);
84 friend reverse_iterator rend(StringRef path);
87 reference operator*() const { return Component; }
88 pointer operator->() const { return &Component; }
89 reverse_iterator &operator++(); // preincrement
90 reverse_iterator &operator++(int); // postincrement
91 bool operator==(const reverse_iterator &RHS) const;
92 bool operator!=(const reverse_iterator &RHS) const { return !(*this == RHS); }
95 /// @brief Get begin iterator over \a path.
96 /// @param path Input path.
97 /// @returns Iterator initialized with the first component of \a path.
98 const_iterator begin(StringRef path);
100 /// @brief Get end iterator over \a path.
101 /// @param path Input path.
102 /// @returns Iterator initialized to the end of \a path.
103 const_iterator end(StringRef path);
105 /// @brief Get reverse begin iterator over \a path.
106 /// @param path Input path.
107 /// @returns Iterator initialized with the first reverse component of \a path.
108 reverse_iterator rbegin(StringRef path);
110 /// @brief Get reverse end iterator over \a path.
111 /// @param path Input path.
112 /// @returns Iterator initialized to the reverse end of \a path.
113 reverse_iterator rend(StringRef path);
116 /// @name Lexical Modifiers
119 /// @brief Remove the last component from \a path unless it is the root dir.
122 /// directory/filename.cpp => directory/
123 /// directory/ => directory
124 /// filename.cpp => <empty>
128 /// @param path A path that is modified to not have a file component.
129 void remove_filename(SmallVectorImpl<char> &path);
131 /// @brief Replace the file extension of \a path with \a extension.
134 /// ./filename.cpp => ./filename.extension
135 /// ./filename => ./filename.extension
136 /// ./ => ./.extension
139 /// @param path A path that has its extension replaced with \a extension.
140 /// @param extension The extension to be added. It may be empty. It may also
141 /// optionally start with a '.', if it does not, one will be
143 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension);
145 /// @brief Append to path.
148 /// /foo + bar/f => /foo/bar/f
149 /// /foo/ + bar/f => /foo/bar/f
150 /// foo + bar/f => foo/bar/f
153 /// @param path Set to \a path + \a component.
154 /// @param a The component to be appended to \a path.
155 void append(SmallVectorImpl<char> &path, const Twine &a,
158 const Twine &d = "");
160 /// @brief Append to path.
163 /// /foo + [bar,f] => /foo/bar/f
164 /// /foo/ + [bar,f] => /foo/bar/f
165 /// foo + [bar,f] => foo/bar/f
168 /// @param path Set to \a path + [\a begin, \a end).
169 /// @param begin Start of components to append.
170 /// @param end One past the end of components to append.
171 void append(SmallVectorImpl<char> &path,
172 const_iterator begin, const_iterator end);
175 /// @name Transforms (or some other better name)
178 /// Convert path to the native form. This is used to give paths to users and
179 /// operating system calls in the platform's normal way. For example, on Windows
180 /// all '/' are converted to '\'.
182 /// @param path A path that is transformed to native format.
183 /// @param result Holds the result of the transformation.
184 void native(const Twine &path, SmallVectorImpl<char> &result);
186 /// Convert path to the native form in place. This is used to give paths to
187 /// users and operating system calls in the platform's normal way. For example,
188 /// on Windows all '/' are converted to '\'.
190 /// @param path A path that is transformed to native format.
191 void native(SmallVectorImpl<char> &path);
194 /// @name Lexical Observers
197 /// @brief Get root name.
200 /// //net/hello => //net
201 /// c:/hello => c: (on Windows, on other platforms nothing)
202 /// /hello => <empty>
205 /// @param path Input path.
206 /// @result The root name of \a path if it has one, otherwise "".
207 const StringRef root_name(StringRef path);
209 /// @brief Get root directory.
214 /// d/file.txt => <empty>
217 /// @param path Input path.
218 /// @result The root directory of \a path if it has one, otherwise
220 const StringRef root_directory(StringRef path);
222 /// @brief Get root path.
224 /// Equivalent to root_name + root_directory.
226 /// @param path Input path.
227 /// @result The root path of \a path if it has one, otherwise "".
228 const StringRef root_path(StringRef path);
230 /// @brief Get relative path.
233 /// C:\hello\world => hello\world
234 /// foo/bar => foo/bar
235 /// /foo/bar => foo/bar
238 /// @param path Input path.
239 /// @result The path starting after root_path if one exists, otherwise "".
240 const StringRef relative_path(StringRef path);
242 /// @brief Get parent path.
247 /// foo/../bar => foo/..
250 /// @param path Input path.
251 /// @result The parent path of \a path if one exists, otherwise "".
252 const StringRef parent_path(StringRef path);
254 /// @brief Get filename.
257 /// /foo.txt => foo.txt
263 /// @param path Input path.
264 /// @result The filename part of \a path. This is defined as the last component
266 const StringRef filename(StringRef path);
270 /// If filename contains a dot but not solely one or two dots, result is the
271 /// substring of filename ending at (but not including) the last dot. Otherwise
275 /// /foo/bar.txt => bar
277 /// /foo/.txt => <empty>
282 /// @param path Input path.
283 /// @result The stem of \a path.
284 const StringRef stem(StringRef path);
286 /// @brief Get extension.
288 /// If filename contains a dot but not solely one or two dots, result is the
289 /// substring of filename starting at (and including) the last dot, and ending
290 /// at the end of \a path. Otherwise "".
293 /// /foo/bar.txt => .txt
294 /// /foo/bar => <empty>
295 /// /foo/.txt => .txt
298 /// @param path Input path.
299 /// @result The extension of \a path.
300 const StringRef extension(StringRef path);
302 /// @brief Check whether the given char is a path separator on the host OS.
304 /// @param value a character
305 /// @result true if \a value is a path separator character on the host OS
306 bool is_separator(char value);
308 /// @brief Return the preferred separator for this platform.
310 /// @result StringRef of the preferred separator, null-terminated.
311 const StringRef get_separator();
313 /// @brief Get the typical temporary directory for the system, e.g.,
314 /// "/var/tmp" or "C:/TEMP"
316 /// @param erasedOnReboot Whether to favor a path that is erased on reboot
317 /// rather than one that potentially persists longer. This parameter will be
318 /// ignored if the user or system has set the typical environment variable
319 /// (e.g., TEMP on Windows, TMPDIR on *nix) to specify a temporary directory.
321 /// @param result Holds the resulting path name.
322 void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result);
324 /// @brief Get the user's home directory.
326 /// @param result Holds the resulting path name.
327 /// @result True if a home directory is set, false otherwise.
328 bool home_directory(SmallVectorImpl<char> &result);
330 /// @brief Has root name?
334 /// @param path Input path.
335 /// @result True if the path has a root name, false otherwise.
336 bool has_root_name(const Twine &path);
338 /// @brief Has root directory?
340 /// root_directory != ""
342 /// @param path Input path.
343 /// @result True if the path has a root directory, false otherwise.
344 bool has_root_directory(const Twine &path);
346 /// @brief Has root path?
350 /// @param path Input path.
351 /// @result True if the path has a root path, false otherwise.
352 bool has_root_path(const Twine &path);
354 /// @brief Has relative path?
356 /// relative_path != ""
358 /// @param path Input path.
359 /// @result True if the path has a relative path, false otherwise.
360 bool has_relative_path(const Twine &path);
362 /// @brief Has parent path?
364 /// parent_path != ""
366 /// @param path Input path.
367 /// @result True if the path has a parent path, false otherwise.
368 bool has_parent_path(const Twine &path);
370 /// @brief Has filename?
374 /// @param path Input path.
375 /// @result True if the path has a filename, false otherwise.
376 bool has_filename(const Twine &path);
382 /// @param path Input path.
383 /// @result True if the path has a stem, false otherwise.
384 bool has_stem(const Twine &path);
386 /// @brief Has extension?
390 /// @param path Input path.
391 /// @result True if the path has a extension, false otherwise.
392 bool has_extension(const Twine &path);
394 /// @brief Is path absolute?
396 /// @param path Input path.
397 /// @result True if the path is absolute, false if it is not.
398 bool is_absolute(const Twine &path);
400 /// @brief Is path relative?
402 /// @param path Input path.
403 /// @result True if the path is relative, false if it is not.
404 bool is_relative(const Twine &path);
406 } // end namespace path
407 } // end namespace sys
408 } // end namespace llvm