https://stackoverflow.com/questions/51352863/what-is-the-idiomatic-c17-standard-approach-to-reading-binary-files
| 50 | |
| 51 | //https://stackoverflow.com/questions/51352863/what-is-the-idiomatic-c17-standard-approach-to-reading-binary-files |
| 52 | std::vector<std::byte> LoadFile(std::string const& filepath) |
| 53 | { |
| 54 | std::ifstream ifs(filepath, std::ios::binary|std::ios::ate); |
| 55 | |
| 56 | if(!ifs) |
| 57 | throw std::runtime_error(filepath + ": " + std::strerror(errno)); |
| 58 | |
| 59 | auto end = ifs.tellg(); |
| 60 | ifs.seekg(0, std::ios::beg); |
| 61 | |
| 62 | auto size = std::size_t(end - ifs.tellg()); |
| 63 | |
| 64 | if(size == 0) // avoid undefined behavior |
| 65 | return {}; |
| 66 | |
| 67 | std::vector<std::byte> buffer(size); |
| 68 | |
| 69 | if(!ifs.read((char*)buffer.data(), buffer.size())) |
| 70 | throw std::runtime_error(filepath + ": " + std::strerror(errno)); |
| 71 | |
| 72 | return buffer; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | int GetPackedArguments(int argc, const char* argv[], const char* bof_args_def, std::string& result); |