mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-03-30 13:05:25 -05:00
build: More repo cleanup, move libimhex and external libs to /lib folder
This commit is contained in:
65
lib/libimhex/source/helpers/encoding_file.cpp
Normal file
65
lib/libimhex/source/helpers/encoding_file.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
#include <hex/helpers/encoding_file.hpp>
|
||||
|
||||
#include <hex/helpers/utils.hpp>
|
||||
|
||||
#include <fstream>
|
||||
|
||||
namespace hex {
|
||||
|
||||
EncodingFile::EncodingFile(Type type, const fs::path &path) {
|
||||
std::ifstream encodingFile(path.c_str());
|
||||
|
||||
switch (type) {
|
||||
case Type::Thingy: parseThingyFile(encodingFile); break;
|
||||
default: return;
|
||||
}
|
||||
|
||||
this->m_valid = true;
|
||||
}
|
||||
|
||||
std::pair<std::string_view, size_t> EncodingFile::getEncodingFor(const std::vector<u8> &buffer) const {
|
||||
for (auto riter = this->m_mapping.crbegin(); riter != this->m_mapping.crend(); ++riter) {
|
||||
const auto &[size, mapping] = *riter;
|
||||
|
||||
if (size > buffer.size()) continue;
|
||||
|
||||
auto key = std::vector<u8>(buffer.begin(), buffer.begin() + size);
|
||||
if (mapping.contains(key))
|
||||
return { mapping.at(key), size };
|
||||
}
|
||||
|
||||
return { ".", 1 };
|
||||
}
|
||||
|
||||
void EncodingFile::parseThingyFile(std::ifstream &content) {
|
||||
for (std::string line; std::getline(content, line);) {
|
||||
|
||||
std::string from, to;
|
||||
{
|
||||
auto delimiterPos = line.find('=', 0);
|
||||
|
||||
if (delimiterPos == std::string::npos)
|
||||
continue;
|
||||
|
||||
from = line.substr(0, delimiterPos);
|
||||
to = line.substr(delimiterPos + 1);
|
||||
|
||||
hex::trim(from);
|
||||
hex::trim(to);
|
||||
|
||||
if (from.empty()) continue;
|
||||
if (to.empty()) to = " ";
|
||||
}
|
||||
|
||||
auto fromBytes = hex::parseByteString(from);
|
||||
if (fromBytes.empty()) continue;
|
||||
|
||||
if (!this->m_mapping.contains(fromBytes.size()))
|
||||
this->m_mapping.insert({ fromBytes.size(), { } });
|
||||
this->m_mapping[fromBytes.size()].insert({ fromBytes, to });
|
||||
|
||||
this->m_longestSequence = std::max(this->m_longestSequence, fromBytes.size());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user