Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

381 lines
11 KiB

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