CmFileSystem.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. bool FileSystem::isFile(const WString& fullPath)
  80. {
  81. if(exists(fullPath.c_str()) && !is_directory(fullPath.c_str()))
  82. return true;
  83. return false;
  84. }
  85. bool FileSystem::isFile(const WPath& fullPath)
  86. {
  87. if(exists(fullPath) && !is_directory(fullPath))
  88. return true;
  89. return false;
  90. }
  91. bool FileSystem::isDirectory(const WString& fullPath)
  92. {
  93. if(exists(fullPath.c_str()) && is_directory(fullPath.c_str()))
  94. return true;
  95. return false;
  96. }
  97. bool FileSystem::isDirectory(const WPath& fullPath)
  98. {
  99. if(exists(fullPath) && is_directory(fullPath))
  100. return true;
  101. return false;
  102. }
  103. void FileSystem::createDir(const WString& fullPath)
  104. {
  105. create_directory(fullPath.c_str());
  106. }
  107. void FileSystem::createDir(const WPath& fullPath)
  108. {
  109. create_directory(fullPath);
  110. }
  111. void FileSystem::getChildren(const WPath& dirPath, Vector<WPath>::type& files, Vector<WPath>::type& directories)
  112. {
  113. directory_iterator dirIter(dirPath.c_str());
  114. Vector<WString>::type foundFiles;
  115. while(dirIter != directory_iterator())
  116. {
  117. boost::filesystem3::path curPath = dirIter->path();
  118. if(is_regular_file(curPath))
  119. files.push_back(curPath);
  120. else if(is_directory(curPath))
  121. directories.push_back(curPath);
  122. dirIter++;
  123. }
  124. }
  125. std::time_t FileSystem::getLastModifiedTime(const WString& fullPath)
  126. {
  127. return last_write_time(fullPath.c_str());
  128. }
  129. WString FileSystem::getWorkingDirectoryPath()
  130. {
  131. return current_path().wstring().c_str();
  132. }
  133. WString FileSystem::getParentDirectory(const WString& path)
  134. {
  135. boost::filesystem3::path p(path.c_str());
  136. if(!is_directory(p))
  137. {
  138. boost::filesystem3::path dir = p.parent_path();
  139. return dir.wstring().c_str();
  140. }
  141. return path;
  142. }
  143. }