#include "core/File.hpp" #include #include "ErrorSimulator.hpp" #include "core/Logger.hpp" static bool readOpenFile(FILE* file, Core::List& f, const char* path) { if(FAIL_STEP || fseek(file, 0, SEEK_END)) { REPORT(LogLevel::ERROR, "cannot seek file end of '#'", path); return true; } long l = ftell(file); if(FAIL_STEP || l < 0) { REPORT(LogLevel::ERROR, "cannot tell file position of '#'", path); return true; } size_t size = static_cast(l); f.resize(size + 1); if(FAIL_STEP || fseek(file, 0, SEEK_SET)) { REPORT(LogLevel::ERROR, "cannot seek file start of '#'", path); return true; } size_t read = fread(&f[0], 1, size, file); f.getLast() = 0; if(FAIL_STEP || read != size) { REPORT( LogLevel::ERROR, "expected to read # bytes from '#' but read #", size, path, read); return true; } return false; } bool Core::readFile(List& f, const char* path) { FILE* file = fopen(path, "rb"); if(file == nullptr) { REPORT(LogLevel::ERROR, "cannot read file '#'", path); return true; } bool r = readOpenFile(file, f, path); if(FAIL_STEP || fclose(file)) { REPORT(LogLevel::ERROR, "cannot close file '#'", path); r = true; } return r; }