From be01fd65ac8ba77ce89061c3d4277a2a4b485254 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Thu, 12 Mar 2020 15:34:33 +0200 Subject: staging/adblock: single line FilterList::parse --- staging/adblock/AdblockPlusPlugin.json | 4 -- staging/adblock/filterlist.cpp | 48 ++++++------------------ staging/adblock/filterlist.h | 27 +++++--------- staging/adblock/meson.build | 31 +++++---------- staging/adblock/plugin.cpp | 29 -------------- staging/adblock/plugin.h | 25 ------------- staging/adblock/plugin/AdblockPlusPlugin.json | 4 ++ staging/adblock/plugin/plugin.cpp | 54 +++++++++++++++++++++++++++ staging/adblock/plugin/plugin.h | 25 +++++++++++++ staging/adblock/test/parser.cpp | 33 ---------------- 10 files changed, 112 insertions(+), 168 deletions(-) delete mode 100644 staging/adblock/AdblockPlusPlugin.json delete mode 100644 staging/adblock/plugin.cpp delete mode 100644 staging/adblock/plugin.h create mode 100644 staging/adblock/plugin/AdblockPlusPlugin.json create mode 100644 staging/adblock/plugin/plugin.cpp create mode 100644 staging/adblock/plugin/plugin.h delete mode 100644 staging/adblock/test/parser.cpp (limited to 'staging/adblock') diff --git a/staging/adblock/AdblockPlusPlugin.json b/staging/adblock/AdblockPlusPlugin.json deleted file mode 100644 index 053826a..0000000 --- a/staging/adblock/AdblockPlusPlugin.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "name": "AdblockPlus Filter Plugin", - "author": "Aqua " -} diff --git a/staging/adblock/filterlist.cpp b/staging/adblock/filterlist.cpp index 9fb53ad..9c80e88 100644 --- a/staging/adblock/filterlist.cpp +++ b/staging/adblock/filterlist.cpp @@ -32,49 +32,23 @@ FilterList::~FilterList() qDeleteAll(m_rules); } -FilterList::ParseResult FilterList::parse(QTextStream &stream) +FilterList::ParseResult FilterList::parse(const QString &line) { - FilterList::ParseResult result; + if(line.startsWith('!')) { + parseComment(line); + return FilterList::Comment; - if(stream.readLine().trimmed() != "[Adblock Plus 2.0]") { - result.state = FilterList::InvalidFormat; - return result; - } + } else if(line.contains("##") || line.contains("#@#")) { + // ## is element hiding rule + // #@# is element hiding exception rule + return FilterList::Unsupported; - QString line; - while(stream.readLineInto(&line)) { - - if(!line.isEmpty()) { - ++result.lines_total; - - if(line.startsWith('!')) { - ++result.lines_comments; - parseComment(line); - - } else if(line.contains("##") || line.contains("#@#")) { - // ## is element hiding rule - // #@# is element hiding exception rule - if(qgetenv("PRINT_IGNORED") == "1") - qDebug("ignored: >%s<", qUtf8Printable(line)); - ++result.lines_ignored; - - } else { - if(parseRule(line)) - ++result.lines_parsed; - else { - if(qgetenv("PRINT_FAILED") == "1") - qDebug("failed: >%s<", qUtf8Printable(line)); - ++result.lines_failed; - } - } - } + } else { + return parseRule(line) ? FilterList::Rule : FilterList::Failed; } - - result.state = FilterList::Ok; - return result; } -void FilterList::parseComment(QString &line) +void FilterList::parseComment(const QString &line) { if(line.startsWith(comment_lastModified)) { lastModified = QDateTime::fromString(line.mid(comment_lastModified.size()), "dd MMM yyyy HH:mm 'UTC'"); diff --git a/staging/adblock/filterlist.h b/staging/adblock/filterlist.h index 59717af..5ac60b6 100644 --- a/staging/adblock/filterlist.h +++ b/staging/adblock/filterlist.h @@ -6,15 +6,13 @@ * SPDX-License-Identifier: GPL-3.0 */ -#include "filterinterface.h" +#include #include #include #include #include #include -class QTextStream; - namespace AdblockPlus { class Rule; @@ -26,20 +24,13 @@ public: explicit FilterList() = default; ~FilterList(); - enum ParseResultState { - Ok, - InvalidFormat, - }; - struct ParseResult { - int lines_total = 0; - int lines_comments = 0; - int lines_ignored = 0; - int lines_parsed = 0; - int lines_failed = 0; - - ParseResultState state; + enum ParseResult { + Comment, + Rule, + Unsupported, + Failed, }; - ParseResult parse(QTextStream &stream); + ParseResult parse(const QString &line); void filter(QWebEngineUrlRequestInfo &info) const override; bool isUpToDate() const override; @@ -53,12 +44,12 @@ public: } private: - void parseComment(QString &line); + void parseComment(const QString &line); bool parseRule(const QString &line); QDateTime lastModified; QDateTime expires; - QVector m_rules; + QVector m_rules; }; } // namespace AdblockPlus diff --git a/staging/adblock/meson.build b/staging/adblock/meson.build index 1b992e5..8d06fb8 100644 --- a/staging/adblock/meson.build +++ b/staging/adblock/meson.build @@ -1,30 +1,17 @@ -dep_adblockfilter = declare_dependency( - include_directories: include_directories('.'), - dependencies: dep_plugininterface, - link_with: static_library('adblockfilter', - [ 'filterlist.cpp', 'rule.cpp', 'options.cpp' ], - dependencies: [ dep_qt5, dep_plugininterface ] - ) +lib_adblockfilter = static_library('adblockfilter', + [ 'filterlist.cpp', 'rule.cpp', 'options.cpp' ], + include_directories: plugininterface_include, + dependencies: [ dep_qt5 ] ) AdblockPlusFilterPlugin = shared_library('AdblockPlusPlugin', - [ 'plugin.cpp', - mod_qt5.preprocess( - include_directories: plugininterface_include, - moc_headers: 'plugin.h', - dependencies: [ dep_qt5 ] - ) + [ 'plugin/plugin.cpp', + mod_qt5.preprocess(include_directories: plugininterface_include, moc_headers: 'plugin/plugin.h', dependencies: dep_qt5) ], - dependencies: [ dep_qt5, dep_plugininterface, dep_adblockfilter ], + include_directories: plugininterface_include, + link_with: lib_adblockfilter, + dependencies: dep_qt5, install: true, install_dir: get_option('libdir')/'smolbote/plugins' ) -test('adblockfilter: parser', - executable('adblockfilter-parsefilter', dependencies: [ dep_qt5, dep_gtest, dep_adblockfilter ], - sources: [ 'test/parser.cpp' ] - ), - workdir: meson.current_source_dir() / 'test', - should_fail: true -) - diff --git a/staging/adblock/plugin.cpp b/staging/adblock/plugin.cpp deleted file mode 100644 index b4f1b56..0000000 --- a/staging/adblock/plugin.cpp +++ /dev/null @@ -1,29 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://library.iserlohn-fortress.net/aqua/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "plugin.h" -#include "filterlist.h" -#include - -Filter* AdblockPlusFilterPlugin::load(QIODevice* from) const -{ - if(!from->isOpen()) - return nullptr; - - QTextStream stream(from); - auto *list = new AdblockPlus::FilterList; - const auto result = list->parse(stream); - - if(result.state != AdblockPlus::FilterList::Ok) { - delete list; - list = nullptr; - } - - return list; -} - diff --git a/staging/adblock/plugin.h b/staging/adblock/plugin.h deleted file mode 100644 index 1ca51aa..0000000 --- a/staging/adblock/plugin.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://library.iserlohn-fortress.net/aqua/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef ADBLOCKPLUSFILTER_PLUGIN_H -#define ADBLOCKPLUSFILTER_PLUGIN_H - -#include - -class AdblockPlusFilterPlugin : public QObject, public FilterPlugin -{ - Q_OBJECT - Q_PLUGIN_METADATA(IID FilterPluginIid FILE "AdblockPlusPlugin.json") - Q_INTERFACES(FilterPlugin) - -public: - Filter* load(QIODevice* from) const override; -}; - -#endif // ADBLOCKPLUSFILTER_PLUGIN_H - diff --git a/staging/adblock/plugin/AdblockPlusPlugin.json b/staging/adblock/plugin/AdblockPlusPlugin.json new file mode 100644 index 0000000..053826a --- /dev/null +++ b/staging/adblock/plugin/AdblockPlusPlugin.json @@ -0,0 +1,4 @@ +{ + "name": "AdblockPlus Filter Plugin", + "author": "Aqua " +} diff --git a/staging/adblock/plugin/plugin.cpp b/staging/adblock/plugin/plugin.cpp new file mode 100644 index 0000000..028c83f --- /dev/null +++ b/staging/adblock/plugin/plugin.cpp @@ -0,0 +1,54 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://library.iserlohn-fortress.net/aqua/smolbote.git + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "plugin.h" +#include "../filterlist.h" +#include + +using namespace AdblockPlus; + +Filter* AdblockPlusFilterPlugin::load(QIODevice* from) const +{ + if(!from->isOpen()) + return nullptr; + + + QTextStream stream(from); + + if(stream.readLine().trimmed() != QLatin1String("[Adblock Plus 2.0]")) { + return nullptr; + } + + auto *list = new FilterList; + QString line; + int total, comments, rules, unsupported, failed; + + while(stream.readLineInto(&line)) { + if(!line.isEmpty()) { + ++total; + + switch(list->parse(line)) + { + case FilterList::Comment: + ++comments; + break; + case FilterList::Rule: + ++rules; + break; + case FilterList::Unsupported: + ++unsupported; + break; + case FilterList::Failed: + break; + } + } + } + + return list; +} + diff --git a/staging/adblock/plugin/plugin.h b/staging/adblock/plugin/plugin.h new file mode 100644 index 0000000..1ca51aa --- /dev/null +++ b/staging/adblock/plugin/plugin.h @@ -0,0 +1,25 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://library.iserlohn-fortress.net/aqua/smolbote.git + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef ADBLOCKPLUSFILTER_PLUGIN_H +#define ADBLOCKPLUSFILTER_PLUGIN_H + +#include + +class AdblockPlusFilterPlugin : public QObject, public FilterPlugin +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID FilterPluginIid FILE "AdblockPlusPlugin.json") + Q_INTERFACES(FilterPlugin) + +public: + Filter* load(QIODevice* from) const override; +}; + +#endif // ADBLOCKPLUSFILTER_PLUGIN_H + diff --git a/staging/adblock/test/parser.cpp b/staging/adblock/test/parser.cpp deleted file mode 100644 index 2d2e59e..0000000 --- a/staging/adblock/test/parser.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include "filterlist.h" -#include -#include - -int main(int argc, char **argv) -{ - if(argc < 2) { - qDebug("usage: %s list1.txt ...", argv[0]); - return 77; - } - - for(int i = 1; i < argc; ++i) { - QFile f(argv[i]); - if(!f.open(QIODevice::ReadOnly | QIODevice::Text)) { - qDebug("could not open %s", argv[i]); - return -1; - } - - AdblockPlus::FilterList list; - QTextStream stream(&f); - const auto result = list.parse(stream); - qDebug("[%s]: %s", argv[i], (result.state == AdblockPlus::FilterList::Ok) ? "okay" : "failed"); - qDebug(" total: %i\t\tmodified: %s", result.lines_total, qUtf8Printable(list.modified().toString())); - qDebug("comments: %i\t\t expires: %s", result.lines_comments, qUtf8Printable(list.expiresOn().toString())); - qDebug(" ignored: %i\t\t valid: %s", result.lines_ignored, list.isUpToDate() ? "yes" : "no"); - qDebug(" parsed: %i", result.lines_parsed); - qDebug(" failed: %i", result.lines_failed); - qDebug("-------- --------"); - - f.close(); - } - return 0; -} -- cgit v1.2.1