feat: Added option to copy custom encoding strings from hex view
This commit is contained in:
parent
c861bf9a5e
commit
c6e1f45dc3
@ -5,6 +5,7 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <span>
|
||||||
|
|
||||||
#include <hex/helpers/fs.hpp>
|
#include <hex/helpers/fs.hpp>
|
||||||
#include <hex/helpers/file.hpp>
|
#include <hex/helpers/file.hpp>
|
||||||
@ -21,7 +22,7 @@ namespace hex {
|
|||||||
EncodingFile() = default;
|
EncodingFile() = default;
|
||||||
EncodingFile(Type type, const std::fs::path &path);
|
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]] size_t getLongestSequence() const { return this->m_longestSequence; }
|
||||||
|
|
||||||
[[nodiscard]] bool valid() const { return this->m_valid; }
|
[[nodiscard]] bool valid() const { return this->m_valid; }
|
||||||
|
@ -17,7 +17,7 @@ namespace hex {
|
|||||||
this->m_valid = true;
|
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) {
|
for (auto riter = this->m_mapping.crbegin(); riter != this->m_mapping.crend(); ++riter) {
|
||||||
const auto &[size, mapping] = *riter;
|
const auto &[size, mapping] = *riter;
|
||||||
|
|
||||||
|
@ -132,6 +132,10 @@ namespace hex::plugin::builtin::ui {
|
|||||||
this->m_characterCellPadding = characterCellPadding;
|
this->m_characterCellPadding = characterCellPadding;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] const std::optional<EncodingFile>& getCustomEncoding() const {
|
||||||
|
return this->m_currCustomEncoding;
|
||||||
|
}
|
||||||
|
|
||||||
void setCustomEncoding(EncodingFile encoding) {
|
void setCustomEncoding(EncodingFile encoding) {
|
||||||
this->m_currCustomEncoding = std::move(encoding);
|
this->m_currCustomEncoding = std::move(encoding);
|
||||||
}
|
}
|
||||||
|
@ -655,6 +655,7 @@
|
|||||||
"hex.builtin.view.hex_editor.copy.cpp": "C++ Array",
|
"hex.builtin.view.hex_editor.copy.cpp": "C++ Array",
|
||||||
"hex.builtin.view.hex_editor.copy.crystal": "Crystal 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.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.go": "Go Array",
|
||||||
"hex.builtin.view.hex_editor.copy.hex_view": "Hex View",
|
"hex.builtin.view.hex_editor.copy.hex_view": "Hex View",
|
||||||
"hex.builtin.view.hex_editor.copy.html": "HTML",
|
"hex.builtin.view.hex_editor.copy.html": "HTML",
|
||||||
|
@ -985,6 +985,23 @@ namespace hex::plugin::builtin {
|
|||||||
if (ImGui::MenuItem("hex.builtin.view.hex_editor.copy.address"_lang))
|
if (ImGui::MenuItem("hex.builtin.view.hex_editor.copy.address"_lang))
|
||||||
ImGui::SetClipboardText(hex::format("0x{:08X}", selection->getStartAddress()).c_str());
|
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();
|
ImGui::Separator();
|
||||||
|
|
||||||
for (const auto &[unlocalizedName, callback] : ContentRegistry::DataFormatter::getEntries()) {
|
for (const auto &[unlocalizedName, callback] : ContentRegistry::DataFormatter::getEntries()) {
|
||||||
|
Loading…
Reference in New Issue
Block a user