2021-12-07 22:47:41 +01:00
|
|
|
#include <hex/helpers/encoding_file.hpp>
|
2021-02-14 01:11:55 +01:00
|
|
|
|
|
|
|
#include <hex/helpers/utils.hpp>
|
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
2022-03-04 11:36:37 +01:00
|
|
|
EncodingFile::EncodingFile(Type type, const std::fs::path &path) {
|
2022-05-17 20:46:42 +02:00
|
|
|
auto file = fs::File(path, fs::File::Mode::Read);
|
2021-02-14 01:11:55 +01:00
|
|
|
switch (type) {
|
2022-02-01 22:09:44 +01:00
|
|
|
case Type::Thingy:
|
2022-05-17 20:46:42 +02:00
|
|
|
parseThingyFile(file);
|
2022-02-01 22:09:44 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
2021-02-14 01:11:55 +01:00
|
|
|
}
|
2022-01-15 23:44:15 +01:00
|
|
|
|
|
|
|
this->m_valid = true;
|
2021-02-14 01:11:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<std::string_view, size_t> EncodingFile::getEncodingFor(const std::vector<u8> &buffer) const {
|
2021-10-09 23:07:58 +02:00
|
|
|
for (auto riter = this->m_mapping.crbegin(); riter != this->m_mapping.crend(); ++riter) {
|
|
|
|
const auto &[size, mapping] = *riter;
|
|
|
|
|
2021-02-14 01:11:55 +01:00
|
|
|
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 };
|
|
|
|
}
|
|
|
|
|
2022-05-17 20:46:42 +02:00
|
|
|
void EncodingFile::parseThingyFile(fs::File &file) {
|
|
|
|
for (const auto &line : splitString(file.readString(), "\n")) {
|
2021-02-14 01:11:55 +01:00
|
|
|
|
2021-02-14 12:05:58 +01:00
|
|
|
std::string from, to;
|
|
|
|
{
|
2022-05-17 20:46:42 +02:00
|
|
|
auto delimiterPos = line.find('=');
|
2021-02-14 01:11:55 +01:00
|
|
|
|
2021-02-14 12:05:58 +01:00
|
|
|
if (delimiterPos == std::string::npos)
|
|
|
|
continue;
|
2022-05-27 20:42:07 +02:00
|
|
|
if (delimiterPos >= line.length())
|
2022-05-17 20:46:42 +02:00
|
|
|
continue;
|
2021-02-14 01:11:55 +01:00
|
|
|
|
2021-02-14 12:05:58 +01:00
|
|
|
from = line.substr(0, delimiterPos);
|
2022-02-01 22:09:44 +01:00
|
|
|
to = line.substr(delimiterPos + 1);
|
2021-02-14 12:05:58 +01:00
|
|
|
|
2021-02-14 12:32:48 +01:00
|
|
|
if (from.empty()) continue;
|
2021-02-14 12:05:58 +01:00
|
|
|
if (to.empty()) to = " ";
|
|
|
|
}
|
2021-02-14 01:11:55 +01:00
|
|
|
|
|
|
|
auto fromBytes = hex::parseByteString(from);
|
2021-02-14 12:32:48 +01:00
|
|
|
if (fromBytes.empty()) continue;
|
|
|
|
|
2021-02-14 01:11:55 +01:00
|
|
|
if (!this->m_mapping.contains(fromBytes.size()))
|
2022-01-24 20:53:17 +01:00
|
|
|
this->m_mapping.insert({ fromBytes.size(), {} });
|
2021-02-14 01:11:55 +01:00
|
|
|
this->m_mapping[fromBytes.size()].insert({ fromBytes, to });
|
|
|
|
|
|
|
|
this->m_longestSequence = std::max(this->m_longestSequence, fromBytes.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|