* Load a file into memory. * @param filename Name of the file to load. * @param[out] lenp Length of loaded data. * @param maxsize Maximum size to load. * @return Pointer to new memory containing the loaded data, or \c nullptr if loading failed. * @note If \a maxsize less than the length of the file, loading fails. */
| 1024 | * @note If \a maxsize less than the length of the file, loading fails. |
| 1025 | */ |
| 1026 | std::unique_ptr<char[]> ReadFileToMem(const std::string &filename, size_t &lenp, size_t maxsize) |
| 1027 | { |
| 1028 | auto in = FileHandle::Open(filename, "rb"); |
| 1029 | if (!in.has_value()) return nullptr; |
| 1030 | |
| 1031 | fseek(*in, 0, SEEK_END); |
| 1032 | size_t len = ftell(*in); |
| 1033 | fseek(*in, 0, SEEK_SET); |
| 1034 | if (len > maxsize) return nullptr; |
| 1035 | |
| 1036 | std::unique_ptr<char[]> mem = std::make_unique<char[]>(len + 1); |
| 1037 | |
| 1038 | mem.get()[len] = 0; |
| 1039 | if (fread(mem.get(), len, 1, *in) != 1) return nullptr; |
| 1040 | |
| 1041 | lenp = len; |
| 1042 | return mem; |
| 1043 | } |
| 1044 | |
| 1045 | /** |
| 1046 | * Helper to see whether a given filename matches the extension. |