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 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_SUPPORT_PATHV2_H
17 #define LLVM_SUPPORT_PATHV2_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 a bidirectional iterator that iterates over the individual
34 /// components in \a path. The forward 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 /// The backwards traversal order is the reverse of forward traversal.
41 /// Iteration examples. Each component is separated by ',':
45 /// /foo/bar => /,foo,bar
47 /// C:\foo\bar => C:,/,foo,bar
49 class const_iterator {
50 StringRef Path; //< The entire path.
51 StringRef Component; //< The current component. Not necessarily in Path.
52 size_t Position; //< The iterators current position within Path.
54 // An end iterator has Position = Path.size() + 1.
55 friend const_iterator begin(const StringRef &path);
56 friend const_iterator end(const StringRef &path);
59 typedef const StringRef value_type;
60 typedef ptrdiff_t difference_type;
61 typedef value_type &reference;
62 typedef value_type *pointer;
63 typedef std::bidirectional_iterator_tag iterator_category;
65 reference operator*() const { return Component; }
66 pointer operator->() const { return &Component; }
67 const_iterator &operator++(); // preincrement
68 const_iterator &operator++(int); // postincrement
69 const_iterator &operator--(); // predecrement
70 const_iterator &operator--(int); // postdecrement
71 bool operator==(const const_iterator &RHS) const;
72 bool operator!=(const const_iterator &RHS) const;
74 /// @brief Difference in bytes between this and RHS.
75 ptrdiff_t operator-(const const_iterator &RHS) const;
78 typedef std::reverse_iterator<const_iterator> reverse_iterator;
80 /// @brief Get begin iterator over \a path.
81 /// @param path Input path.
82 /// @returns Iterator initialized with the first component of \a path.
83 const_iterator begin(const StringRef &path);
85 /// @brief Get end iterator over \a path.
86 /// @param path Input path.
87 /// @returns Iterator initialized to the end of \a path.
88 const_iterator end(const StringRef &path);
90 /// @brief Get reverse begin iterator over \a path.
91 /// @param path Input path.
92 /// @returns Iterator initialized with the first reverse component of \a path.
93 inline reverse_iterator rbegin(const StringRef &path) {
94 return reverse_iterator(end(path));
97 /// @brief Get reverse end iterator over \a path.
98 /// @param path Input path.
99 /// @returns Iterator initialized to the reverse end of \a path.
100 inline reverse_iterator rend(const StringRef &path) {
101 return reverse_iterator(begin(path));
105 /// @name Lexical Modifiers
108 /// @brief Remove the last component from \a path unless it is the root dir.
110 /// directory/filename.cpp => directory/
111 /// directory/ => directory
114 /// @param path A path that is modified to not have a file component.
115 void remove_filename(SmallVectorImpl<char> &path);
117 /// @brief Replace the file extension of \a path with \a extension.
119 /// ./filename.cpp => ./filename.extension
120 /// ./filename => ./filename.extension
121 /// ./ => ./.extension
123 /// @param path A path that has its extension replaced with \a extension.
124 /// @param extension The extension to be added. It may be empty. It may also
125 /// optionally start with a '.', if it does not, one will be
127 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension);
129 /// @brief Append to path.
131 /// /foo + bar/f => /foo/bar/f
132 /// /foo/ + bar/f => /foo/bar/f
133 /// foo + bar/f => foo/bar/f
135 /// @param path Set to \a path + \a component.
136 /// @param component The component to be appended to \a path.
137 void append(SmallVectorImpl<char> &path, const Twine &a,
140 const Twine &d = "");
142 /// @brief Append to path.
144 /// /foo + [bar,f] => /foo/bar/f
145 /// /foo/ + [bar,f] => /foo/bar/f
146 /// foo + [bar,f] => foo/bar/f
148 /// @param path Set to \a path + [\a begin, \a end).
149 /// @param begin Start of components to append.
150 /// @param end One past the end of components to append.
151 void append(SmallVectorImpl<char> &path,
152 const_iterator begin, const_iterator end);
155 /// @name Transforms (or some other better name)
158 /// Convert path to the native form. This is used to give paths to users and
159 /// operating system calls in the platform's normal way. For example, on Windows
160 /// all '/' are converted to '\'.
162 /// @param path A path that is transformed to native format.
163 /// @param result Holds the result of the transformation.
164 void native(const Twine &path, SmallVectorImpl<char> &result);
167 /// @name Lexical Observers
170 /// @brief Get root name.
172 /// //net/hello => //net
173 /// c:/hello => c: (on Windows, on other platforms nothing)
174 /// /hello => <empty>
176 /// @param path Input path.
177 /// @result The root name of \a path if it has one, otherwise "".
178 const StringRef root_name(const StringRef &path);
180 /// @brief Get root directory.
184 /// d/file.txt => <empty>
186 /// @param path Input path.
187 /// @result The root directory of \a path if it has one, otherwise
189 const StringRef root_directory(const StringRef &path);
191 /// @brief Get root path.
193 /// Equivalent to root_name + root_directory.
195 /// @param path Input path.
196 /// @result The root path of \a path if it has one, otherwise "".
197 const StringRef root_path(const StringRef &path);
199 /// @brief Get relative path.
201 /// C:\hello\world => hello\world
202 /// foo/bar => foo/bar
203 /// /foo/bar => foo/bar
205 /// @param path Input path.
206 /// @result The path starting after root_path if one exists, otherwise "".
207 const StringRef relative_path(const StringRef &path);
209 /// @brief Get parent path.
213 /// foo/../bar => foo/..
215 /// @param path Input path.
216 /// @result The parent path of \a path if one exists, otherwise "".
217 const StringRef parent_path(const StringRef &path);
219 /// @brief Get filename.
221 /// /foo.txt => foo.txt
226 /// @param path Input path.
227 /// @result The filename part of \a path. This is defined as the last component
229 const StringRef filename(const StringRef &path);
233 /// If filename contains a dot but not solely one or two dots, result is the
234 /// substring of filename ending at (but not including) the last dot. Otherwise
237 /// /foo/bar.txt => bar
239 /// /foo/.txt => <empty>
243 /// @param path Input path.
244 /// @result The stem of \a path.
245 const StringRef stem(const StringRef &path);
247 /// @brief Get extension.
249 /// If filename contains a dot but not solely one or two dots, result is the
250 /// substring of filename starting at (and including) the last dot, and ending
251 /// at the end of \a path. Otherwise "".
253 /// /foo/bar.txt => .txt
254 /// /foo/bar => <empty>
255 /// /foo/.txt => .txt
257 /// @param path Input path.
258 /// @result The extension of \a path.
259 const StringRef extension(const StringRef &path);
261 /// @brief Has root name?
265 /// @param path Input path.
266 /// @result True if the path has a root name, false otherwise.
267 bool has_root_name(const Twine &path);
269 /// @brief Has root directory?
271 /// root_directory != ""
273 /// @param path Input path.
274 /// @result True if the path has a root directory, false otherwise.
275 bool has_root_directory(const Twine &path);
277 /// @brief Has root path?
281 /// @param path Input path.
282 /// @result True if the path has a root path, false otherwise.
283 bool has_root_path(const Twine &path);
285 /// @brief Has relative path?
287 /// relative_path != ""
289 /// @param path Input path.
290 /// @result True if the path has a relative path, false otherwise.
291 bool has_relative_path(const Twine &path);
293 /// @brief Has parent path?
295 /// parent_path != ""
297 /// @param path Input path.
298 /// @result True if the path has a parent path, false otherwise.
299 bool has_parent_path(const Twine &path);
301 /// @brief Has filename?
305 /// @param path Input path.
306 /// @result True if the path has a filename, false otherwise.
307 bool has_filename(const Twine &path);
313 /// @param path Input path.
314 /// @result True if the path has a stem, false otherwise.
315 bool has_stem(const Twine &path);
317 /// @brief Has extension?
321 /// @param path Input path.
322 /// @result True if the path has a extension, false otherwise.
323 bool has_extension(const Twine &path);
325 /// @brief Is path absolute?
327 /// @param path Input path.
328 /// @result True if the path is absolute, false if it is not.
329 bool is_absolute(const Twine &path);
331 /// @brief Is path relative?
333 /// @param path Input path.
334 /// @result True if the path is relative, false if it is not.
335 bool is_relative(const Twine &path);
337 } // end namespace path
338 } // end namespace sys
339 } // end namespace llvm