163 lines
4.8 KiB
C++
163 lines
4.8 KiB
C++
#include "mainwidget.h"
|
|
#include "./ui_mainwidget.h"
|
|
#include <QDir>
|
|
#include <QMessageBox>
|
|
#include <regex>
|
|
|
|
CTryReg::CTryReg(QWidget* parent) : QWidget(parent), ui(new Ui::CTryReg)
|
|
{
|
|
ui->setupUi(this);
|
|
|
|
setWindowTitle("STD_REG 1.0");
|
|
ui->edRet->setReadOnly(true);
|
|
connect(ui->btnSearch, &QPushButton::clicked, this, &CTryReg::reg_search);
|
|
connect(ui->btnMatch, &QPushButton::clicked, this, &CTryReg::reg_match);
|
|
connect(ui->btnIterator, &QPushButton::clicked, this, &CTryReg::reg_iterator);
|
|
connect(ui->btnExit, &QPushButton::clicked, this, &CTryReg::close);
|
|
connect(ui->btnOpenHis, &QPushButton::clicked, this, &CTryReg::open_his);
|
|
|
|
QString home = QDir::homePath();
|
|
QDir dir(home);
|
|
his_path_ = dir.absoluteFilePath(".config/std_reg");
|
|
QDir path(his_path_);
|
|
if (!path.exists()) {
|
|
path.mkpath(his_path_);
|
|
}
|
|
his_path_ = his_path_ + "/history.txt";
|
|
}
|
|
|
|
CTryReg::~CTryReg()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void CTryReg::reg_search()
|
|
{
|
|
ui->listWidget->clear();
|
|
ui->edRet->clear();
|
|
std::string pattern = ui->edReg->text().toStdString();
|
|
std::string str = ui->plainTextEdit->toPlainText().toStdString();
|
|
|
|
if (pattern.empty() || str.empty()) {
|
|
return;
|
|
}
|
|
try {
|
|
std::smatch match;
|
|
std::regex rg(pattern);
|
|
if (std::regex_search(str, match, rg)) {
|
|
ui->edRet->setText("Success.");
|
|
for (const auto& m : match) {
|
|
ui->listWidget->addItem(QString::fromStdString(m.str()));
|
|
}
|
|
} else {
|
|
ui->edRet->setText("Failed.");
|
|
}
|
|
} catch (const std::exception& e) {
|
|
ui->edRet->setText(QString::fromStdString(e.what()));
|
|
}
|
|
append_his(QString::fromStdString(str), QString::fromStdString(pattern));
|
|
}
|
|
|
|
void CTryReg::reg_match()
|
|
{
|
|
ui->listWidget->clear();
|
|
ui->edRet->clear();
|
|
std::string pattern = ui->edReg->text().toStdString();
|
|
std::string str = ui->plainTextEdit->toPlainText().toStdString();
|
|
|
|
if (pattern.empty() || str.empty()) {
|
|
return;
|
|
}
|
|
try {
|
|
std::regex rg(pattern);
|
|
if (std::regex_match(str, rg)) {
|
|
ui->edRet->setText("Success.");
|
|
} else {
|
|
ui->edRet->setText("Failed.");
|
|
}
|
|
} catch (const std::exception& e) {
|
|
ui->edRet->setText(QString::fromStdString(e.what()));
|
|
}
|
|
append_his(QString::fromStdString(str), QString::fromStdString(pattern));
|
|
}
|
|
|
|
void CTryReg::reg_iterator()
|
|
{
|
|
ui->listWidget->clear();
|
|
ui->edRet->clear();
|
|
std::string pattern = ui->edReg->text().toStdString();
|
|
std::string str = ui->plainTextEdit->toPlainText().toStdString();
|
|
|
|
if (pattern.empty() || str.empty()) {
|
|
return;
|
|
}
|
|
try {
|
|
std::regex rg(pattern);
|
|
auto begin = std::sregex_iterator(str.begin(), str.end(), rg);
|
|
auto end = std::sregex_iterator();
|
|
if (begin != end) {
|
|
ui->edRet->setText("Success.");
|
|
while (begin != end) {
|
|
for (const auto& item : *begin) {
|
|
ui->listWidget->addItem(QString::fromStdString(item.str()));
|
|
}
|
|
++begin;
|
|
}
|
|
} else {
|
|
ui->edRet->setText("Failed.");
|
|
}
|
|
} catch (const std::exception& e) {
|
|
ui->edRet->setText(QString::fromStdString(e.what()));
|
|
}
|
|
append_his(QString::fromStdString(str), QString::fromStdString(pattern));
|
|
}
|
|
|
|
bool CTryReg::append_his(const QString& da, const QString& db)
|
|
{
|
|
QFile file(his_path_);
|
|
if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) {
|
|
return false;
|
|
}
|
|
|
|
QTextStream in(&file);
|
|
QString content = in.readAll();
|
|
|
|
QString search_pattern = "【字符串】\n" + da + "\n【表达式】\n" + db + "\n";
|
|
if (content.contains(search_pattern)) {
|
|
file.close();
|
|
return true;
|
|
}
|
|
|
|
QTextStream out(&file);
|
|
out.setDevice(&file); // 重新设置输出流到文件末尾
|
|
QString currentDateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
|
|
out << currentDateTime << "\n";
|
|
out << "【字符串】\n" << da << "\n";
|
|
out << "【表达式】\n" << db << "\n\n";
|
|
file.close();
|
|
return true;
|
|
}
|
|
|
|
void CTryReg::open_his()
|
|
{
|
|
if (!QFileInfo::exists(his_path_)) {
|
|
QMessageBox::warning(nullptr, "错误", "文件不存在:" + his_path_);
|
|
return;
|
|
}
|
|
|
|
#ifdef Q_OS_WIN
|
|
// Windows 系统使用 "notepad" 或者直接调用文件
|
|
process.start("notepad", QStringList() << his_path_);
|
|
#elif defined(Q_OS_MAC)
|
|
// macOS 系统使用 "open" 命令
|
|
process.start("open", QStringList() << his_path_);
|
|
#else
|
|
// Linux 或其他类 Unix 系统使用 "xdg-open"
|
|
process.start("xdg-open", QStringList() << his_path_);
|
|
#endif
|
|
// 检查是否启动成功
|
|
if (!process.waitForStarted()) {
|
|
QMessageBox::warning(nullptr, "错误", "无法启动系统编辑器!");
|
|
}
|
|
}
|