CmFileSystem.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include "CmFileSystem.h"
  2. #include "CmDataStream.h"
  3. #include "CmPath.h"
  4. #include "CmException.h"
  5. #include "CmDebug.h"
  6. #include <boost/filesystem.hpp>
  7. using namespace boost::filesystem3;
  8. namespace CamelotFramework
  9. {
  10. DataStreamPtr FileSystem::openFile(const WString& fullPath, bool readOnly)
  11. {
  12. UINT64 fileSize = getFileSize(fullPath);
  13. // Always open in binary mode
  14. // Also, always include reading
  15. std::ios::openmode mode = std::ios::in | std::ios::binary;
  16. std::shared_ptr<std::istream> baseStream = 0;
  17. std::shared_ptr<std::ifstream> roStream = 0;
  18. std::shared_ptr<std::fstream> rwStream = 0;
  19. if (!readOnly)
  20. {
  21. mode |= std::ios::out;
  22. rwStream = cm_shared_ptr<std::fstream, ScratchAlloc>();
  23. rwStream->open(fullPath.c_str(), mode);
  24. baseStream = rwStream;
  25. }
  26. else
  27. {
  28. roStream = cm_shared_ptr<std::ifstream, ScratchAlloc>();
  29. roStream->open(fullPath.c_str(), mode);
  30. baseStream = roStream;
  31. }
  32. // Should check ensure open succeeded, in case fail for some reason.
  33. if (baseStream->fail())
  34. CM_EXCEPT(FileNotFoundException, "Cannot open file: " + toString(fullPath));
  35. /// Construct return stream, tell it to delete on destroy
  36. FileDataStream* stream = 0;
  37. if (rwStream)
  38. {
  39. // use the writeable stream
  40. stream = cm_new<FileDataStream, ScratchAlloc>(rwStream, (size_t)fileSize, true);
  41. }
  42. else
  43. {
  44. // read-only stream
  45. stream = cm_new<FileDataStream, ScratchAlloc>(roStream, (size_t)fileSize, true);
  46. }
  47. return cm_shared_ptr<FileDataStream, ScratchAlloc>(stream);
  48. }
  49. DataStreamPtr FileSystem::createAndOpenFile(const WString& fullPath)
  50. {
  51. // Always open in binary mode
  52. // Also, always include reading
  53. std::ios::openmode mode = std::ios::out | std::ios::binary;
  54. std::shared_ptr<std::fstream> rwStream = cm_shared_ptr<std::fstream, ScratchAlloc>();
  55. rwStream->open(fullPath.c_str(), mode);
  56. // Should check ensure open succeeded, in case fail for some reason.
  57. if (rwStream->fail())
  58. CM_EXCEPT(FileNotFoundException, "Cannot open file: " + toString(fullPath));
  59. /// Construct return stream, tell it to delete on destroy
  60. return cm_shared_ptr<FileDataStream, ScratchAlloc>(rwStream, 0, true);
  61. }
  62. UINT64 FileSystem::getFileSize(const WString& fullPath)
  63. {
  64. return file_size(fullPath.c_str());
  65. }
  66. void FileSystem::remove(const WString& fullPath, bool recursively)
  67. {
  68. if(recursively)
  69. boost::filesystem3::remove_all(fullPath.c_str());
  70. else
  71. boost::filesystem3::remove(fullPath.c_str());
  72. }
  73. void FileSystem::move(const WString& oldPath, const WString& newPath, bool overwriteExisting)
  74. {
  75. boost::filesystem3::path oldPathInternal = oldPath.c_str();
  76. boost::filesystem3::path newPathInternal = newPath.c_str();
  77. if(boost::filesystem3::exists(newPathInternal))
  78. {
  79. if(overwriteExisting)
  80. boost::filesystem3::remove_all(newPathInternal);
  81. else
  82. {
  83. CM_EXCEPT(InvalidStateException, "Move operation failed because another file already exists at the new path: \"" + toString(WString(newPathInternal.c_str())) + "\"");
  84. }
  85. }
  86. boost::filesystem3::rename(oldPathInternal, newPathInternal);
  87. }
  88. bool FileSystem::exists(const WString& fullPath)
  89. {
  90. return boost::filesystem3::exists(fullPath.c_str());
  91. }
  92. bool FileSystem::isFile(const WString& fullPath)
  93. {
  94. if(boost::filesystem3::exists(fullPath.c_str()) && !is_directory(fullPath.c_str()))
  95. return true;
  96. return false;
  97. }
  98. bool FileSystem::isDirectory(const WString& fullPath)
  99. {
  100. if(boost::filesystem3::exists(fullPath.c_str()) && is_directory(fullPath.c_str()))
  101. return true;
  102. return false;
  103. }
  104. void FileSystem::createDir(const WString& fullPath)
  105. {
  106. boost::filesystem3::path fullPathInternal = fullPath.c_str();
  107. if(fullPathInternal.empty())
  108. return;
  109. boost::filesystem3::path parentPath = fullPathInternal;
  110. auto pathEnd = fullPathInternal.end();
  111. while(!boost::filesystem3::exists(parentPath))
  112. {
  113. if(pathEnd == fullPathInternal.begin())
  114. break;
  115. parentPath = parentPath.parent_path();
  116. pathEnd--;
  117. }
  118. for(; pathEnd != fullPathInternal.end(); ++pathEnd)
  119. {
  120. create_directory(parentPath);
  121. parentPath /= *pathEnd;
  122. }
  123. create_directory(parentPath);
  124. }
  125. void FileSystem::getChildren(const WString& dirPath, Vector<WString>::type& files, Vector<WString>::type& directories)
  126. {
  127. directory_iterator dirIter(dirPath.c_str());
  128. Vector<WString>::type foundFiles;
  129. while(dirIter != directory_iterator())
  130. {
  131. boost::filesystem3::path curPath = dirIter->path();
  132. if(is_regular_file(curPath))
  133. files.push_back(curPath.c_str());
  134. else if(is_directory(curPath))
  135. directories.push_back(curPath.c_str());
  136. dirIter++;
  137. }
  138. }
  139. std::time_t FileSystem::getLastModifiedTime(const WString& fullPath)
  140. {
  141. return last_write_time(fullPath.c_str());
  142. }
  143. WString FileSystem::getWorkingDirectoryPath()
  144. {
  145. return current_path().wstring().c_str();
  146. }
  147. WString FileSystem::getParentDirectory(const WString& fullPath)
  148. {
  149. boost::filesystem3::path p(fullPath.c_str());
  150. if(!is_directory(p))
  151. {
  152. boost::filesystem3::path dir = p.parent_path();
  153. return dir.wstring().c_str();
  154. }
  155. return fullPath;
  156. }
  157. }