|
23 | 23 | #include "tools/pathTools.h" |
24 | 24 |
|
25 | 25 | #include <pugixml.hpp> |
| 26 | +#include <filesystem> |
| 27 | +#include <iostream> |
| 28 | +#include <set> |
| 29 | + |
| 30 | +namespace fs = std::filesystem; |
26 | 31 |
|
27 | 32 | namespace kiwix |
28 | 33 | { |
@@ -251,6 +256,46 @@ bool Manager::addBookFromPath(const std::string& pathToOpen, |
251 | 256 | .empty()); |
252 | 257 | } |
253 | 258 |
|
| 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 | + |
254 | 299 | bool Manager::readBookFromPath(const std::string& path, kiwix::Book* book) |
255 | 300 | { |
256 | 301 | std::string tmp_path = path; |
|
0 commit comments