file_utils.cpp 860 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. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "file_utils.h"
  9. #include <cstring>
  10. namespace igl {
  11. IGL_INLINE void read_file_binary(FILE *fp,
  12. std::vector<uint8_t> &fileBufferBytes) {
  13. if (!ferror(fp)) {
  14. fseek(fp, 0, SEEK_END);
  15. size_t sizeBytes = ftell(fp);
  16. fseek(fp, 0, SEEK_SET);
  17. fileBufferBytes.resize(sizeBytes);
  18. if (fread((char*)fileBufferBytes.data(), 1, sizeBytes, fp) == sizeBytes) {
  19. fclose(fp);
  20. return;
  21. }
  22. }
  23. throw std::runtime_error("error reading from file");
  24. }
  25. }