CmFileSystem.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include "CmFileSystem.h"
  2. #include "CmDataStream.h"
  3. #include "CmPath.h"
  4. #include "CmException.h"
  5. #include <boost/filesystem.hpp>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #if CM_PLATFORM == CM_PLATFORM_LINUX || CM_PLATFORM == CM_PLATFORM_APPLE || \
  9. CM_PLATFORM == CM_PLATFORM_SYMBIAN || CM_PLATFORM == CM_PLATFORM_IPHONE
  10. # include <sys/param.h>
  11. # define MAX_PATH MAXPATHLEN
  12. #endif
  13. #if CM_PLATFORM == CM_PLATFORM_WIN32
  14. # define WIN32_LEAN_AND_MEAN
  15. # if !defined(NOMINMAX) && defined(_MSC_VER)
  16. # define NOMINMAX // required to stop windows.h messing up std::min
  17. # endif
  18. # include <windows.h>
  19. # include <direct.h>
  20. # include <io.h>
  21. #endif
  22. namespace CamelotFramework
  23. {
  24. DataStreamPtr FileSystem::open(const String& fullPath, bool readOnly)
  25. {
  26. // Use filesystem to determine size
  27. // (quicker than streaming to the end and back)
  28. struct stat tagStat;
  29. int ret = stat(fullPath.c_str(), &tagStat);
  30. assert(ret == 0 && "Problem getting file size" );
  31. (void)ret; // Silence warning
  32. // Always open in binary mode
  33. // Also, always include reading
  34. std::ios::openmode mode = std::ios::in | std::ios::binary;
  35. std::shared_ptr<std::istream> baseStream = 0;
  36. std::shared_ptr<std::ifstream> roStream = 0;
  37. std::shared_ptr<std::fstream> rwStream = 0;
  38. if (!readOnly)
  39. {
  40. mode |= std::ios::out;
  41. rwStream = cm_shared_ptr<std::fstream, ScratchAlloc>();
  42. rwStream->open(fullPath.c_str(), mode);
  43. baseStream = rwStream;
  44. }
  45. else
  46. {
  47. roStream = cm_shared_ptr<std::ifstream, ScratchAlloc>();
  48. roStream->open(fullPath.c_str(), mode);
  49. baseStream = roStream;
  50. }
  51. // Should check ensure open succeeded, in case fail for some reason.
  52. if (baseStream->fail())
  53. CM_EXCEPT(FileNotFoundException, "Cannot open file: " + fullPath);
  54. /// Construct return stream, tell it to delete on destroy
  55. FileDataStream* stream = 0;
  56. if (rwStream)
  57. {
  58. // use the writeable stream
  59. stream = cm_new<FileDataStream, ScratchAlloc>(fullPath, rwStream, (size_t)tagStat.st_size, true);
  60. }
  61. else
  62. {
  63. // read-only stream
  64. stream = cm_new<FileDataStream, ScratchAlloc>(fullPath, roStream, (size_t)tagStat.st_size, true);
  65. }
  66. return DataStreamPtr(stream, &MemAllocDeleter<FileDataStream, ScratchAlloc>::deleter);
  67. }
  68. DataStreamPtr FileSystem::create(const String& fullPath)
  69. {
  70. // Always open in binary mode
  71. // Also, always include reading
  72. std::ios::openmode mode = std::ios::out | std::ios::binary;
  73. std::shared_ptr<std::fstream> rwStream = cm_shared_ptr<std::fstream, ScratchAlloc>();
  74. rwStream->open(fullPath.c_str(), mode);
  75. // Should check ensure open succeeded, in case fail for some reason.
  76. if (rwStream->fail())
  77. CM_EXCEPT(FileNotFoundException, "Cannot open file: " + fullPath);
  78. /// Construct return stream, tell it to delete on destroy
  79. return cm_shared_ptr<FileDataStream, ScratchAlloc>(fullPath, rwStream, 0, true);
  80. }
  81. void FileSystem::remove(const String& fullPath)
  82. {
  83. ::remove(fullPath.c_str());
  84. }
  85. bool FileSystem::fileExists(const String& fullPath)
  86. {
  87. if(boost::filesystem::exists(fullPath) && !boost::filesystem::is_directory(fullPath))
  88. return true;
  89. return false;
  90. }
  91. bool FileSystem::dirExists(const String& fullPath)
  92. {
  93. if(boost::filesystem::exists(fullPath) && boost::filesystem::is_directory(fullPath))
  94. return true;
  95. return false;
  96. }
  97. void FileSystem::createDir(const String& fullPath)
  98. {
  99. boost::filesystem::create_directory(fullPath);
  100. }
  101. void FileSystem::deleteDir(const String& fullPath)
  102. {
  103. boost::filesystem::remove_all(fullPath);
  104. }
  105. vector<String>::type FileSystem::getFiles(const String& dirPath)
  106. {
  107. boost::filesystem::directory_iterator dirIter(dirPath);
  108. vector<String>::type foundFiles;
  109. while(dirIter != boost::filesystem::directory_iterator())
  110. {
  111. if(boost::filesystem::is_regular_file(dirIter->path()))
  112. foundFiles.push_back(dirIter->path().string());
  113. dirIter++;
  114. }
  115. return foundFiles;
  116. }
  117. String FileSystem::getCurrentPath()
  118. {
  119. return boost::filesystem::current_path().string();
  120. }
  121. bool FileSystem::isValidFileName(const String& name)
  122. {
  123. return boost::filesystem::portable_file_name(name);
  124. }
  125. String FileSystem::getDirectoryPath(const String& path)
  126. {
  127. boost::filesystem::path p(path);
  128. if(!is_directory(p))
  129. {
  130. boost::filesystem::path dir = p.parent_path();
  131. return dir.string();
  132. }
  133. return path;
  134. }
  135. }