| 66 | } |
| 67 | |
| 68 | bool 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 | |