]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/llvm/include/llvm/Support/PathV2.h
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / llvm / 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 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_SUPPORT_PATHV2_H
17 #define LLVM_SUPPORT_PATHV2_H
18
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/DataTypes.h"
22 #include <iterator>
23
24 namespace llvm {
25 namespace sys {
26 namespace path {
27
28 /// @name Lexical Component Iterator
29 /// @{
30
31 /// @brief Path iterator.
32 ///
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.
40 ///
41 /// Iteration examples. Each component is separated by ',':
42 /// @code
43 ///   /          => /
44 ///   /foo       => /,foo
45 ///   foo/       => foo,.
46 ///   /foo/bar   => /,foo,bar
47 ///   ../        => ..,.
48 ///   C:\foo\bar => C:,/,foo,bar
49 /// @endcode
50 class const_iterator {
51   StringRef Path;      ///< The entire path.
52   StringRef Component; ///< The current component. Not necessarily in Path.
53   size_t    Position;  ///< The iterators current position within Path.
54
55   // An end iterator has Position = Path.size() + 1.
56   friend const_iterator begin(StringRef path);
57   friend const_iterator end(StringRef path);
58
59 public:
60   typedef const StringRef value_type;
61   typedef ptrdiff_t difference_type;
62   typedef value_type &reference;
63   typedef value_type *pointer;
64   typedef std::bidirectional_iterator_tag iterator_category;
65
66   reference operator*() const { return Component; }
67   pointer   operator->() const { return &Component; }
68   const_iterator &operator++();    // preincrement
69   const_iterator &operator++(int); // postincrement
70   const_iterator &operator--();    // predecrement
71   const_iterator &operator--(int); // postdecrement
72   bool operator==(const const_iterator &RHS) const;
73   bool operator!=(const const_iterator &RHS) const;
74
75   /// @brief Difference in bytes between this and RHS.
76   ptrdiff_t operator-(const const_iterator &RHS) const;
77 };
78
79 typedef std::reverse_iterator<const_iterator> reverse_iterator;
80
81 /// @brief Get begin iterator over \a path.
82 /// @param path Input path.
83 /// @returns Iterator initialized with the first component of \a path.
84 const_iterator begin(StringRef path);
85
86 /// @brief Get end iterator over \a path.
87 /// @param path Input path.
88 /// @returns Iterator initialized to the end of \a path.
89 const_iterator end(StringRef path);
90
91 /// @brief Get reverse begin iterator over \a path.
92 /// @param path Input path.
93 /// @returns Iterator initialized with the first reverse component of \a path.
94 inline reverse_iterator rbegin(StringRef path) {
95   return reverse_iterator(end(path));
96 }
97
98 /// @brief Get reverse end iterator over \a path.
99 /// @param path Input path.
100 /// @returns Iterator initialized to the reverse end of \a path.
101 inline reverse_iterator rend(StringRef path) {
102   return reverse_iterator(begin(path));
103 }
104
105 /// @}
106 /// @name Lexical Modifiers
107 /// @{
108
109 /// @brief Remove the last component from \a path unless it is the root dir.
110 ///
111 /// @code
112 ///   directory/filename.cpp => directory/
113 ///   directory/             => directory
114 ///   /                      => /
115 /// @endcode
116 ///
117 /// @param path A path that is modified to not have a file component.
118 void remove_filename(SmallVectorImpl<char> &path);
119
120 /// @brief Replace the file extension of \a path with \a extension.
121 ///
122 /// @code
123 ///   ./filename.cpp => ./filename.extension
124 ///   ./filename     => ./filename.extension
125 ///   ./             => ./.extension
126 /// @endcode
127 ///
128 /// @param path A path that has its extension replaced with \a extension.
129 /// @param extension The extension to be added. It may be empty. It may also
130 ///                  optionally start with a '.', if it does not, one will be
131 ///                  prepended.
132 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension);
133
134 /// @brief Append to path.
135 ///
136 /// @code
137 ///   /foo  + bar/f => /foo/bar/f
138 ///   /foo/ + bar/f => /foo/bar/f
139 ///   foo   + bar/f => foo/bar/f
140 /// @endcode
141 ///
142 /// @param path Set to \a path + \a component.
143 /// @param a The component to be appended to \a path.
144 void append(SmallVectorImpl<char> &path, const Twine &a,
145                                          const Twine &b = "",
146                                          const Twine &c = "",
147                                          const Twine &d = "");
148
149 /// @brief Append to path.
150 ///
151 /// @code
152 ///   /foo  + [bar,f] => /foo/bar/f
153 ///   /foo/ + [bar,f] => /foo/bar/f
154 ///   foo   + [bar,f] => foo/bar/f
155 /// @endcode
156 ///
157 /// @param path Set to \a path + [\a begin, \a end).
158 /// @param begin Start of components to append.
159 /// @param end One past the end of components to append.
160 void append(SmallVectorImpl<char> &path,
161             const_iterator begin, const_iterator end);
162
163 /// @}
164 /// @name Transforms (or some other better name)
165 /// @{
166
167 /// Convert path to the native form. This is used to give paths to users and
168 /// operating system calls in the platform's normal way. For example, on Windows
169 /// all '/' are converted to '\'.
170 ///
171 /// @param path A path that is transformed to native format.
172 /// @param result Holds the result of the transformation.
173 void native(const Twine &path, SmallVectorImpl<char> &result);
174
175 /// @}
176 /// @name Lexical Observers
177 /// @{
178
179 /// @brief Get root name.
180 ///
181 /// @code
182 ///   //net/hello => //net
183 ///   c:/hello    => c: (on Windows, on other platforms nothing)
184 ///   /hello      => <empty>
185 /// @endcode
186 ///
187 /// @param path Input path.
188 /// @result The root name of \a path if it has one, otherwise "".
189 const StringRef root_name(StringRef path);
190
191 /// @brief Get root directory.
192 ///
193 /// @code
194 ///   /goo/hello => /
195 ///   c:/hello   => /
196 ///   d/file.txt => <empty>
197 /// @endcode
198 ///
199 /// @param path Input path.
200 /// @result The root directory of \a path if it has one, otherwise
201 ///               "".
202 const StringRef root_directory(StringRef path);
203   
204 /// @brief Get root path.
205 ///
206 /// Equivalent to root_name + root_directory.
207 ///
208 /// @param path Input path.
209 /// @result The root path of \a path if it has one, otherwise "".
210 const StringRef root_path(StringRef path);
211
212 /// @brief Get relative path.
213 ///
214 /// @code
215 ///   C:\hello\world => hello\world
216 ///   foo/bar        => foo/bar
217 ///   /foo/bar       => foo/bar
218 /// @endcode
219 ///
220 /// @param path Input path.
221 /// @result The path starting after root_path if one exists, otherwise "".
222 const StringRef relative_path(StringRef path);
223
224 /// @brief Get parent path.
225 ///
226 /// @code
227 ///   /          => <empty>
228 ///   /foo       => /
229 ///   foo/../bar => foo/..
230 /// @endcode
231 ///
232 /// @param path Input path.
233 /// @result The parent path of \a path if one exists, otherwise "".
234 const StringRef parent_path(StringRef path);
235
236 /// @brief Get filename.
237 ///
238 /// @code
239 ///   /foo.txt    => foo.txt
240 ///   .          => .
241 ///   ..         => ..
242 ///   /          => /
243 /// @endcode
244 ///
245 /// @param path Input path.
246 /// @result The filename part of \a path. This is defined as the last component
247 ///         of \a path.
248 const StringRef filename(StringRef path);
249
250 /// @brief Get stem.
251 ///
252 /// If filename contains a dot but not solely one or two dots, result is the
253 /// substring of filename ending at (but not including) the last dot. Otherwise
254 /// it is filename.
255 ///
256 /// @code
257 ///   /foo/bar.txt => bar
258 ///   /foo/bar     => bar
259 ///   /foo/.txt    => <empty>
260 ///   /foo/.       => .
261 ///   /foo/..      => ..
262 /// @endcode
263 ///
264 /// @param path Input path.
265 /// @result The stem of \a path.
266 const StringRef stem(StringRef path);
267
268 /// @brief Get extension.
269 ///
270 /// If filename contains a dot but not solely one or two dots, result is the
271 /// substring of filename starting at (and including) the last dot, and ending
272 /// at the end of \a path. Otherwise "".
273 ///
274 /// @code
275 ///   /foo/bar.txt => .txt
276 ///   /foo/bar     => <empty>
277 ///   /foo/.txt    => .txt
278 /// @endcode
279 ///
280 /// @param path Input path.
281 /// @result The extension of \a path.
282 const StringRef extension(StringRef path);
283
284 /// @brief Check whether the given char is a path separator on the host OS.
285 ///
286 /// @param value a character
287 /// @result true if \a value is a path separator character on the host OS
288 bool is_separator(char value);
289
290 /// @brief Get the typical temporary directory for the system, e.g., 
291 /// "/var/tmp" or "C:/TEMP"
292 ///
293 /// @param erasedOnReboot Whether to favor a path that is erased on reboot
294 /// rather than one that potentially persists longer. This parameter will be
295 /// ignored if the user or system has set the typical environment variable
296 /// (e.g., TEMP on Windows, TMPDIR on *nix) to specify a temporary directory.
297 ///
298 /// @param result Holds the resulting path name.
299 void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result);
300
301 /// @brief Has root name?
302 ///
303 /// root_name != ""
304 ///
305 /// @param path Input path.
306 /// @result True if the path has a root name, false otherwise.
307 bool has_root_name(const Twine &path);
308
309 /// @brief Has root directory?
310 ///
311 /// root_directory != ""
312 ///
313 /// @param path Input path.
314 /// @result True if the path has a root directory, false otherwise.
315 bool has_root_directory(const Twine &path);
316
317 /// @brief Has root path?
318 ///
319 /// root_path != ""
320 ///
321 /// @param path Input path.
322 /// @result True if the path has a root path, false otherwise.
323 bool has_root_path(const Twine &path);
324
325 /// @brief Has relative path?
326 ///
327 /// relative_path != ""
328 ///
329 /// @param path Input path.
330 /// @result True if the path has a relative path, false otherwise.
331 bool has_relative_path(const Twine &path);
332
333 /// @brief Has parent path?
334 ///
335 /// parent_path != ""
336 ///
337 /// @param path Input path.
338 /// @result True if the path has a parent path, false otherwise.
339 bool has_parent_path(const Twine &path);
340
341 /// @brief Has filename?
342 ///
343 /// filename != ""
344 ///
345 /// @param path Input path.
346 /// @result True if the path has a filename, false otherwise.
347 bool has_filename(const Twine &path);
348
349 /// @brief Has stem?
350 ///
351 /// stem != ""
352 ///
353 /// @param path Input path.
354 /// @result True if the path has a stem, false otherwise.
355 bool has_stem(const Twine &path);
356
357 /// @brief Has extension?
358 ///
359 /// extension != ""
360 ///
361 /// @param path Input path.
362 /// @result True if the path has a extension, false otherwise.
363 bool has_extension(const Twine &path);
364
365 /// @brief Is path absolute?
366 ///
367 /// @param path Input path.
368 /// @result True if the path is absolute, false if it is not.
369 bool is_absolute(const Twine &path);
370
371 /// @brief Is path relative?
372 ///
373 /// @param path Input path.
374 /// @result True if the path is relative, false if it is not.
375 bool is_relative(const Twine &path);
376
377 } // end namespace path
378 } // end namespace sys
379 } // end namespace llvm
380
381 #endif