summaryrefslogtreecommitdiffstats
path: root/base/file_path.cc
diff options
context:
space:
mode:
authorerikkay@google.com <erikkay@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-01-09 18:26:19 +0000
committererikkay@google.com <erikkay@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-01-09 18:26:19 +0000
commit235c17a4631e92a4cbd214863758165a2f573801 (patch)
tree51a7a19215a774f558744e9cedd611a86dbd6ea7 /base/file_path.cc
parent7d927f8c4baddd6d358bb7f47001e99cf757d65d (diff)
downloadchromium_src-235c17a4631e92a4cbd214863758165a2f573801.zip
chromium_src-235c17a4631e92a4cbd214863758165a2f573801.tar.gz
chromium_src-235c17a4631e92a4cbd214863758165a2f573801.tar.bz2
Add implementations of various extension related methods (derived from file_util):
Extension, RemoveExtension, InsertBeforeExtension, ReplaceExtension I didn't reimplement the old file_util ones since they actually modify the FilePath in place, which isn't the style of the rest of the FilePath methods. I'll file a cleanup bug after this for callers to switch to the new methods. Review URL: http://codereview.chromium.org/17243 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@7811 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/file_path.cc')
-rw-r--r--base/file_path.cc85
1 files changed, 81 insertions, 4 deletions
diff --git a/base/file_path.cc b/base/file_path.cc
index 9a645b0..8738c37 100644
--- a/base/file_path.cc
+++ b/base/file_path.cc
@@ -21,6 +21,9 @@ const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("/");
const FilePath::CharType FilePath::kCurrentDirectory[] = FILE_PATH_LITERAL(".");
const FilePath::CharType FilePath::kParentDirectory[] = FILE_PATH_LITERAL("..");
+const FilePath::CharType FilePath::kExtensionSeparator = FILE_PATH_LITERAL('.');
+
+
namespace {
// If this FilePath contains a drive letter specification, returns the
@@ -38,10 +41,8 @@ FilePath::StringType::size_type FindDriveLetter(
(path[0] >= L'a' && path[0] <= L'z'))) {
return 1;
}
- return FilePath::StringType::npos;
-#else // FILE_PATH_USES_DRIVE_LETTERS
- return FilePath::StringType::npos;
#endif // FILE_PATH_USES_DRIVE_LETTERS
+ return FilePath::StringType::npos;
}
bool IsPathAbsolute(const FilePath::StringType& path) {
@@ -136,7 +137,83 @@ FilePath FilePath::BaseName() const {
return new_path;
}
-FilePath FilePath::Append(const FilePath::StringType& component) const {
+FilePath::StringType FilePath::Extension() const {
+ // BaseName() calls StripTrailingSeparators, so cases like /foo.baz/// work.
+ StringType base = BaseName().value();
+
+ // Special case "." and ".."
+ if (base == kCurrentDirectory || base == kParentDirectory)
+ return StringType();
+
+ const StringType::size_type last_dot = base.rfind(kExtensionSeparator);
+ if (last_dot == StringType::npos)
+ return StringType();
+ return StringType(base, last_dot);
+}
+
+FilePath FilePath::RemoveExtension() const {
+ StringType ext = Extension();
+ // It's important to check Extension() since that verifies that the
+ // kExtensionSeparator actually appeared in the last path component.
+ if (ext.empty())
+ return FilePath(path_);
+ // Since Extension() verified that the extension is in fact in the last path
+ // component, this substr will effectively strip trailing separators.
+ const StringType::size_type last_dot = path_.rfind(kExtensionSeparator);
+ return FilePath(path_.substr(0, last_dot));
+}
+
+FilePath FilePath::InsertBeforeExtension(const StringType& suffix) const {
+ if (suffix.empty())
+ return FilePath(path_);
+
+ if (path_.empty())
+ return FilePath();
+
+ StringType base = BaseName().value();
+ if (base.empty())
+ return FilePath();
+ if (*(base.end() - 1) == kExtensionSeparator) {
+ // Special case "." and ".."
+ if (base == kCurrentDirectory || base == kParentDirectory) {
+ return FilePath();
+ }
+ }
+
+ StringType ext = Extension();
+ StringType ret = RemoveExtension().value();
+ ret.append(suffix);
+ ret.append(ext);
+ return FilePath(ret);
+}
+
+FilePath FilePath::ReplaceExtension(const StringType& extension) const {
+ if (path_.empty())
+ return FilePath();
+
+ StringType base = BaseName().value();
+ if (base.empty())
+ return FilePath();
+ if (*(base.end() - 1) == kExtensionSeparator) {
+ // Special case "." and ".."
+ if (base == kCurrentDirectory || base == kParentDirectory) {
+ return FilePath();
+ }
+ }
+
+ FilePath no_ext = RemoveExtension();
+ // If the new extension is "" or ".", then just remove the current extension.
+ if (extension.empty() || extension == StringType(1, kExtensionSeparator))
+ return no_ext;
+
+ StringType str = no_ext.value();
+ if (extension[0] != kExtensionSeparator)
+ str.append(1, kExtensionSeparator);
+ str.append(extension);
+ return FilePath(str);
+}
+
+FilePath FilePath::Append(const StringType& component) const {
DCHECK(!IsPathAbsolute(component));
if (path_.compare(kCurrentDirectory) == 0) {
// Append normally doesn't do any normalization, but as a special case,