Skip to content

Commit

Permalink
qt/main: Provide useful error message if file can't be loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonKagstrom committed Jul 7, 2024
1 parent 80e2714 commit cde1e34
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
10 changes: 9 additions & 1 deletion qt/emilpro/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ main(int argc, char* argv[])
}

MainWindow w;
w.Init(argc, argv);

if (argc > 1)
{
if (auto err = w.LoadFile(argv[1]); err)
{
fmt::print("Error loading file: {}\n\n", err);
Usage(argv[0]);
}
}

w.show();

Expand Down
42 changes: 17 additions & 25 deletions qt/emilpro/mainwindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,6 @@ MainWindow::MainWindow(QWidget* parent)
, m_ui(new Ui::MainWindow)
, m_forward_item_delegate(JumpLaneDelegate::Direction::kForward)
, m_backward_item_delegate(JumpLaneDelegate::Direction::kBackward)
{
}

MainWindow::~MainWindow()
{
SaveSettings();

delete m_instruction_view_model;
delete m_symbol_view_model;
delete m_ui;
}

bool
MainWindow::Init(int argc, char* argv[])
{
m_ui->setupUi(this);

Expand Down Expand Up @@ -75,29 +61,29 @@ MainWindow::Init(int argc, char* argv[])
m_ui->sourceTextEdit->setExtraSelections(extras);

m_ui->menuBar->setNativeMenuBar(false);
}

if (argc > 1)
{
LoadFile(argv[1]);
}
MainWindow::~MainWindow()
{
SaveSettings();

return true;
delete m_instruction_view_model;
delete m_symbol_view_model;
delete m_ui;
}

void
const char*
MainWindow::LoadFile(const std::string& filename)
{
auto parser = emilpro::IBinaryParser::FromFile(filename);
if (!parser)
{
// for now
exit(1);
return "parse error";
}
auto disassembler = emilpro::IDisassembler::CreateFromArchitecture(parser->GetMachine());
if (!disassembler)
{
// for now
exit(1);
return "unsupported architecture";
}


Expand Down Expand Up @@ -186,6 +172,8 @@ MainWindow::LoadFile(const std::string& filename)
m_ui->symbolTableView->setCurrentIndex(m_symbol_view_model->index(0, 0));
}
m_visible_symbols = m_database.Symbols();

return nullptr;
}

void
Expand Down Expand Up @@ -296,7 +284,11 @@ MainWindow::on_action_Open_triggered(bool activated)
{
auto filename = QFileDialog::getOpenFileName(this, tr("Open binary"));

LoadFile(filename.toStdString());
if (auto err = LoadFile(filename.toStdString()); err)
{
QMessageBox::critical(
this, "?LOAD ERROR", fmt::format("Cannot load file: {}", err).c_str());
}
}

void
Expand Down
5 changes: 2 additions & 3 deletions qt/emilpro/mainwindow.hh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public:
explicit MainWindow(QWidget* parent = 0);
~MainWindow();

bool Init(int argc, char* argv[]);
/// Parse a file, and return nullptr if successful, otherwise an error string
const char* LoadFile(const std::string& filename);

// On quit etc
void UpdatePreferences();
Expand Down Expand Up @@ -93,8 +94,6 @@ private:

void SetupInfoBox();

void LoadFile(const std::string& filename);

void SaveSettings();

void RestoreSettings();
Expand Down

0 comments on commit cde1e34

Please sign in to comment.