1
0
mirror of synced 2024-11-24 15:50:16 +01:00

feat: Added option to copy custom encoding strings from hex view

This commit is contained in:
WerWolv 2023-01-28 21:12:35 +01:00
parent c861bf9a5e
commit c6e1f45dc3
5 changed files with 25 additions and 2 deletions

View File

@ -5,6 +5,7 @@
#include <map>
#include <string_view>
#include <vector>
#include <span>
#include <hex/helpers/fs.hpp>
#include <hex/helpers/file.hpp>
@ -21,7 +22,7 @@ namespace hex {
EncodingFile() = default;
EncodingFile(Type type, const std::fs::path &path);
[[nodiscard]] std::pair<std::string_view, size_t> getEncodingFor(const std::vector<u8> &buffer) const;
[[nodiscard]] std::pair<std::string_view, size_t> getEncodingFor(std::span<u8> buffer) const;
[[nodiscard]] size_t getLongestSequence() const { return this->m_longestSequence; }
[[nodiscard]] bool valid() const { return this->m_valid; }

View File

@ -17,7 +17,7 @@ namespace hex {
this->m_valid = true;
}
std::pair<std::string_view, size_t> EncodingFile::getEncodingFor(const std::vector<u8> &buffer) const {
std::pair<std::string_view, size_t> EncodingFile::getEncodingFor(std::span<u8> buffer) const {
for (auto riter = this->m_mapping.crbegin(); riter != this->m_mapping.crend(); ++riter) {
const auto &[size, mapping] = *riter;

View File

@ -132,6 +132,10 @@ namespace hex::plugin::builtin::ui {
this->m_characterCellPadding = characterCellPadding;
}
[[nodiscard]] const std::optional<EncodingFile>& getCustomEncoding() const {
return this->m_currCustomEncoding;
}
void setCustomEncoding(EncodingFile encoding) {
this->m_currCustomEncoding = std::move(encoding);
}

View File

@ -655,6 +655,7 @@
"hex.builtin.view.hex_editor.copy.cpp": "C++ Array",
"hex.builtin.view.hex_editor.copy.crystal": "Crystal Array",
"hex.builtin.view.hex_editor.copy.csharp": "C# Array",
"hex.builtin.view.hex_editor.copy.custom_encoding": "Custom Encoding",
"hex.builtin.view.hex_editor.copy.go": "Go Array",
"hex.builtin.view.hex_editor.copy.hex_view": "Hex View",
"hex.builtin.view.hex_editor.copy.html": "HTML",

View File

@ -985,6 +985,23 @@ namespace hex::plugin::builtin {
if (ImGui::MenuItem("hex.builtin.view.hex_editor.copy.address"_lang))
ImGui::SetClipboardText(hex::format("0x{:08X}", selection->getStartAddress()).c_str());
auto &customEncoding = this->m_hexEditor.getCustomEncoding();
if (ImGui::MenuItem("hex.builtin.view.hex_editor.copy.custom_encoding"_lang, "", false, customEncoding.has_value())) {
std::vector<u8> buffer(customEncoding->getLongestSequence(), 0x00);
std::string string;
u64 offset = selection->getStartAddress();
while (offset < selection->getEndAddress()) {
provider->read(offset, buffer.data(), std::min(buffer.size(), selection->size - (offset - selection->getStartAddress())));
auto [result, size] = customEncoding->getEncodingFor(buffer);
string += result;
offset += size;
};
ImGui::SetClipboardText(string.c_str());
}
ImGui::Separator();
for (const auto &[unlocalizedName, callback] : ContentRegistry::DataFormatter::getEntries()) {