From cde1e349f1de1d5b2c9c55d74059abe370cd3b7b Mon Sep 17 00:00:00 2001 From: Simon Kagstrom Date: Sun, 7 Jul 2024 12:45:49 +0200 Subject: [PATCH] qt/main: Provide useful error message if file can't be loaded --- qt/emilpro/main.cc | 10 +++++++++- qt/emilpro/mainwindow.cc | 42 ++++++++++++++++------------------------ qt/emilpro/mainwindow.hh | 5 ++--- 3 files changed, 28 insertions(+), 29 deletions(-) diff --git a/qt/emilpro/main.cc b/qt/emilpro/main.cc index 0226d39..08a490a 100644 --- a/qt/emilpro/main.cc +++ b/qt/emilpro/main.cc @@ -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(); diff --git a/qt/emilpro/mainwindow.cc b/qt/emilpro/mainwindow.cc index 2292ab3..89261cf 100644 --- a/qt/emilpro/mainwindow.cc +++ b/qt/emilpro/mainwindow.cc @@ -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); @@ -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"; } @@ -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 @@ -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 diff --git a/qt/emilpro/mainwindow.hh b/qt/emilpro/mainwindow.hh index bc68a47..f41ad0d 100644 --- a/qt/emilpro/mainwindow.hh +++ b/qt/emilpro/mainwindow.hh @@ -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(); @@ -93,8 +94,6 @@ private: void SetupInfoBox(); - void LoadFile(const std::string& filename); - void SaveSettings(); void RestoreSettings();