HexDocument.cpp 6.2 KB

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