CmFileSystem.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. remove_all(fullPath.c_str());
  69. else
  70. boost::filesystem3::remove(fullPath.c_str());
  71. }
  72. bool FileSystem::fileExists(const WString& fullPath)
  73. {
  74. if(exists(fullPath.c_str()) && !is_directory(fullPath.c_str()))
  75. return true;
  76. return false;
  77. }
  78. bool FileSystem::dirExists(const WString& fullPath)
  79. {
  80. if(exists(fullPath.c_str()) && is_directory(fullPath.c_str()))
  81. return true;
  82. return false;
  83. }
  84. void FileSystem::createDir(const WString& fullPath)
  85. {
  86. create_directory(fullPath.c_str());
  87. }
  88. Vector<WString>::type FileSystem::getFiles(const WString& dirPath)
  89. {
  90. directory_iterator dirIter(dirPath.c_str());
  91. Vector<WString>::type foundFiles;
  92. while(dirIter != directory_iterator())
  93. {
  94. if(is_regular_file(dirIter->path()))
  95. foundFiles.push_back(dirIter->path().wstring().c_str());
  96. dirIter++;
  97. }
  98. return foundFiles;
  99. }
  100. WString FileSystem::getWorkingDirectoryPath()
  101. {
  102. return current_path().wstring().c_str();
  103. }
  104. WString FileSystem::getParentDirectory(const WString& path)
  105. {
  106. boost::filesystem3::path p(path.c_str());
  107. if(!is_directory(p))
  108. {
  109. boost::filesystem3::path dir = p.parent_path();
  110. return dir.wstring().c_str();
  111. }
  112. return path;
  113. }
  114. }