diff --git a/include/library.h b/include/library.h index 4c629874b..30dbe046f 100644 --- a/include/library.h +++ b/include/library.h @@ -24,7 +24,7 @@ #include #include #include -#include +#include #include "common/regexTools.h" #include "common/stringTools.h" @@ -88,7 +88,7 @@ class Book */ class Library { - std::vector books; + std::map books; public: Library(); ~Library(); @@ -108,7 +108,6 @@ class Library Book& getBookById(const std::string& id); - bool removeBookByIndex(const unsigned int bookIndex); /** * Remove a book from the library. * diff --git a/include/manager.h b/include/manager.h index 4ac7e260a..19f26d3bb 100644 --- a/include/manager.h +++ b/include/manager.h @@ -99,14 +99,6 @@ class Manager */ bool readOpds(const string& content, const std::string& urlHost); - /** - * Remove a book from the library. - * - * @param bookIndex the index of the book to remove - * @return True - */ - bool removeBookByIndex(const unsigned int bookIndex); - /** * Remove a book from the library. * diff --git a/src/library.cpp b/src/library.cpp index 3ac04f3b2..628b99f33 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -144,55 +144,33 @@ bool Library::addBook(const Book& book) { /* Try to find it */ std::vector::iterator itr; - for (itr = this->books.begin(); itr != this->books.end(); ++itr) { - if (itr->id == book.id) { - itr->update(book); - return false; - } + try { + auto& oldbook = books.at(book.id); + oldbook.update(book); + return false; + } catch (std::out_of_range&) { + books[book.id] = book; + return true; } - - /* otherwise */ - this->books.push_back(book); - return true; } -bool Library::removeBookByIndex(const unsigned int bookIndex) -{ - books.erase(books.begin() + bookIndex); - return true; -} - bool Library::removeBookById(const std::string& id) { - auto itr = books.begin(); - for(; itr != books.end(); itr ++) { - if (itr->id == id) { - break; - } - } - if (itr != books.end()) { - books.erase(itr); - return true; - } - return false; + return books.erase(id) == 1; } Book& Library::getBookById(const std::string& id) { - for(auto& book: books) { - if(book.id == id) { - return book; - } - } - throw std::runtime_error(""); + return books.at(id); } unsigned int Library::getBookCount(const bool localBooks, const bool remoteBooks) { unsigned int result = 0; - for (auto& book: books) { + for (auto& pair: books) { + auto& book = pair.second; if ((!book.path.empty() && localBooks) || (book.path.empty() && remoteBooks)) { result++; @@ -208,7 +186,8 @@ Library Library::filter(const std::string& search) { return library; } - for(auto& book:books) { + for(auto& pair:books) { + auto& book = pair.second; if (matchRegex(book.title, "\\Q" + search + "\\E") || matchRegex(book.description, "\\Q" + search + "\\E")) { library.addBook(book); @@ -228,7 +207,8 @@ bool Library::writeToFile(const std::string& path) { libraryNode.append_attribute("version") = version.c_str(); /* Add each book */ - for (auto& book: books) { + for (auto& pair: books) { + auto& book = pair.second; if (!book.readOnly) { pugi::xml_node bookNode = libraryNode.append_child("book"); bookNode.append_attribute("id") = book.id.c_str(); @@ -312,7 +292,8 @@ std::vector Library::getBooksLanguages() std::vector booksLanguages; std::map booksLanguagesMap; - for (auto& book: books) { + for (auto& pair: books) { + auto& book = pair.second; if (booksLanguagesMap.find(book.language) == booksLanguagesMap.end()) { if (book.origId.empty()) { booksLanguagesMap[book.language] = true; @@ -329,7 +310,8 @@ std::vector Library::getBooksCreators() std::vector booksCreators; std::map booksCreatorsMap; - for (auto& book: books) { + for (auto& pair: books) { + auto& book = pair.second; if (booksCreatorsMap.find(book.creator) == booksCreatorsMap.end()) { if (book.origId.empty()) { booksCreatorsMap[book.creator] = true; @@ -345,8 +327,8 @@ std::vector Library::getBooksIds() { std::vector booksIds; - for (auto& book: books) { - booksIds.push_back(book.id); + for (auto& pair: books) { + booksIds.push_back(pair.first); } return booksIds; @@ -357,7 +339,8 @@ std::vector Library::getBooksPublishers() std::vector booksPublishers; std::map booksPublishersMap; - for (auto& book:books) { + for (auto& pair:books) { + auto& book = pair.second; if (booksPublishersMap.find(book.publisher) == booksPublishersMap.end()) { if (book.origId.empty()) { booksPublishersMap[book.publisher] = true; diff --git a/src/manager.cpp b/src/manager.cpp index 7e6648f83..73f87a6e1 100644 --- a/src/manager.cpp +++ b/src/manager.cpp @@ -285,11 +285,6 @@ bool Manager::readBookFromPath(const string path, kiwix::Book* book) return true; } -bool Manager::removeBookByIndex(const unsigned int bookIndex) -{ - return this->library.removeBookByIndex(bookIndex); -} - bool Manager::removeBookById(const string id) { return library.removeBookById(id); diff --git a/src/opds_dumper.cpp b/src/opds_dumper.cpp index 9aa31bb51..4ad1173e0 100644 --- a/src/opds_dumper.cpp +++ b/src/opds_dumper.cpp @@ -110,8 +110,8 @@ string OPDSDumper::dumpOPDSFeed() search_link.append_attribute("href") = searchDescriptionUrl.c_str(); } - for (auto book: library.books) { - handleBook(book, root_node); + for (auto& pair: library.books) { + handleBook(pair.second, root_node); } return nodeToString(root_node);