File.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include "core/File.hpp"
  2. #include <stdio.h>
  3. #include "ErrorSimulator.hpp"
  4. #include "core/Logger.hpp"
  5. static bool readOpenFile(FILE* file, Core::List<char>& f, const char* path) {
  6. if(FAIL_STEP || fseek(file, 0, SEEK_END)) {
  7. REPORT(LogLevel::ERROR, "cannot seek file end of '#'", path);
  8. return true;
  9. }
  10. long l = ftell(file);
  11. if(FAIL_STEP || l < 0) {
  12. REPORT(LogLevel::ERROR, "cannot tell file position of '#'", path);
  13. return true;
  14. }
  15. size_t size = static_cast<size_t>(l);
  16. f.resize(size + 1);
  17. if(FAIL_STEP || fseek(file, 0, SEEK_SET)) {
  18. REPORT(LogLevel::ERROR, "cannot seek file start of '#'", path);
  19. return true;
  20. }
  21. size_t read = fread(&f[0], 1, size, file);
  22. f.getLast() = 0;
  23. if(FAIL_STEP || read != size) {
  24. REPORT(
  25. LogLevel::ERROR, "expected to read # bytes from '#' but read #",
  26. size, path, read);
  27. return true;
  28. }
  29. return false;
  30. }
  31. bool Core::readFile(List<char>& f, const char* path) {
  32. FILE* file = fopen(path, "rb");
  33. if(file == nullptr) {
  34. REPORT(LogLevel::ERROR, "cannot read file '#'", path);
  35. return true;
  36. }
  37. bool r = readOpenFile(file, f, path);
  38. if(FAIL_STEP || fclose(file)) {
  39. REPORT(LogLevel::ERROR, "cannot close file '#'", path);
  40. r = true;
  41. }
  42. return r;
  43. }