Skip to content

Commit a3780f9

Browse files
juuz0kelson42
authored andcommitted
Introduce Manager::addBooksFromDirectory()
Added a function to load books from a directory. Requires rootPath to iterate over.
1 parent 2a858dc commit a3780f9

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

include/manager.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,17 @@ class Manager
155155
const std::string& url = "",
156156
const bool checkMetaData = false);
157157

158+
/**
159+
* Add books from a directory into the libary.
160+
*
161+
* @param path The path of the directory to scan.
162+
* @param path If the function should stop for an invalid file.
163+
* @param verboseFlag Verbose logs flag.
164+
*/
165+
void addBooksFromDirectory(const std::string& path,
166+
const bool skipInvalid = true,
167+
const bool verboseFlag = false);
168+
158169
std::string writableLibraryPath;
159170

160171
bool m_hasSearchResult = false;

src/manager.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
#include "tools/pathTools.h"
2424

2525
#include <pugixml.hpp>
26+
#include <filesystem>
27+
#include <iostream>
28+
#include <set>
29+
30+
namespace fs = std::filesystem;
2631

2732
namespace kiwix
2833
{
@@ -251,6 +256,46 @@ bool Manager::addBookFromPath(const std::string& pathToOpen,
251256
.empty());
252257
}
253258

259+
void Manager::addBooksFromDirectory(const std::string& path,
260+
const bool skipInvalid,
261+
const bool verboseFlag)
262+
{
263+
std::set<std::string> iteratedDirs;
264+
std::queue<std::string> dirQueue;
265+
dirQueue.push(fs::absolute(path).u8string());
266+
267+
while (!dirQueue.empty()) {
268+
const auto currentPath = dirQueue.front();
269+
dirQueue.pop();
270+
if (verboseFlag)
271+
std::cout << "Iterating over directory: " << currentPath << std::endl;
272+
for (const auto& dirEntry : fs::directory_iterator(currentPath)) {
273+
auto resolvedPath = dirEntry.path();
274+
if (fs::is_symlink(dirEntry)) {
275+
resolvedPath = fs::canonical(dirEntry.path());
276+
}
277+
const std::string pathString = resolvedPath.u8string();
278+
if (fs::is_directory(resolvedPath)) {
279+
if (iteratedDirs.find(pathString) == iteratedDirs.end())
280+
dirQueue.push(pathString);
281+
else if (verboseFlag)
282+
std::cout << "Already iterated over " << pathString << ". Skipping..." << std::endl;
283+
} else {
284+
if (!this->addBookFromPath(pathString, pathString, "", false)) {
285+
if (skipInvalid)
286+
std::cerr << "Skipping invalid file: " << pathString << std::endl;
287+
else {
288+
throw std::runtime_error("Unable to add file: " + pathString + " into the library.");
289+
}
290+
}
291+
}
292+
}
293+
if (verboseFlag)
294+
std::cout << "Completed iterating over directory: " << currentPath << std::endl;
295+
iteratedDirs.insert(currentPath);
296+
}
297+
}
298+
254299
bool Manager::readBookFromPath(const std::string& path, kiwix::Book* book)
255300
{
256301
std::string tmp_path = path;

0 commit comments

Comments
 (0)