diff --git a/src/common/kiwix/manager.cpp b/src/common/kiwix/manager.cpp index fefa1ebd0..38e232b5a 100644 --- a/src/common/kiwix/manager.cpp +++ b/src/common/kiwix/manager.cpp @@ -436,7 +436,7 @@ namespace kiwix { } bool Manager::listBooks(const supportedListMode mode, const supportedListSortBy sortBy, const unsigned int maxSize, - const string language, const string publisher) { + const string language, const string publisher, const string search) { this->bookIdList.clear(); std::vector::iterator itr; @@ -478,6 +478,9 @@ namespace kiwix { if (ok == true && !publisher.empty() && itr->creator != publisher) ok = false; + if (ok == true && !search.empty() && !(matchRegex(itr->title, search) || matchRegex(itr->description, search))) + ok = false; + if (ok == true) { this->bookIdList.push_back(itr->id); } diff --git a/src/common/kiwix/manager.h b/src/common/kiwix/manager.h index 75fe7325d..d3f738cd2 100644 --- a/src/common/kiwix/manager.h +++ b/src/common/kiwix/manager.h @@ -32,6 +32,7 @@ #include #include "../base64.h" +#include "../regexTools.h" #include #include @@ -64,7 +65,7 @@ namespace kiwix { bool updateBookLastOpenDateById(const string id); void removeBookPaths(); bool listBooks(const supportedListMode mode, const supportedListSortBy sortBy, const unsigned int maxSize, - const string language, const string publisher); + const string language, const string publisher, const string search); vector getBooksLanguages(); vector getBooksPublishers(); diff --git a/src/common/pathTools.h b/src/common/pathTools.h index 31577026d..b91292574 100644 --- a/src/common/pathTools.h +++ b/src/common/pathTools.h @@ -22,7 +22,7 @@ #include "nsStringAPI.h" -const char *nsStringToCString(const nsAString &path); +const char *nsStringToCString(const nsAString &str); const char *nsStringToUTF8(const nsAString &str); #endif diff --git a/src/common/regexTools.cpp b/src/common/regexTools.cpp new file mode 100644 index 000000000..6413a2190 --- /dev/null +++ b/src/common/regexTools.cpp @@ -0,0 +1,30 @@ +/* + * Copyright 2011 Emmanuel Engelhart + * + * This program 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 + * any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include "regexTools.h" + +bool matchRegex(const std::string &content, const std::string regex) { + regex_t regexp; + bool result = false; + + regcomp(®exp, regex.data(), REG_ICASE); + result = !regexec(®exp, content.data(), 0, 0, 0); + regfree(®exp); + return result; +} diff --git a/src/common/regexTools.h b/src/common/regexTools.h new file mode 100644 index 000000000..e43b79b8d --- /dev/null +++ b/src/common/regexTools.h @@ -0,0 +1,29 @@ +/* + * Copyright 2011 Emmanuel Engelhart + * + * This program 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 + * any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#ifndef KIWIX_REGEXTOOLS_H +#define KIWIX_REGEXTOOLS_H + +#include +#include +#include + +bool matchRegex(const std::string &content, const std::string regex); + +#endif