475082812d4c1bb3d4abd1c737b809d31a89aa0b
[oota-llvm.git] / include / llvm / Support / PathV2.h
1 //===- llvm/Support/PathV2.h - Path Operating System Concept ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
12 // path class.
13 //
14 // All functions return void and their actual work via the last out argument.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_SUPPORT_PATHV2_H
19 #define LLVM_SUPPORT_PATHV2_H
20
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/Support/DataTypes.h"
24 #include <iterator>
25
26 namespace llvm {
27 namespace sys {
28 namespace path {
29
30 /// @name Lexical Component Iterator
31 /// @{
32
33 /// @brief Path iterator.
34 ///
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.
42 ///
43 /// Iteration examples. Each component is separated by ',':
44 /// /          => /
45 /// /foo       => /,foo
46 /// foo/       => foo,.
47 /// /foo/bar   => /,foo,bar
48 /// ../        => ..,.
49 /// C:\foo\bar => C:,/,foo,bar
50 ///
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.
55
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);
59
60 public:
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;
66
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;
75
76   /// @brief Difference in bytes between this and RHS.
77   ptrdiff_t operator-(const const_iterator &RHS) const;
78 };
79
80 typedef std::reverse_iterator<const_iterator> reverse_iterator;
81
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);
86
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);
91
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));
97 }
98
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));
104 }
105
106 /// @}
107 /// @name Lexical Modifiers
108 /// @{
109
110 /// @brief Remove the last component from \a path unless it is the root dir.
111 ///
112 /// directory/filename.cpp => directory/
113 /// directory/             => directory
114 /// /                      => /
115 ///
116 /// @param path A path that is modified to not have a file component.
117 void remove_filename(SmallVectorImpl<char> &path);
118
119 /// @brief Replace the file extension of \a path with \a extension.
120 ///
121 /// ./filename.cpp => ./filename.extension
122 /// ./filename     => ./filename.extension
123 /// ./             => ./.extension
124 ///
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
128 ///                  prepended.
129 void replace_extension(SmallVectorImpl<char> &path,
130                              const Twine &extension);
131
132 /// @brief Append to path.
133 ///
134 /// /foo  + bar/f => /foo/bar/f
135 /// /foo/ + bar/f => /foo/bar/f
136 /// foo   + bar/f => foo/bar/f
137 ///
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,
141                                                const Twine &b = "",
142                                                const Twine &c = "",
143                                                const Twine &d = "");
144
145 /// @brief Append to path.
146 ///
147 /// /foo  + [bar,f] => /foo/bar/f
148 /// /foo/ + [bar,f] => /foo/bar/f
149 /// foo   + [bar,f] => foo/bar/f
150 ///
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);
156
157 /// @}
158 /// @name Transforms (or some other better name)
159 /// @{
160
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 '\'.
164 ///
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);
168
169 /// @}
170 /// @name Lexical Observers
171 /// @{
172
173 /// @brief Get root name.
174 ///
175 /// //net/hello => //net
176 /// c:/hello    => c: (on Windows, on other platforms nothing)
177 /// /hello      => <empty>
178 ///
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);
182
183 /// @brief Get root directory.
184 ///
185 /// /goo/hello => /
186 /// c:/hello   => /
187 /// d/file.txt => <empty>
188 ///
189 /// @param path Input path.
190 /// @param result Set to the root directory of \a path if it has one, otherwise
191 ///               "".
192 void root_directory(const StringRef &path, StringRef &result);
193
194 /// @brief Get root path.
195 ///
196 /// Equivalent to root_name + root_directory.
197 ///
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);
201
202 /// @brief Get relative path.
203 ///
204 /// C:\hello\world => hello\world
205 /// foo/bar        => foo/bar
206 /// /foo/bar       => foo/bar
207 ///
208 /// @param path Input path.
209 /// @param result Set to the path starting after root_path if one exists,
210 ///               otherwise "".
211 void relative_path(const StringRef &path, StringRef &result);
212
213 /// @brief Get parent path.
214 ///
215 /// /          => <empty>
216 /// /foo       => /
217 /// foo/../bar => foo/..
218 ///
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);
222
223 /// @brief Get filename.
224 ///
225 /// /foo.txt    => foo.txt
226 /// .          => .
227 /// ..         => ..
228 /// /          => /
229 ///
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);
234
235 /// @brief Get stem.
236 ///
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
239 /// it is filename.
240 ///
241 /// /foo/bar.txt => bar
242 /// /foo/bar     => bar
243 /// /foo/.txt    => <empty>
244 /// /foo/.       => .
245 /// /foo/..      => ..
246 ///
247 /// @param path Input path.
248 /// @param result Set to the stem of \a path.
249 void stem(const StringRef &path, StringRef &result);
250
251 /// @brief Get extension.
252 ///
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 "".
256 ///
257 /// /foo/bar.txt => .txt
258 /// /foo/bar     => <empty>
259 /// /foo/.txt    => .txt
260 ///
261 /// @param path Input path.
262 /// @param result Set to the extension of \a path.
263 void extension(const StringRef &path, StringRef &result);
264
265 /// @brief Has root name?
266 ///
267 /// root_name != ""
268 ///
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);
272
273 /// @brief Has root directory?
274 ///
275 /// root_directory != ""
276 ///
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);
280
281 /// @brief Has root path?
282 ///
283 /// root_path != ""
284 ///
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);
288
289 /// @brief Has relative path?
290 ///
291 /// relative_path != ""
292 ///
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);
296
297 /// @brief Has parent path?
298 ///
299 /// parent_path != ""
300 ///
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);
304
305 /// @brief Has filename?
306 ///
307 /// filename != ""
308 ///
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);
312
313 /// @brief Has stem?
314 ///
315 /// stem != ""
316 ///
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);
320
321 /// @brief Has extension?
322 ///
323 /// extension != ""
324 ///
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);
328
329 /// @brief Is path absolute?
330 ///
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);
334
335 /// @brief Is path relative?
336 ///
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);
340
341 } // end namespace path
342 } // end namespace sys
343 } // end namespace llvm
344
345 #endif