Loads an entire file in to memory
| 17 | |
| 18 | // Loads an entire file in to memory |
| 19 | std::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 |
no test coverage detected