summaryrefslogtreecommitdiffstats
path: root/base/file_path.cc
diff options
context:
space:
mode:
authorrafaelw@chromium.org <rafaelw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-24 21:28:30 +0000
committerrafaelw@chromium.org <rafaelw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-24 21:28:30 +0000
commit0e4f68db63e565201a6eaa2f600441c37b150d1b (patch)
treeb4cfc4f35e6d8ae7fa6266fd2c3faaed9e006adb /base/file_path.cc
parenta86c97ccfe0038b1672d5a06d283561b7f294640 (diff)
downloadchromium_src-0e4f68db63e565201a6eaa2f600441c37b150d1b.zip
chromium_src-0e4f68db63e565201a6eaa2f600441c37b150d1b.tar.gz
chromium_src-0e4f68db63e565201a6eaa2f600441c37b150d1b.tar.bz2
Move PathComponents from file_util to FilePath, add FilePath::IsParent()
r=erikkay,mark Review URL: http://codereview.chromium.org/145026 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@19174 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/file_path.cc')
-rw-r--r--base/file_path.cc72
1 files changed, 72 insertions, 0 deletions
diff --git a/base/file_path.cc b/base/file_path.cc
index a00659e..e3dfbbd 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