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