Added utilities

This commit is contained in:
Valentino Orlandi 2022-06-23 04:00:37 +02:00
parent b0230bb9ff
commit 174e693ebe
Signed by: elB4RTO
GPG Key ID: 1719E976DB2D4E71
11 changed files with 250 additions and 14 deletions

View File

@ -21,11 +21,17 @@ set(TS_FILES
set(PROJECT_SOURCES
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.cpp
mainwindow.ui
window/craplog.h
window/craplog.cpp
windows/craplog.h
windows/craplog.cpp
utilities/io.h
utilities/io.cpp
utilities/strings.h
utilities/strings.cpp
utilities/rtf.h
utilities/rtf.cpp
resources/resources.qrc
${TS_FILES}
)

View File

@ -47,13 +47,13 @@ void MainWindow::on_buttonRefreshList_clicked()
// clear the current tree
this->ui->listLogFiles->clear();
// iterate over elements of list
for ( Craplog::LogFile& log_file : this->craplog.getLogsList(true) ) {
for ( const Craplog::LogFile& log_file : this->craplog.getLogsList(true) ) {
// new entry for the tree widget
QTreeWidgetItem * item = new QTreeWidgetItem();
// set unchecked
item->setCheckState(0, Qt::CheckState::Unchecked );
// name to be showed
item-> setText( 0, log_file.name );
item->setText( 0, log_file.name );
item->setFont( 0, this->FONTS["main"] );
// size to be showed
float size = (float)log_file.size / 1024;
@ -122,7 +122,11 @@ void MainWindow::on_checkAllLogFiles_stateChanged(int arg1)
void MainWindow::on_buttonViewFile_clicked()
{
QString file_name = this->ui->listLogFiles->selectedItems().takeFirst()->text(0);
std::string file_path = this->craplog.getLogFilePath( file_name );
std::string content = IOutils::readFile( file_path );
this->ui->textLogFiles->setText( RichText::enrichLogs( content ) );
this->ui->textLogFiles->setFont( this->FONTS["main"] );
}

View File

@ -1,13 +1,16 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFontDatabase>
#include <QMessageBox>
#include "qmainwindow.h"
#include "qfontdatabase.h"
#include "qmessagebox.h"
#include "./window/craplog.h"
#include "qtreewidget.h"
#include "utilities/io.h"
#include "utilities/rtf.h"
#include "windows/craplog.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
@ -39,8 +42,6 @@ private:
std::unordered_map<std::string, QColor> COLORS;
std::unordered_map<std::string, QFont> FONTS;
QString font_name;
int font_size, font_size_big, font_size_small;
QFont main_font;
};
#endif // MAINWINDOW_H

25
craplog/utilities/io.cpp Normal file
View File

@ -0,0 +1,25 @@
#include "io.h"
IOutils::IOutils()
{
}
auto IOutils::readFile( std::string path ) -> std::string
{
constexpr std::size_t read_size = std::size_t(4096);
std::ifstream file = std::ifstream(path.data());
file.exceptions(std::ios_base::badbit);
std::string content = std::string();
std::string buf = std::string(read_size, '\0');
while (file.read(& buf[0], read_size)) {
content.append(buf, 0, file.gcount());
}
content.append(buf, 0, file.gcount());
file.close();
return content;
}

17
craplog/utilities/io.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef IO_H
#define IO_H
#include <string>
#include <iostream>
#include <fstream>
class IOutils
{
public:
IOutils();
static std::string readFile( std::string path );
};
#endif // IOUTILS_H

56
craplog/utilities/rtf.cpp Normal file
View File

@ -0,0 +1,56 @@
#include "rtf.h"
RichText::RichText()
{
}
QString RichText::enrichLogs( std::string content, int color_scheme )
{
QString style;
switch ( color_scheme ) {
case 1:
// breeze
style = QString("<style>"
"body{background-color:#ffffff}"
"p{color:#9c9c9b}"
"span.ip{color:#644a9b}"
"span.pt{color:#d5bc79}"
"span.date{color:#d685d6}"
"span.ua{color:#006e28}"
"span.req_err{color:#54b8ff}"
"span.res_lev{color:#d24f4f}"
"<style/>");
case 2:
// monokai
style = QString("<style>"
"body{background-color:#272822}"
"p{color:#706c5a}"
"span.ip{color:#57adbc}"
"span.pt{color:#c1b864}"
"span.date{color:#9773db}"
"span.ua{color:#a6e22e}"
"span.req_err{color:#bebeb8}"
"span.res_lev{color:#f92672}"
"<style/>");
case 3:
// radical
style = QString("<style>"
"body{background-color:#141322}"
"p{color:#749295}"
"span.ip{color:#fda8bc}"
"span.pt{color:#ff85a1}"
"span.date{color:#7c9c9e}"
"span.ua{color:#42a784}"
"span.req_err{color:#d5358f}"
"span.res_lev{color:#56e8e4}"
"<style/>");
default:
style = "";
}
QString rich_content = QString("<html><head>%1<head/><body>")
.arg( style );
return rich_content;
}

15
craplog/utilities/rtf.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef RTF_H
#define RTF_H
#include "qfont.h"
#include "qstring.h"
class RichText
{
public:
RichText();
static QString enrichLogs( std::string content, int color_scheme=0 );
};
#endif // RICHTEXT_H

View File

@ -0,0 +1,92 @@
#include "strings.h"
StringOp::StringOp()
{
}
std::string StringOp::strip( std::string str, std::string chars )
{
std::string stripped;
stripped = StringOp::lstrip( str, chars );
stripped = StringOp::rstrip( str, chars );
return stripped;
}
std::string StringOp::lstrip( std::string str, std::string chars )
{
bool found = true;
int i = 0;
while ( i < str.size() ) {
found = false;
char str_index = str[i];
for ( const char& chr : chars ) {
if ( str_index == chr ) {
found = true;
break;
}
}
if ( found == false ) {
break;
}
i++;
}
std::string stripped = "";
if ( i < str.size() ) {
stripped = str.substr( i );
}
return stripped;
}
std::string StringOp::rstrip( std::string str, std::string chars )
{
bool found = true;
int i = str.size() - 1;
while ( i >= 0 ) {
found = false;
char str_index = str[i];
for ( const char& chr : chars ) {
if ( str_index == chr ) {
found = true;
break;
}
}
if ( found == false ) {
break;
}
i--;
}
std::string stripped = "";
if ( i > 0 ) {
stripped = str.substr( 0, str.size() - i );
}
return stripped;
}
std::vector<std::string> StringOp::split( std::string str, std::string sep )
{
std::vector<std::string> splitted;
std::string slice;
int start=0, stop=0;
while (true) {
stop = str.find( sep );
if ( stop >= str.size() ) {
slice = str.substr( start );
if ( slice.empty() == false ) {
splitted.push_back( slice );
}
break;
} else {
slice = str.substr( start, stop-start );
if ( slice.empty() == false ) {
splitted.push_back( slice );
}
start = stop+sep.size();
}
}
}

View File

@ -0,0 +1,20 @@
#ifndef STRINGS_H
#define STRINGS_H
#include <string>
#include <vector>
#include "qstring.h"
class StringOp
{
public:
StringOp();
std::string strip( std::string str, std::string chars=" \n\t\b\r\v" );
std::string lstrip( std::string str, std::string chars=" \n\t\b\r\v" );
std::string rstrip( std::string str, std::string chars=" \n\t\b\r\v" );
std::vector<std::string> split( std::string str, std::string sep );
};
#endif // STRINGOP_H

View File

@ -27,7 +27,7 @@ std::vector<Craplog::LogFile> Craplog::getLogsList( bool fresh )
std::string Craplog::getLogFilePath( QString file_name )
{
std::string path;
for ( Craplog::LogFile& item : this->logs_list ) {
for ( const Craplog::LogFile& item : this->logs_list ) {
if ( item.name == file_name ) {
path = item.path;
break;
@ -55,7 +55,7 @@ void Craplog::scanLogsDir()
{
this->logs_list.clear();
// iterate over entries in the logs folder
for (auto const& dir_entry : std::filesystem::directory_iterator{this->logs_path}) {
for ( const std::filesystem::directory_entry& dir_entry : std::filesystem::directory_iterator{this->logs_path}) {
// get the attributes
int size = dir_entry.file_size();
std::string path = dir_entry.path().string();