1
0
mirror of synced 2024-11-30 18:34:29 +01:00
ImHex/source/helpers/encoding_file.cpp
Kuruyia 72ec6baf79
sys: Fix macOS compilation (as of 2dc1886) (#317)
* sys: Updated curl to latest version

* sys: Fix macOS compilation

* ui: Fix splash screen OpenGL init for macOS

* sys: Fix std::min compile errors

* git: Re-enabled macos workflow

* sys: Remove includes of the range library

* build: Find OpenGL using CMake

* sys/build: Fix bundled plugins on macOS

* build: Copy plugins to bundle when creating a bundle

* build: Fixup bundled plugins

* sys: Search for plugins in the bundle instead of in Application Support

* sys: Allow resources to be placed in multiple directories on macOS

* build: Output built plugins to the plugins/ directory when not creating a bundle on macOS

* sys: Fix Application Support paths on macOS

* sys: Define ftruncate64 on macOS

* sys: Fix absolute value computation for std::string::at on macOS

Co-authored-by: WerWolv <werwolv98@gmail.com>
2021-10-09 23:07:58 +02:00

63 lines
1.9 KiB
C++

#include "helpers/encoding_file.hpp"
#include <hex/helpers/utils.hpp>
#include <fstream>
namespace hex {
EncodingFile::EncodingFile(Type type, const std::string &path) {
std::ifstream encodingFile(path.c_str());
switch (type) {
case Type::Thingy: parseThingyFile(encodingFile); break;
default: throw std::runtime_error("Invalid encoding file type");
}
}
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());
}
}
}