read_file_binary.cpp 914 B

12345678910111213141516171819202122232425262728293031
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2020 Jérémie Dumas <[email protected]>
  4. // Copyright (C) 2021 Alec Jacobson <[email protected]>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla Public License
  7. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  8. // obtain one at http://mozilla.org/MPL/2.0/.
  9. #include "read_file_binary.h"
  10. #include <cstring>
  11. #include <cstdint>
  12. IGL_INLINE void igl::read_file_binary(
  13. FILE *fp,
  14. std::vector<std::uint8_t> &fileBufferBytes)
  15. {
  16. if (!ferror(fp))
  17. {
  18. fseek(fp, 0, SEEK_END);
  19. size_t sizeBytes = ftell(fp);
  20. fseek(fp, 0, SEEK_SET);
  21. fileBufferBytes.resize(sizeBytes);
  22. if(fread((char*)fileBufferBytes.data(), 1, sizeBytes, fp) == sizeBytes)
  23. {
  24. fclose(fp);
  25. return;
  26. }
  27. }
  28. throw std::runtime_error("error reading from file");
  29. }