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 // All functions return void and their actual work via the last out argument.
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_SUPPORT_PATHV2_H
19 #define LLVM_SUPPORT_PATHV2_H
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/Support/DataTypes.h"
30 /// @name Lexical Component Iterator
33 /// @brief Path iterator.
35 /// This is a bidirectional iterator that iterates over the individual
36 /// components in \a path. The forward 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 /// The backwards traversal order is the reverse of forward traversal.
43 /// Iteration examples. Each component is separated by ',':
47 /// /foo/bar => /,foo,bar
49 /// C:\foo\bar => C:,/,foo,bar
51 class const_iterator {
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(const StringRef &path);
58 friend const_iterator end(const StringRef &path);
61 typedef const StringRef value_type;
62 typedef ptrdiff_t difference_type;
63 typedef value_type &reference;
64 typedef value_type *pointer;
65 typedef std::bidirectional_iterator_tag iterator_category;
67 reference operator*() const { return Component; }
68 pointer operator->() const { return &Component; }
69 const_iterator &operator++(); // preincrement
70 const_iterator &operator++(int); // postincrement
71 const_iterator &operator--(); // predecrement
72 const_iterator &operator--(int); // postdecrement
73 bool operator==(const const_iterator &RHS) const;
74 bool operator!=(const const_iterator &RHS) const;
76 /// @brief Difference in bytes between this and RHS.
77 ptrdiff_t operator-(const const_iterator &RHS) const;
80 typedef std::reverse_iterator<const_iterator> reverse_iterator;
82 /// @brief Get begin iterator over \a path.
83 /// @param path Input path.
84 /// @returns Iterator initialized with the first component of \a path.
85 const_iterator begin(const StringRef &path);
87 /// @brief Get end iterator over \a path.
88 /// @param path Input path.
89 /// @returns Iterator initialized to the end of \a path.
90 const_iterator end(const StringRef &path);
92 /// @brief Get reverse begin iterator over \a path.
93 /// @param path Input path.
94 /// @returns Iterator initialized with the first reverse component of \a path.
95 inline reverse_iterator rbegin(const StringRef &path) {
96 return reverse_iterator(end(path));
99 /// @brief Get reverse end iterator over \a path.
100 /// @param path Input path.
101 /// @returns Iterator initialized to the reverse end of \a path.
102 inline reverse_iterator rend(const StringRef &path) {
103 return reverse_iterator(begin(path));
107 /// @name Lexical Modifiers
110 /// @brief Remove the last component from \a path unless it is the root dir.
112 /// directory/filename.cpp => directory/
113 /// directory/ => directory
116 /// @param path A path that is modified to not have a file component.
117 void remove_filename(SmallVectorImpl<char> &path);
119 /// @brief Replace the file extension of \a path with \a extension.
121 /// ./filename.cpp => ./filename.extension
122 /// ./filename => ./filename.extension
123 /// ./ => ./.extension
125 /// @param path A path that has its extension replaced with \a extension.
126 /// @param extension The extension to be added. It may be empty. It may also
127 /// optionally start with a '.', if it does not, one will be
129 void replace_extension(SmallVectorImpl<char> &path,
130 const Twine &extension);
132 /// @brief Append to path.
134 /// /foo + bar/f => /foo/bar/f
135 /// /foo/ + bar/f => /foo/bar/f
136 /// foo + bar/f => foo/bar/f
138 /// @param path Set to \a path + \a component.
139 /// @param component The component to be appended to \a path.
140 void append(SmallVectorImpl<char> &path, const Twine &a,
143 const Twine &d = "");
145 /// @brief Append to path.
147 /// /foo + [bar,f] => /foo/bar/f
148 /// /foo/ + [bar,f] => /foo/bar/f
149 /// foo + [bar,f] => foo/bar/f
151 /// @param path Set to \a path + [\a begin, \a end).
152 /// @param begin Start of components to append.
153 /// @param end One past the end of components to append.
154 void append(SmallVectorImpl<char> &path,
155 const_iterator begin, const_iterator end);
158 /// @name Transforms (or some other better name)
161 /// Convert path to the native form. This is used to give paths to users and
162 /// operating system calls in the platform's normal way. For example, on Windows
163 /// all '/' are converted to '\'.
165 /// @param path A path that is transformed to native format.
166 /// @param result Holds the result of the transformation.
167 void native(const Twine &path, SmallVectorImpl<char> &result);
170 /// @name Lexical Observers
173 /// @brief Get root name.
175 /// //net/hello => //net
176 /// c:/hello => c: (on Windows, on other platforms nothing)
177 /// /hello => <empty>
179 /// @param path Input path.
180 /// @param result Set to the root name of \a path if it has one, otherwise "".
181 void root_name(const StringRef &path, StringRef &result);
183 /// @brief Get root directory.
187 /// d/file.txt => <empty>
189 /// @param path Input path.
190 /// @param result Set to the root directory of \a path if it has one, otherwise
192 void root_directory(const StringRef &path, StringRef &result);
194 /// @brief Get root path.
196 /// Equivalent to root_name + root_directory.
198 /// @param path Input path.
199 /// @param result Set to the root path of \a path if it has one, otherwise "".
200 void root_path(const StringRef &path, StringRef &result);
202 /// @brief Get relative path.
204 /// C:\hello\world => hello\world
205 /// foo/bar => foo/bar
206 /// /foo/bar => foo/bar
208 /// @param path Input path.
209 /// @param result Set to the path starting after root_path if one exists,
211 void relative_path(const StringRef &path, StringRef &result);
213 /// @brief Get parent path.
217 /// foo/../bar => foo/..
219 /// @param path Input path.
220 /// @param result Set to the parent path of \a path if one exists, otherwise "".
221 void parent_path(const StringRef &path, StringRef &result);
223 /// @brief Get filename.
225 /// /foo.txt => foo.txt
230 /// @param path Input path.
231 /// @param result Set to the filename part of \a path. This is defined as the
232 /// last component of \a path.
233 void filename(const StringRef &path, StringRef &result);
237 /// If filename contains a dot but not solely one or two dots, result is the
238 /// substring of filename ending at (but not including) the last dot. Otherwise
241 /// /foo/bar.txt => bar
243 /// /foo/.txt => <empty>
247 /// @param path Input path.
248 /// @param result Set to the stem of \a path.
249 void stem(const StringRef &path, StringRef &result);
251 /// @brief Get extension.
253 /// If filename contains a dot but not solely one or two dots, result is the
254 /// substring of filename starting at (and including) the last dot, and ending
255 /// at the end of \a path. Otherwise "".
257 /// /foo/bar.txt => .txt
258 /// /foo/bar => <empty>
259 /// /foo/.txt => .txt
261 /// @param path Input path.
262 /// @param result Set to the extension of \a path.
263 void extension(const StringRef &path, StringRef &result);
265 /// @brief Has root name?
269 /// @param path Input path.
270 /// @param result Set to true if the path has a root name, false otherwise.
271 void has_root_name(const Twine &path, bool &result);
273 /// @brief Has root directory?
275 /// root_directory != ""
277 /// @param path Input path.
278 /// @param result Set to true if the path has a root directory, false otherwise.
279 void has_root_directory(const Twine &path, bool &result);
281 /// @brief Has root path?
285 /// @param path Input path.
286 /// @param result Set to true if the path has a root path, false otherwise.
287 void has_root_path(const Twine &path, bool &result);
289 /// @brief Has relative path?
291 /// relative_path != ""
293 /// @param path Input path.
294 /// @param result Set to true if the path has a relative path, false otherwise.
295 void has_relative_path(const Twine &path, bool &result);
297 /// @brief Has parent path?
299 /// parent_path != ""
301 /// @param path Input path.
302 /// @param result Set to true if the path has a parent path, false otherwise.
303 void has_parent_path(const Twine &path, bool &result);
305 /// @brief Has filename?
309 /// @param path Input path.
310 /// @param result Set to true if the path has a filename, false otherwise.
311 void has_filename(const Twine &path, bool &result);
317 /// @param path Input path.
318 /// @param result Set to true if the path has a stem, false otherwise.
319 void has_stem(const Twine &path, bool &result);
321 /// @brief Has extension?
325 /// @param path Input path.
326 /// @param result Set to true if the path has a extension, false otherwise.
327 void has_extension(const Twine &path, bool &result);
329 /// @brief Is path absolute?
331 /// @param path Input path.
332 /// @param result Set to true if the path is absolute, false if it is not.
333 void is_absolute(const Twine &path, bool &result);
335 /// @brief Is path relative?
337 /// @param path Input path.
338 /// @param result Set to true if the path is relative, false if it is not.
339 void is_relative(const Twine &path, bool &result);
341 } // end namespace path
342 } // end namespace sys
343 } // end namespace llvm