Path.h 12 KB

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