FileSystem.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. fread(buffer, sizeof(char), length, file);
  30. fclose(file);
  31. return true;
  32. }
  33. bool
  34. FileSystem::WriteFile(const Ogre::String &path, const void* buffer, const unsigned int length)
  35. {
  36. FILE* file = fopen(path.c_str(), "ab");
  37. if (file == NULL)
  38. {
  39. return false;
  40. }
  41. fwrite(buffer, sizeof(char), length, file);
  42. fclose(file);
  43. return true;
  44. }
  45. bool
  46. FileSystem::WriteNewFile(const Ogre::String &path, const void* buffer, unsigned int length)
  47. {
  48. RemoveFile(path);
  49. return !!(WriteFile(path, buffer, length));
  50. }
  51. bool
  52. FileSystem::RemoveFile(const Ogre::String &path)
  53. {
  54. return (remove(path.c_str()) == 0);
  55. }