! loadFile_orDie() : * load file into buffer (memory). * * Note: This function will send an error to stderr and exit if it * cannot read data from the given file path. * * @return If successful this function will load file into buffer and * return file size, otherwise it will printout an error to stderr and exit. */
| 178 | * return file size, otherwise it will printout an error to stderr and exit. |
| 179 | */ |
| 180 | static size_t loadFile_orDie(const char* fileName, void* buffer, size_t bufferSize) |
| 181 | { |
| 182 | size_t const fileSize = fsize_orDie(fileName); |
| 183 | CHECK(fileSize <= bufferSize, "File too large!"); |
| 184 | |
| 185 | FILE* const inFile = fopen_orDie(fileName, "rb"); |
| 186 | size_t const readSize = fread(buffer, 1, fileSize, inFile); |
| 187 | if (readSize != (size_t)fileSize) { |
| 188 | fprintf(stderr, "fread: %s : %s \n", fileName, strerror(errno)); |
| 189 | exit(ERROR_fread); |
| 190 | } |
| 191 | fclose(inFile); /* can't fail, read only */ |
| 192 | return fileSize; |
| 193 | } |
| 194 | |
| 195 | /*! mallocAndLoadFile_orDie() : |
| 196 | * allocate memory buffer and then load file into it. |
no test coverage detected