/src/shared/load_data.cpp

https://bitbucket.org/vivkin/gam3b00bs/ · C++ · 45 lines · 36 code · 7 blank · 2 comment · 5 complexity · d80c86f1ff928d6bc5df9614668dcefe MD5 · raw file

  1. #include "load_data.h"
  2. #include "log.h"
  3. #include "common.h"
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. //-----------------------------------------------------------------------------
  7. uint32 load_data( const char* path, uint8** out_data, uint32* out_size )
  8. {
  9. ASSERT(path);
  10. ASSERT(out_data);
  11. ASSERT(out_size);
  12. FILE* file = fopen(path, "rb");
  13. if( !file )
  14. {
  15. log_write("Error: unable to open file %s for reading");
  16. return 1;
  17. }
  18. fseek(file, 0, SEEK_END);
  19. *out_size = ftell(file);
  20. rewind(file);
  21. if( *out_size == 0 )
  22. {
  23. fclose(file);
  24. log_write("Error: file %s is empty");
  25. return 2;
  26. }
  27. *out_data = (uint8*)malloc(*out_size);
  28. if( fread(*out_data, *out_size, 1, file) != 1 )
  29. {
  30. fclose(file);
  31. free(out_data);
  32. log_write("Error: unable to read %s");
  33. return 3;
  34. }
  35. fclose(file);
  36. return 0;
  37. }
  38. //-----------------------------------------------------------------------------