Play-/tools/ElfView/mainwindow.cpp

67 lines
1.5 KiB
C++
Raw Permalink Normal View History

2022-03-16 16:53:43 -04:00
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ElfViewEx.h"
#include "DebugSupportSettings.h"
#include "AppConfig.h"
#include "StdStreamUtils.h"
2022-03-16 16:53:43 -04:00
#include <QFileDialog>
#include <QMessageBox>
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
CDebugSupportSettings::GetInstance().Initialize(&CAppConfig::GetInstance());
2022-04-10 10:52:53 -04:00
2022-03-16 16:53:43 -04:00
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionOpen_triggered()
{
QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::ExistingFile);
dialog.setNameFilter(tr("ELF files (*.elf;*.o);;All files(*.*)"));
2022-03-16 16:53:43 -04:00
if(dialog.exec())
{
try
{
auto selectedPath = dialog.selectedFiles().first();
auto view = CreateElfViewFromPath(ui->mdiArea, fs::path(selectedPath.toStdString()));
2022-03-16 16:53:43 -04:00
view->show();
}
catch(const std::exception& ex)
{
auto errorMessage = QString("Failed to open ELF: %1").arg(ex.what());
QMessageBox messageBox;
messageBox.critical(this, "Error", errorMessage);
}
}
}
void MainWindow::on_actionExit_triggered()
{
close();
}
QMdiSubWindow* MainWindow::CreateElfViewFromPath(QMdiArea* mdiArea, const fs::path& elfPath)
{
uint8 headerId[0x10] = {};
auto stream = Framework::CreateInputStdStream(elfPath.native());
stream.Read(headerId, sizeof(headerId));
switch(headerId[ELF::EI_CLASS])
{
case ELF::ELFCLASS32:
return new CElfViewEx<CELF32>(mdiArea, elfPath);
case ELF::ELFCLASS64:
return new CElfViewEx<CELF64>(mdiArea, elfPath);
default:
throw std::runtime_error("Unknown ELF class type.");
}
}