LLVM API Documentation

Path.h
Go to the documentation of this file.
00001 //===- llvm/Support/Path.h - Path Operating System Concept ------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file declares the llvm::sys::path namespace. It is designed after
00011 // TR2/boost filesystem (v3), but modified to remove exception handling and the
00012 // path class.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_SUPPORT_PATH_H
00017 #define LLVM_SUPPORT_PATH_H
00018 
00019 #include "llvm/ADT/SmallString.h"
00020 #include "llvm/ADT/Twine.h"
00021 #include "llvm/Support/DataTypes.h"
00022 #include <iterator>
00023 
00024 namespace llvm {
00025 namespace sys {
00026 namespace path {
00027 
00028 /// @name Lexical Component Iterator
00029 /// @{
00030 
00031 /// @brief Path iterator.
00032 ///
00033 /// This is an input iterator that iterates over the individual components in
00034 /// \a path. The traversal order is as follows:
00035 /// * The root-name element, if present.
00036 /// * The root-directory element, if present.
00037 /// * Each successive filename element, if present.
00038 /// * Dot, if one or more trailing non-root slash characters are present.
00039 /// Traversing backwards is possible with \a reverse_iterator
00040 ///
00041 /// Iteration examples. Each component is separated by ',':
00042 /// @code
00043 ///   /          => /
00044 ///   /foo       => /,foo
00045 ///   foo/       => foo,.
00046 ///   /foo/bar   => /,foo,bar
00047 ///   ../        => ..,.
00048 ///   C:\foo\bar => C:,/,foo,bar
00049 /// @endcode
00050 class const_iterator
00051     : public std::iterator<std::input_iterator_tag, const StringRef> {
00052   StringRef Path;      ///< The entire path.
00053   StringRef Component; ///< The current component. Not necessarily in Path.
00054   size_t    Position;  ///< The iterators current position within Path.
00055 
00056   // An end iterator has Position = Path.size() + 1.
00057   friend const_iterator begin(StringRef path);
00058   friend const_iterator end(StringRef path);
00059 
00060 public:
00061   reference operator*() const { return Component; }
00062   pointer   operator->() const { return &Component; }
00063   const_iterator &operator++();    // preincrement
00064   const_iterator &operator++(int); // postincrement
00065   bool operator==(const const_iterator &RHS) const;
00066   bool operator!=(const const_iterator &RHS) const { return !(*this == RHS); }
00067 
00068   /// @brief Difference in bytes between this and RHS.
00069   ptrdiff_t operator-(const const_iterator &RHS) const;
00070 };
00071 
00072 /// @brief Reverse path iterator.
00073 ///
00074 /// This is an input iterator that iterates over the individual components in
00075 /// \a path in reverse order. The traversal order is exactly reversed from that
00076 /// of \a const_iterator
00077 class reverse_iterator
00078     : public std::iterator<std::input_iterator_tag, const StringRef> {
00079   StringRef Path;      ///< The entire path.
00080   StringRef Component; ///< The current component. Not necessarily in Path.
00081   size_t    Position;  ///< The iterators current position within Path.
00082 
00083   friend reverse_iterator rbegin(StringRef path);
00084   friend reverse_iterator rend(StringRef path);
00085 
00086 public:
00087   reference operator*() const { return Component; }
00088   pointer   operator->() const { return &Component; }
00089   reverse_iterator &operator++();    // preincrement
00090   reverse_iterator &operator++(int); // postincrement
00091   bool operator==(const reverse_iterator &RHS) const;
00092   bool operator!=(const reverse_iterator &RHS) const { return !(*this == RHS); }
00093 };
00094 
00095 /// @brief Get begin iterator over \a path.
00096 /// @param path Input path.
00097 /// @returns Iterator initialized with the first component of \a path.
00098 const_iterator begin(StringRef path);
00099 
00100 /// @brief Get end iterator over \a path.
00101 /// @param path Input path.
00102 /// @returns Iterator initialized to the end of \a path.
00103 const_iterator end(StringRef path);
00104 
00105 /// @brief Get reverse begin iterator over \a path.
00106 /// @param path Input path.
00107 /// @returns Iterator initialized with the first reverse component of \a path.
00108 reverse_iterator rbegin(StringRef path);
00109 
00110 /// @brief Get reverse end iterator over \a path.
00111 /// @param path Input path.
00112 /// @returns Iterator initialized to the reverse end of \a path.
00113 reverse_iterator rend(StringRef path);
00114 
00115 /// @}
00116 /// @name Lexical Modifiers
00117 /// @{
00118 
00119 /// @brief Remove the last component from \a path unless it is the root dir.
00120 ///
00121 /// @code
00122 ///   directory/filename.cpp => directory/
00123 ///   directory/             => directory
00124 ///   filename.cpp           => <empty>
00125 ///   /                      => /
00126 /// @endcode
00127 ///
00128 /// @param path A path that is modified to not have a file component.
00129 void remove_filename(SmallVectorImpl<char> &path);
00130 
00131 /// @brief Replace the file extension of \a path with \a extension.
00132 ///
00133 /// @code
00134 ///   ./filename.cpp => ./filename.extension
00135 ///   ./filename     => ./filename.extension
00136 ///   ./             => ./.extension
00137 /// @endcode
00138 ///
00139 /// @param path A path that has its extension replaced with \a extension.
00140 /// @param extension The extension to be added. It may be empty. It may also
00141 ///                  optionally start with a '.', if it does not, one will be
00142 ///                  prepended.
00143 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension);
00144 
00145 /// @brief Append to path.
00146 ///
00147 /// @code
00148 ///   /foo  + bar/f => /foo/bar/f
00149 ///   /foo/ + bar/f => /foo/bar/f
00150 ///   foo   + bar/f => foo/bar/f
00151 /// @endcode
00152 ///
00153 /// @param path Set to \a path + \a component.
00154 /// @param a The component to be appended to \a path.
00155 void append(SmallVectorImpl<char> &path, const Twine &a,
00156                                          const Twine &b = "",
00157                                          const Twine &c = "",
00158                                          const Twine &d = "");
00159 
00160 /// @brief Append to path.
00161 ///
00162 /// @code
00163 ///   /foo  + [bar,f] => /foo/bar/f
00164 ///   /foo/ + [bar,f] => /foo/bar/f
00165 ///   foo   + [bar,f] => foo/bar/f
00166 /// @endcode
00167 ///
00168 /// @param path Set to \a path + [\a begin, \a end).
00169 /// @param begin Start of components to append.
00170 /// @param end One past the end of components to append.
00171 void append(SmallVectorImpl<char> &path,
00172             const_iterator begin, const_iterator end);
00173 
00174 /// @}
00175 /// @name Transforms (or some other better name)
00176 /// @{
00177 
00178 /// Convert path to the native form. This is used to give paths to users and
00179 /// operating system calls in the platform's normal way. For example, on Windows
00180 /// all '/' are converted to '\'.
00181 ///
00182 /// @param path A path that is transformed to native format.
00183 /// @param result Holds the result of the transformation.
00184 void native(const Twine &path, SmallVectorImpl<char> &result);
00185 
00186 /// Convert path to the native form in place. This is used to give paths to
00187 /// users and operating system calls in the platform's normal way. For example,
00188 /// on Windows all '/' are converted to '\'.
00189 ///
00190 /// @param path A path that is transformed to native format.
00191 void native(SmallVectorImpl<char> &path);
00192 
00193 /// @}
00194 /// @name Lexical Observers
00195 /// @{
00196 
00197 /// @brief Get root name.
00198 ///
00199 /// @code
00200 ///   //net/hello => //net
00201 ///   c:/hello    => c: (on Windows, on other platforms nothing)
00202 ///   /hello      => <empty>
00203 /// @endcode
00204 ///
00205 /// @param path Input path.
00206 /// @result The root name of \a path if it has one, otherwise "".
00207 StringRef root_name(StringRef path);
00208 
00209 /// @brief Get root directory.
00210 ///
00211 /// @code
00212 ///   /goo/hello => /
00213 ///   c:/hello   => /
00214 ///   d/file.txt => <empty>
00215 /// @endcode
00216 ///
00217 /// @param path Input path.
00218 /// @result The root directory of \a path if it has one, otherwise
00219 ///               "".
00220 StringRef root_directory(StringRef path);
00221   
00222 /// @brief Get root path.
00223 ///
00224 /// Equivalent to root_name + root_directory.
00225 ///
00226 /// @param path Input path.
00227 /// @result The root path of \a path if it has one, otherwise "".
00228 StringRef root_path(StringRef path);
00229 
00230 /// @brief Get relative path.
00231 ///
00232 /// @code
00233 ///   C:\hello\world => hello\world
00234 ///   foo/bar        => foo/bar
00235 ///   /foo/bar       => foo/bar
00236 /// @endcode
00237 ///
00238 /// @param path Input path.
00239 /// @result The path starting after root_path if one exists, otherwise "".
00240 StringRef relative_path(StringRef path);
00241 
00242 /// @brief Get parent path.
00243 ///
00244 /// @code
00245 ///   /          => <empty>
00246 ///   /foo       => /
00247 ///   foo/../bar => foo/..
00248 /// @endcode
00249 ///
00250 /// @param path Input path.
00251 /// @result The parent path of \a path if one exists, otherwise "".
00252 StringRef parent_path(StringRef path);
00253 
00254 /// @brief Get filename.
00255 ///
00256 /// @code
00257 ///   /foo.txt    => foo.txt
00258 ///   .          => .
00259 ///   ..         => ..
00260 ///   /          => /
00261 /// @endcode
00262 ///
00263 /// @param path Input path.
00264 /// @result The filename part of \a path. This is defined as the last component
00265 ///         of \a path.
00266 StringRef filename(StringRef path);
00267 
00268 /// @brief Get stem.
00269 ///
00270 /// If filename contains a dot but not solely one or two dots, result is the
00271 /// substring of filename ending at (but not including) the last dot. Otherwise
00272 /// it is filename.
00273 ///
00274 /// @code
00275 ///   /foo/bar.txt => bar
00276 ///   /foo/bar     => bar
00277 ///   /foo/.txt    => <empty>
00278 ///   /foo/.       => .
00279 ///   /foo/..      => ..
00280 /// @endcode
00281 ///
00282 /// @param path Input path.
00283 /// @result The stem of \a path.
00284 StringRef stem(StringRef path);
00285 
00286 /// @brief Get extension.
00287 ///
00288 /// If filename contains a dot but not solely one or two dots, result is the
00289 /// substring of filename starting at (and including) the last dot, and ending
00290 /// at the end of \a path. Otherwise "".
00291 ///
00292 /// @code
00293 ///   /foo/bar.txt => .txt
00294 ///   /foo/bar     => <empty>
00295 ///   /foo/.txt    => .txt
00296 /// @endcode
00297 ///
00298 /// @param path Input path.
00299 /// @result The extension of \a path.
00300 StringRef extension(StringRef path);
00301 
00302 /// @brief Check whether the given char is a path separator on the host OS.
00303 ///
00304 /// @param value a character
00305 /// @result true if \a value is a path separator character on the host OS
00306 bool is_separator(char value);
00307 
00308 /// @brief Return the preferred separator for this platform.
00309 ///
00310 /// @result StringRef of the preferred separator, null-terminated.
00311 StringRef get_separator();
00312 
00313 /// @brief Get the typical temporary directory for the system, e.g., 
00314 /// "/var/tmp" or "C:/TEMP"
00315 ///
00316 /// @param erasedOnReboot Whether to favor a path that is erased on reboot
00317 /// rather than one that potentially persists longer. This parameter will be
00318 /// ignored if the user or system has set the typical environment variable
00319 /// (e.g., TEMP on Windows, TMPDIR on *nix) to specify a temporary directory.
00320 ///
00321 /// @param result Holds the resulting path name.
00322 void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result);
00323 
00324 /// @brief Get the user's home directory.
00325 ///
00326 /// @param result Holds the resulting path name.
00327 /// @result True if a home directory is set, false otherwise.
00328 bool home_directory(SmallVectorImpl<char> &result);
00329 
00330 /// @brief Has root name?
00331 ///
00332 /// root_name != ""
00333 ///
00334 /// @param path Input path.
00335 /// @result True if the path has a root name, false otherwise.
00336 bool has_root_name(const Twine &path);
00337 
00338 /// @brief Has root directory?
00339 ///
00340 /// root_directory != ""
00341 ///
00342 /// @param path Input path.
00343 /// @result True if the path has a root directory, false otherwise.
00344 bool has_root_directory(const Twine &path);
00345 
00346 /// @brief Has root path?
00347 ///
00348 /// root_path != ""
00349 ///
00350 /// @param path Input path.
00351 /// @result True if the path has a root path, false otherwise.
00352 bool has_root_path(const Twine &path);
00353 
00354 /// @brief Has relative path?
00355 ///
00356 /// relative_path != ""
00357 ///
00358 /// @param path Input path.
00359 /// @result True if the path has a relative path, false otherwise.
00360 bool has_relative_path(const Twine &path);
00361 
00362 /// @brief Has parent path?
00363 ///
00364 /// parent_path != ""
00365 ///
00366 /// @param path Input path.
00367 /// @result True if the path has a parent path, false otherwise.
00368 bool has_parent_path(const Twine &path);
00369 
00370 /// @brief Has filename?
00371 ///
00372 /// filename != ""
00373 ///
00374 /// @param path Input path.
00375 /// @result True if the path has a filename, false otherwise.
00376 bool has_filename(const Twine &path);
00377 
00378 /// @brief Has stem?
00379 ///
00380 /// stem != ""
00381 ///
00382 /// @param path Input path.
00383 /// @result True if the path has a stem, false otherwise.
00384 bool has_stem(const Twine &path);
00385 
00386 /// @brief Has extension?
00387 ///
00388 /// extension != ""
00389 ///
00390 /// @param path Input path.
00391 /// @result True if the path has a extension, false otherwise.
00392 bool has_extension(const Twine &path);
00393 
00394 /// @brief Is path absolute?
00395 ///
00396 /// @param path Input path.
00397 /// @result True if the path is absolute, false if it is not.
00398 bool is_absolute(const Twine &path);
00399 
00400 /// @brief Is path relative?
00401 ///
00402 /// @param path Input path.
00403 /// @result True if the path is relative, false if it is not.
00404 bool is_relative(const Twine &path);
00405 
00406 } // end namespace path
00407 } // end namespace sys
00408 } // end namespace llvm
00409 
00410 #endif