diff options
author | gman@google.com <gman@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-23 00:18:29 +0000 |
---|---|---|
committer | gman@google.com <gman@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-23 00:18:29 +0000 |
commit | cb27fe7df0f1c308ea5b87c5dc42a91a960d979e (patch) | |
tree | 0a9f5c760215d895243972a1030f76db19e40c4c /o3d/utils | |
parent | 2383c9497d4ea626fd4612f979cf81cca5a4dbbe (diff) | |
download | chromium_src-cb27fe7df0f1c308ea5b87c5dc42a91a960d979e.zip chromium_src-cb27fe7df0f1c308ea5b87c5dc42a91a960d979e.tar.gz chromium_src-cb27fe7df0f1c308ea5b87c5dc42a91a960d979e.tar.bz2 |
Add --file_paths to converter to allow converter to run
with typical collada files found on the net.
Often someone will export a collada file with absolute paths
like "z:\someplace\somewhere\texture.tga" and
"k:\shared\stuff\othertexture.tga"
The converter needed a way to handle these without requiring
an artist to manually edit XML files.
I also refactored the original_data_ stuff to make sure
we don't get 2 datas for the same path. I'm doing that
in prepartion for shortening the paths where possible
using base-path or some mechanism.
Review URL: http://codereview.chromium.org/159118
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@21352 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'o3d/utils')
-rw-r--r-- | o3d/utils/cross/file_path_utils.cc | 53 | ||||
-rw-r--r-- | o3d/utils/cross/file_path_utils.h | 21 | ||||
-rw-r--r-- | o3d/utils/cross/file_path_utils_test.cc | 52 |
3 files changed, 124 insertions, 2 deletions
diff --git a/o3d/utils/cross/file_path_utils.cc b/o3d/utils/cross/file_path_utils.cc index db4216e..910c3a6 100644 --- a/o3d/utils/cross/file_path_utils.cc +++ b/o3d/utils/cross/file_path_utils.cc @@ -158,4 +158,57 @@ bool GetRelativePathIfPossible(const FilePath& base_dir, } } +namespace { // anonymous namespace. + +// Tries to find a file path_to_find in path_to_search. Will start with +// base name and progressively try each higher path. Example: +// +// FindFile('this/that', 'foo/bar/baf.txt'); +// +// Looks for: +// this/that/foo/bar/baf.txt +// this/that/bar/baf.txt +// this/that/baf.txt +bool FindFileHelper(const FilePath& path_to_search, + const FilePath& path_to_find, + FilePath* found_path) { + std::vector<FilePath::StringType> parts; + file_util::PathComponents(path_to_find, &parts); + + for (size_t ii = 0; ii < parts.size(); ++ii) { + // build a path from parts. + FilePath path(path_to_search); + for (size_t jj = ii; jj < parts.size(); ++jj) { + path = path.Append(parts[jj]); + } + if (file_util::PathExists(path)) { + *found_path = path; + return true; + } + } + + return false; +} + +} // anonymous namespace + +bool FindFile(const std::vector<FilePath>& paths_to_search, + const FilePath& path_to_find, + FilePath* found_path) { + if (file_util::PathExists(path_to_find)) { + *found_path = path_to_find; + return true; + } + + for (size_t ii = 0; ii < paths_to_search.size(); ++ii) { + FilePath absolute_path(paths_to_search[ii]); + o3d::AbsolutePath(&absolute_path); + + if (FindFileHelper(absolute_path, path_to_find, found_path)) { + return true; + } + } + return false; +} + } // end namespace o3d diff --git a/o3d/utils/cross/file_path_utils.h b/o3d/utils/cross/file_path_utils.h index 30de903..7398e8b 100644 --- a/o3d/utils/cross/file_path_utils.h +++ b/o3d/utils/cross/file_path_utils.h @@ -37,10 +37,10 @@ #define O3D_UTILS_CROSS_FILE_PATH_UTILS_H_ #include <string> +#include <vector> +#include "base/file_path.h" #include "core/cross/types.h" -class FilePath; - namespace o3d { // TODO: Go through the process to add these to FilePath // itself in the Chromium depot. @@ -67,6 +67,23 @@ bool GetRelativePathIfPossible(const FilePath& base_dir, const FilePath& candidate, FilePath *result); +// Tries to find a file path_to_find in paths_to_search. Will start with +// base name and progressively try each higher path. Example: +// +// FindFile(['this/that', 'there'], 'foo/bar/baf.txt'); +// +// Looks for: +// foo/bar/baf.txt +// this/that/foo/bar/baf.txt +// this/that/bar/baf.txt +// this/that/baf.txt +// there/foo/bar/baf.txt +// there/bar/baf.txt +// there/baf.txt +bool FindFile(const std::vector<FilePath>& paths_to_search, + const FilePath& path_to_find, + FilePath* found_path); + } // namespace o3d #endif // O3D_UTILS_CROSS_FILE_PATH_UTILS_H_ diff --git a/o3d/utils/cross/file_path_utils_test.cc b/o3d/utils/cross/file_path_utils_test.cc index e1447fc..e5b9ede 100644 --- a/o3d/utils/cross/file_path_utils_test.cc +++ b/o3d/utils/cross/file_path_utils_test.cc @@ -40,6 +40,26 @@ namespace o3d { +namespace { // anonymous namespace + +bool FilePathsEqual(const FilePath& path_1, const FilePath path_2) { + const FilePath::StringType& p1(path_1.value()); + const FilePath::StringType& p2(path_2.value()); + if (p1.size() == p2.size()) { + for (size_t ii = 0; ii < p1.size(); ++ii) { + if (p1[ii] != p2[ii]) { + if (!FilePath::IsSeparator(p1[ii]) || !FilePath::IsSeparator(p2[ii])) { + return false; + } + } + } + return true; + } + return false; +} + +} // anonymous namespace + class FilePathUtilsTest : public testing::Test { }; @@ -180,4 +200,36 @@ TEST_F(FilePathUtilsTest, RelativePathsRelativeInputs) { EXPECT_STREQ(expected_result.value().c_str(), result.value().c_str()); EXPECT_TRUE(is_relative); } + +TEST_F(FilePathUtilsTest, FindFile) { + String folder_name_1(*g_program_path + "/unittest_data"); + String folder_name_2(*g_program_path + "/bitmap_test"); + FilePath folder_path_1 = UTF8ToFilePath(folder_name_1); + FilePath folder_path_2 = UTF8ToFilePath(folder_name_2); + String file_name_1("fur.fx"); + String file_name_2("someplace/somewhere/tga-256x256-32bit.tga"); + FilePath file_path_1 = UTF8ToFilePath(file_name_1); + FilePath file_path_2 = UTF8ToFilePath(file_name_2); + FilePath out_path; + + FilePath expected_path_1(folder_path_1); + expected_path_1 = expected_path_1.Append(file_path_1); + FilePath expected_path_2(folder_path_2); + expected_path_2 = + expected_path_2.Append(UTF8ToFilePath("tga-256x256-32bit.tga")); + + std::vector<FilePath> paths; + EXPECT_FALSE(FindFile(paths, file_path_1, &out_path)); + EXPECT_FALSE(FindFile(paths, file_path_2, &out_path)); + paths.push_back(folder_path_1); + EXPECT_TRUE(FindFile(paths, file_path_1, &out_path)); + EXPECT_FALSE(FindFile(paths, file_path_2, &out_path)); + EXPECT_TRUE(FilePathsEqual(out_path, expected_path_1)); + paths.push_back(folder_path_2); + EXPECT_TRUE(FindFile(paths, file_path_1, &out_path)); + EXPECT_TRUE(FilePathsEqual(out_path, expected_path_1)); + EXPECT_TRUE(FindFile(paths, file_path_2, &out_path)); + EXPECT_TRUE(FilePathsEqual(out_path, expected_path_2)); +} + } // namespace o3d |