summaryrefslogtreecommitdiff
path: root/Userland/Applications
diff options
context:
space:
mode:
authorLucas CHOLLET <lucas.chollet@free.fr>2023-01-14 21:47:50 -0500
committerAndrew Kaster <andrewdkaster@gmail.com>2023-02-04 17:20:31 -0700
commit3d914247ccf73a273649923b795d00c96521ccf1 (patch)
tree94b968330b737bbeb708963418a88bd2b9118c56 /Userland/Applications
parentb1d8404c92c79b31643282a64cf2462bf583c1f9 (diff)
downloadserenity-3d914247ccf73a273649923b795d00c96521ccf1.zip
HexEditor: Port to `Core::Stream`
Diffstat (limited to 'Userland/Applications')
-rw-r--r--Userland/Applications/HexEditor/HexDocument.cpp57
-rw-r--r--Userland/Applications/HexEditor/HexDocument.h14
-rw-r--r--Userland/Applications/HexEditor/HexEditor.cpp10
-rw-r--r--Userland/Applications/HexEditor/HexEditor.h4
-rw-r--r--Userland/Applications/HexEditor/HexEditorWidget.cpp24
-rw-r--r--Userland/Applications/HexEditor/HexEditorWidget.h2
-rw-r--r--Userland/Applications/HexEditor/main.cpp4
7 files changed, 58 insertions, 57 deletions
diff --git a/Userland/Applications/HexEditor/HexDocument.cpp b/Userland/Applications/HexEditor/HexDocument.cpp
index 07dfd8bdd4..8498e733e4 100644
--- a/Userland/Applications/HexEditor/HexDocument.cpp
+++ b/Userland/Applications/HexEditor/HexDocument.cpp
@@ -5,6 +5,7 @@
*/
#include "HexDocument.h"
+#include <LibCore/Stream.h>
void HexDocument::set(size_t position, u8 value)
{
@@ -57,20 +58,20 @@ void HexDocumentMemory::clear_changes()
m_changes.clear();
}
-bool HexDocumentMemory::write_to_file(NonnullRefPtr<Core::File> file)
+bool HexDocumentMemory::write_to_file(Core::Stream::File& file)
{
- if (!file->seek(0))
+ if (file.seek(0, SeekMode::SetPosition).is_error())
return false;
- if (!file->write(m_buffer.data(), m_buffer.size()))
+ if (file.write(m_buffer).is_error())
return false;
for (auto& change : m_changes) {
- file->seek(change.key, SeekMode::SetPosition);
- file->write(&change.value, 1);
+ file.seek(change.key, SeekMode::SetPosition).release_value_but_fixme_should_propagate_errors();
+ file.write({ &change.value, 1 }).release_value_but_fixme_should_propagate_errors();
}
return true;
}
-ErrorOr<NonnullOwnPtr<HexDocumentFile>> HexDocumentFile::create(NonnullRefPtr<Core::File> file)
+ErrorOr<NonnullOwnPtr<HexDocumentFile>> HexDocumentFile::create(NonnullOwnPtr<Core::Stream::File> file)
{
auto document = TRY(adopt_nonnull_own_or_enomem(new HexDocumentFile(move(file))));
// FIXME: Remove this hackery
@@ -79,42 +80,43 @@ ErrorOr<NonnullOwnPtr<HexDocumentFile>> HexDocumentFile::create(NonnullRefPtr<Co
return document;
}
-HexDocumentFile::HexDocumentFile(NonnullRefPtr<Core::File> file)
- : m_file(file)
+HexDocumentFile::HexDocumentFile(NonnullOwnPtr<Core::Stream::File> file)
+ : m_file(move(file))
{
}
void HexDocumentFile::write_to_file()
{
for (auto& change : m_changes) {
- m_file->seek(change.key, SeekMode::SetPosition);
- m_file->write(&change.value, 1);
+ m_file->seek(change.key, SeekMode::SetPosition).release_value_but_fixme_should_propagate_errors();
+ m_file->write({ &change.value, 1 }).release_value_but_fixme_should_propagate_errors();
}
clear_changes();
// make sure the next get operation triggers a read
m_buffer_file_pos = m_file_size + 1;
}
-bool HexDocumentFile::write_to_file(NonnullRefPtr<Core::File> file)
+bool HexDocumentFile::write_to_file(Core::Stream::File& file)
{
- if (!file->truncate(size())) {
+ if (file.truncate(size()).is_error()) {
return false;
}
- if (!file->seek(0) || !m_file->seek(0)) {
+ if (file.seek(0, SeekMode::SetPosition).is_error() || m_file->seek(0, SeekMode::SetPosition).is_error()) {
return false;
}
while (true) {
- auto copy_buffer = m_file->read(64 * KiB);
+ Array<u8, 64 * KiB> buffer;
+ auto copy_buffer = m_file->read(buffer).release_value_but_fixme_should_propagate_errors();
if (copy_buffer.size() == 0)
break;
- file->write(copy_buffer.data(), copy_buffer.size());
+ file.write(copy_buffer).release_value_but_fixme_should_propagate_errors();
}
for (auto& change : m_changes) {
- file->seek(change.key, SeekMode::SetPosition);
- file->write(&change.value, 1);
+ file.seek(change.key, SeekMode::SetPosition).release_value_but_fixme_should_propagate_errors();
+ file.write({ &change.value, 1 }).release_value_but_fixme_should_propagate_errors();
}
return true;
@@ -152,24 +154,23 @@ void HexDocumentFile::clear_changes()
m_changes.clear();
}
-void HexDocumentFile::set_file(NonnullRefPtr<Core::File> file)
+void HexDocumentFile::set_file(NonnullOwnPtr<Core::Stream::File> file)
{
- m_file = file;
+ m_file = move(file);
- off_t size = 0;
- if (!file->seek(0, SeekMode::FromEndPosition, &size)) {
+ if (auto result = m_file->seek(0, SeekMode::FromEndPosition); result.is_error())
m_file_size = 0;
- } else {
- m_file_size = size;
- }
- file->seek(0, SeekMode::SetPosition);
+ else
+ m_file_size = result.value();
+
+ m_file->seek(0, SeekMode::SetPosition).release_value_but_fixme_should_propagate_errors();
clear_changes();
// make sure the next get operation triggers a read
m_buffer_file_pos = m_file_size + 1;
}
-NonnullRefPtr<Core::File> HexDocumentFile::file() const
+NonnullOwnPtr<Core::Stream::File> const& HexDocumentFile::file() const
{
return m_file;
}
@@ -177,8 +178,8 @@ NonnullRefPtr<Core::File> HexDocumentFile::file() const
void HexDocumentFile::ensure_position_in_buffer(size_t position)
{
if (position < m_buffer_file_pos || position >= m_buffer_file_pos + m_buffer.size()) {
- m_file->seek(position, SeekMode::SetPosition);
- m_file->read(m_buffer.data(), m_buffer.size());
+ m_file->seek(position, SeekMode::SetPosition).release_value_but_fixme_should_propagate_errors();
+ m_file->read(m_buffer).release_value_but_fixme_should_propagate_errors();
m_buffer_file_pos = position;
}
}
diff --git a/Userland/Applications/HexEditor/HexDocument.h b/Userland/Applications/HexEditor/HexDocument.h
index 2bfb7f3c66..e18d87c1f3 100644
--- a/Userland/Applications/HexEditor/HexDocument.h
+++ b/Userland/Applications/HexEditor/HexDocument.h
@@ -50,7 +50,7 @@ public:
size_t size() const override;
Type type() const override;
void clear_changes() override;
- bool write_to_file(NonnullRefPtr<Core::File> file);
+ bool write_to_file(Core::Stream::File& file);
private:
ByteBuffer m_buffer;
@@ -58,16 +58,16 @@ private:
class HexDocumentFile final : public HexDocument {
public:
- static ErrorOr<NonnullOwnPtr<HexDocumentFile>> create(NonnullRefPtr<Core::File> file);
+ static ErrorOr<NonnullOwnPtr<HexDocumentFile>> create(NonnullOwnPtr<Core::Stream::File> file);
virtual ~HexDocumentFile() = default;
HexDocumentFile(HexDocumentFile&&) = default;
HexDocumentFile(HexDocumentFile const&) = delete;
- void set_file(NonnullRefPtr<Core::File> file);
- NonnullRefPtr<Core::File> file() const;
+ void set_file(NonnullOwnPtr<Core::Stream::File> file);
+ NonnullOwnPtr<Core::Stream::File> const& file() const;
void write_to_file();
- bool write_to_file(NonnullRefPtr<Core::File> file);
+ bool write_to_file(Core::Stream::File& file);
Cell get(size_t position) override;
u8 get_unchanged(size_t position) override;
size_t size() const override;
@@ -75,10 +75,10 @@ public:
void clear_changes() override;
private:
- explicit HexDocumentFile(NonnullRefPtr<Core::File> file);
+ explicit HexDocumentFile(NonnullOwnPtr<Core::Stream::File> file);
void ensure_position_in_buffer(size_t position);
- NonnullRefPtr<Core::File> m_file;
+ NonnullOwnPtr<Core::Stream::File> m_file;
size_t m_file_size;
Array<u8, 2048> m_buffer;
diff --git a/Userland/Applications/HexEditor/HexEditor.cpp b/Userland/Applications/HexEditor/HexEditor.cpp
index 0a776c642b..ab20f05d6b 100644
--- a/Userland/Applications/HexEditor/HexEditor.cpp
+++ b/Userland/Applications/HexEditor/HexEditor.cpp
@@ -63,7 +63,7 @@ ErrorOr<void> HexEditor::open_new_file(size_t size)
return {};
}
-void HexEditor::open_file(NonnullRefPtr<Core::File> file)
+void HexEditor::open_file(NonnullOwnPtr<Core::Stream::File> file)
{
m_document = HexDocumentFile::create(move(file)).release_value_but_fixme_should_propagate_errors();
set_content_length(m_document->size());
@@ -135,16 +135,16 @@ void HexEditor::set_selection(size_t position, size_t length)
scroll_position_into_view(position);
update_status();
}
-bool HexEditor::save_as(NonnullRefPtr<Core::File> new_file)
+bool HexEditor::save_as(NonnullOwnPtr<Core::Stream::File> new_file)
{
if (m_document->type() == HexDocument::Type::File) {
auto& file_document = static_cast<HexDocumentFile&>(*m_document);
- if (!file_document.write_to_file(new_file))
+ if (!file_document.write_to_file(*new_file))
return false;
- file_document.set_file(new_file);
+ file_document.set_file(move(new_file));
} else {
auto& memory_document = static_cast<HexDocumentMemory&>(*m_document);
- if (!memory_document.write_to_file(new_file))
+ if (!memory_document.write_to_file(*new_file))
return false;
m_document = HexDocumentFile::create(move(new_file)).release_value_but_fixme_should_propagate_errors();
}
diff --git a/Userland/Applications/HexEditor/HexEditor.h b/Userland/Applications/HexEditor/HexEditor.h
index d7e085b49c..7207134e4d 100644
--- a/Userland/Applications/HexEditor/HexEditor.h
+++ b/Userland/Applications/HexEditor/HexEditor.h
@@ -35,10 +35,10 @@ public:
size_t buffer_size() const { return m_document->size(); }
ErrorOr<void> open_new_file(size_t size);
- void open_file(NonnullRefPtr<Core::File> file);
+ void open_file(NonnullOwnPtr<Core::Stream::File> file);
ErrorOr<void> fill_selection(u8 fill_byte);
Optional<u8> get_byte(size_t position);
- bool save_as(NonnullRefPtr<Core::File>);
+ bool save_as(NonnullOwnPtr<Core::Stream::File>);
bool save();
bool undo();
diff --git a/Userland/Applications/HexEditor/HexEditorWidget.cpp b/Userland/Applications/HexEditor/HexEditorWidget.cpp
index bc6f490da6..0ac4bc9471 100644
--- a/Userland/Applications/HexEditor/HexEditorWidget.cpp
+++ b/Userland/Applications/HexEditor/HexEditorWidget.cpp
@@ -17,7 +17,7 @@
#include <AK/StringBuilder.h>
#include <Applications/HexEditor/HexEditorWindowGML.h>
#include <LibConfig/Client.h>
-#include <LibCore/File.h>
+#include <LibCore/Stream.h>
#include <LibDesktop/Launcher.h>
#include <LibFileSystemAccessClient/Client.h>
#include <LibGUI/Action.h>
@@ -121,11 +121,11 @@ HexEditorWidget::HexEditorWidget()
if (!request_close())
return;
- auto response = FileSystemAccessClient::Client::the().try_open_file_deprecated(window(), {}, Core::StandardPaths::home_directory(), Core::OpenMode::ReadWrite);
+ auto response = FileSystemAccessClient::Client::the().open_file(window(), {}, Core::StandardPaths::home_directory(), Core::Stream::OpenMode::ReadWrite);
if (response.is_error())
return;
- open_file(response.value());
+ open_file(response.value().filename(), response.value().release_stream());
});
m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
@@ -142,18 +142,18 @@ HexEditorWidget::HexEditorWidget()
});
m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
- auto response = FileSystemAccessClient::Client::the().try_save_file_deprecated(window(), m_name, m_extension, Core::OpenMode::ReadWrite | Core::OpenMode::Truncate);
+ auto response = FileSystemAccessClient::Client::the().save_file(window(), m_name, m_extension, Core::Stream::OpenMode::ReadWrite | Core::Stream::OpenMode::Truncate);
if (response.is_error())
return;
auto file = response.release_value();
- if (!m_editor->save_as(file)) {
+ if (!m_editor->save_as(file.release_stream())) {
GUI::MessageBox::show(window(), "Unable to save file.\n"sv, "Error"sv, GUI::MessageBox::Type::Error);
return;
}
window()->set_modified(false);
- set_path(file->filename());
- dbgln("Wrote document to {}", file->filename());
+ set_path(file.filename());
+ dbgln("Wrote document to {}", file.filename());
});
m_undo_action = GUI::CommonActions::make_undo_action([&](auto&) {
@@ -525,11 +525,11 @@ void HexEditorWidget::update_title()
window()->set_title(builder.to_deprecated_string());
}
-void HexEditorWidget::open_file(NonnullRefPtr<Core::File> file)
+void HexEditorWidget::open_file(String const& filename, NonnullOwnPtr<Core::Stream::File> file)
{
window()->set_modified(false);
- m_editor->open_file(file);
- set_path(file->filename());
+ m_editor->open_file(move(file));
+ set_path(filename.to_deprecated_string());
}
bool HexEditorWidget::request_close()
@@ -586,9 +586,9 @@ void HexEditorWidget::drop_event(GUI::DropEvent& event)
return;
// TODO: A drop event should be considered user consent for opening a file
- auto response = FileSystemAccessClient::Client::the().try_request_file_deprecated(window(), urls.first().path(), Core::OpenMode::ReadOnly);
+ auto response = FileSystemAccessClient::Client::the().request_file(window(), urls.first().path(), Core::Stream::OpenMode::Read);
if (response.is_error())
return;
- open_file(response.value());
+ open_file(response.value().filename(), response.value().release_stream());
}
}
diff --git a/Userland/Applications/HexEditor/HexEditorWidget.h b/Userland/Applications/HexEditor/HexEditorWidget.h
index 7695fb3a58..addddd246f 100644
--- a/Userland/Applications/HexEditor/HexEditorWidget.h
+++ b/Userland/Applications/HexEditor/HexEditorWidget.h
@@ -25,7 +25,7 @@ class HexEditorWidget final : public GUI::Widget {
C_OBJECT(HexEditorWidget)
public:
virtual ~HexEditorWidget() override = default;
- void open_file(NonnullRefPtr<Core::File>);
+ void open_file(String const& filename, NonnullOwnPtr<Core::Stream::File>);
void initialize_menubar(GUI::Window&);
bool request_close();
diff --git a/Userland/Applications/HexEditor/main.cpp b/Userland/Applications/HexEditor/main.cpp
index 97cd3e138c..f3311bc0e6 100644
--- a/Userland/Applications/HexEditor/main.cpp
+++ b/Userland/Applications/HexEditor/main.cpp
@@ -53,10 +53,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (arguments.argc > 1) {
// FIXME: Using `try_request_file_read_only_approved` doesn't work here since the file stored in the editor is only readable.
- auto response = FileSystemAccessClient::Client::the().try_request_file_deprecated(window, arguments.strings[1], Core::OpenMode::ReadWrite);
+ auto response = FileSystemAccessClient::Client::the().request_file(window, arguments.strings[1], Core::Stream::OpenMode::ReadWrite);
if (response.is_error())
return 1;
- hex_editor_widget->open_file(response.value());
+ hex_editor_widget->open_file(response.value().filename(), response.value().release_stream());
}
return app->exec();