Home | History | Annotate | Download | only in Support

Lines Matching full:path

1 //===-- Path.cpp - Implement OS Path Concept ------------------------------===//
10 // This file implements the operating system Path API.
19 #include "llvm/Support/Path.h"
35 using llvm::sys::path::is_separator;
45 StringRef find_first_component(StringRef path) {
52 if (path.empty())
53 return path;
57 if (path.size() >= 2 && std::isalpha(static_cast<unsigned char>(path[0])) &&
58 path[1] == ':')
59 return path.substr(0, 2);
63 if ((path.size() > 2) &&
64 is_separator(path[0]) &&
65 path[0] == path[1] &&
66 !is_separator(path[2])) {
68 size_t end = path.find_first_of(separators, 2);
69 return path.substr(0, end);
73 if (is_separator(path[0]))
74 return path.substr(0, 1);
77 size_t end = path.find_first_of(separators);
78 return path.substr(0, end);
134 size_t parent_path_end(StringRef path) {
135 size_t end_pos = filename_pos(path);
137 bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
140 size_t root_dir_pos = root_dir_start(path.substr(0, end_pos));
144 is_separator(path[end_pos - 1]))
169 if (!sys::path::is_absolute(Twine(ModelStorage))) {
171 sys::path::system_temp_directory(true, TDir);
172 sys::path::append(TDir, Twine(ModelStorage));
178 // path already exists.
230 namespace path {
232 const_iterator begin(StringRef path) {
234 i.Path = path;
235 i.Component = find_first_component(path);
240 const_iterator end(StringRef path) {
242 i.Path = path;
243 i.Position = path.size();
248 assert(Position < Path.size() && "Tried to increment past end!");
254 if (Position == Path.size()) {
267 if (is_separator(Path[Position])) {
275 Component = Path.substr(Position, 1);
280 while (Position != Path.size() &&
281 is_separator(Path[Position])) {
286 if (Position == Path.size()) {
294 size_t end_pos = Path.find_first_of(separators, Position);
295 Component = Path.slice(Position, end_pos);
301 return Path.begin() == RHS.Path.begin() && Position == RHS.Position;
308 reverse_iterator rbegin(StringRef Path) {
310 I.Path = Path;
311 I.Position = Path.size();
315 reverse_iterator rend(StringRef Path) {
317 I.Path = Path;
318 I.Component = Path.substr(0, 0);
325 // we are the root path.
326 size_t root_dir_pos = root_dir_start(Path);
327 if (Position == Path.size() &&
328 Path.size() > root_dir_pos + 1 &&
329 is_separator(Path[Position - 1])) {
340 is_separator(Path[end_pos - 1]))
344 size_t start_pos = filename_pos(Path.substr(0, end_pos));
345 Component = Path.slice(start_pos, end_pos);
351 return Path.begin() == RHS.Path.begin() && Component == RHS.Component &&
355 StringRef root_path(StringRef path) {
356 const_iterator b = begin(path),
358 e = end(path);
371 return path.substr(0, b->size() + pos->size());
387 StringRef root_name(StringRef path) {
388 const_iterator b = begin(path),
389 e = end(path);
405 // No path or no name.
409 StringRef root_directory(StringRef path) {
410 const_iterator b = begin(path),
412 e = end(path);
434 // No path or no root.
438 StringRef relative_path(StringRef path) {
439 StringRef root = root_path(path);
440 return path.substr(root.size());
443 void append(SmallVectorImpl<char> &path, const Twine &a,
459 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
469 path.append(c.begin(), c.end());
473 if (!component_has_sep && !(path.empty() || is_root_name)) {
475 path.push_back(preferred_separator);
478 path.append(component.begin(), component.end());
482 void append(SmallVectorImpl<char> &path,
485 path::append(path, *begin);
488 StringRef parent_path(StringRef path) {
489 size_t end_pos = parent_path_end(path);
493 return path.substr(0, end_pos);
496 void remove_filename(SmallVectorImpl<char> &path) {
497 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
499 path.set_size(end_pos);
502 void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
503 StringRef p(path.begin(), path.size());
510 path.set_size(pos);
514 path.push_back('.');
517 path.append(ext.begin(), ext.end());
520 void native(const Twine &path, SmallVectorImpl<char> &result) {
521 assert((!path.isSingleStringRef() ||
522 path.getSingleStringRef().data() != result.data()) &&
523 "path and result are not allowed to overlap!");
526 path.toVector(result);
530 void native(SmallVectorImpl<char> &Path) {
532 std::replace(Path.begin(), Path.end(), '/', '\\');
534 for (auto PI = Path.begin(), PE = Path.end(); PI < PE; ++PI) {
546 StringRef filename(StringRef path) {
547 return *rbegin(path);
550 StringRef stem(StringRef path) {
551 StringRef fname = filename(path);
563 StringRef extension(StringRef path) {
564 StringRef fname = filename(path);
592 bool has_root_name(const Twine &path) {
594 StringRef p = path.toStringRef(path_storage);
599 bool has_root_directory(const Twine &path) {
601 StringRef p = path.toStringRef(path_storage);
606 bool has_root_path(const Twine &path) {
608 StringRef p = path.toStringRef(path_storage);
613 bool has_relative_path(const Twine &path) {
615 StringRef p = path.toStringRef(path_storage);
620 bool has_filename(const Twine &path) {
622 StringRef p = path.toStringRef(path_storage);
627 bool has_parent_path(const Twine &path) {
629 StringRef p = path.toStringRef(path_storage);
634 bool has_stem(const Twine &path) {
636 StringRef p = path.toStringRef(path_storage);
641 bool has_extension(const Twine &path) {
643 StringRef p = path.toStringRef(path_storage);
648 bool is_absolute(const Twine &path) {
650 StringRef p = path.toStringRef(path_storage);
662 bool is_relative(const Twine &path) { return !is_absolute(path); }
664 StringRef remove_leading_dotslash(StringRef Path) {
666 while (Path.size() > 2 && Path[0] == '.' && is_separator(Path[1])) {
667 Path = Path.substr(2);
668 while (Path.size() > 0 && is_separator(Path[0]))
669 Path = Path.substr(1);
671 return Path;
674 static SmallString<256> remove_dots(StringRef path, bool remove_dot_dot) {
677 // Skip the root path, then look for traversal in the components.
678 StringRef rel = path::relative_path(path);
679 for (StringRef C : llvm::make_range(path::begin(rel), path::end(rel))) {
692 SmallString<256> buffer = path::root_path(path);
694 path::append(buffer, C);
698 bool remove_dots(SmallVectorImpl<char> &path, bool remove_dot_dot) {
699 StringRef p(path.data(), path.size());
702 if (result == path)
705 path.swap(result);
709 } // end namespace path
713 std::error_code getUniqueID(const Twine Path, UniqueID &Result) {
715 std::error_code EC = status(Path, Status);
777 SmallVectorImpl<char> &path,
779 StringRef p(path.data(), path.size());
781 bool rootDirectory = path::has_root_directory(p),
783 rootName = path::has_root_name(p);
799 // Relative path. Prepend the current directory.
801 // Append path to the current directory.
802 path::append(current_dir, p);
803 // Set path to the result.
804 path.swap(current_dir);
809 StringRef cdrn = path::root_name(current_dir);
811 path::append(curDirRootName, p);
812 // Set path to the result.
813 path.swap(curDirRootName);
818 StringRef pRootName = path::root_name(p);
819 StringRef bRootDirectory = path::root_directory(current_dir);
820 StringRef bRelativePath = path::relative_path(current_dir);
821 StringRef pRelativePath = path::relative_path(p);
824 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
825 path.swap(res);
834 SmallVectorImpl<char> &path) {
835 return make_absolute(current_directory, path, true);
838 std::error_code make_absolute(SmallVectorImpl<char> &path) {
839 return make_absolute(Twine(), path, false);
842 std::error_code create_directories(const Twine &Path, bool IgnoreExisting,
845 StringRef P = Path.toStringRef(PathStorage);
856 StringRef Parent = path::parent_path(P);
912 std::error_code is_directory(const Twine &path, bool &result) {
914 if (std::error_code ec = status(path, st))
924 std::error_code is_regular_file(const Twine &path, bool &result) {
926 if (std::error_code ec = status(path, st))
938 std::error_code is_other(const Twine &Path, bool &Result) {
940 if (std::error_code EC = status(Path, FileStatus))
947 SmallString<128> path = path::parent_path(Path);
948 path::append(path, filename);
949 Path = path.str();
1103 std::error_code identify_magic(const Twine &Path, file_magic &Result) {
1105 if (std::error_code EC = openFileForRead(Path, FD))
1118 return fs::status(Path, result);
1127 #include "Unix/Path.inc"
1130 #include "Windows/Path.inc"
1135 namespace path {
1146 } // end namespace path