Commit | Line | Data |
---|---|---|
0b9614ad FD |
1 | |
2 | /* | |
3 | * Copyright (c) 2022 Francis Deslauriers <francis.deslauriers@efficios.com> | |
4 | * | |
5 | * SPDX-License-Identifier: MIT | |
6 | */ | |
7 | #include <fstream> | |
8 | ||
9 | #include "exc.hpp" | |
10 | #include "file-utils.hpp" | |
11 | ||
12 | namespace bt2c { | |
13 | ||
14 | std::vector<std::uint8_t> dataFromFile(const char * const filePath) | |
15 | { | |
16 | /* | |
17 | * Open a file stream and seek to the end of the stream to compute the size | |
18 | * of the buffer required. | |
19 | */ | |
20 | std::ifstream file {filePath, std::ios::binary | std::ios::ate}; | |
21 | ||
22 | if (!file) { | |
23 | throw NoSuchFileOrDirectoryError {}; | |
24 | } | |
25 | ||
26 | const auto size = file.tellg(); | |
27 | std::vector<uint8_t> buffer(static_cast<std::size_t>(size)); | |
28 | ||
29 | /* | |
30 | * Seek the reading head back at the beginning of the stream to actually | |
31 | * read the content. | |
32 | */ | |
33 | file.seekg(0, std::ios::beg); | |
34 | file.read(reinterpret_cast<char *>(buffer.data()), size); | |
35 | return buffer; | |
36 | } | |
37 | ||
38 | } /* namespace bt2c */ |