CmFileSystem.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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::remove(const WPath& fullPath, bool recursively)
  73. {
  74. if(recursively)
  75. boost::filesystem3::remove_all(fullPath);
  76. else
  77. boost::filesystem3::remove(fullPath);
  78. }
  79. void FileSystem::move(const WPath& oldPath, const WPath& newPath, bool overwriteExisting)
  80. {
  81. if(FileSystem::isFile(newPath) || FileSystem::isDirectory(newPath))
  82. {
  83. if(overwriteExisting)
  84. FileSystem::remove(newPath, true);
  85. else
  86. {
  87. CM_EXCEPT(InvalidStateException, "Move operation failed because another file already exists at the new path: \"" + toString(newPath) + "\"");
  88. }
  89. }
  90. boost::filesystem3::rename(oldPath, newPath);
  91. }
  92. bool FileSystem::exists(const WString& fullPath)
  93. {
  94. return boost::filesystem3::exists(fullPath.c_str());
  95. }
  96. bool FileSystem::exists(const WPath& fullPath)
  97. {
  98. return boost::filesystem3::exists(fullPath);
  99. }
  100. bool FileSystem::isFile(const WString& fullPath)
  101. {
  102. if(boost::filesystem3::exists(fullPath.c_str()) && !is_directory(fullPath.c_str()))
  103. return true;
  104. return false;
  105. }
  106. bool FileSystem::isFile(const WPath& fullPath)
  107. {
  108. if(exists(fullPath) && !is_directory(fullPath))
  109. return true;
  110. return false;
  111. }
  112. bool FileSystem::isDirectory(const WString& fullPath)
  113. {
  114. if(boost::filesystem3::exists(fullPath.c_str()) && is_directory(fullPath.c_str()))
  115. return true;
  116. return false;
  117. }
  118. bool FileSystem::isDirectory(const WPath& fullPath)
  119. {
  120. if(boost::filesystem3::exists(fullPath) && is_directory(fullPath))
  121. return true;
  122. return false;
  123. }
  124. void FileSystem::createDir(const WString& fullPath)
  125. {
  126. create_directory(fullPath.c_str());
  127. }
  128. void FileSystem::createDir(const WPath& fullPath)
  129. {
  130. create_directory(fullPath);
  131. }
  132. void FileSystem::getChildren(const WPath& dirPath, Vector<WPath>::type& files, Vector<WPath>::type& directories)
  133. {
  134. directory_iterator dirIter(dirPath.c_str());
  135. Vector<WString>::type foundFiles;
  136. while(dirIter != directory_iterator())
  137. {
  138. boost::filesystem3::path curPath = dirIter->path();
  139. if(is_regular_file(curPath))
  140. files.push_back(curPath);
  141. else if(is_directory(curPath))
  142. directories.push_back(curPath);
  143. dirIter++;
  144. }
  145. }
  146. std::time_t FileSystem::getLastModifiedTime(const WString& fullPath)
  147. {
  148. return last_write_time(fullPath.c_str());
  149. }
  150. std::time_t FileSystem::getLastModifiedTime(const WPath& fullPath)
  151. {
  152. return last_write_time(fullPath);
  153. }
  154. WString FileSystem::getWorkingDirectoryPath()
  155. {
  156. return current_path().wstring().c_str();
  157. }
  158. WString FileSystem::getParentDirectory(const WString& path)
  159. {
  160. boost::filesystem3::path p(path.c_str());
  161. if(!is_directory(p))
  162. {
  163. boost::filesystem3::path dir = p.parent_path();
  164. return dir.wstring().c_str();
  165. }
  166. return path;
  167. }
  168. }