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
This commit is contained in:
base
file_path.ccfile_path.hfile_util.ccfile_util.hfile_util_linux.ccfile_util_mac.mmfile_util_unittest.ccfile_util_win.cc
chrome/browser/safe_browsing
net/url_request
webkit
@ -18,10 +18,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("..");
|
||||
|
||||
// Returns true if |character| is in kSeparators.
|
||||
static bool IsSeparator(FilePath::CharType character) {
|
||||
for (size_t i = 0; i < arraysize(FilePath::kSeparators) - 1; ++i) {
|
||||
if (character == FilePath::kSeparators[i]) {
|
||||
bool FilePath::IsSeparator(CharType character) {
|
||||
for (size_t i = 0; i < arraysize(kSeparators) - 1; ++i) {
|
||||
if (character == kSeparators[i]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -123,6 +123,9 @@ class FilePath {
|
||||
|
||||
const StringType& value() const { return path_; }
|
||||
|
||||
// Returns true if |character| is in kSeparators.
|
||||
static bool IsSeparator(CharType character);
|
||||
|
||||
// Returns a FilePath corresponding to the directory containing the path
|
||||
// named by this object, stripping away the file component. If this object
|
||||
// only contains one component, returns a FilePath identifying
|
||||
|
@ -13,41 +13,45 @@
|
||||
#include "base/string_util.h"
|
||||
#include "unicode/uniset.h"
|
||||
|
||||
#include "base/string_piece.h"
|
||||
#include "base/sys_string_conversions.h"
|
||||
|
||||
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)
|
||||
void PathComponents(const FilePath& path,
|
||||
std::vector<FilePath::StringType>* components) {
|
||||
DCHECK(components);
|
||||
if (!components)
|
||||
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".
|
||||
FilePath::StringType path_str = path.value();
|
||||
FilePath::StringType::size_type start = 0;
|
||||
FilePath::StringType::size_type end =
|
||||
path_str.find_first_of(FilePath::kSeparators);
|
||||
|
||||
// If the path starts with a separator, add it to components.
|
||||
if (end == start) {
|
||||
components->push_back(std::wstring(path, 0, 1));
|
||||
components->push_back(FilePath::StringType(path_str, 0, 1));
|
||||
start = end + 1;
|
||||
end = path.find(kPathSeparator, start);
|
||||
end = path_str.find_first_of(FilePath::kSeparators, start);
|
||||
}
|
||||
while (end != std::wstring::npos) {
|
||||
std::wstring component = std::wstring(path, start, end - start);
|
||||
while (end != FilePath::StringType::npos) {
|
||||
FilePath::StringType component =
|
||||
FilePath::StringType(path_str, start, end - start);
|
||||
components->push_back(component);
|
||||
start = end + 1;
|
||||
end = path.find(kPathSeparator, start);
|
||||
end = path_str.find_first_of(FilePath::kSeparators, start);
|
||||
}
|
||||
std::wstring component = std::wstring(path, start);
|
||||
components->push_back(component);
|
||||
|
||||
components->push_back(FilePath::StringType(path_str, start));
|
||||
}
|
||||
|
||||
bool EndsWithSeparator(const FilePath& file_path) {
|
||||
std::wstring path = file_path.ToWStringHack();
|
||||
bool is_sep = (path.length() > 0 &&
|
||||
path[path.length() - 1] == kPathSeparator);
|
||||
return is_sep;
|
||||
bool EndsWithSeparator(const FilePath& path) {
|
||||
FilePath::StringType value = path.value();
|
||||
if (value.empty())
|
||||
return false;
|
||||
|
||||
return FilePath::IsSeparator(value[value.size() - 1]);
|
||||
}
|
||||
|
||||
bool EnsureEndsWithSeparator(FilePath* path) {
|
||||
@ -69,34 +73,6 @@ void TrimTrailingSeparator(std::wstring* dir) {
|
||||
dir->resize(dir->length() - 1);
|
||||
}
|
||||
|
||||
void UpOneDirectory(std::wstring* dir) {
|
||||
TrimTrailingSeparator(dir);
|
||||
|
||||
std::wstring::size_type last_sep = dir->find_last_of(kPathSeparator);
|
||||
if (last_sep != std::wstring::npos)
|
||||
dir->resize(last_sep);
|
||||
}
|
||||
|
||||
void UpOneDirectoryOrEmpty(std::wstring* dir) {
|
||||
TrimTrailingSeparator(dir);
|
||||
|
||||
std::wstring::size_type last_sep = dir->find_last_of(kPathSeparator);
|
||||
if (last_sep != std::wstring::npos)
|
||||
dir->resize(last_sep);
|
||||
else
|
||||
dir->clear();
|
||||
}
|
||||
|
||||
void TrimFilename(std::wstring* path) {
|
||||
if (EndsWithSeparator(path)) {
|
||||
TrimTrailingSeparator(path);
|
||||
} else {
|
||||
std::wstring::size_type last_sep = path->find_last_of(kPathSeparator);
|
||||
if (last_sep != std::wstring::npos)
|
||||
path->resize(last_sep);
|
||||
}
|
||||
}
|
||||
|
||||
std::wstring GetFilenameFromPath(const std::wstring& path) {
|
||||
// TODO(erikkay): fix this - it's not using kPathSeparator, but win unit test
|
||||
// are exercising '/' as a path separator as well.
|
||||
@ -118,34 +94,6 @@ std::wstring GetFilenameWithoutExtensionFromPath(const std::wstring& path) {
|
||||
return file_name.substr(0, last_dot);
|
||||
}
|
||||
|
||||
void AppendToPath(std::wstring* path, const std::wstring& new_ending) {
|
||||
if (!path) {
|
||||
NOTREACHED();
|
||||
return; // Don't crash in this function in release builds.
|
||||
}
|
||||
|
||||
if (!EndsWithSeparator(path))
|
||||
path->push_back(kPathSeparator);
|
||||
path->append(new_ending);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void ReplaceIllegalCharacters(std::wstring* file_name, int replace_char) {
|
||||
DCHECK(file_name);
|
||||
|
||||
@ -216,39 +164,6 @@ void ReplaceIllegalCharacters(std::wstring* file_name, int replace_char) {
|
||||
#endif
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
bool ContentsEqual(const FilePath& filename1, const FilePath& filename2) {
|
||||
// We open the file in binary format even if they are text files because
|
||||
// we are just comparing that bytes are exactly same in both files and not
|
||||
@ -257,7 +172,7 @@ bool ContentsEqual(const FilePath& filename1, const FilePath& filename2) {
|
||||
std::ios::in | std::ios::binary);
|
||||
std::ifstream file2(filename2.value().c_str(),
|
||||
std::ios::in | std::ios::binary);
|
||||
|
||||
|
||||
// Even if both files aren't openable (and thus, in some sense, "equal"),
|
||||
// any unusable file yields a result of "false".
|
||||
if (!file1.is_open() || !file2.is_open())
|
||||
@ -323,22 +238,15 @@ bool AbsolutePath(std::wstring* path_str) {
|
||||
*path_str = path.ToWStringHack();
|
||||
return true;
|
||||
}
|
||||
bool Delete(const std::wstring& path, bool recursive) {
|
||||
return Delete(FilePath::FromWStringHack(path), recursive);
|
||||
}
|
||||
bool EndsWithSeparator(std::wstring* path) {
|
||||
return EndsWithSeparator(FilePath::FromWStringHack(*path));
|
||||
}
|
||||
bool EndsWithSeparator(const std::wstring& path) {
|
||||
return EndsWithSeparator(FilePath::FromWStringHack(path));
|
||||
}
|
||||
bool Move(const std::wstring& from_path, const std::wstring& to_path) {
|
||||
return Move(FilePath::FromWStringHack(from_path),
|
||||
FilePath::FromWStringHack(to_path));
|
||||
}
|
||||
bool CopyFile(const std::wstring& from_path, const std::wstring& to_path) {
|
||||
return CopyFile(FilePath::FromWStringHack(from_path),
|
||||
FilePath::FromWStringHack(to_path));
|
||||
void AppendToPath(std::wstring* path, const std::wstring& new_ending) {
|
||||
if (!path) {
|
||||
NOTREACHED();
|
||||
return; // Don't crash in this function in release builds.
|
||||
}
|
||||
|
||||
if (!EndsWithSeparator(path))
|
||||
path->push_back(FilePath::kSeparators[0]);
|
||||
path->append(new_ending);
|
||||
}
|
||||
bool CopyDirectory(const std::wstring& from_path, const std::wstring& to_path,
|
||||
bool recursive) {
|
||||
@ -346,17 +254,15 @@ bool CopyDirectory(const std::wstring& from_path, const std::wstring& to_path,
|
||||
FilePath::FromWStringHack(to_path),
|
||||
recursive);
|
||||
}
|
||||
bool PathExists(const std::wstring& path) {
|
||||
return PathExists(FilePath::FromWStringHack(path));
|
||||
}
|
||||
bool DirectoryExists(const std::wstring& path) {
|
||||
return DirectoryExists(FilePath::FromWStringHack(path));
|
||||
}
|
||||
bool ContentsEqual(const std::wstring& filename1,
|
||||
const std::wstring& filename2) {
|
||||
return ContentsEqual(FilePath::FromWStringHack(filename1),
|
||||
FilePath::FromWStringHack(filename2));
|
||||
}
|
||||
bool CopyFile(const std::wstring& from_path, const std::wstring& to_path) {
|
||||
return CopyFile(FilePath::FromWStringHack(from_path),
|
||||
FilePath::FromWStringHack(to_path));
|
||||
}
|
||||
bool CreateDirectory(const std::wstring& full_path) {
|
||||
return CreateDirectory(FilePath::FromWStringHack(full_path));
|
||||
}
|
||||
@ -367,6 +273,18 @@ bool CreateTemporaryFileName(std::wstring* temp_file) {
|
||||
*temp_file = temp_file_path.ToWStringHack();
|
||||
return true;
|
||||
}
|
||||
bool Delete(const std::wstring& path, bool recursive) {
|
||||
return Delete(FilePath::FromWStringHack(path), recursive);
|
||||
}
|
||||
bool DirectoryExists(const std::wstring& path) {
|
||||
return DirectoryExists(FilePath::FromWStringHack(path));
|
||||
}
|
||||
bool EndsWithSeparator(std::wstring* path) {
|
||||
return EndsWithSeparator(FilePath::FromWStringHack(*path));
|
||||
}
|
||||
bool EndsWithSeparator(const std::wstring& path) {
|
||||
return EndsWithSeparator(FilePath::FromWStringHack(path));
|
||||
}
|
||||
bool GetCurrentDirectory(std::wstring* path_str) {
|
||||
FilePath path;
|
||||
if (!GetCurrentDirectory(&path))
|
||||
@ -387,12 +305,43 @@ bool GetTempDir(std::wstring* path_str) {
|
||||
*path_str = path.ToWStringHack();
|
||||
return true;
|
||||
}
|
||||
bool Move(const std::wstring& from_path, const std::wstring& to_path) {
|
||||
return Move(FilePath::FromWStringHack(from_path),
|
||||
FilePath::FromWStringHack(to_path));
|
||||
}
|
||||
FILE* OpenFile(const std::wstring& filename, const char* mode) {
|
||||
return OpenFile(FilePath::FromWStringHack(filename), mode);
|
||||
}
|
||||
bool PathExists(const std::wstring& path) {
|
||||
return PathExists(FilePath::FromWStringHack(path));
|
||||
}
|
||||
bool SetCurrentDirectory(const std::wstring& directory) {
|
||||
return SetCurrentDirectory(FilePath::FromWStringHack(directory));
|
||||
}
|
||||
|
||||
void TrimFilename(std::wstring* path) {
|
||||
if (EndsWithSeparator(path)) {
|
||||
TrimTrailingSeparator(path);
|
||||
} else {
|
||||
*path = FilePath::FromWStringHack(*path).DirName().ToWStringHack();
|
||||
}
|
||||
}
|
||||
void UpOneDirectory(std::wstring* dir) {
|
||||
FilePath path = FilePath::FromWStringHack(*dir);
|
||||
FilePath directory = path.DirName();
|
||||
// If there is no separator, we will get back kCurrentDirectory.
|
||||
// In this case don't change |dir|.
|
||||
if (directory.value() != FilePath::kCurrentDirectory)
|
||||
*dir = directory.ToWStringHack();
|
||||
}
|
||||
void UpOneDirectoryOrEmpty(std::wstring* dir) {
|
||||
FilePath path = FilePath::FromWStringHack(*dir);
|
||||
FilePath directory = path.DirName();
|
||||
// If there is no separator, we will get back kCurrentDirectory.
|
||||
// In this case, clear dir.
|
||||
if (directory == path || directory.value() == FilePath::kCurrentDirectory)
|
||||
dir->clear();
|
||||
else
|
||||
*dir = directory.ToWStringHack();
|
||||
}
|
||||
} // namespace
|
||||
|
||||
|
@ -24,23 +24,31 @@
|
||||
#include <vector>
|
||||
|
||||
#include "base/basictypes.h"
|
||||
|
||||
class FilePath;
|
||||
#include "base/file_path.h"
|
||||
|
||||
namespace file_util {
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Constants
|
||||
|
||||
#if defined(OS_WIN)
|
||||
// The use of this constant is deprecated. Instead use file_util or FilePath
|
||||
// functions (Append, TrimTrailingSeparator, etc.), or use
|
||||
// FilePath::kSeparator[0].
|
||||
extern const wchar_t kPathSeparator;
|
||||
|
||||
#endif
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Functions that operate purely on a path string w/o touching the filesystem:
|
||||
|
||||
// Returns a vector of all of the components of the provided path.
|
||||
void PathComponents(const FilePath& path,
|
||||
std::vector<FilePath::StringType>* components);
|
||||
#if defined(OS_WIN)
|
||||
// Deprecated temporary compatibility function.
|
||||
void PathComponents(const std::wstring& path,
|
||||
std::vector<std::wstring>* components);
|
||||
#endif
|
||||
|
||||
// Returns true if the given path ends with a path separator character.
|
||||
bool EndsWithSeparator(const FilePath& path);
|
||||
@ -58,14 +66,17 @@ void TrimTrailingSeparator(std::wstring* dir);
|
||||
// Strips the topmost directory from the end of 'dir'. Assumes 'dir' does not
|
||||
// refer to a file.
|
||||
// If 'dir' is a root directory, return without change.
|
||||
// Deprecated. Use FilePath::DirName instead.
|
||||
void UpOneDirectory(std::wstring* dir);
|
||||
|
||||
// Strips the topmost directory from the end of 'dir'. Assumes 'dir' does not
|
||||
// refer to a file.
|
||||
// If 'dir' is a root directory, the result becomes empty string.
|
||||
// Deprecated. Use FilePath::DirName instead.
|
||||
void UpOneDirectoryOrEmpty(std::wstring* dir);
|
||||
|
||||
// Strips the filename component from the end of 'path'.
|
||||
// Strips the filename component from the end of 'path'. If path ends with a
|
||||
// separator, then just drop the separator.
|
||||
// Deprecated. Use FilePath::DirName instead.
|
||||
void TrimFilename(std::wstring* path);
|
||||
|
||||
// Returns the filename portion of 'path', without any leading \'s or /'s.
|
||||
@ -98,6 +109,9 @@ bool AbsolutePath(FilePath* path);
|
||||
// Deprecated temporary compatibility function.
|
||||
bool AbsolutePath(std::wstring* path);
|
||||
|
||||
// TODO(port): create FilePath versions of these functions, and remove this
|
||||
// platform define.
|
||||
#if defined(OS_WIN)
|
||||
// Inserts |suffix| after the file name portion of |path| but before the
|
||||
// extension.
|
||||
// Examples:
|
||||
@ -107,6 +121,12 @@ bool AbsolutePath(std::wstring* path);
|
||||
// path == "C:\pics.old\jojo" suffix == " (1)", returns "C:\pics.old\jojo (1)"
|
||||
void InsertBeforeExtension(std::wstring* path, const std::wstring& suffix);
|
||||
|
||||
// Replaces the extension of |file_name| with |extension|. If |file_name|
|
||||
// does not have an extension, them |extension| is added. If |extension| is
|
||||
// empty, then the extension is removed from |file_name|.
|
||||
void ReplaceExtension(std::wstring* file_name, const std::wstring& extension);
|
||||
#endif
|
||||
|
||||
// Replaces characters in 'file_name' that are illegal for file names with
|
||||
// 'replace_char'. 'file_name' must not be a full or relative path, but just the
|
||||
// file name component. Any leading or trailing whitespace in 'file_name' is
|
||||
@ -116,11 +136,6 @@ void InsertBeforeExtension(std::wstring* path, const std::wstring& suffix);
|
||||
// 'replace_char' is '-'.
|
||||
void ReplaceIllegalCharacters(std::wstring* file_name, int replace_char);
|
||||
|
||||
// Replaces the extension of |file_name| with |extension|. If |file_name|
|
||||
// does not have an extension, them |extension| is added. If |extention| is
|
||||
// empty, then the extension is removed from |file_name|.
|
||||
void ReplaceExtension(std::wstring* file_name, const std::wstring& extension);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Functions that involve filesystem access or modification:
|
||||
|
||||
|
@ -15,8 +15,6 @@
|
||||
|
||||
namespace file_util {
|
||||
|
||||
const wchar_t kPathSeparator = L'/';
|
||||
|
||||
bool GetTempDir(FilePath* path) {
|
||||
const char* tmp = getenv("TMPDIR");
|
||||
if (tmp)
|
||||
@ -30,7 +28,7 @@ bool CopyFile(const FilePath& from_path, const FilePath& to_path) {
|
||||
int infile = open(from_path.value().c_str(), O_RDONLY);
|
||||
if (infile < 0)
|
||||
return false;
|
||||
|
||||
|
||||
int outfile = creat(to_path.value().c_str(), 0666);
|
||||
if (outfile < 0) {
|
||||
close(infile);
|
||||
|
@ -13,8 +13,6 @@
|
||||
|
||||
namespace file_util {
|
||||
|
||||
const wchar_t kPathSeparator = L'/';
|
||||
|
||||
bool GetTempDir(FilePath* path) {
|
||||
NSString* tmp = NSTemporaryDirectory();
|
||||
if (tmp == nil)
|
||||
|
@ -147,6 +147,8 @@ TEST_F(FileUtilTest, AppendToPath) {
|
||||
#endif
|
||||
}
|
||||
|
||||
// TODO(port): enable this test for non-Windows.
|
||||
#if defined(OS_WIN)
|
||||
static const struct InsertBeforeExtensionCase {
|
||||
std::wstring path;
|
||||
std::wstring suffix;
|
||||
@ -204,6 +206,7 @@ TEST_F(FileUtilTest, InsertBeforeExtensionTest) {
|
||||
EXPECT_EQ(path, kInsertBeforeExtension[i].result);
|
||||
}
|
||||
}
|
||||
#endif // defined(OS_WIN)
|
||||
|
||||
static const struct filename_case {
|
||||
const wchar_t* path;
|
||||
@ -785,6 +788,8 @@ TEST_F(FileUtilTest, ReplaceIllegalCharactersTest) {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(port): enable this test for non-windows.
|
||||
#if defined(OS_WIN)
|
||||
static const struct ReplaceExtensionCase {
|
||||
std::wstring file_name;
|
||||
std::wstring extension;
|
||||
@ -827,6 +832,7 @@ TEST_F(FileUtilTest, ReplaceExtensionTestWithPathSeparators) {
|
||||
file_util::ReplaceExtension(&result_path, L".baz");
|
||||
EXPECT_EQ(path + L".baz", result_path);
|
||||
}
|
||||
#endif // defined(OS_WIN)
|
||||
|
||||
TEST_F(FileUtilTest, FileEnumeratorTest) {
|
||||
// Test an empty directory.
|
||||
@ -925,4 +931,56 @@ TEST_F(FileUtilTest, FileEnumeratorTest) {
|
||||
// (we don't care what).
|
||||
}
|
||||
|
||||
|
||||
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('/', 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('/', 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('/', start);
|
||||
}
|
||||
std::wstring component = std::wstring(path, start);
|
||||
components->push_back(component);
|
||||
}
|
||||
|
||||
static const struct PathComponentsCase {
|
||||
std::wstring path;
|
||||
FilePath::StringType result;
|
||||
} kPathComponents[] = {
|
||||
{L"/foo/bar/baz/", FILE_PATH_LITERAL("/|foo|bar|baz|")},
|
||||
{L"/foo/bar/baz", FILE_PATH_LITERAL("/|foo|bar|baz")},
|
||||
{L"e:/foo", FILE_PATH_LITERAL("e:|foo")},
|
||||
};
|
||||
|
||||
TEST_F(FileUtilTest, PathComponentsTest) {
|
||||
for (size_t i = 0; i < arraysize(kPathComponents); ++i) {
|
||||
FilePath path = FilePath::FromWStringHack(kPathComponents[i].path);
|
||||
std::vector<FilePath::StringType> comps;
|
||||
file_util::PathComponents(path, &comps);
|
||||
|
||||
FilePath::StringType result;
|
||||
for (size_t j = 0; j < comps.size(); ++j) {
|
||||
result.append(comps[j]);
|
||||
if (j < comps.size() - 1)
|
||||
result.append(FILE_PATH_LITERAL("|"), 1);
|
||||
}
|
||||
EXPECT_EQ(kPathComponents[i].result, result);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
@ -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
|
||||
|
@ -5,6 +5,7 @@
|
||||
// Unit tests for the SafeBrowsing storage system.
|
||||
|
||||
#include "base/command_line.h"
|
||||
#include "base/file_path.h"
|
||||
#include "base/file_util.h"
|
||||
#include "base/logging.h"
|
||||
#include "base/path_service.h"
|
||||
@ -21,7 +22,6 @@
|
||||
|
||||
using base::Time;
|
||||
|
||||
static const wchar_t kSafeBrowsingTestDatabase[] = L"SafeBrowsingTestDatabase";
|
||||
static const wchar_t kBloomSuffix[] = L" Bloom";
|
||||
|
||||
namespace {
|
||||
@ -59,11 +59,10 @@ namespace {
|
||||
|
||||
// Common database test set up code.
|
||||
std::wstring GetTestDatabaseName() {
|
||||
std::wstring filename;
|
||||
FilePath filename;
|
||||
PathService::Get(base::DIR_TEMP, &filename);
|
||||
filename.push_back(file_util::kPathSeparator);
|
||||
filename.append(kSafeBrowsingTestDatabase);
|
||||
return filename;
|
||||
filename = filename.Append(FILE_PATH_LITERAL("SafeBrowsingTestDatabase"));
|
||||
return filename.ToWStringHack();
|
||||
}
|
||||
|
||||
SafeBrowsingDatabase* SetupTestDatabase() {
|
||||
@ -1042,10 +1041,11 @@ void PeformUpdate(const std::wstring& initial_db,
|
||||
IoCounters before, after;
|
||||
#endif
|
||||
|
||||
std::wstring filename;
|
||||
PathService::Get(base::DIR_TEMP, &filename);
|
||||
filename.push_back(file_util::kPathSeparator);
|
||||
filename.append(L"SafeBrowsingTestDatabase");
|
||||
FilePath path;
|
||||
PathService::Get(base::DIR_TEMP, &path);
|
||||
path = path.Append(FILE_PATH_LITERAL("SafeBrowsingTestDatabase"));
|
||||
std::wstring filename = path.ToWStringHack();
|
||||
|
||||
// In case it existed from a previous run.
|
||||
file_util::Delete(filename, false);
|
||||
|
||||
|
@ -395,17 +395,13 @@ TEST_F(URLRequestTest, AboutBlankTest) {
|
||||
}
|
||||
|
||||
TEST_F(URLRequestTest, FileTest) {
|
||||
std::wstring app_path;
|
||||
FilePath app_path;
|
||||
PathService::Get(base::FILE_EXE, &app_path);
|
||||
|
||||
std::string app_url = WideToUTF8(app_path);
|
||||
std::replace(app_url.begin(), app_url.end(),
|
||||
file_util::kPathSeparator, L'/');
|
||||
app_url.insert(0, "file:///");
|
||||
GURL app_url = net::FilePathToFileURL(app_path.ToWStringHack());
|
||||
|
||||
TestDelegate d;
|
||||
{
|
||||
TestURLRequest r(GURL(app_url), &d);
|
||||
TestURLRequest r(app_url, &d);
|
||||
|
||||
r.Start();
|
||||
EXPECT_TRUE(r.is_pending());
|
||||
|
@ -279,9 +279,11 @@ class TestServer : public base::ProcessFilter {
|
||||
std::wstring test_data_directory;
|
||||
PathService::Get(base::DIR_SOURCE_ROOT, &test_data_directory);
|
||||
std::wstring normalized_document_root = document_root;
|
||||
#if defined(OS_WIN)
|
||||
std::replace(normalized_document_root.begin(),
|
||||
normalized_document_root.end(),
|
||||
L'/', file_util::kPathSeparator);
|
||||
L'/', FilePath::kSeparators[0]);
|
||||
#endif
|
||||
file_util::AppendToPath(&test_data_directory, normalized_document_root);
|
||||
|
||||
#if defined(OS_WIN)
|
||||
|
@ -22,6 +22,7 @@ MSVC_PUSH_WARNING_LEVEL(0);
|
||||
MSVC_POP_WARNING();
|
||||
#undef LOG
|
||||
|
||||
#include "base/file_path.h"
|
||||
#include "base/file_util.h"
|
||||
#include "base/hash_tables.h"
|
||||
#include "base/string_util.h"
|
||||
@ -572,15 +573,18 @@ TEST_F(DomSerializerTests, SerialzeHTMLDOMWithEntitiesInAttributeValue) {
|
||||
TEST_F(DomSerializerTests, SerialzeHTMLDOMWithBaseTag) {
|
||||
// There are total 2 available base tags in this test file.
|
||||
const int kTotalBaseTagCountInTestFile = 2;
|
||||
std::wstring page_file_path = data_dir_;
|
||||
file_util::AppendToPath(&page_file_path, L"dom_serializer");
|
||||
page_file_path.append(1, file_util::kPathSeparator);
|
||||
|
||||
FilePath page_file_path = FilePath::FromWStringHack(data_dir_).Append(
|
||||
FILE_PATH_LITERAL("dom_serializer"));
|
||||
file_util::EnsureEndsWithSeparator(&page_file_path);
|
||||
|
||||
// Get page dir URL which is base URL of this file.
|
||||
GURL path_dir_url = net::FilePathToFileURL(page_file_path);
|
||||
GURL path_dir_url = net::FilePathToFileURL(page_file_path.ToWStringHack());
|
||||
// Get file path.
|
||||
file_util::AppendToPath(&page_file_path, L"html_doc_has_base_tag.htm");
|
||||
page_file_path =
|
||||
page_file_path.Append(FILE_PATH_LITERAL("html_doc_has_base_tag.htm"));
|
||||
// Get file URL.
|
||||
GURL file_url = net::FilePathToFileURL(page_file_path);
|
||||
GURL file_url = net::FilePathToFileURL(page_file_path.ToWStringHack());
|
||||
ASSERT_TRUE(file_url.SchemeIsFile());
|
||||
std::wstring page_url = ASCIIToWide(file_url.spec());
|
||||
// Load the test file.
|
||||
|
@ -532,6 +532,7 @@ void TestShell::DumpRenderTree()
|
||||
file_path);
|
||||
}
|
||||
|
||||
// static
|
||||
std::string TestShell::RewriteLocalUrl(const std::string& url) {
|
||||
// Convert file:///tmp/LayoutTests urls to the actual location on disk.
|
||||
const char kPrefix[] = "file:///tmp/LayoutTests/";
|
||||
@ -539,18 +540,14 @@ std::string TestShell::RewriteLocalUrl(const std::string& url) {
|
||||
|
||||
std::string new_url(url);
|
||||
if (url.compare(0, kPrefixLen, kPrefix, kPrefixLen) == 0) {
|
||||
std::wstring replace_url;
|
||||
PathService::Get(base::DIR_EXE, &replace_url);
|
||||
file_util::UpOneDirectory(&replace_url);
|
||||
file_util::UpOneDirectory(&replace_url);
|
||||
file_util::AppendToPath(&replace_url, L"webkit");
|
||||
file_util::AppendToPath(&replace_url, L"data");
|
||||
file_util::AppendToPath(&replace_url, L"layout_tests");
|
||||
file_util::AppendToPath(&replace_url, L"LayoutTests");
|
||||
replace_url.push_back(file_util::kPathSeparator);
|
||||
new_url = std::string("file:///") +
|
||||
WideToUTF8(replace_url).append(url.substr(kPrefixLen));
|
||||
FilePath replace_path;
|
||||
PathService::Get(base::DIR_EXE, &replace_path);
|
||||
replace_path = replace_path.DirName().DirName().Append(
|
||||
"webkit/data/layout_tests/LayoutTests/");
|
||||
new_url = std::string("file://") + replace_path.value() +
|
||||
url.substr(kPrefixLen);
|
||||
}
|
||||
|
||||
return new_url;
|
||||
}
|
||||
|
||||
|
@ -576,7 +576,7 @@ void TestShell::DumpDocumentText()
|
||||
std::wstring file_path;
|
||||
if (!PromptForSaveFile(L"Dump document text", &file_path))
|
||||
return;
|
||||
|
||||
|
||||
WriteTextToFile(
|
||||
WideToUTF8(webkit_glue::DumpDocumentText(webView()->GetMainFrame())),
|
||||
WideToUTF8(file_path));
|
||||
@ -587,33 +587,28 @@ void TestShell::DumpRenderTree()
|
||||
std::wstring file_path;
|
||||
if (!PromptForSaveFile(L"Dump render tree", &file_path))
|
||||
return;
|
||||
|
||||
|
||||
WriteTextToFile(
|
||||
WideToUTF8(webkit_glue::DumpRenderer(webView()->GetMainFrame())),
|
||||
WideToUTF8(file_path));
|
||||
}
|
||||
|
||||
/* static */
|
||||
// static
|
||||
std::string TestShell::RewriteLocalUrl(const std::string& url) {
|
||||
// Convert file:///tmp/LayoutTests urls to the actual location on disk.
|
||||
const char kPrefix[] = "file:///tmp/LayoutTests/";
|
||||
const int kPrefixLen = arraysize(kPrefix) - 1;
|
||||
|
||||
|
||||
std::string new_url(url);
|
||||
if (url.compare(0, kPrefixLen, kPrefix, kPrefixLen) == 0) {
|
||||
std::wstring replace_url;
|
||||
PathService::Get(base::DIR_EXE, &replace_url);
|
||||
file_util::UpOneDirectory(&replace_url);
|
||||
file_util::UpOneDirectory(&replace_url);
|
||||
file_util::AppendToPath(&replace_url, L"webkit");
|
||||
file_util::AppendToPath(&replace_url, L"data");
|
||||
file_util::AppendToPath(&replace_url, L"layout_tests");
|
||||
file_util::AppendToPath(&replace_url, L"LayoutTests");
|
||||
replace_url.push_back(file_util::kPathSeparator);
|
||||
std::string replace_url8 = WideToUTF8(replace_url);
|
||||
new_url = std::string("file:///") +
|
||||
replace_url8.append(url.substr(kPrefixLen));
|
||||
FilePath replace_path;
|
||||
PathService::Get(base::DIR_EXE, &replace_path);
|
||||
replace_path = replace_path.DirName().DirName().Append(
|
||||
"webkit/data/layout_tests/LayoutTests/");
|
||||
new_url = std::string("file://") + replace_path.value() +
|
||||
url.substr(kPrefixLen);
|
||||
}
|
||||
|
||||
return new_url;
|
||||
}
|
||||
|
||||
@ -629,7 +624,7 @@ namespace webkit_glue {
|
||||
std::wstring GetLocalizedString(int message_id) {
|
||||
NSString* idString = [NSString stringWithFormat:@"%d", message_id];
|
||||
NSString* localString = NSLocalizedString(idString, @"");
|
||||
|
||||
|
||||
return UTF8ToWide([localString UTF8String]);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user