Textractor_test/extensions/regexfilter.cpp

73 lines
2.5 KiB
C++
Raw Normal View History

2019-06-29 11:16:31 +08:00
#include "qtcommon.h"
#include "extension.h"
#include "ui_regexfilter.h"
2020-02-28 17:33:51 +08:00
#include "module.h"
#include "blockmarkup.h"
#include <fstream>
2019-02-28 00:33:17 +08:00
extern const char* REGEX_FILTER;
extern const char* INVALID_REGEX;
extern const char* CURRENT_FILTER;
2020-02-28 17:33:51 +08:00
const char* REGEX_SAVE_FILE = "SavedRegexFilters.txt";
std::optional<std::wregex> regex;
std::wstring replace = L"$1";
2021-06-05 16:58:53 +08:00
concurrency::reader_writer_lock m;
2021-11-06 18:53:31 +08:00
DWORD (*GetSelectedProcessId)() = [] { return 0UL; };
class Window : public QDialog, Localizer
{
2019-06-21 13:29:48 +08:00
public:
2021-01-31 03:07:37 +08:00
Window() : QDialog(nullptr, Qt::WindowMinMaxButtonsHint)
{
ui.setupUi(this);
2019-06-21 13:29:48 +08:00
connect(ui.regexEdit, &QLineEdit::textEdited, this, &Window::SetRegex);
connect(ui.saveButton, &QPushButton::clicked, this, &Window::Save);
2019-06-21 13:29:48 +08:00
setWindowTitle(REGEX_FILTER);
2019-06-21 13:29:48 +08:00
QMetaObject::invokeMethod(this, &QWidget::show, Qt::QueuedConnection);
}
void SetRegex(QString regex)
{
ui.regexEdit->setText(regex);
std::scoped_lock lock(m);
2020-02-28 17:33:51 +08:00
if (!regex.isEmpty()) try { ::regex = S(regex); }
2019-06-21 13:29:48 +08:00
catch (std::regex_error) { return ui.output->setText(INVALID_REGEX); }
2020-02-28 17:33:51 +08:00
else ::regex = std::nullopt;
2019-06-29 11:16:31 +08:00
ui.output->setText(QString(CURRENT_FILTER).arg(regex));
}
2019-06-21 13:29:48 +08:00
2020-02-28 17:33:51 +08:00
private:
void Save()
2020-02-28 17:33:51 +08:00
{
auto formatted = FormatString(
2021-01-31 04:11:46 +08:00
L"\xfeff|PROCESS|%s|FILTER|%s|END|\r\n",
2020-03-03 14:49:31 +08:00
GetModuleFilename(GetSelectedProcessId()).value_or(FormatString(L"Error getting name of process 0x%X", GetSelectedProcessId())),
2021-01-31 04:11:46 +08:00
S(ui.regexEdit->text())
2020-02-28 17:33:51 +08:00
);
std::ofstream(REGEX_SAVE_FILE, std::ios::binary | std::ios::app).write((const char*)formatted.c_str(), formatted.size() * sizeof(wchar_t));
}
2019-06-21 13:29:48 +08:00
Ui::FilterWindow ui;
} window;
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
{
2021-08-18 14:22:23 +08:00
static auto _ = GetSelectedProcessId = (DWORD(*)())sentenceInfo["get selected process id"];
2019-02-22 02:09:44 +08:00
if (sentenceInfo["text number"] == 0) return false;
2021-03-08 23:41:34 +08:00
if (/*sentenceInfo["current select"] && */!regex) if (auto processName = GetModuleFilename(sentenceInfo["process id"]))
2020-02-28 17:33:51 +08:00
{
2020-03-03 14:49:31 +08:00
std::ifstream stream(REGEX_SAVE_FILE, std::ios::binary);
2021-01-31 04:11:46 +08:00
BlockMarkupIterator savedFilters(stream, Array<std::wstring_view>{ L"|PROCESS|", L"|FILTER|" });
std::vector<std::wstring> 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, std::bind(&Window::SetRegex, &window, S(regexes.back())), Qt::BlockingQueuedConnection);
2020-02-28 17:33:51 +08:00
}
2021-06-05 16:58:53 +08:00
concurrency::reader_writer_lock::scoped_lock_read readLock(m);
if (regex) sentence = std::regex_replace(sentence, regex.value(), replace);
return true;
}