FileSystem.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "common/FileSystem.h"
  2. #include "core/Logger.h"
  3. #include <stdio.h>
  4. unsigned int
  5. FileSystem::GetFileSize(const Ogre::String &path)
  6. {
  7. FILE* file = fopen(path.c_str(), "rb");
  8. if (file == NULL)
  9. {
  10. LOG_ERROR("Can't open file " + path + ".\n");
  11. return 0;
  12. }
  13. // set cursor to end of file
  14. fseek(file, 0, SEEK_END);
  15. unsigned int size = ftell(file);
  16. fclose(file);
  17. return size;
  18. }
  19. bool
  20. FileSystem::ReadFile(const Ogre::String &path, void* buffer, const unsigned int start, const unsigned int length)
  21. {
  22. FILE* file = fopen(path.c_str(), "rb");
  23. if (file == NULL)
  24. {
  25. LOG_ERROR("Can't open file " + path + ".\n");
  26. return false;
  27. }
  28. fseek(file, start, SEEK_SET);
  29. const auto ret = fread(buffer, sizeof(char), length, file);
  30. fclose(file);
  31. if (ret != sizeof(char))
  32. {
  33. LOG_ERROR("Failed to read all data\n");
  34. return false;
  35. }
  36. return true;
  37. }
  38. bool
  39. FileSystem::WriteFile(const Ogre::String &path, const void* buffer, const unsigned int length)
  40. {
  41. FILE* file = fopen(path.c_str(), "ab");
  42. if (file == NULL)
  43. {
  44. return false;
  45. }
  46. fwrite(buffer, sizeof(char), length, file);
  47. fclose(file);
  48. return true;
  49. }
  50. bool
  51. FileSystem::WriteNewFile(const Ogre::String &path, const void* buffer, unsigned int length)
  52. {
  53. RemoveFile(path);
  54. return !!(WriteFile(path, buffer, length));
  55. }
  56. bool
  57. FileSystem::RemoveFile(const Ogre::String &path)
  58. {
  59. return (remove(path.c_str()) == 0);
  60. }