Abstracted FILE handle into a generic data provider class

This commit is contained in:
WerWolv
2020-11-11 09:18:35 +01:00
parent c87bc6aebe
commit 434bb3494a
11 changed files with 229 additions and 93 deletions

View File

@@ -1,20 +1,20 @@
#include "views/view_hexeditor.hpp"
#include "providers/file_provider.hpp"
namespace hex {
ViewHexEditor::ViewHexEditor(FILE *&file, std::vector<Highlight> &highlights)
: View(), m_file(file), m_highlights(highlights) {
ViewHexEditor::ViewHexEditor(prv::Provider* &dataProvider, std::vector<Highlight> &highlights)
: View(), m_dataProvider(dataProvider), m_highlights(highlights) {
this->m_memoryEditor.ReadFn = [](const ImU8 *data, size_t off) -> ImU8 {
ViewHexEditor *_this = (ViewHexEditor *) data;
if (_this->m_file == nullptr)
if (!_this->m_dataProvider->isAvailable() || !_this->m_dataProvider->isReadable())
return 0x00;
fseek(_this->m_file, off, SEEK_SET);
ImU8 byte;
fread(&byte, sizeof(ImU8), 1, _this->m_file);
_this->m_dataProvider->read(off, &byte, sizeof(ImU8));
return byte;
};
@@ -22,13 +22,10 @@ namespace hex {
this->m_memoryEditor.WriteFn = [](ImU8 *data, size_t off, ImU8 d) -> void {
ViewHexEditor *_this = (ViewHexEditor *) data;
if (_this->m_file == nullptr)
if (!_this->m_dataProvider->isAvailable() || !_this->m_dataProvider->isWritable())
return;
fseek(_this->m_file, off, SEEK_SET);
fwrite(&d, sizeof(ImU8), 1, _this->m_file);
_this->m_dataProvider->write(off, &d, sizeof(ImU8));
};
this->m_memoryEditor.HighlightFn = [](const ImU8 *data, size_t off, bool next) -> bool {
@@ -53,7 +50,7 @@ namespace hex {
ViewHexEditor::~ViewHexEditor() {}
void ViewHexEditor::createView() {
this->m_memoryEditor.DrawWindow("Hex Editor", this, this->m_file == nullptr ? 0x00 : this->m_fileSize);
this->m_memoryEditor.DrawWindow("Hex Editor", this, (this->m_dataProvider == nullptr || !this->m_dataProvider->isReadable()) ? 0x00 : this->m_dataProvider->getSize());
}
void ViewHexEditor::createMenu() {
@@ -61,14 +58,10 @@ namespace hex {
if (ImGui::MenuItem("Open File...")) {
auto filePath = openFileDialog();
if (filePath.has_value()) {
if (this->m_file != nullptr)
fclose(this->m_file);
if (this->m_dataProvider != nullptr)
delete this->m_dataProvider;
this->m_file = fopen(filePath->c_str(), "r+b");
fseek(this->m_file, 0, SEEK_END);
this->m_fileSize = ftell(this->m_file);
rewind(this->m_file);
this->m_dataProvider = new prv::FileProvider(filePath.value());
}
}