CmFileSystem.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "CmFileSystem.h"
  2. #include "CmDataStream.h"
  3. #include "CmPath.h"
  4. #include "CmException.h"
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #if CM_PLATFORM == CM_PLATFORM_LINUX || CM_PLATFORM == CM_PLATFORM_APPLE || \
  8. CM_PLATFORM == CM_PLATFORM_SYMBIAN || CM_PLATFORM == CM_PLATFORM_IPHONE
  9. # include <sys/param.h>
  10. # define MAX_PATH MAXPATHLEN
  11. #endif
  12. #if CM_PLATFORM == CM_PLATFORM_WIN32
  13. # define WIN32_LEAN_AND_MEAN
  14. # if !defined(NOMINMAX) && defined(_MSC_VER)
  15. # define NOMINMAX // required to stop windows.h messing up std::min
  16. # endif
  17. # include <windows.h>
  18. # include <direct.h>
  19. # include <io.h>
  20. #endif
  21. namespace CamelotEngine
  22. {
  23. DataStreamPtr FileSystem::open(const String& fullPath, bool readOnly)
  24. {
  25. // Use filesystem to determine size
  26. // (quicker than streaming to the end and back)
  27. struct stat tagStat;
  28. int ret = stat(fullPath.c_str(), &tagStat);
  29. assert(ret == 0 && "Problem getting file size" );
  30. (void)ret; // Silence warning
  31. // Always open in binary mode
  32. // Also, always include reading
  33. std::ios::openmode mode = std::ios::in | std::ios::binary;
  34. std::istream* baseStream = 0;
  35. std::ifstream* roStream = 0;
  36. std::fstream* rwStream = 0;
  37. if (!readOnly)
  38. {
  39. mode |= std::ios::out;
  40. rwStream = new std::fstream();
  41. rwStream->open(fullPath.c_str(), mode);
  42. baseStream = rwStream;
  43. }
  44. else
  45. {
  46. roStream = new std::ifstream();
  47. roStream->open(fullPath.c_str(), mode);
  48. baseStream = roStream;
  49. }
  50. // Should check ensure open succeeded, in case fail for some reason.
  51. if (baseStream->fail())
  52. {
  53. if(roStream != nullptr)
  54. delete roStream;
  55. if(rwStream != nullptr)
  56. delete rwStream;
  57. CM_EXCEPT(FileNotFoundException, "Cannot open file: " + fullPath);
  58. }
  59. /// Construct return stream, tell it to delete on destroy
  60. FileDataStream* stream = 0;
  61. if (rwStream)
  62. {
  63. // use the writeable stream
  64. stream = new FileDataStream(fullPath, rwStream, (size_t)tagStat.st_size, true);
  65. }
  66. else
  67. {
  68. // read-only stream
  69. stream = new FileDataStream(fullPath, roStream, (size_t)tagStat.st_size, true);
  70. }
  71. return DataStreamPtr(stream);
  72. }
  73. DataStreamPtr FileSystem::create(const String& fullPath)
  74. {
  75. // Always open in binary mode
  76. // Also, always include reading
  77. std::ios::openmode mode = std::ios::out | std::ios::binary;
  78. std::fstream* rwStream = new std::fstream();
  79. rwStream->open(fullPath.c_str(), mode);
  80. // Should check ensure open succeeded, in case fail for some reason.
  81. if (rwStream->fail())
  82. {
  83. if(rwStream != nullptr)
  84. delete rwStream;
  85. CM_EXCEPT(FileNotFoundException, "Cannot open file: " + fullPath);
  86. }
  87. /// Construct return stream, tell it to delete on destroy
  88. FileDataStream* stream = new FileDataStream(fullPath, rwStream, 0, true);
  89. return DataStreamPtr(stream);
  90. }
  91. void FileSystem::remove(const String& fullPath)
  92. {
  93. ::remove(fullPath.c_str());
  94. }
  95. }