#ifdef LLVM_ON_WIN32
const StringRef separators = "\\/";
- const char prefered_separator = '\\';
+ const char prefered_separator = '\\';
#else
const StringRef separators = "/";
const char prefered_separator = '/';
const llvm::error_code success;
- StringRef find_first_component(const StringRef &path) {
+ StringRef find_first_component(StringRef path) {
// Look for this first component in the following order.
// * empty (in this case we return an empty string)
// * either C: or {//,\\}net.
if (path.empty())
return path;
+#ifdef LLVM_ON_WIN32
// C:
if (path.size() >= 2 && std::isalpha(path[0]) && path[1] == ':')
- return StringRef(path.begin(), 2);
+ return path.substr(0, 2);
+#endif
// //net
if ((path.size() > 2) &&
- (path.startswith("\\\\") || path.startswith("//")) &&
- (path[2] != '\\' && path[2] != '/')) {
+ is_separator(path[0]) &&
+ path[0] == path[1] &&
+ !is_separator(path[2])) {
// Find the next directory separator.
- size_t end = path.find_first_of("\\/", 2);
- if (end == StringRef::npos)
- return path;
- else
- return StringRef(path.begin(), end);
+ size_t end = path.find_first_of(separators, 2);
+ return path.substr(0, end);
}
// {/,\}
- if (path[0] == '\\' || path[0] == '/')
- return StringRef(path.begin(), 1);
+ if (is_separator(path[0]))
+ return path.substr(0, 1);
if (path.startswith(".."))
- return StringRef(path.begin(), 2);
+ return path.substr(0, 2);
if (path[0] == '.')
- return StringRef(path.begin(), 1);
+ return path.substr(0, 1);
// * {file,directory}name
- size_t end = path.find_first_of("\\/", 2);
- if (end == StringRef::npos)
- return path;
- else
- return StringRef(path.begin(), end);
-
- return StringRef();
+ size_t end = path.find_first_of(separators, 2);
+ return path.substr(0, end);
}
- size_t filename_pos(const StringRef &str) {
+ size_t filename_pos(StringRef str) {
if (str.size() == 2 &&
is_separator(str[0]) &&
- is_separator(str[1]))
+ str[0] == str[1])
return 0;
if (str.size() > 0 && is_separator(str[str.size() - 1]))
return pos + 1;
}
- size_t root_dir_start(const StringRef &str) {
+ size_t root_dir_start(StringRef str) {
// case "c:/"
#ifdef LLVM_ON_WIN32
if (str.size() > 2 &&
return StringRef::npos;
}
- size_t parent_path_end(const StringRef &path) {
+ size_t parent_path_end(StringRef path) {
size_t end_pos = filename_pos(path);
bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
// Skip separators except for root dir.
- size_t root_dir_pos = root_dir_start(StringRef(path.begin(), end_pos));
+ size_t root_dir_pos = root_dir_start(path.substr(0, end_pos));
while(end_pos > 0 &&
(end_pos - 1) != root_dir_pos &&
namespace sys {
namespace path {
-const_iterator begin(const StringRef &path) {
+const_iterator begin(StringRef path) {
const_iterator i;
i.Path = path;
i.Component = find_first_component(path);
return i;
}
-const_iterator end(const StringRef &path) {
+const_iterator end(StringRef path) {
const_iterator i;
i.Path = path;
i.Position = path.size();
|| Component.endswith(":")
#endif
) {
- Component = StringRef(Path.begin() + Position, 1);
+ Component = Path.substr(Position, 1);
return *this;
}
// Find next component.
size_t end_pos = Path.find_first_of(separators, Position);
- if (end_pos == StringRef::npos)
- end_pos = Path.size();
- Component = StringRef(Path.begin() + Position, end_pos - Position);
+ Component = Path.slice(Position, end_pos);
return *this;
}
--end_pos;
// Find next separator.
- size_t start_pos = filename_pos(StringRef(Path.begin(), end_pos));
- Component = StringRef(Path.begin() + start_pos, end_pos - start_pos);
+ size_t start_pos = filename_pos(Path.substr(0, end_pos));
+ Component = Path.slice(start_pos, end_pos);
Position = start_pos;
return *this;
}
return Position - RHS.Position;
}
-void root_path(const StringRef &path, StringRef &result) {
+const StringRef root_path(StringRef path) {
const_iterator b = begin(path),
pos = b,
e = end(path);
if (has_net || has_drive) {
if ((++pos != e) && is_separator((*pos)[0])) {
// {C:/,//net/}, so get the first two components.
- result = StringRef(path.begin(), b->size() + pos->size());
- return;
+ return path.substr(0, b->size() + pos->size());
} else {
// just {C:,//net}, return the first component.
- result = *b;
- return;
+ return *b;
}
}
// POSIX style root directory.
if (is_separator((*b)[0])) {
- result = *b;
- return;
+ return *b;
}
}
- result = StringRef();
+ return StringRef();
}
-void root_name(const StringRef &path, StringRef &result) {
+const StringRef root_name(StringRef path) {
const_iterator b = begin(path),
e = end(path);
if (b != e) {
if (has_net || has_drive) {
// just {C:,//net}, return the first component.
- result = *b;
- return;
+ return *b;
}
}
// No path or no name.
- result = StringRef();
- return;
+ return StringRef();
}
-void root_directory(const StringRef &path, StringRef &result) {
+const StringRef root_directory(StringRef path) {
const_iterator b = begin(path),
pos = b,
e = end(path);
if ((has_net || has_drive) &&
// {C:,//net}, skip to the next component.
(++pos != e) && is_separator((*pos)[0])) {
- result = *pos;
- return;
+ return *pos;
}
// POSIX style root directory.
if (!has_net && is_separator((*b)[0])) {
- result = *b;
- return;
+ return *b;
}
}
// No path or no root.
- result = StringRef();
- return;
+ return StringRef();
}
-void relative_path(const StringRef &path, StringRef &result) {
- StringRef root;
- root_path(path, root);
- result = StringRef(path.begin() + root.size(), path.size() - root.size());
- return;
+const StringRef relative_path(StringRef path) {
+ StringRef root = root_path(path);
+ return root.substr(root.size());
}
void append(SmallVectorImpl<char> &path, const Twine &a,
i != e; ++i) {
bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
bool component_has_sep = !i->empty() && is_separator((*i)[0]);
- bool is_root_name = false;
- has_root_name(*i, is_root_name);
+ bool is_root_name = has_root_name(*i);
if (path_has_sep) {
// Strip separators from beginning of component.
size_t loc = i->find_first_not_of(separators);
- StringRef c = StringRef(i->begin() + loc, i->size() - loc);
+ StringRef c = i->substr(loc);
// Append it.
path.append(c.begin(), c.end());
}
}
-void parent_path(const StringRef &path, StringRef &result) {
+const StringRef parent_path(StringRef path) {
size_t end_pos = parent_path_end(path);
if (end_pos == StringRef::npos)
- result = StringRef();
+ return StringRef();
else
- result = StringRef(path.data(), end_pos);
+ return path.substr(0, end_pos);
}
void remove_filename(SmallVectorImpl<char> &path) {
path.set_size(end_pos);
}
-void replace_extension(SmallVectorImpl<char> &path,
- const Twine &extension) {
+void replace_extension(SmallVectorImpl<char> &path, const Twine &extension) {
StringRef p(path.begin(), path.size());
SmallString<32> ext_storage;
StringRef ext = extension.toStringRef(ext_storage);
#endif
}
-void filename(const StringRef &path, StringRef &result) {
- result = *(--end(path));
+const StringRef filename(StringRef path) {
+ return *(--end(path));
}
-void stem(const StringRef &path, StringRef &result) {
- StringRef fname;
- filename(path, fname);
+const StringRef stem(StringRef path) {
+ StringRef fname = filename(path);
size_t pos = fname.find_last_of('.');
if (pos == StringRef::npos)
- result = fname;
+ return fname;
else
if ((fname.size() == 1 && fname == ".") ||
(fname.size() == 2 && fname == ".."))
- result = fname;
+ return fname;
else
- result = StringRef(fname.begin(), pos);
+ return fname.substr(0, pos);
}
-void extension(const StringRef &path, StringRef &result) {
- StringRef fname;
- filename(path, fname);
+const StringRef extension(StringRef path) {
+ StringRef fname = filename(path);
size_t pos = fname.find_last_of('.');
if (pos == StringRef::npos)
- result = StringRef();
+ return StringRef();
else
if ((fname.size() == 1 && fname == ".") ||
(fname.size() == 2 && fname == ".."))
- result = StringRef();
+ return StringRef();
else
- result = StringRef(fname.begin() + pos, fname.size() - pos);
+ return fname.substr(pos);
}
-void has_root_name(const Twine &path, bool &result) {
+bool has_root_name(const Twine &path) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
- root_name(p, p);
-
- result = !p.empty();
+ return !root_name(p).empty();
}
-void has_root_directory(const Twine &path, bool &result) {
+bool has_root_directory(const Twine &path) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
- root_directory(p, p);
-
- result = !p.empty();
+ return !root_directory(p).empty();
}
-void has_root_path(const Twine &path, bool &result) {
+bool has_root_path(const Twine &path) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
- root_path(p, p);
-
- result = !p.empty();
+ return !root_path(p).empty();
}
-void has_filename(const Twine &path, bool &result) {
+bool has_filename(const Twine &path) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
- filename(p, p);
-
- result = !p.empty();
+ return !filename(p).empty();
}
-void has_parent_path(const Twine &path, bool &result) {
+bool has_parent_path(const Twine &path) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
- parent_path(p, p);
-
- result = !p.empty();
+ return !parent_path(p).empty();
}
-void has_stem(const Twine &path, bool &result) {
+bool has_stem(const Twine &path) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
- stem(p, p);
-
- result = !p.empty();
+ return !stem(p).empty();
}
-void has_extension(const Twine &path, bool &result) {
+bool has_extension(const Twine &path) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
- extension(p, p);
-
- result = !p.empty();
+ return !extension(p).empty();
}
-void is_absolute(const Twine &path, bool &result) {
+bool is_absolute(const Twine &path) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
- bool rootDir = false,
- rootName = false;
- has_root_directory(p, rootDir);
+ bool rootDir = has_root_directory(p),
#ifdef LLVM_ON_WIN32
- has_root_name(p, rootName);
+ rootName = has_root_name(p);
#else
- rootName = true;
+ rootName = true;
#endif
- result = rootDir && rootName;
+ return rootDir && rootName;
}
-void is_relative(const Twine &path, bool &result) {
- bool res;
- is_absolute(path, res);
- result = !res;
+bool is_relative(const Twine &path) {
+ return !is_absolute(path);
}
} // end namespace path
error_code make_absolute(SmallVectorImpl<char> &path) {
StringRef p(path.data(), path.size());
- bool rootName = false, rootDirectory = false;
- path::has_root_name(p, rootName);
- path::has_root_directory(p, rootDirectory);
+ bool rootName = path::has_root_name(p),
+ rootDirectory = path::has_root_directory(p);
// Already absolute.
if (rootName && rootDirectory)
}
if (!rootName && rootDirectory) {
- StringRef cdrn;
- path::root_name(current_dir, cdrn);
+ StringRef cdrn = path::root_name(current_dir);
SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
path::append(curDirRootName, p);
// Set path to the result.
}
if (rootName && !rootDirectory) {
- StringRef pRootName;
- StringRef bRootDirectory;
- StringRef bRelativePath;
- StringRef pRelativePath;
- path::root_name(p, pRootName);
- path::root_directory(current_dir, bRootDirectory);
- path::relative_path(current_dir, bRelativePath);
- path::relative_path(p, pRelativePath);
+ StringRef pRootName = path::root_name(p);
+ StringRef bRootDirectory = path::root_directory(current_dir);
+ StringRef bRelativePath = path::relative_path(current_dir);
+ StringRef pRelativePath = path::relative_path(p);
SmallString<128> res;
path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
- StringRef parent;
+ StringRef parent = path::parent_path(p);
bool parent_exists;
- path::parent_path(p, parent);
+
if (error_code ec = fs::exists(parent, parent_exists)) return ec;
if (!parent_exists)
return create_directory(p, existed);
}
+bool exists(file_status status) {
+ return status_known(status) && status.type() != file_type::file_not_found;
+}
+
+bool status_known(file_status s) {
+ return s.type() != file_type::status_error;
+}
+
+bool is_directory(file_status status) {
+ return status.type() == file_type::directory_file;
+}
+
+bool is_regular_file(file_status status) {
+ return status.type() == file_type::regular_file;
+}
+
+bool is_symlink(file_status status) {
+ return status.type() == file_type::symlink_file;
+}
+
+bool is_other(file_status status) {
+ return exists(status) &&
+ !is_regular_file(status) &&
+ !is_directory(status) &&
+ !is_symlink(status);
+}
+
void directory_entry::replace_filename(const Twine &filename, file_status st,
file_status symlink_st) {
SmallString<128> path(Path.begin(), Path.end());