spiegel-keyman/core/tests/unit/load_kmx_file.cpp
Eberhard Beilharz d06aa29956
feat(core): implement loading KMX from blob
- split keyboard loading into loading KMX file into blob and then
  loading the keyboard processor from the blob.
- deprecate `km_core_keyboard_load`
- move file access next to deprecated method. This is now the only place
  that loads a file in Core; unit tests have some more places that
  load files.
- introduce GTest and add unit tests for loading from blob

Part-of: #11293
2024-09-24 16:30:39 +02:00

34 lines
724 B
C++

#include <fstream>
#include <iostream>
#include <list>
#include <string>
#include "kmx_file.h"
#include "path.hpp"
#include "utfcodec.hpp"
namespace km::tests {
std::vector<uint8_t> load_kmx_file(km::core::path const& kb_path) {
std::vector<uint8_t> data;
std::ifstream file(static_cast<std::string>(kb_path), std::ios::binary | std::ios::ate);
if (file.fail()) {
return std::vector<uint8_t>();
}
const std::streamsize size = file.tellg();
if (size >= KMX_MAX_ALLOWED_FILE_SIZE) {
return std::vector<uint8_t>();
}
file.seekg(0, std::ios::beg);
data.resize((size_t)size);
if (!file.read((char*)data.data(), size)) {
return std::vector<uint8_t>();
}
file.close();
return data;
}
}