1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2025-02-17 11:45:50 +00:00

New Indexer class.

This commit is contained in:
King_DuckZ 2015-11-06 19:48:38 +00:00
parent 4fcc2cf25c
commit d07dc9a7da
4 changed files with 119 additions and 31 deletions

View file

@ -10,6 +10,7 @@ add_executable(${PROJECT_NAME}
src/main.cpp
src/filesearcher.cpp
src/pathname.cpp
src/indexer.cpp
)
target_include_directories(${PROJECT_NAME} SYSTEM

65
src/indexer.cpp Normal file
View file

@ -0,0 +1,65 @@
/* Copyright 2015, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#include "indexer.hpp"
#include "pathname.hpp"
#include <algorithm>
#if !defined(NDEBUG)
# include <iostream>
#endif
namespace din {
bool Indexer::add_path (const char* parPath, int parLevel, bool parIsDir, bool) {
if (parLevel > 0) {
PathLists& path_lists = (parIsDir ? m_directories : m_files);
const auto size = static_cast<std::size_t>(parLevel);
if (size > path_lists.size()) {
path_lists.resize(size);
}
std::string path(parPath);
auto insert_point = std::lower_bound(path_lists[size - 1].begin(), path_lists[size - 1].end(), path);
path_lists[size - 1].insert(insert_point, std::move(path));
} else {
m_base_path = parPath;
}
return true;
}
#if !defined(NDEBUG)
void Indexer::dump() const {
PathName base_path(m_base_path);
std::cout << "---------------- FILE LIST ----------------\n";
for (const auto& cur_list : m_files) {
for (const auto& cur_itm : cur_list) {
PathName cur_path(cur_itm);
std::cout << make_relative_path(base_path, cur_path).path() << '\n';
}
}
std::cout << "---------------- DIRECTORY LIST ----------------\n";
for (const auto& cur_list : m_directories) {
for (const auto& cur_itm : cur_list) {
PathName cur_path(cur_itm);
std::cout << make_relative_path(base_path, cur_path).path() << '\n';
}
}
}
#endif
} //namespace din

44
src/indexer.hpp Normal file
View file

@ -0,0 +1,44 @@
/* Copyright 2015, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef idE555EF56730442C1ADDC7B2AE7A9340E
#define idE555EF56730442C1ADDC7B2AE7A9340E
#include <vector>
#include <string>
namespace din {
class Indexer {
public:
Indexer ( void ) = default;
~Indexer ( void ) noexcept = default;
bool add_path ( const char* parPath, int parLevel, bool parIsDir, bool parIsSymlink );
#if !defined(NDEBUG)
void dump ( void ) const;
#endif
private:
typedef std::vector<std::vector<std::string>> PathLists;
PathLists m_directories;
PathLists m_files;
std::string m_base_path;
};
} //namespace din
#endif

View file

@ -18,48 +18,26 @@
#include <iostream>
#include <ciso646>
#include "filesearcher.hpp"
#include "pathname.hpp"
bool my_callback (const char* parPath, int parLevel, bool parDir, bool parSymlink) {
if (parLevel > 0 and not parDir)
std::cout << parPath << '\n';
return true;
}
#include "indexer.hpp"
int main (int parArgc, char* parArgv[]) {
using std::placeholders::_1;
using std::placeholders::_2;
using std::placeholders::_3;
using std::placeholders::_4;
std::cout << "argc: " << parArgc << '\n';
for (int z = 0; z < parArgc; ++z) {
std::cout << "argv[" << z << "]: " << parArgv[z] << '\n';
}
std::cout << std::endl;
din::Indexer indexer;
fastf::FileSearcher searcher("/home/duckz/dev/code/cpp/dindexer/test");
fastf::FileSearcher::ConstCharVecType ext, ignore;
searcher.SetFollowSymlinks(true);
searcher.SetCallback(fastf::FileSearcher::CallbackType(&my_callback));
//searcher.Search(ext, ignore);
searcher.SetCallback(fastf::FileSearcher::CallbackType(std::bind(&din::Indexer::add_path, &indexer, _1, _2, _3, _4)));
searcher.Search(ext, ignore);
din::PathName pn("/home/duckz/dev/code/cpp/dindexer/test");
din::PathName pn1("///home/duckz/dev/code/cpp/dindexer/test");
din::PathName pn2("home/duckz////dev/code/cpp/dindexer/test");
din::PathName pn3("/home/duckz/dev/code/cpp/dindexer/test/");
din::PathName pn4("/home/duckz/dev/code/cpp/dindexer/test////");
std::cout << pn.path() << '\n';
std::cout << pn1.path() << '\n';
std::cout << pn2.path() << '\n';
std::cout << pn3.path() << '\n';
std::cout << pn4.path() << '\n';
pn1.join(pn2);
pn1.join("..");
pn1.join("..");
pn1.join("..");
pn1.join("..");
pn1.join("..");
pn1.join("..");
pn1.join("..");
pn1.join("code");
std::cout << pn1.path() << '\n';
indexer.dump();
return 0;
}