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"
38 using llvm::sys::path::is_separator;
39 using llvm::sys::path::Style;
62 StringRef find_first_component(StringRef path, Style style) {
69 if (path.empty())
70 return path;
74 if (path.size() >= 2 &&
75 std::isalpha(static_cast<unsigned char>(path[0])) && path[1] == ':')
76 return path.substr(0, 2);
80 if ((path.size() > 2) && is_separator(path[0], style) &&
81 path[0] == path[1] && !is_separator(path[2], style)) {
83 size_t end = path.find_first_of(separators(style), 2);
84 return path.substr(0, end);
88 if (is_separator(path[0], style))
89 return path.substr(0, 1);
92 size_t end = path.find_first_of(separators(style));
93 return path.substr(0, end);
137 // Returns the position past the end of the "parent path" of path. The parent
138 // path will not end in '/', unless the parent is the root directory. If the
139 // path has no parent, 0 is returned.
140 size_t parent_path_end(StringRef path, Style style) {
141 size_t end_pos = filename_pos(path, style);
144 path.size() > 0 && is_separator(path[end_pos], style);
147 size_t root_dir_pos = root_dir_start(path, style);
150 is_separator(path[end_pos - 1], style))
154 // We've reached the root dir and the input path was *not* ending in a
155 // sequence of slashes. Include the root dir in the parent path.
225 namespace path {
227 const_iterator begin(StringRef path, Style style) {
229 i.Path = path;
230 i.Component = find_first_component(path, style);
236 const_iterator end(StringRef path) {
238 i.Path = path;
239 i.Position = path.size();
244 assert(Position < Path.size() && "Tried to increment past end!");
250 if (Position == Path.size()) {
261 if (is_separator(Path[Position], S)) {
266 Component = Path.substr(Position, 1);
271 while (Position != Path.size() && is_separator(Path[Position], S)) {
276 if (Position == Path.size() && Component != "/") {
284 size_t end_pos = Path.find_first_of(separators(S), Position);
285 Component = Path.slice(Position, end_pos);
291 return Path.begin() == RHS.Path.begin() && Position == RHS.Position;
298 reverse_iterator rbegin(StringRef Path, Style style) {
300 I.Path = Path;
301 I.Position = Path.size();
307 reverse_iterator rend(StringRef Path) {
309 I.Path = Path;
310 I.Component = Path.substr(0, 0);
316 size_t root_dir_pos = root_dir_start(Path, S);
321 is_separator(Path[end_pos - 1], S))
325 if (Position == Path.size() && !Path.empty() &&
326 is_separator(Path.back(), S) &&
334 size_t start_pos = filename_pos(Path.substr(0, end_pos), S);
335 Component = Path.slice(start_pos, end_pos);
341 return Path.begin() == RHS.Path.begin() && Component == RHS.Component &&
349 StringRef root_path(StringRef path, Style style) {
350 const_iterator b = begin(path, style), pos = b, e = end(path);
359 return path.substr(0, b->size() + pos->size());
374 StringRef root_name(StringRef path, Style style) {
375 const_iterator b = begin(path, style), e = end(path);
387 // No path or no name.
391 StringRef root_directory(StringRef path, Style style) {
392 const_iterator b = begin(path, style), pos = b, e = end(path);
410 // No path or no root.
414 StringRef relative_path(StringRef path, Style style) {
415 StringRef root = root_path(path, style);
416 return path.substr(root.size());
419 void append(SmallVectorImpl<char> &path, Style style, const Twine &a,
434 !path.empty() && is_separator(path[path.size() - 1], style);
441 path.append(c.begin(), c.end());
448 !(path.empty() || has_root_name(component, style))) {
450 path.push_back(preferred_separator(style));
453 path.append(component.begin(), component.end());
457 void append(SmallVectorImpl<char> &path, const Twine &a, const Twine &b,
459 append(path, Style::native, a, b, c, d);
462 void append(SmallVectorImpl<char> &path, const_iterator begin,
465 path::append(path, style, *begin);
468 StringRef parent_path(StringRef path, Style style) {
469 size_t end_pos = parent_path_end(path, style);
472 return path.substr(0, end_pos);
475 void remove_filename(SmallVectorImpl<char> &path, Style style) {
476 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()), style);
478 path.truncate(end_pos);
481 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension,
483 StringRef p(path.begin(), path.size());
490 path.truncate(pos);
494 path.push_back('.');
497 path.append(ext.begin(), ext.end());
500 static bool starts_with(StringRef Path, StringRef Prefix,
504 if (Path.size() < Prefix.size())
507 bool SepPath = is_separator(Path[I], style);
511 if (!SepPath && toLower(Path[I]) != toLower(Prefix[I]))
516 return Path.starts_with(Prefix);
519 bool replace_path_prefix(SmallVectorImpl<char> &Path, StringRef OldPrefix,
524 StringRef OrigPath(Path.begin(), Path.size());
530 llvm::copy(NewPrefix, Path.begin());
537 Path.swap(NewPath);
541 void native(const Twine &path, SmallVectorImpl<char> &result, Style style) {
542 assert((!path.isSingleStringRef() ||
543 path.getSingleStringRef().data() != result.data()) &&
544 "path and result are not allowed to overlap!");
547 path.toVector(result);
551 void native(SmallVectorImpl<char> &Path, Style style) {
552 if (Path.empty())
555 for (char &Ch : Path)
558 if (Path[0] == '~' && (Path.size() == 1 || is_separator(Path[1], style))) {
561 PathHome.append(Path.begin() + 1, Path.end());
562 Path = PathHome;
565 std::replace(Path.begin(), Path.end(), '\\', '/');
569 std::string convert_to_slash(StringRef path, Style style) {
571 return std::string(path);
573 std::string s = path.str();
578 StringRef filename(StringRef path, Style style) { return *rbegin(path, style); }
580 StringRef stem(StringRef path, Style style) {
581 StringRef fname = filename(path, style);
591 StringRef extension(StringRef path, Style style) {
592 StringRef fname = filename(path, style);
616 bool has_root_name(const Twine &path, Style style) {
618 StringRef p = path.toStringRef(path_storage);
623 bool has_root_directory(const Twine &path, Style style) {
625 StringRef p = path.toStringRef(path_storage);
630 bool has_root_path(const Twine &path, Style style) {
632 StringRef p = path.toStringRef(path_storage);
637 bool has_relative_path(const Twine &path, Style style) {
639 StringRef p = path.toStringRef(path_storage);
644 bool has_filename(const Twine &path, Style style) {
646 StringRef p = path.toStringRef(path_storage);
651 bool has_parent_path(const Twine &path, Style style) {
653 StringRef p = path.toStringRef(path_storage);
658 bool has_stem(const Twine &path, Style style) {
660 StringRef p = path.toStringRef(path_storage);
665 bool has_extension(const Twine &path, Style style) {
667 StringRef p = path.toStringRef(path_storage);
672 bool is_absolute(const Twine &path, Style style) {
674 StringRef p = path.toStringRef(path_storage);
682 bool is_absolute_gnu(const Twine &path, Style style) {
684 StringRef p = path.toStringRef(path_storage);
700 bool is_relative(const Twine &path, Style style) {
701 return !is_absolute(path, style);
704 StringRef remove_leading_dotslash(StringRef Path, Style style) {
706 while (Path.size() > 2 && Path[0] == '.' && is_separator(Path[1], style)) {
707 Path = Path.substr(2);
708 while (Path.size() > 0 && is_separator(Path[0], style))
709 Path = Path.substr(1);
711 return Path;
714 // Remove path traversal components ("." and "..") when possible, and
723 // Consume the root path, if present.
724 StringRef root = path::root_path(remaining, style);
729 // Loop over path components manually. This makes it easier to detect
742 // The path needs to be rewritten if it has a trailing slash.
747 // Check for path traversal components or double separators.
753 // beginning of a relative path, keep the ".." component.
769 // Avoid rewriting the path unless we have to.
784 } // end namespace path
788 std::error_code getUniqueID(const Twine Path, UniqueID &Result) {
790 std::error_code EC = status(Path, Status);
804 if (!sys::path::is_absolute(Twine(ModelStorage))) {
806 sys::path::system_temp_directory(true, TDir);
807 sys::path::append(TDir, Twine(ModelStorage));
908 SmallVectorImpl<char> &path) {
909 StringRef p(path.data(), path.size());
911 bool rootDirectory = path::has_root_directory(p);
912 bool rootName = path::has_root_name(p);
922 // Relative path. Prepend the current directory.
924 // Append path to the current directory.
925 path::append(current_dir, p);
926 // Set path to the result.
927 path.swap(current_dir);
932 StringRef cdrn = path::root_name(current_dir);
934 path::append(curDirRootName, p);
935 // Set path to the result.
936 path.swap(curDirRootName);
941 StringRef pRootName = path::root_name(p);
942 StringRef bRootDirectory = path::root_directory(current_dir);
943 StringRef bRelativePath = path::relative_path(current_dir);
944 StringRef pRelativePath = path::relative_path(p);
947 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
948 path.swap(res);
956 std::error_code make_absolute(SmallVectorImpl<char> &path) {
957 if (path::is_absolute(path))
964 make_absolute(current_dir, path);
968 std::error_code create_directories(const Twine &Path, bool IgnoreExisting,
971 StringRef P = Path.toStringRef(PathStorage);
982 StringRef Parent = path::parent_path(P);
1068 ErrorOr<MD5::MD5Result> md5_contents(const Twine &Path) {
1070 if (auto EC = openFileForRead(Path, FD, OF_None))
1086 file_type get_file_type(const Twine &Path, bool Follow) {
1088 if (status(Path, st, Follow))
1097 std::error_code is_directory(const Twine &path, bool &result) {
1099 if (std::error_code ec = status(path, st))
1109 std::error_code is_regular_file(const Twine &path, bool &result) {
1111 if (std::error_code ec = status(path, st))
1121 std::error_code is_symlink_file(const Twine &path, bool &result) {
1123 if (std::error_code ec = status(path, st, false))
1135 std::error_code is_other(const Twine &Path, bool &Result) {
1137 if (std::error_code EC = status(Path, FileStatus))
1145 SmallString<128> PathStr = path::parent_path(Path);
1146 path::append(PathStr, Filename);
1147 this->Path = std::string(PathStr);
1152 ErrorOr<perms> getPermissions(const Twine &Path) {
1154 if (std::error_code EC = status(Path, Status))
1200 #include "Unix/Path.inc"
1203 #include "Windows/Path.inc"