Tools: Add simple tool to triage daca results

This commit is contained in:
Daniel Marjamäki 2018-01-01 09:22:11 +01:00
parent 1f3ca7ddf6
commit 4c490b599b
9 changed files with 467 additions and 2 deletions

View File

@ -32,7 +32,9 @@ $ASTYLE $style $options test/*.cpp
$ASTYLE $style $options test/cfg/*.c*
$ASTYLE $style $options test/*.h
$ASTYLE $style $options tools/*.cpp
$ASTYLE $style $options --recursive "tools/*.cpp"
$ASTYLE $style $options --recursive "tools/*.h"
$ASTYLE $style $options --recursive "samples/*.c"
$ASTYLE $style $options --recursive "samples/*.cpp"

View File

@ -15,6 +15,7 @@ astyle %STYLE% %OPTIONS% lib/*.h
astyle %STYLE% %OPTIONS% test/*.cpp
astyle %STYLE% %OPTIONS% test/cfg/*.cpp
astyle %STYLE% %OPTIONS% test/*.h
astyle %STYLE% %OPTIONS% tools/*.cpp
astyle %STYLE% %OPTIONS% -r tools/*.cpp
astyle %STYLE% %OPTIONS% -r tools/*.h
astyle %STYLE% %OPTIONS% -r samples/*.c
astyle %STYLE% %OPTIONS% -r samples/*.cpp

View File

@ -0,0 +1,111 @@
#include <QtWidgets>
#include "codeeditor.h"
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
{
lineNumberArea = new LineNumberArea(this);
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
updateLineNumberAreaWidth(0);
highlightCurrentLine();
}
int CodeEditor::lineNumberAreaWidth()
{
int digits = 1;
int max = qMax(1, blockCount());
while (max >= 10) {
max /= 10;
++digits;
}
int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits;
return space;
}
void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */)
{
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
}
void CodeEditor::updateLineNumberArea(const QRect &rect, int dy)
{
if (dy)
lineNumberArea->scroll(0, dy);
else
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
if (rect.contains(viewport()->rect()))
updateLineNumberAreaWidth(0);
}
void CodeEditor::resizeEvent(QResizeEvent *e)
{
QPlainTextEdit::resizeEvent(e);
QRect cr = contentsRect();
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
}
void CodeEditor::highlightCurrentLine()
{
QList<QTextEdit::ExtraSelection> extraSelections;
if (!isReadOnly()) {
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(Qt::yellow).lighter(160);
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = textCursor();
selection.cursor.clearSelection();
extraSelections.append(selection);
}
setExtraSelections(extraSelections);
}
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
{
QPainter painter(lineNumberArea);
painter.fillRect(event->rect(), Qt::lightGray);
QTextBlock block = firstVisibleBlock();
int blockNumber = block.blockNumber();
int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
int bottom = top + (int) blockBoundingRect(block).height();
while (block.isValid() && top <= event->rect().bottom()) {
if (block.isVisible() && bottom >= event->rect().top()) {
QString number = QString::number(blockNumber + 1);
painter.setPen(Qt::black);
painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
Qt::AlignRight, number);
}
block = block.next();
top = bottom;
bottom = top + (int) blockBoundingRect(block).height();
++blockNumber;
}
}

View File

@ -0,0 +1,58 @@
#ifndef CODEEDITOR_H
#define CODEEDITOR_H
#include <QPlainTextEdit>
#include <QObject>
class QPaintEvent;
class QResizeEvent;
class QSize;
class QWidget;
class LineNumberArea;
class CodeEditor : public QPlainTextEdit
{
Q_OBJECT
public:
CodeEditor(QWidget *parent = 0);
void lineNumberAreaPaintEvent(QPaintEvent *event);
int lineNumberAreaWidth();
protected:
void resizeEvent(QResizeEvent *event) override;
private slots:
void updateLineNumberAreaWidth(int newBlockCount);
void highlightCurrentLine();
void updateLineNumberArea(const QRect &, int);
private:
QWidget *lineNumberArea;
};
class LineNumberArea : public QWidget
{
public:
LineNumberArea(CodeEditor *editor) : QWidget(editor) {
codeEditor = editor;
}
QSize sizeHint() const override {
return QSize(codeEditor->lineNumberAreaWidth(), 0);
}
protected:
void paintEvent(QPaintEvent *event) override {
codeEditor->lineNumberAreaPaintEvent(event);
}
private:
CodeEditor *codeEditor;
};
#endif // CODEEDITOR_H

