CmFileSystem.cpp 5.2 KB

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