CMountPointReader.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #include "CMountPointReader.h"
  5. #ifdef __IRR_COMPILE_WITH_MOUNT_ARCHIVE_LOADER_
  6. #include "CReadFile.h"
  7. #include "os.h"
  8. namespace irr
  9. {
  10. namespace io
  11. {
  12. //! Constructor
  13. CArchiveLoaderMount::CArchiveLoaderMount( io::IFileSystem* fs)
  14. : FileSystem(fs)
  15. {
  16. #ifdef _DEBUG
  17. setDebugName("CArchiveLoaderMount");
  18. #endif
  19. }
  20. //! returns true if the file maybe is able to be loaded by this class
  21. bool CArchiveLoaderMount::isALoadableFileFormat(const io::path& filename) const
  22. {
  23. io::path fname(filename);
  24. deletePathFromFilename(fname);
  25. if (!fname.size())
  26. return true;
  27. IFileList* list = FileSystem->createFileList();
  28. bool ret = false;
  29. if (list)
  30. {
  31. // check if name is found as directory
  32. if (list->findFile(filename, true))
  33. ret=true;
  34. list->drop();
  35. }
  36. return ret;
  37. }
  38. //! Check to see if the loader can create archives of this type.
  39. bool CArchiveLoaderMount::isALoadableFileFormat(E_FILE_ARCHIVE_TYPE fileType) const
  40. {
  41. return fileType == EFAT_FOLDER;
  42. }
  43. //! Check if the file might be loaded by this class
  44. bool CArchiveLoaderMount::isALoadableFileFormat(io::IReadFile* file) const
  45. {
  46. return false;
  47. }
  48. //! Creates an archive from the filename
  49. IFileArchive* CArchiveLoaderMount::createArchive(const io::path& filename, bool ignoreCase, bool ignorePaths) const
  50. {
  51. IFileArchive *archive = 0;
  52. EFileSystemType current = FileSystem->setFileListSystem(FILESYSTEM_NATIVE);
  53. const io::path save = FileSystem->getWorkingDirectory();
  54. io::path fullPath = FileSystem->getAbsolutePath(filename);
  55. FileSystem->flattenFilename(fullPath);
  56. if (FileSystem->changeWorkingDirectoryTo(fullPath))
  57. {
  58. archive = new CMountPointReader(FileSystem, fullPath, ignoreCase, ignorePaths);
  59. }
  60. FileSystem->changeWorkingDirectoryTo(save);
  61. FileSystem->setFileListSystem(current);
  62. return archive;
  63. }
  64. //! creates/loads an archive from the file.
  65. //! \return Pointer to the created archive. Returns 0 if loading failed.
  66. IFileArchive* CArchiveLoaderMount::createArchive(io::IReadFile* file, bool ignoreCase, bool ignorePaths) const
  67. {
  68. return 0;
  69. }
  70. //! compatible Folder Architecture
  71. CMountPointReader::CMountPointReader(IFileSystem * parent, const io::path& basename, bool ignoreCase, bool ignorePaths)
  72. : CFileList(basename, ignoreCase, ignorePaths), Parent(parent)
  73. {
  74. //! ensure CFileList path ends in a slash
  75. if (Path.lastChar() != '/' )
  76. Path.append('/');
  77. const io::path& work = Parent->getWorkingDirectory();
  78. Parent->changeWorkingDirectoryTo(basename);
  79. buildDirectory();
  80. Parent->changeWorkingDirectoryTo(work);
  81. sort();
  82. }
  83. //! returns the list of files
  84. const IFileList* CMountPointReader::getFileList() const
  85. {
  86. return this;
  87. }
  88. void CMountPointReader::buildDirectory()
  89. {
  90. IFileList * list = Parent->createFileList();
  91. if (!list)
  92. return;
  93. const u32 size = list->getFileCount();
  94. for (u32 i=0; i < size; ++i)
  95. {
  96. io::path full = list->getFullFileName(i);
  97. full = full.subString(Path.size(), full.size() - Path.size());
  98. if (!list->isDirectory(i))
  99. {
  100. addItem(full, list->getFileOffset(i), list->getFileSize(i), false, RealFileNames.size());
  101. RealFileNames.push_back(list->getFullFileName(i));
  102. }
  103. else
  104. {
  105. const io::path rel = list->getFileName(i);
  106. RealFileNames.push_back(list->getFullFileName(i));
  107. io::path pwd = Parent->getWorkingDirectory();
  108. if (pwd.lastChar() != '/')
  109. pwd.append('/');
  110. pwd.append(rel);
  111. if ( rel != "." && rel != ".." )
  112. {
  113. addItem(full, 0, 0, true, 0);
  114. Parent->changeWorkingDirectoryTo(pwd);
  115. buildDirectory();
  116. Parent->changeWorkingDirectoryTo("..");
  117. }
  118. }
  119. }
  120. list->drop();
  121. }
  122. //! opens a file by index
  123. IReadFile* CMountPointReader::createAndOpenFile(u32 index)
  124. {
  125. if (index >= Files.size())
  126. return 0;
  127. return createReadFile(RealFileNames[Files[index].ID]);
  128. }
  129. //! opens a file by file name
  130. IReadFile* CMountPointReader::createAndOpenFile(const io::path& filename)
  131. {
  132. s32 index = findFile(filename, false);
  133. if (index != -1)
  134. return createAndOpenFile(index);
  135. else
  136. return 0;
  137. }
  138. } // io
  139. } // irr
  140. #endif // __IRR_COMPILE_WITH_MOUNT_ARCHIVE_LOADER_