xref: /third_party/json/tests/src/test_utils.hpp (revision c5f01b2f)
1//     __ _____ _____ _____
2//  __|  |   __|     |   | |  JSON for Modern C++ (supporting code)
3// |  |  |__   |  |  | | | |  version 3.11.2
4// |_____|_____|_____|_|___|  https://github.com/nlohmann/json
5//
6// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
7// SPDX-License-Identifier: MIT
8
9#pragma once
10
11#include <cstdint> // uint8_t
12#include <fstream> // ifstream, istreambuf_iterator, ios
13#include <vector> // vector
14
15namespace utils
16{
17
18inline std::vector<std::uint8_t> read_binary_file(const std::string& filename)
19{
20    std::ifstream file(filename, std::ios::binary);
21    file.unsetf(std::ios::skipws);
22
23    file.seekg(0, std::ios::end);
24    const auto size = file.tellg();
25    file.seekg(0, std::ios::beg);
26
27    std::vector<std::uint8_t> byte_vector;
28    byte_vector.reserve(static_cast<std::size_t>(size));
29    byte_vector.insert(byte_vector.begin(), std::istream_iterator<std::uint8_t>(file), std::istream_iterator<std::uint8_t>());
30    return byte_vector;
31}
32
33} // namespace utils
34