MCPcopy Create free account
hub / github.com/OpenBoardView/OpenBoardView / file_as_buffer

Function file_as_buffer

src/openboardview/utils.cpp:19–48  ·  view source on GitHub ↗

Loads an entire file in to memory

Source from the content-addressed store, hash-verified

17
18// Loads an entire file in to memory
19std::vector<char> file_as_buffer(const filesystem::path &filepath, std::string &error_msg) {
20 std::vector<char> data;
21
22 if (!filesystem::is_regular_file(filepath)) {
23 error_msg = "Not a regular file";
24 SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Error opening %s: %s", filepath.string().c_str(), error_msg.c_str());
25 return data;
26 }
27
28 ifstream file;
29 file.open(filepath, std::ios::in | std::ios::binary | std::ios::ate);
30
31 if (!file.is_open()) {
32 error_msg = strerror(errno);
33 SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Error opening %s: %s", filepath.string().c_str(), error_msg.c_str());
34 return data;
35 }
36
37 file.seekg(0, std::ios_base::end);
38 std::streampos sz = file.tellg();
39 ENSURE(sz >= 0, error_msg);
40 data.reserve(sz);
41 file.seekg(0, std::ios_base::beg);
42 data.assign(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
43
44 ENSURE(data.size() == static_cast<unsigned int>(sz), error_msg);
45 file.close();
46
47 return data;
48}
49
50// Extract extension from filename and check against given fileext
51// fileext must be lowercase

Callers 3

LoadFileMethod · 0.85
read_ascMethod · 0.85
loadTextureFromFileMethod · 0.85

Calls 1

closeMethod · 0.80

Tested by

no test coverage detected