From f6cf3c9c62f843d6f75971b5399242b51bcf9fb0 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Fri, 28 Feb 2020 02:33:51 -0700 Subject: [PATCH] save regex --- extensions/regexfilter.cpp | 39 ++++++++++++++++++++++++++++++++++---- extensions/regexfilter.ui | 9 ++++++++- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/extensions/regexfilter.cpp b/extensions/regexfilter.cpp index b9cff0d..7391bdc 100644 --- a/extensions/regexfilter.cpp +++ b/extensions/regexfilter.cpp @@ -1,13 +1,19 @@ #include "qtcommon.h" #include "extension.h" #include "ui_regexfilter.h" +#include "module.h" +#include "blockmarkup.h" +#include extern const char* REGEX_FILTER; extern const char* INVALID_REGEX; extern const char* CURRENT_FILTER; -std::wregex regex; +const char* REGEX_SAVE_FILE = "SavedRegexFilters.txt"; + +std::optional regex; std::shared_mutex m; +std::atomic selectedProcessId; class Window : public QMainWindow { @@ -17,27 +23,52 @@ public: ui.setupUi(this); connect(ui.input, &QLineEdit::textEdited, this, &Window::setRegex); + connect(ui.save, &QPushButton::clicked, this, &Window::saveRegex); setWindowTitle(REGEX_FILTER); QMetaObject::invokeMethod(this, &QWidget::show, Qt::QueuedConnection); } -private: void setRegex(QString regex) { + ui.input->setText(regex); std::lock_guard l(m); - try { ::regex = S(regex); } + if (!regex.isEmpty()) try { ::regex = S(regex); } catch (std::regex_error) { return ui.output->setText(INVALID_REGEX); } + else ::regex = std::nullopt; ui.output->setText(QString(CURRENT_FILTER).arg(regex)); } +private: + void saveRegex() + { + auto formatted = FormatString( + L"\xfeff|PROCESS|%s|FILTER|%s|END|\r\n", + GetModuleFilename(selectedProcessId.load()).value_or(FormatString(L"Error getting name of process 0x%X", selectedProcessId.load())), + S(ui.input->text()) + ); + std::ofstream(REGEX_SAVE_FILE, std::ios::binary | std::ios::app).write((const char*)formatted.c_str(), formatted.size() * sizeof(wchar_t)); + } + Ui::FilterWindow ui; } window; bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) { if (sentenceInfo["text number"] == 0) return false; + if (sentenceInfo["current select"]) + { + selectedProcessId = sentenceInfo["process id"]; + if (!regex) if (auto processName = GetModuleFilename(sentenceInfo["process id"])) + { + std::ifstream stream(REGEX_SAVE_FILE, std::ios::binary); + BlockMarkupIterator savedFilters(stream, Array{ L"|PROCESS|", L"|FILTER|" }); + std::vector regexes; + while (auto read = savedFilters.Next()) if (read->at(0) == processName) regexes.push_back(std::move(read->at(1))); + if (!regexes.empty()) QMetaObject::invokeMethod(&window, [regex = S(regexes.back())]{ window.setRegex(regex); }, Qt::BlockingQueuedConnection); + } + } std::shared_lock l(m); - sentence = std::regex_replace(sentence, regex, L""); + if (regex) sentence = std::regex_replace(sentence, regex.value(), L""); return true; } diff --git a/extensions/regexfilter.ui b/extensions/regexfilter.ui index b679af4..4771596 100644 --- a/extensions/regexfilter.ui +++ b/extensions/regexfilter.ui @@ -7,7 +7,7 @@ 0 0 350 - 76 + 105 @@ -25,6 +25,13 @@ + + + + Save + + +