Lines Matching full:path
1 //===-- Path.cpp - Implement OS Path Concept ------------------------------===//
9 // This file implements the operating system Path API.
13 #include "llvm/Support/Path.h"
37 using llvm::sys::path::is_separator;
38 using llvm::sys::path::Style;
61 StringRef find_first_component(StringRef path, Style style) {
68 if (path.empty())
69 return path;
73 if (path.size() >= 2 &&
74 std::isalpha(static_cast<unsigned char>(path[0])) && path[1] == ':')
75 return path.substr(0, 2);
79 if ((path.size() > 2) && is_separator(path[0], style) &&
80 path[0] == path[1] && !is_separator(path[2], style)) {
82 size_t end = path.find_first_of(separators(style), 2);
83 return path.substr(0, end);
87 if (is_separator(path[0], style))
88 return path.substr(0, 1);
91 size_t end = path.find_first_of(separators(style));
92 return path.substr(0, end);
136 // Returns the position past the end of the "parent path" of path. The parent
137 // path will not end in '/', unless the parent is the root directory. If the
138 // path has no parent, 0 is returned.
139 size_t parent_path_end(StringRef path, Style style) {
140 size_t end_pos = filename_pos(path, style);
143 path.size() > 0 && is_separator(path[end_pos], style);
146 size_t root_dir_pos = root_dir_start(path, style);
149 is_separator(path[end_pos - 1], style))
153 // We've reached the root dir and the input path was *not* ending in a
154 // sequence of slashes. Include the root dir in the parent path.
224 namespace path {
226 const_iterator begin(StringRef path, Style style) {
228 i.Path = path;
229 i.Component = find_first_component(path, style);
235 const_iterator end(StringRef path) {
237 i.Path = path;
238 i.Position = path.size();
243 assert(Position < Path.size() && "Tried to increment past end!");
249 if (Position == Path.size()) {
260 if (is_separator(Path[Position], S)) {
265 Component = Path.substr(Position, 1);
270 while (Position != Path.size() && is_separator(Path[Position], S)) {
275 if (Position == Path.size() && Component != "/") {
283 size_t end_pos = Path.find_first_of(separators(S), Position);
284 Component = Path.slice(Position, end_pos);
290 return Path.begin() == RHS.Path.begin() && Position == RHS.Position;
297 reverse_iterator rbegin(StringRef Path, Style style) {
299 I.Path = Path;
300 I.Position = Path.size();
306 reverse_iterator rend(StringRef Path) {
308 I.Path = Path;
309 I.Component = Path.substr(0, 0);
315 size_t root_dir_pos = root_dir_start(Path, S);
320 is_separator(Path[end_pos - 1], S))
324 if (Position == Path.size() && !Path.empty() &&
325 is_separator(Path.back(), S) &&
333 size_t start_pos = filename_pos(Path.substr(0, end_pos), S);
334 Component = Path.slice(start_pos, end_pos);
340 return Path.begin() == RHS.Path.begin() && Component == RHS.Component &&
348 StringRef root_path(StringRef path, Style style) {
349 const_iterator b = begin(path, style), pos = b, e = end(path);
358 return path.substr(0, b->size() + pos->size());
373 StringRef root_name(StringRef path, Style style) {
374 const_iterator b = begin(path, style), e = end(path);
386 // No path or no name.
390 StringRef root_directory(StringRef path, Style style) {
391 const_iterator b = begin(path, style), pos = b, e = end(path);
409 // No path or no root.
413 StringRef relative_path(StringRef path, Style style) {
414 StringRef root = root_path(path, style);
415 return path.substr(root.size());
418 void append(SmallVectorImpl<char> &path, Style style, const Twine &a,
433 !path.empty() && is_separator(path[path.size() - 1], style);
440 path.append(c.begin(), c.end());
447 !(path.empty() || has_root_name(component, style))) {
449 path.push_back(preferred_separator(style));
452 path.append(component.begin(), component.end());
456 void append(SmallVectorImpl<char> &path, const Twine &a, const Twine &b,
458 append(path, Style::native, a, b, c, d);
461 void append(SmallVectorImpl<char> &path, const_iterator begin,
464 path::append(path, style, *begin);
467 StringRef parent_path(StringRef path, Style style) {
468 size_t end_pos = parent_path_end(path, style);
471 return path.substr(0, end_pos);
474 void remove_filename(SmallVectorImpl<char> &path, Style style) {
475 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()), style);
477 path.truncate(end_pos);
480 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension,
482 StringRef p(path.begin(), path.size());
489 path.truncate(pos);
493 path.push_back('.');
496 path.append(ext.begin(), ext.end());
499 static bool starts_with(StringRef Path, StringRef Prefix,
503 if (Path.size() < Prefix.size())
506 bool SepPath = is_separator(Path[I], style);
510 if (!SepPath && toLower(Path[I]) != toLower(Prefix[I]))
515 return Path.starts_with(Prefix);
518 bool replace_path_prefix(SmallVectorImpl<char> &Path, StringRef OldPrefix,
523 StringRef OrigPath(Path.begin(), Path.size());
529 llvm::copy(NewPrefix, Path.begin());
536 Path.swap(NewPath);
540 void native(const Twine &path, SmallVectorImpl<char> &result, Style style) {
541 assert((!path.isSingleStringRef() ||
542 path.getSingleStringRef().data() != result.data()) &&
543 "path and result are not allowed to overlap!");
546 path.toVector(result);
550 void native(SmallVectorImpl<char> &Path, Style style) {
551 if (Path.empty())
554 for (char &Ch : Path)
557 if (Path[0] == '~' && (Path.size() == 1 || is_separator(Path[1], style))) {
560 PathHome.append(Path.begin() + 1, Path.end());
561 Path = PathHome;
564 std::replace(Path.begin(), Path.end(), '\\', '/');
568 std::string convert_to_slash(StringRef path, Style style) {
570 return std::string(path);
572 std::string s = path.str();
577 StringRef filename(StringRef path, Style style) { return *rbegin(path, style); }
579 StringRef stem(StringRef path, Style style) {
580 StringRef fname = filename(path, style);
590 StringRef extension(StringRef path, Style style) {
591 StringRef fname = filename(path, style);
615 bool has_root_name(const Twine &path, Style style) {
617 StringRef p = path.toStringRef(path_storage);
622 bool has_root_directory(const Twine &path, Style style) {
624 StringRef p = path.toStringRef(path_storage);
629 bool has_root_path(const Twine &path, Style style) {
631 StringRef p = path.toStringRef(path_storage);
636 bool has_relative_path(const Twine &path, Style style) {
638 StringRef p = path.toStringRef(path_storage);
643 bool has_filename(const Twine &path, Style style) {
645 StringRef p = path.toStringRef(path_storage);
650 bool has_parent_path(const Twine &path, Style style) {
652 StringRef p = path.toStringRef(path_storage);
657 bool has_stem(const Twine &path, Style style) {
659 StringRef p = path.toStringRef(path_storage);
664 bool has_extension(const Twine &path, Style style) {
666 StringRef p = path.toStringRef(path_storage);
671 bool is_absolute(const Twine &path, Style style) {
673 StringRef p = path.toStringRef(path_storage);
681 bool is_absolute_gnu(const Twine &path, Style style) {
683 StringRef p = path.toStringRef(path_storage);
699 bool is_relative(const Twine &path, Style style) {
700 return !is_absolute(path, style);
703 StringRef remove_leading_dotslash(StringRef Path, Style style) {
705 while (Path.size() > 2 && Path[0] == '.' && is_separator(Path[1], style)) {
706 Path = Path.substr(2);
707 while (Path.size() > 0 && is_separator(Path[0], style))
708 Path = Path.substr(1);
710 return Path;
713 // Remove path traversal components ("." and "..") when possible, and
722 // Consume the root path, if present.
723 StringRef root = path::root_path(remaining, style);
728 // Loop over path components manually. This makes it easier to detect
741 // The path needs to be rewritten if it has a trailing slash.
746 // Check for path traversal components or double separators.
752 // beginning of a relative path, keep the ".." component.
768 // Avoid rewriting the path unless we have to.
783 } // end namespace path
787 std::error_code getUniqueID(const Twine Path, UniqueID &Result) {
789 std::error_code EC = status(Path, Status);
803 if (!sys::path::is_absolute(Twine(ModelStorage))) {
805 sys::path::system_temp_directory(true, TDir);
806 sys::path::append(TDir, Twine(ModelStorage));
907 SmallVectorImpl<char> &path) {
908 StringRef p(path.data(), path.size());
910 bool rootDirectory = path::has_root_directory(p);
911 bool rootName = path::has_root_name(p);
921 // Relative path. Prepend the current directory.
923 // Append path to the current directory.
924 path::append(current_dir, p);
925 // Set path to the result.
926 path.swap(current_dir);
931 StringRef cdrn = path::root_name(current_dir);
933 path::append(curDirRootName, p);
934 // Set path to the result.
935 path.swap(curDirRootName);
940 StringRef pRootName = path::root_name(p);
941 StringRef bRootDirectory = path::root_directory(current_dir);
942 StringRef bRelativePath = path::relative_path(current_dir);
943 StringRef pRelativePath = path::relative_path(p);
946 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
947 path.swap(res);
955 std::error_code make_absolute(SmallVectorImpl<char> &path) {
956 if (path::is_absolute(path))
963 make_absolute(current_dir, path);
967 std::error_code create_directories(const Twine &Path, bool IgnoreExisting,
970 StringRef P = Path.toStringRef(PathStorage);
981 StringRef Parent = path::parent_path(P);
1067 ErrorOr<MD5::MD5Result> md5_contents(const Twine &Path) {
1069 if (auto EC = openFileForRead(Path, FD, OF_None))
1085 file_type get_file_type(const Twine &Path, bool Follow) {
1087 if (status(Path, st, Follow))
1096 std::error_code is_directory(const Twine &path, bool &result) {
1098 if (std::error_code ec = status(path, st))
1108 std::error_code is_regular_file(const Twine &path, bool &result) {
1110 if (std::error_code ec = status(path, st))
1120 std::error_code is_symlink_file(const Twine &path, bool &result) {
1122 if (std::error_code ec = status(path, st, false))
1134 std::error_code is_other(const Twine &Path, bool &Result) {
1136 if (std::error_code EC = status(Path, FileStatus))
1144 SmallString<128> PathStr = path::parent_path(Path);
1145 path::append(PathStr, Filename);
1146 this->Path = std::string(PathStr);
1151 ErrorOr<perms> getPermissions(const Twine &Path) {
1153 if (std::error_code EC = status(Path, Status))
1199 #include "Unix/Path.inc"
1202 #include "Windows/Path.inc"