CmFileSystem.cpp 3.8 KB

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