Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Unified Diff: base/file_path.cc

Issue 145026: Move PathComponents from file_util to FilePath, add FilePath::IsParent() (Closed)
Patch Set: noop Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/file_path.h ('k') | base/file_path_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/file_path.cc
diff --git a/base/file_path.cc b/base/file_path.cc
index a00659ed2c78e738cf679e61224a9d0a87b17367..e3dfbbd194be413b9137db7aac0edd140b81a227 100644
--- a/base/file_path.cc
+++ b/base/file_path.cc
@@ -61,6 +61,16 @@ bool IsPathAbsolute(const FilePath::StringType& path) {
#endif // FILE_PATH_USES_DRIVE_LETTERS
}
+bool AreAllSeparators(FilePath::StringType input) {
+ for (FilePath::StringType::const_iterator it = input.begin();
+ it != input.end(); ++it) {
+ if (!FilePath::IsSeparator(*it))
+ return false;
+ }
+
+ return true;
+}
+
} // namespace
bool FilePath::IsSeparator(CharType character) {
@@ -73,6 +83,68 @@ bool FilePath::IsSeparator(CharType character) {
return false;
}
+void FilePath::GetComponents(std::vector<FilePath::StringType>* components)
+ const {
+ DCHECK(components);
+ if (!components)
+ return;
+ components->clear();
+ if (value().empty())
+ return;
+
+ std::vector<FilePath::StringType> ret_val;
+ FilePath current = *this;
+ FilePath base;
+
+ // Capture path components.
+ while (current != current.DirName()) {
+ base = current.BaseName();
+ if (!AreAllSeparators(base.value()))
+ ret_val.push_back(base.value());
+ current = current.DirName();
+ }
+
+ // Capture root, if any.
+ base = current.BaseName();
+ if (!base.value().empty() && base.value() != kCurrentDirectory)
+ ret_val.push_back(current.BaseName().value());
+
+ // Capture drive letter, if any.
+ FilePath dir = current.DirName();
+ StringType::size_type letter = FindDriveLetter(dir.value());
+ if (letter != FilePath::StringType::npos) {
+ ret_val.push_back(FilePath::StringType(dir.value(), 0, letter + 1));
+ }
+
+ *components = std::vector<FilePath::StringType>(ret_val.rbegin(),
+ ret_val.rend());
+}
+
+bool FilePath::IsParent(const FilePath& child) const {
+ std::vector<FilePath::StringType> parent_components;
+ std::vector<FilePath::StringType> child_components;
+ GetComponents(&parent_components);
+ child.GetComponents(&child_components);
+
+ if (parent_components.size() >= child_components.size())
+ return false;
+ if (parent_components.size() == 0)
+ return false;
+
+ std::vector<FilePath::StringType>::const_iterator parent_comp =
+ parent_components.begin();
+ std::vector<FilePath::StringType>::const_iterator child_comp =
+ child_components.begin();
+ while (parent_comp != parent_components.end()) {
+ if (*parent_comp != *child_comp)
+ return false;
+ ++parent_comp;
+ ++child_comp;
+ }
+
+ return true;
+}
+
// libgen's dirname and basename aren't guaranteed to be thread-safe and aren't
// guaranteed to not modify their input strings, and in fact are implemented
// differently in this regard on different platforms. Don't use them, but
« no previous file with comments | « base/file_path.h ('k') | base/file_path_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698