summaryrefslogtreecommitdiff
path: root/Userland/Applications/HexEditor
diff options
context:
space:
mode:
authorJames Puleo <james@jame.xyz>2022-07-28 03:34:15 -0400
committerTim Flynn <trflynn89@pm.me>2022-08-03 10:12:11 -0400
commit88cf40179dc8f6e977b838bc046f53155d38b9e4 (patch)
tree88bfb742cf2002b6d01b6a34062d7ac841003583 /Userland/Applications/HexEditor
parent035d63f5280c9ecc5241770841ccc782991922c2 (diff)
downloadserenity-88cf40179dc8f6e977b838bc046f53155d38b9e4.zip
HexEditor: Make `HexEditor::open_new_file` fallible and reduce branching
Returning a `bool` is meaningless, so let's make it more expresive :^)
Diffstat (limited to 'Userland/Applications/HexEditor')
-rw-r--r--Userland/Applications/HexEditor/HexEditor.cpp11
-rw-r--r--Userland/Applications/HexEditor/HexEditor.h2
-rw-r--r--Userland/Applications/HexEditor/HexEditorWidget.cpp20
3 files changed, 15 insertions, 18 deletions
diff --git a/Userland/Applications/HexEditor/HexEditor.cpp b/Userland/Applications/HexEditor/HexEditor.cpp
index d27c7f9cd7..addf4047fe 100644
--- a/Userland/Applications/HexEditor/HexEditor.cpp
+++ b/Userland/Applications/HexEditor/HexEditor.cpp
@@ -49,14 +49,9 @@ HexEditor::HexEditor()
m_blink_timer->start();
}
-bool HexEditor::open_new_file(size_t size)
+ErrorOr<void> HexEditor::open_new_file(size_t size)
{
- auto maybe_buffer = ByteBuffer::create_zeroed(size);
- if (maybe_buffer.is_error()) {
- return false;
- }
-
- m_document = make<HexDocumentMemory>(maybe_buffer.release_value());
+ m_document = make<HexDocumentMemory>(TRY(ByteBuffer::create_zeroed(size)));
set_content_length(m_document->size());
m_position = 0;
m_cursor_at_low_nibble = false;
@@ -66,7 +61,7 @@ bool HexEditor::open_new_file(size_t size)
update();
update_status();
- return true;
+ return {};
}
void HexEditor::open_file(NonnullRefPtr<Core::File> file)
diff --git a/Userland/Applications/HexEditor/HexEditor.h b/Userland/Applications/HexEditor/HexEditor.h
index 233a251e73..aafef92d00 100644
--- a/Userland/Applications/HexEditor/HexEditor.h
+++ b/Userland/Applications/HexEditor/HexEditor.h
@@ -33,7 +33,7 @@ public:
virtual ~HexEditor() override = default;
size_t buffer_size() const { return m_document->size(); }
- bool open_new_file(size_t size);
+ ErrorOr<void> open_new_file(size_t size);
void open_file(NonnullRefPtr<Core::File> file);
void fill_selection(u8 fill_byte);
Optional<u8> get_byte(size_t position);
diff --git a/Userland/Applications/HexEditor/HexEditorWidget.cpp b/Userland/Applications/HexEditor/HexEditorWidget.cpp
index d04abf1313..16b88d02fa 100644
--- a/Userland/Applications/HexEditor/HexEditorWidget.cpp
+++ b/Userland/Applications/HexEditor/HexEditorWidget.cpp
@@ -95,17 +95,19 @@ HexEditorWidget::HexEditorWidget()
m_new_action = GUI::Action::create("New", { Mod_Ctrl, Key_N }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/new.png"sv).release_value_but_fixme_should_propagate_errors(), [this](const GUI::Action&) {
String value;
if (request_close() && GUI::InputBox::show(window(), value, "Enter new file size:"sv, "New file size"sv) == GUI::InputBox::ExecResult::OK && !value.is_empty()) {
- auto file_size = value.to_int();
- if (file_size.has_value() && file_size.value() > 0) {
- window()->set_modified(false);
- if (!m_editor->open_new_file(file_size.value())) {
- GUI::MessageBox::show(window(), "Entered file size is too large."sv, "Error"sv, GUI::MessageBox::Type::Error);
- return;
- }
- set_path({});
- } else {
+ auto file_size = value.to_uint();
+ if (!file_size.has_value()) {
GUI::MessageBox::show(window(), "Invalid file size entered."sv, "Error"sv, GUI::MessageBox::Type::Error);
+ return;
}
+
+ if (auto error = m_editor->open_new_file(file_size.value()); error.is_error()) {
+ GUI::MessageBox::show(window(), String::formatted("Unable to open new file: {}"sv, error.error()), "Error"sv, GUI::MessageBox::Type::Error);
+ return;
+ }
+
+ set_path({});
+ window()->set_modified(false);
}
});