HexDocument.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2021, Arne Elster <arne@elster.li>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "HexDocument.h"
  7. void HexDocument::set(size_t position, u8 value)
  8. {
  9. auto unchanged_value = get_unchanged(position);
  10. if (value == unchanged_value) {
  11. m_changes.remove(position);
  12. } else {
  13. m_changes.set(position, value);
  14. }
  15. }
  16. bool HexDocument::is_dirty() const
  17. {
  18. return m_changes.size() > 0;
  19. }
  20. HexDocumentMemory::HexDocumentMemory(ByteBuffer&& buffer)
  21. : m_buffer(move(buffer))
  22. {
  23. }
  24. HexDocument::Cell HexDocumentMemory::get(size_t position)
  25. {
  26. auto tracked_change = m_changes.get(position);
  27. if (tracked_change.has_value()) {
  28. return Cell { tracked_change.value(), true };
  29. } else {
  30. return Cell { m_buffer[position], false };
  31. }
  32. }
  33. u8 HexDocumentMemory::get_unchanged(size_t position)
  34. {
  35. return m_buffer[position];
  36. }
  37. size_t HexDocumentMemory::size() const
  38. {
  39. return m_buffer.size();
  40. }
  41. HexDocument::Type HexDocumentMemory::type() const
  42. {
  43. return Type::Memory;
  44. }
  45. void HexDocumentMemory::clear_changes()
  46. {
  47. m_changes.clear();
  48. }
  49. bool HexDocumentMemory::write_to_file(NonnullRefPtr<Core::File> file)
  50. {
  51. if (!file->seek(0))
  52. return false;
  53. if (!file->write(m_buffer.data(), m_buffer.size()))
  54. return false;
  55. for (auto& change : m_changes) {
  56. file->seek(change.key, Core::SeekMode::SetPosition);
  57. file->write(&change.value, 1);
  58. }
  59. return true;
  60. }
  61. HexDocumentFile::HexDocumentFile(NonnullRefPtr<Core::File> file)
  62. : m_file(file)
  63. {
  64. set_file(file);
  65. }
  66. void HexDocumentFile::write_to_file()
  67. {
  68. for (auto& change : m_changes) {
  69. m_file->seek(change.key, Core::SeekMode::SetPosition);
  70. m_file->write(&change.value, 1);
  71. }
  72. clear_changes();
  73. // make sure the next get operation triggers a read
  74. m_buffer_file_pos = m_file_size + 1;
  75. }
  76. bool HexDocumentFile::write_to_file(NonnullRefPtr<Core::File> file)
  77. {
  78. if (!file->truncate(size())) {
  79. return false;
  80. }
  81. if (!file->seek(0) || !m_file->seek(0)) {
  82. return false;
  83. }
  84. while (true) {
  85. auto copy_buffer = m_file->read(64 * KiB);
  86. if (copy_buffer.size() == 0)
  87. break;
  88. file->write(copy_buffer.data(), copy_buffer.size());
  89. }
  90. for (auto& change : m_changes) {
  91. file->seek(change.key, Core::SeekMode::SetPosition);
  92. file->write(&change.value, 1);
  93. }
  94. return true;
  95. }
  96. HexDocument::Cell HexDocumentFile::get(size_t position)
  97. {
  98. auto tracked_change = m_changes.get(position);
  99. if (tracked_change.has_value()) {
  100. return Cell { tracked_change.value(), true };
  101. }
  102. ensure_position_in_buffer(position);
  103. return { m_buffer[position - m_buffer_file_pos], false };
  104. }
  105. u8 HexDocumentFile::get_unchanged(size_t position)
  106. {
  107. ensure_position_in_buffer(position);
  108. return m_buffer[position - m_buffer_file_pos];
  109. }
  110. size_t HexDocumentFile::size() const
  111. {
  112. return m_file_size;
  113. }
  114. HexDocument::Type HexDocumentFile::type() const
  115. {
  116. return Type::File;
  117. }
  118. void HexDocumentFile::clear_changes()
  119. {
  120. m_changes.clear();
  121. }
  122. void HexDocumentFile::set_file(NonnullRefPtr<Core::File> file)
  123. {
  124. m_file = file;
  125. off_t size = 0;
  126. if (!file->seek(0, Core::SeekMode::FromEndPosition, &size)) {
  127. m_file_size = 0;
  128. } else {
  129. m_file_size = size;
  130. }
  131. file->seek(0, Core::SeekMode::SetPosition);
  132. clear_changes();
  133. // make sure the next get operation triggers a read
  134. m_buffer_file_pos = m_file_size + 1;
  135. }
  136. NonnullRefPtr<Core::File> HexDocumentFile::file() const
  137. {
  138. return m_file;
  139. }
  140. void HexDocumentFile::ensure_position_in_buffer(size_t position)
  141. {
  142. if (position < m_buffer_file_pos || position >= m_buffer_file_pos + m_buffer.size()) {
  143. m_file->seek(position, Core::SeekMode::SetPosition);
  144. m_file->read(m_buffer.data(), m_buffer.size());
  145. m_buffer_file_pos = position;
  146. }
  147. }