3 * Copyright (c) 2022 Francis Deslauriers <francis.deslauriers@efficios.com>
5 * SPDX-License-Identifier: MIT
12 #include "file-utils.hpp"
16 std::vector
<std::uint8_t> dataFromFile(const CStringView filePath
, const Logger
& logger
,
17 const bool fatalError
)
20 * Open a file stream and seek to the end of the stream to compute the size
21 * of the buffer required.
23 std::ifstream file
{filePath
.data(), std::ios::binary
| std::ios::ate
};
26 constexpr const char *msg
= "No such file or directory: path=\"{}\"";
29 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, msg
, filePath
);
31 BT_CPPLOGD_SPEC(logger
, msg
, filePath
);
34 throw NoSuchFileOrDirectoryError
{};
37 const auto size
= file
.tellg();
38 std::vector
<uint8_t> buffer(static_cast<std::size_t>(size
));
41 * Seek the reading head back at the beginning of the stream to actually
44 file
.seekg(0, std::ios::beg
);
45 file
.read(reinterpret_cast<char *>(buffer
.data()), size
);
49 } /* namespace bt2c */