View File

@ -0,0 +1,11 @@
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

View File

@ -0,0 +1,97 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QClipboard>
#include <QProcess>
#include <QFile>
#include <QTextStream>
#include <QDir>
#include <QFileInfo>
const QString WORK_FOLDER(QDir::homePath() + "/triage");
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::pasteResults()
{
ui->results->clear();
const QStringList lines = QApplication::clipboard()->text().split("\n");
QString url;
foreach (const QString line, lines) {
if (line.startsWith("ftp://"))
url = line;
else if (!url.isEmpty()) {
ui->results->addItem(url + "\n" + line);
url.clear();
}
}
}
void MainWindow::showResult(QListWidgetItem *item)
{
if (!item->text().startsWith("ftp://"))
return;
const QStringList lines = item->text().split("\n");
if (lines.size() != 2)
return;
const QString url = lines[0];
const QString msg = lines[1];
const QString archiveName = url.mid(url.lastIndexOf("/") + 1);
const int pos1 = msg.indexOf(":");
const int pos2 = msg.indexOf(":", pos1+1);
const QString fileName = msg.left(msg.indexOf(":"));
const int lineNumber = msg.mid(pos1+1,pos2-pos1-1).toInt();
QProcess process;
process.setWorkingDirectory(WORK_FOLDER);
// Download archive
if (!QFileInfo(WORK_FOLDER + '/' + archiveName).exists()) {
process.start("wget", QStringList()<<url);
if (!process.waitForFinished(-1))
return;
}
// Unpack archive
QStringList args;
if (url.endsWith(".tar.gz"))
args << "xzvf";
else if (url.endsWith(".tar.bz2"))
args << "xjvf";
else if (url.endsWith(".tar.xz"))
args << "xJvf";
args << archiveName;
process.start("tar", args);
if (!process.waitForFinished(-1))
return;
// Open file
ui->code->setFocus();
QFile f(WORK_FOLDER + '/' + fileName);
f.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream textStream(&f);
const QString fileData = textStream.readAll();
ui->code->setPlainText(fileData);
for (int pos = 0, line = 1; pos < fileData.size(); ++pos) {
if (fileData[pos] == '\n') {
++line;
if (line == lineNumber) {
QTextCursor textCursor = ui->code->textCursor();
textCursor.setPosition(pos+1);
ui->code->setTextCursor(textCursor);
ui->code->centerCursor();
break;
}
}
}
}

View File

@ -0,0 +1,27 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QListWidgetItem>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void pasteResults();
void showResult(QListWidgetItem *item);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

View File

@ -0,0 +1,125 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>683</width>
<height>465</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="pasteResults">
<property name="text">
<string>Paste results</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="QListWidget" name="results"/>
</item>
</layout>
</item>
<item>
<widget class="CodeEditor" name="code">
<property name="readOnly">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>683</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>CodeEditor</class>
<extends>QPlainTextEdit</extends>
<header>codeeditor.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections>
<connection>
<sender>pasteResults</sender>
<signal>clicked()</signal>
<receiver>MainWindow</receiver>
<slot>pasteResults()</slot>
<hints>
<hint type="sourcelabel">
<x>86</x>
<y>68</y>
</hint>
<hint type="destinationlabel">
<x>135</x>
<y>68</y>
</hint>
</hints>
</connection>
<connection>
<sender>results</sender>
<signal>itemDoubleClicked(QListWidgetItem*)</signal>
<receiver>MainWindow</receiver>
<slot>showResult(QListWidgetItem*)</slot>
<hints>
<hint type="sourcelabel">
<x>28</x>
<y>104</y>
</hint>
<hint type="destinationlabel">
<x>5</x>
<y>104</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>pasteResults()</slot>
<slot>showResult(QListWidgetItem*)</slot>
</slots>
</ui>

View File

@ -0,0 +1,33 @@
#-------------------------------------------------
#
# Project created by QtCreator 2017-12-31T22:22:56
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = triage
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += main.cpp\
mainwindow.cpp \
codeeditor.cpp
HEADERS += mainwindow.h \
codeeditor.h
FORMS += mainwindow.ui