blob: 256f753f65d860a96a47ae2d81b2edabe73140dc (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
/*
* Copyright (c) 2021, Arne Elster <arne@elster.li>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/HashMap.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/StringView.h>
#include <AK/Time.h>
#include <AK/Types.h>
#include <AK/WeakPtr.h>
#include <LibCore/Forward.h>
#include <LibGUI/Command.h>
constexpr Time COMMAND_COMMIT_TIME = Time::from_milliseconds(400);
class HexDocument : public Weakable<HexDocument> {
public:
enum class Type {
Memory,
File
};
struct Cell {
u8 value;
bool modified;
};
virtual ~HexDocument() = default;
virtual Cell get(size_t position) = 0;
virtual u8 get_unchanged(size_t position) = 0;
virtual void set(size_t position, u8 value);
virtual size_t size() const = 0;
virtual Type type() const = 0;
virtual bool is_dirty() const;
virtual void clear_changes() = 0;
protected:
HashMap<size_t, u8> m_changes;
};
class HexDocumentMemory final : public HexDocument {
public:
explicit HexDocumentMemory(ByteBuffer&& buffer);
virtual ~HexDocumentMemory() = default;
Cell get(size_t position) override;
u8 get_unchanged(size_t position) override;
size_t size() const override;
Type type() const override;
void clear_changes() override;
ErrorOr<void> write_to_file(Core::File& file);
private:
ByteBuffer m_buffer;
};
class HexDocumentFile final : public HexDocument {
public:
static ErrorOr<NonnullOwnPtr<HexDocumentFile>> create(NonnullOwnPtr<Core::File> file);
virtual ~HexDocumentFile() = default;
HexDocumentFile(HexDocumentFile&&) = default;
HexDocumentFile(HexDocumentFile const&) = delete;
ErrorOr<void> set_file(NonnullOwnPtr<Core::File> file);
NonnullOwnPtr<Core::File> const& file() const;
ErrorOr<void> write_to_file();
ErrorOr<void> write_to_file(Core::File& file);
Cell get(size_t position) override;
u8 get_unchanged(size_t position) override;
size_t size() const override;
Type type() const override;
void clear_changes() override;
private:
explicit HexDocumentFile(NonnullOwnPtr<Core::File> file);
ErrorOr<void> initialize_internal_state();
void ensure_position_in_buffer(size_t position);
NonnullOwnPtr<Core::File> m_file;
size_t m_file_size;
Array<u8, 2048> m_buffer;
size_t m_buffer_file_pos;
};
class HexDocumentUndoCommand : public GUI::Command {
public:
HexDocumentUndoCommand(WeakPtr<HexDocument> document, size_t position);
virtual void undo() override;
virtual void redo() override;
virtual DeprecatedString action_text() const override { return "Update cell"; }
virtual bool merge_with(GUI::Command const& other) override;
ErrorOr<void> try_add_changed_byte(u8 old_value, u8 new_value);
ErrorOr<void> try_add_changed_bytes(ByteBuffer old_values, ByteBuffer new_values);
private:
bool commit_time_expired() const { return Time::now_monotonic() - m_timestamp >= COMMAND_COMMIT_TIME; }
Time m_timestamp = Time::now_monotonic();
WeakPtr<HexDocument> m_document;
size_t m_position;
ByteBuffer m_old;
ByteBuffer m_new;
};
|