MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / FileToBuffer

Function FileToBuffer

lib/mdflib/mdflib/src/at4block.cpp:68–98  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

66}
67
68bool FileToBuffer(const std::string& filename, mdf::ByteArray& dest) {
69 try {
70 path fullname = u8path(filename);
71 const auto size = file_size(fullname);
72 if (size > 0) {
73 dest.resize(static_cast<size_t>(size), 0);
74 std::FILE* file = nullptr;
75 Platform::fileopen(&file, filename.c_str(), "rb");
76 if (file != nullptr) {
77 const auto nof_bytes = fread(dest.data(), 1, static_cast<size_t>(size), file);
78 if (nof_bytes < size) {
79 dest.resize(nof_bytes);
80 }
81 } else {
82 MDF_ERROR() << "Failed to open file. File: " << filename;
83 dest.clear();
84 dest.shrink_to_fit();
85 return false;
86 }
87 } else {
88 dest.clear();
89 dest.shrink_to_fit();
90 }
91
92 } catch (const std::exception& err) {
93 MDF_ERROR() << "File error when reading file to byte array. Error: "
94 << err.what() << ", File: " << filename;
95 return false;
96 }
97 return true;
98}
99
100} // namespace
101

Callers 1

WriteMethod · 0.85

Calls 4

fileopenFunction · 0.85
resizeMethod · 0.45
dataMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected