HexDocument.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (c) 2021, Arne Elster <arne@elster.li>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "HexDocument.h"
  7. #include <LibCore/Stream.h>
  8. void HexDocument::set(size_t position, u8 value)
  9. {
  10. auto unchanged_value = get_unchanged(position);
  11. if (value == unchanged_value) {
  12. m_changes.remove(position);
  13. } else {
  14. m_changes.set(position, value);
  15. }
  16. }
  17. bool HexDocument::is_dirty() const
  18. {
  19. return m_changes.size() > 0;
  20. }
  21. HexDocumentMemory::HexDocumentMemory(ByteBuffer&& buffer)
  22. : m_buffer(move(buffer))
  23. {
  24. }
  25. HexDocument::Cell HexDocumentMemory::get(size_t position)
  26. {
  27. auto tracked_change = m_changes.get(position);
  28. if (tracked_change.has_value()) {
  29. return Cell { tracked_change.value(), true };
  30. } else {
  31. return Cell { m_buffer[position], false };
  32. }
  33. }
  34. u8 HexDocumentMemory::get_unchanged(size_t position)
  35. {
  36. return m_buffer[position];
  37. }
  38. size_t HexDocumentMemory::size() const
  39. {
  40. return m_buffer.size();
  41. }
  42. HexDocument::Type HexDocumentMemory::type() const
  43. {
  44. return Type::Memory;
  45. }
  46. void HexDocumentMemory::clear_changes()
  47. {
  48. m_changes.clear();
  49. }
  50. ErrorOr<void> HexDocumentMemory::write_to_file(Core::Stream::File& file)
  51. {
  52. TRY(file.seek(0, SeekMode::SetPosition));
  53. TRY(file.write(m_buffer));
  54. for (auto& change : m_changes) {
  55. TRY(file.seek(change.key, SeekMode::SetPosition));
  56. TRY(file.write({ &change.value, 1 }));
  57. }
  58. return {};
  59. }
  60. ErrorOr<NonnullOwnPtr<HexDocumentFile>> HexDocumentFile::create(NonnullOwnPtr<Core::Stream::File> file)
  61. {
  62. auto document = TRY(adopt_nonnull_own_or_enomem(new HexDocumentFile(move(file))));
  63. // FIXME: Remove this hackery
  64. document->set_file(move(document->m_file));
  65. return document;
  66. }
  67. HexDocumentFile::HexDocumentFile(NonnullOwnPtr<Core::Stream::File> file)
  68. : m_file(move(file))
  69. {
  70. }
  71. void HexDocumentFile::write_to_file()
  72. {
  73. for (auto& change : m_changes) {
  74. m_file->seek(change.key, SeekMode::SetPosition).release_value_but_fixme_should_propagate_errors();
  75. m_file->write({ &change.value, 1 }).release_value_but_fixme_should_propagate_errors();
  76. }
  77. clear_changes();
  78. // make sure the next get operation triggers a read
  79. m_buffer_file_pos = m_file_size + 1;
  80. }
  81. ErrorOr<void> HexDocumentFile::write_to_file(Core::Stream::File& file)
  82. {
  83. TRY(file.truncate(size()));
  84. TRY(file.seek(0, SeekMode::SetPosition));
  85. TRY(m_file->seek(0, SeekMode::SetPosition));
  86. while (true) {
  87. Array<u8, 64 * KiB> buffer;
  88. auto copy_buffer = TRY(m_file->read(buffer));
  89. if (copy_buffer.size() == 0)
  90. break;
  91. TRY(file.write(copy_buffer));
  92. }
  93. for (auto& change : m_changes) {
  94. TRY(file.seek(change.key, SeekMode::SetPosition));
  95. TRY(file.write({ &change.value, 1 }));
  96. }
  97. return {};
  98. }
  99. HexDocument::Cell HexDocumentFile::get(size_t position)
  100. {
  101. auto tracked_change = m_changes.get(position);
  102. if (tracked_change.has_value()) {
  103. return Cell { tracked_change.value(), true };
  104. }
  105. ensure_position_in_buffer(position);
  106. return { m_buffer[position - m_buffer_file_pos], false };
  107. }
  108. u8 HexDocumentFile::get_unchanged(size_t position)
  109. {
  110. ensure_position_in_buffer(position);
  111. return m_buffer[position - m_buffer_file_pos];
  112. }
  113. size_t HexDocumentFile::size() const
  114. {
  115. return m_file_size;
  116. }
  117. HexDocument::Type HexDocumentFile::type() const
  118. {
  119. return Type::File;
  120. }
  121. void HexDocumentFile::clear_changes()
  122. {
  123. m_changes.clear();
  124. }
  125. void HexDocumentFile::set_file(NonnullOwnPtr<Core::Stream::File> file)
  126. {
  127. m_file = move(file);
  128. if (auto result = m_file->seek(0, SeekMode::FromEndPosition); result.is_error())
  129. m_file_size = 0;
  130. else
  131. m_file_size = result.value();
  132. m_file->seek(0, SeekMode::SetPosition).release_value_but_fixme_should_propagate_errors();
  133. clear_changes();
  134. // make sure the next get operation triggers a read
  135. m_buffer_file_pos = m_file_size + 1;
  136. }
  137. NonnullOwnPtr<Core::Stream::File> const& HexDocumentFile::file() const
  138. {
  139. return m_file;
  140. }
  141. void HexDocumentFile::ensure_position_in_buffer(size_t position)
  142. {
  143. if (position < m_buffer_file_pos || position >= m_buffer_file_pos + m_buffer.size()) {
  144. m_file->seek(position, SeekMode::SetPosition).release_value_but_fixme_should_propagate_errors();
  145. m_file->read(m_buffer).release_value_but_fixme_should_propagate_errors();
  146. m_buffer_file_pos = position;
  147. }
  148. }
  149. HexDocumentUndoCommand::HexDocumentUndoCommand(WeakPtr<HexDocument> document, size_t position)
  150. : m_document(move(document))
  151. , m_position(position)
  152. {
  153. }
  154. void HexDocumentUndoCommand::undo()
  155. {
  156. for (size_t i = 0; i < m_old.size(); i++)
  157. m_document->set(m_position + i, m_old[i]);
  158. }
  159. void HexDocumentUndoCommand::redo()
  160. {
  161. for (size_t i = 0; i < m_new.size(); i++)
  162. m_document->set(m_position + i, m_new[i]);
  163. }
  164. bool HexDocumentUndoCommand::merge_with(GUI::Command const& other)
  165. {
  166. if (!is<HexDocumentUndoCommand>(other) || commit_time_expired())
  167. return false;
  168. auto const& typed_other = static_cast<HexDocumentUndoCommand const&>(other);
  169. size_t relative_start = typed_other.m_position - m_position;
  170. size_t other_length = typed_other.m_old.size();
  171. size_t length = m_old.size();
  172. if (typed_other.m_position < m_position || m_position + length < typed_other.m_position)
  173. return false;
  174. m_old.resize(relative_start + other_length);
  175. m_new.resize(relative_start + other_length);
  176. for (size_t i = 0; i < other_length; i++) {
  177. m_new[relative_start + i] = typed_other.m_new[i];
  178. if (relative_start + i >= length)
  179. m_old[relative_start + i] = typed_other.m_old[i];
  180. }
  181. m_timestamp = Time::now_monotonic();
  182. return true;
  183. }
  184. ErrorOr<void> HexDocumentUndoCommand::try_add_changed_byte(u8 old_value, u8 new_value)
  185. {
  186. TRY(m_old.try_append(old_value));
  187. TRY(m_new.try_append(new_value));
  188. return {};
  189. }
  190. ErrorOr<void> HexDocumentUndoCommand::try_add_changed_bytes(ByteBuffer old_values, ByteBuffer new_values)
  191. {
  192. TRY(m_old.try_append(move(old_values)));
  193. TRY(m_new.try_append(move(new_values)));
  194. return {};
  195. }