diff options
author | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-27 04:03:57 +0000 |
---|---|---|
committer | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-27 04:03:57 +0000 |
commit | b9e04f0654a99b6bb0b626df14e15ec4f9a8bf9d (patch) | |
tree | 3b738efc6107e6ea501790f0a8ba26c7d041ed3d /base/file_util_win.cc | |
parent | f6c1f2470b243628a36981efae7c3a6ce3914a92 (diff) | |
download | chromium_src-b9e04f0654a99b6bb0b626df14e15ec4f9a8bf9d.zip chromium_src-b9e04f0654a99b6bb0b626df14e15ec4f9a8bf9d.tar.gz chromium_src-b9e04f0654a99b6bb0b626df14e15ec4f9a8bf9d.tar.bz2 |
Remove file_util::kPathSeparator from posix.
Review URL: http://codereview.chromium.org/12489
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@6099 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/file_util_win.cc')
-rw-r--r-- | base/file_util_win.cc | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/base/file_util_win.cc b/base/file_util_win.cc index fdfd6dd..c107711 100644 --- a/base/file_util_win.cc +++ b/base/file_util_win.cc @@ -19,6 +19,12 @@ namespace file_util { const wchar_t kPathSeparator = L'\\'; +const wchar_t kExtensionSeparator = L'.'; + +void PathComponents(const std::wstring& path, + std::vector<std::wstring>* components) { + PathComponents(FilePath(path), components); +} std::wstring GetDirectoryFromPath(const std::wstring& path) { wchar_t path_buffer[MAX_PATH]; @@ -40,7 +46,56 @@ bool AbsolutePath(FilePath* path) { *path = FilePath(file_path_buf); return true; } - + +void InsertBeforeExtension(std::wstring* path, const std::wstring& suffix) { + DCHECK(path); + + const std::wstring::size_type last_dot = path->rfind(kExtensionSeparator); + const std::wstring::size_type last_sep = path->rfind(kPathSeparator); + + if (last_dot == std::wstring::npos || + (last_sep != std::wstring::npos && last_dot < last_sep)) { + // The path looks something like "C:\pics.old\jojo" or "C:\pics\jojo". + // We should just append the suffix to the entire path. + path->append(suffix); + return; + } + + path->insert(last_dot, suffix); +} + +// Appends the extension to file adding a '.' if extension doesn't contain one. +// This does nothing if extension is empty or '.'. This is used internally by +// ReplaceExtension. +static void AppendExtension(const std::wstring& extension, + std::wstring* file) { + if (!extension.empty() && extension != L".") { + if (extension[0] != L'.') + file->append(L"."); + file->append(extension); + } +} + +void ReplaceExtension(std::wstring* file_name, const std::wstring& extension) { + const std::wstring::size_type last_dot = file_name->rfind(L'.'); + if (last_dot == std::wstring::npos) { + // No extension, just append the supplied extension. + AppendExtension(extension, file_name); + return; + } + const std::wstring::size_type last_separator = + file_name->rfind(kPathSeparator); + if (last_separator != std::wstring::npos && last_dot < last_separator) { + // File name doesn't have extension, but one of the directories does; don't + // replace it, just append the supplied extension. For example + // 'c:\tmp.bar\foo'. + AppendExtension(extension, file_name); + return; + } + std::wstring result = file_name->substr(0, last_dot); + AppendExtension(extension, &result); + file_name->swap(result); +} int CountFilesCreatedAfter(const std::wstring& path, const FILETIME& comparison_time) { int file_count = 0; @@ -696,5 +751,4 @@ std::wstring FileEnumerator::Next() { } return (file_type_ & FileEnumerator::FILES) ? cur_file : Next(); } - } // namespace file_util |