Remove unused file_finder
parent
0bc33fa86a
commit
b3735981d6
@ -1,66 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <fstream>
|
||||
#include "components/file_finder/file_finder.hpp"
|
||||
|
||||
struct FileFinderTest : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
FileFinderTest()
|
||||
: mTestDir("./filefinder_test_dir/")
|
||||
, mTestFile("test.txt")
|
||||
, mTestFileUppercase("TEST.TXT")
|
||||
, mTestFileNotFound("foobarbaz.txt")
|
||||
{
|
||||
}
|
||||
|
||||
virtual void SetUp()
|
||||
{
|
||||
boost::filesystem::create_directory(boost::filesystem::path(mTestDir));
|
||||
|
||||
std::ofstream ofs(std::string(mTestDir + mTestFile).c_str(), std::ofstream::out);
|
||||
ofs << std::endl;
|
||||
ofs.close();
|
||||
}
|
||||
|
||||
virtual void TearDown()
|
||||
{
|
||||
boost::filesystem::remove_all(boost::filesystem::path(mTestDir));
|
||||
}
|
||||
|
||||
std::string mTestDir;
|
||||
std::string mTestFile;
|
||||
std::string mTestFileUppercase;
|
||||
std::string mTestFileNotFound;
|
||||
};
|
||||
|
||||
TEST_F(FileFinderTest, FileFinder_has_file)
|
||||
{
|
||||
FileFinder::FileFinder fileFinder(mTestDir);
|
||||
ASSERT_TRUE(fileFinder.has(mTestFile));
|
||||
ASSERT_TRUE(fileFinder.has(mTestFileUppercase));
|
||||
ASSERT_TRUE(fileFinder.lookup(mTestFile) == std::string(mTestDir + mTestFile));
|
||||
ASSERT_TRUE(fileFinder.lookup(mTestFileUppercase) == std::string(mTestDir + mTestFile));
|
||||
}
|
||||
|
||||
TEST_F(FileFinderTest, FileFinder_does_not_have_file)
|
||||
{
|
||||
FileFinder::FileFinder fileFinder(mTestDir);
|
||||
ASSERT_FALSE(fileFinder.has(mTestFileNotFound));
|
||||
ASSERT_TRUE(fileFinder.lookup(mTestFileNotFound).empty());
|
||||
}
|
||||
|
||||
TEST_F(FileFinderTest, FileFinderStrict_has_file)
|
||||
{
|
||||
FileFinder::FileFinderStrict fileFinder(mTestDir);
|
||||
ASSERT_TRUE(fileFinder.has(mTestFile));
|
||||
ASSERT_FALSE(fileFinder.has(mTestFileUppercase));
|
||||
ASSERT_TRUE(fileFinder.lookup(mTestFile) == std::string(mTestDir + mTestFile));
|
||||
ASSERT_FALSE(fileFinder.lookup(mTestFileUppercase) == std::string(mTestDir + mTestFile));
|
||||
}
|
||||
|
||||
TEST_F(FileFinderTest, FileFinderStrict_does_not_have_file)
|
||||
{
|
||||
FileFinder::FileFinderStrict fileFinder(mTestDir);
|
||||
ASSERT_FALSE(fileFinder.has(mTestFileNotFound));
|
||||
ASSERT_TRUE(fileFinder.lookup(mTestFileNotFound).empty());
|
||||
}
|
@ -1,74 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <fstream>
|
||||
|
||||
#include "components/file_finder/search.hpp"
|
||||
|
||||
struct SearchTest : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
SearchTest()
|
||||
: mTestDir("./search_test_dir/")
|
||||
{
|
||||
}
|
||||
|
||||
virtual void SetUp()
|
||||
{
|
||||
boost::filesystem::create_directory(boost::filesystem::path(mTestDir));
|
||||
|
||||
std::ofstream ofs(std::string(mTestDir + "test2.txt").c_str(), std::ofstream::out);
|
||||
ofs << std::endl;
|
||||
ofs.close();
|
||||
}
|
||||
|
||||
virtual void TearDown()
|
||||
{
|
||||
boost::filesystem::remove_all(boost::filesystem::path(mTestDir));
|
||||
}
|
||||
|
||||
std::string mTestDir;
|
||||
};
|
||||
|
||||
TEST_F(SearchTest, file_not_found)
|
||||
{
|
||||
struct Result : public FileFinder::ReturnPath
|
||||
{
|
||||
Result(const boost::filesystem::path& expectedPath)
|
||||
: mExpectedPath(expectedPath)
|
||||
{
|
||||
}
|
||||
|
||||
void add(const boost::filesystem::path& p)
|
||||
{
|
||||
ASSERT_FALSE(p == mExpectedPath);
|
||||
}
|
||||
|
||||
private:
|
||||
boost::filesystem::path mExpectedPath;
|
||||
|
||||
} r(boost::filesystem::path(mTestDir + "test.txt"));
|
||||
|
||||
FileFinder::find(mTestDir, r, false);
|
||||
}
|
||||
|
||||
TEST_F(SearchTest, file_found)
|
||||
{
|
||||
struct Result : public FileFinder::ReturnPath
|
||||
{
|
||||
Result(const boost::filesystem::path& expectedPath)
|
||||
: mExpectedPath(expectedPath)
|
||||
{
|
||||
}
|
||||
|
||||
void add(const boost::filesystem::path& p)
|
||||
{
|
||||
ASSERT_TRUE(p == mExpectedPath);
|
||||
}
|
||||
|
||||
private:
|
||||
boost::filesystem::path mExpectedPath;
|
||||
|
||||
} r(boost::filesystem::path(mTestDir + "test2.txt"));
|
||||
|
||||
FileFinder::find(mTestDir, r, false);
|
||||
}
|
@ -1,142 +0,0 @@
|
||||
#ifndef FILE_FINDER_MAIN_H
|
||||
#define FILE_FINDER_MAIN_H
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "search.hpp"
|
||||
#include "filename_less.hpp"
|
||||
#include <components/files/multidircollection.hpp>
|
||||
|
||||
namespace FileFinder
|
||||
{
|
||||
|
||||
template <typename LESS>
|
||||
class FileFinderT
|
||||
{
|
||||
typedef std::map<std::string, std::string, LESS> TableContainer;
|
||||
TableContainer table;
|
||||
|
||||
struct Inserter : ReturnPath
|
||||
{
|
||||
FileFinderT<LESS> *owner;
|
||||
int cut;
|
||||
|
||||
void add(const boost::filesystem::path &pth)
|
||||
{
|
||||
std::string file = pth.string();
|
||||
std::string key = file.substr(cut);
|
||||
owner->table[key] = file;
|
||||
}
|
||||
};
|
||||
|
||||
Inserter inserter;
|
||||
|
||||
public:
|
||||
FileFinderT(const boost::filesystem::path &path, bool recurse=true)
|
||||
{
|
||||
inserter.owner = this;
|
||||
|
||||
// Remember the original path length, so we can cut it away from
|
||||
// the relative paths used as keys
|
||||
const std::string& pstring = path.string();
|
||||
inserter.cut = pstring.size();
|
||||
|
||||
// If the path does not end in a slash, then boost will add one
|
||||
// later, which means one more character we have to remove.
|
||||
char last = *pstring.rbegin();
|
||||
if(last != '\\' && last != '/')
|
||||
inserter.cut++;
|
||||
|
||||
// Fill the map
|
||||
find(path, inserter, recurse);
|
||||
}
|
||||
|
||||
bool has(const std::string& file) const
|
||||
{
|
||||
return table.find(file) != table.end();
|
||||
}
|
||||
|
||||
// Find the full path from a relative path.
|
||||
const std::string &lookup(const std::string& file) const
|
||||
{
|
||||
static std::string empty;
|
||||
typename TableContainer::const_iterator it = table.find(file);
|
||||
return (it != table.end()) ? it->second : empty;
|
||||
}
|
||||
};
|
||||
|
||||
template
|
||||
<
|
||||
class LESS
|
||||
>
|
||||
struct TreeFileFinder
|
||||
{
|
||||
typedef TreeFileFinder<LESS> finder_t;
|
||||
|
||||
TreeFileFinder(const Files::PathContainer& paths, bool recurse = true)
|
||||
{
|
||||
struct : ReturnPath
|
||||
{
|
||||
finder_t *owner;
|
||||
int cut;
|
||||
|
||||
void add(const boost::filesystem::path &pth)
|
||||
{
|
||||
std::string file = pth.string();
|
||||
std::string key = file.substr(cut);
|
||||
owner->mTable[key] = file;
|
||||
}
|
||||
} inserter;
|
||||
|
||||
inserter.owner = this;
|
||||
|
||||
for (Files::PathContainer::const_iterator it = paths.begin(); it != paths.end(); ++it)
|
||||
{
|
||||
|
||||
// Remember the original path length, so we can cut it away from
|
||||
// the relative paths used as keys
|
||||
const std::string& pstring = it->string();
|
||||
inserter.cut = pstring.size();
|
||||
|
||||
// If the path does not end in a slash, then boost will add one
|
||||
// later, which means one more character we have to remove.
|
||||
char last = *pstring.rbegin();
|
||||
if (last != '\\' && last != '/')
|
||||
{
|
||||
inserter.cut++;
|
||||
}
|
||||
|
||||
// Fill the map
|
||||
find(*it, inserter, recurse);
|
||||
}
|
||||
}
|
||||
|
||||
bool has(const std::string& file) const
|
||||
{
|
||||
return mTable.find(file) != mTable.end();
|
||||
}
|
||||
|
||||
const std::string& lookup(const std::string& file) const
|
||||
{
|
||||
static std::string empty;
|
||||
typename TableContainer::const_iterator it = mTable.find(file);
|
||||
return (it != mTable.end()) ? it->second : empty;
|
||||
}
|
||||
|
||||
private:
|
||||
typedef std::map<std::string, std::string, LESS> TableContainer;
|
||||
TableContainer mTable;
|
||||
|
||||
// Inserter inserter;
|
||||
};
|
||||
|
||||
|
||||
// The default is to use path_less for equality checks
|
||||
typedef FileFinderT<path_less> FileFinder;
|
||||
typedef FileFinderT<path_slash> FileFinderStrict;
|
||||
|
||||
typedef TreeFileFinder<path_less> LessTreeFileFinder;
|
||||
typedef TreeFileFinder<path_slash> StrictTreeFileFinder;
|
||||
|
||||
} /* namespace FileFinder */
|
||||
#endif /* FILE_FINDER_MAIN_H */
|
@ -1,84 +0,0 @@
|
||||
#ifndef FILE_FINDER_LESS_H
|
||||
#define FILE_FINDER_LESS_H
|
||||
|
||||
#include <libs/platform/strings.h>
|
||||
#include <string>
|
||||
|
||||
namespace FileFinder{
|
||||
|
||||
// Used for maps of file paths. Compares file paths, but ignores case
|
||||
// AND treats \ and / as the same character.
|
||||
struct path_less
|
||||
{
|
||||
int compareChar(char a, char b) const
|
||||
{
|
||||
if(a>b) return 1;
|
||||
else if(a<b) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int comparePathChar(char a, char b) const
|
||||
{
|
||||
if(a >= 'a' && a <= 'z') a += 'A'-'a';
|
||||
else if(a == '\\') a = '/';
|
||||
if(b >= 'a' && b <= 'z') b += 'A'-'a';
|
||||
else if(b == '\\') b = '/';
|
||||
return compareChar(a,b);
|
||||
}
|
||||
|
||||
int compareString(const char *a, const char *b) const
|
||||
{
|
||||
while(*a && *b)
|
||||
{
|
||||
int i = comparePathChar(*a,*b);
|
||||
if(i != 0) return i;
|
||||
a++; b++;
|
||||
}
|
||||
// At this point, one or both of the chars is a null terminator.
|
||||
// Normal char comparison will get the correct final result here.
|
||||
return compareChar(*a,*b);
|
||||
}
|
||||
|
||||
bool operator() (const std::string& a, const std::string& b) const
|
||||
{
|
||||
return compareString(a.c_str(), b.c_str()) < 0;
|
||||
}
|
||||
};
|
||||
|
||||
struct path_slash
|
||||
{
|
||||
int compareChar(char a, char b) const
|
||||
{
|
||||
if(a>b) return 1;
|
||||
else if(a<b) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int comparePathChar(char a, char b) const
|
||||
{
|
||||
if(a == '\\') a = '/';
|
||||
if(b == '\\') b = '/';
|
||||
return compareChar(a,b);
|
||||
}
|
||||
|
||||
int compareString(const char *a, const char *b) const
|
||||
{
|
||||
while(*a && *b)
|
||||
{
|
||||
int i = comparePathChar(*a,*b);
|
||||
if(i != 0) return i;
|
||||
a++; b++;
|
||||
}
|
||||
// At this point, one or both of the chars is a null terminator.
|
||||
// Normal char comparison will get the correct final result here.
|
||||
return compareChar(*a,*b);
|
||||
}
|
||||
|
||||
bool operator() (const std::string& a, const std::string& b) const
|
||||
{
|
||||
return compareString(a.c_str(), b.c_str()) < 0;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
#endif
|
@ -1,36 +0,0 @@
|
||||
#include "search.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
void FileFinder::find(const boost::filesystem::path & dir_path, ReturnPath &ret, bool recurse)
|
||||
{
|
||||
if (boost::filesystem::exists(dir_path))
|
||||
{
|
||||
if (!recurse)
|
||||
{
|
||||
boost::filesystem::directory_iterator end_itr; // default construction yields past-the-end
|
||||
for (boost::filesystem::directory_iterator itr(dir_path); itr != end_itr; ++itr)
|
||||
{
|
||||
if (!boost::filesystem::is_directory( *itr ))
|
||||
{
|
||||
ret.add(*itr);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
boost::filesystem::recursive_directory_iterator end_itr; // default construction yields past-the-end
|
||||
for (boost::filesystem::recursive_directory_iterator itr(dir_path); itr != end_itr; ++itr)
|
||||
{
|
||||
if (!boost::filesystem::is_directory(*itr))
|
||||
{
|
||||
ret.add(*itr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Path " << dir_path << " not found" << std::endl;
|
||||
}
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
#ifndef FILE_FINDER_SEARCH_H
|
||||
#define FILE_FINDER_SEARCH_H
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace FileFinder
|
||||
{
|
||||
struct ReturnPath
|
||||
{
|
||||
virtual void add(const boost::filesystem::path &pth) = 0;
|
||||
};
|
||||
|
||||
/** Search the given path and return all file paths through 'ret'. If
|
||||
recurse==true, all files in subdirectories are returned as well.
|
||||
*/
|
||||
void find(const boost::filesystem::path & dir_path, ReturnPath &ret, bool recurse=true);
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue