diff options
author | erikkay@google.com <erikkay@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-15 17:32:10 +0000 |
---|---|---|
committer | erikkay@google.com <erikkay@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-15 17:32:10 +0000 |
commit | 37088fefa67704ad6f18e1e72e2e6292ba48ee1b (patch) | |
tree | ef54e2c068b1a63dfe29b01fb59af6e1148d16d3 /base/file_util.cc | |
parent | e87ce0ff4a37f8e5bd0e4d7938bcf93b66d29ec9 (diff) | |
download | chromium_src-37088fefa67704ad6f18e1e72e2e6292ba48ee1b.zip chromium_src-37088fefa67704ad6f18e1e72e2e6292ba48ee1b.tar.gz chromium_src-37088fefa67704ad6f18e1e72e2e6292ba48ee1b.tar.bz2 |
Part two of file_util porting. Almost all of the functionality has been ported, including the unit tests now. Some of this API isn't great, and should be cleaned up, but I'd like to hold off and do that in a followup changelist. More general code cleanup is likely needed here as well.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@944 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/file_util.cc')
-rw-r--r-- | base/file_util.cc | 40 |
1 files changed, 35 insertions, 5 deletions
diff --git a/base/file_util.cc b/base/file_util.cc index 234c466..4594144 100644 --- a/base/file_util.cc +++ b/base/file_util.cc @@ -30,7 +30,6 @@ #include "base/file_util.h" #include <fstream> -#include <string> #include "base/logging.h" #include "base/string_util.h" @@ -40,12 +39,43 @@ namespace file_util { const wchar_t kExtensionSeparator = L'.'; +void PathComponents(const std::wstring& path, + std::vector<std::wstring>* components) { + DCHECK(components != NULL); + if (components == NULL) + return; + std::wstring::size_type start = 0; + std::wstring::size_type end = path.find(kPathSeparator, start); + + // Special case the "/" or "\" directory. On Windows with a drive letter, + // this code path won't hit, but the right thing should still happen. + // "E:\foo" will turn into "E:","foo". + if (end == start) { + components->push_back(std::wstring(path, 0, 1)); + start = end + 1; + end = path.find(kPathSeparator, start); + } + while (end != std::wstring::npos) { + std::wstring component = std::wstring(path, start, end - start); + components->push_back(component); + start = end + 1; + end = path.find(kPathSeparator, start); + } + std::wstring component = std::wstring(path, start); + components->push_back(component); +} + bool EndsWithSeparator(std::wstring* path) { - return (!path->empty() && (*path)[path->length() - 1] == kPathSeparator); + return EndsWithSeparator(*path); +} + +bool EndsWithSeparator(const std::wstring& path) { + bool is_sep = ((path)[path.length() - 1] == kPathSeparator); + return is_sep; } void TrimTrailingSeparator(std::wstring* dir) { - while (EndsWithSeparator(dir)) + while (dir->length() > 1 && EndsWithSeparator(dir)) dir->resize(dir->length() - 1); } @@ -77,9 +107,8 @@ void TrimFilename(std::wstring* path) { } } -// TODO(mpcomplete): Make this platform-independent, etc. std::wstring GetFilenameFromPath(const std::wstring& path) { - std::wstring::size_type pos = path.find_last_of(L"\\/"); + std::wstring::size_type pos = path.find_last_of(kPathSeparator); return std::wstring(path, pos == std::wstring::npos ? 0 : pos+1); } @@ -180,6 +209,7 @@ void ReplaceIllegalCharacters(std::wstring* file_name, int replace_char) { if (illegal_characters.contains(wstr[i])) { (*file_name)[i] = replace_char; } + ++i; } #else #error wchar_t* should be either UTF-16 or UTF-32 |