CFileList.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "CFileList.h"
  5. #include "IrrCompileConfig.h"
  6. #include "irrArray.h"
  7. #include "coreutil.h"
  8. #include "os.h"
  9. namespace irr
  10. {
  11. namespace io
  12. {
  13. static const io::path emptyFileListEntry;
  14. CFileList::CFileList(const io::path& path, bool ignoreCase, bool ignorePaths)
  15. : IgnorePaths(ignorePaths), IgnoreCase(ignoreCase), Path(path)
  16. {
  17. #ifdef _DEBUG
  18. setDebugName("CFileList");
  19. #endif
  20. Path.replace('\\', '/');
  21. }
  22. CFileList::~CFileList()
  23. {
  24. Files.clear();
  25. }
  26. u32 CFileList::getFileCount() const
  27. {
  28. return Files.size();
  29. }
  30. void CFileList::sort()
  31. {
  32. Files.sort();
  33. }
  34. const io::path& CFileList::getFileName(u32 index) const
  35. {
  36. if (index >= Files.size())
  37. return emptyFileListEntry;
  38. return Files[index].Name;
  39. }
  40. //! Gets the full name of a file in the list, path included, based on an index.
  41. const io::path& CFileList::getFullFileName(u32 index) const
  42. {
  43. if (index >= Files.size())
  44. return emptyFileListEntry;
  45. return Files[index].FullName;
  46. }
  47. //! adds a file or folder
  48. u32 CFileList::addItem(const io::path& fullPath, u32 offset, u32 size, bool isDirectory, u32 id)
  49. {
  50. SFileListEntry entry;
  51. entry.ID = id ? id : Files.size();
  52. entry.Offset = offset;
  53. entry.Size = size;
  54. entry.Name = fullPath;
  55. entry.Name.replace('\\', '/');
  56. entry.IsDirectory = isDirectory;
  57. // remove trailing slash
  58. if (entry.Name.lastChar() == '/')
  59. {
  60. entry.IsDirectory = true;
  61. entry.Name[entry.Name.size()-1] = 0;
  62. entry.Name.validate();
  63. }
  64. if (IgnoreCase)
  65. entry.Name.make_lower();
  66. entry.FullName = entry.Name;
  67. core::deletePathFromFilename(entry.Name);
  68. if (IgnorePaths)
  69. entry.FullName = entry.Name;
  70. //os::Printer::log(Path.c_str(), entry.FullName);
  71. Files.push_back(entry);
  72. return Files.size() - 1;
  73. }
  74. //! Returns the ID of a file in the file list, based on an index.
  75. u32 CFileList::getID(u32 index) const
  76. {
  77. return index < Files.size() ? Files[index].ID : 0;
  78. }
  79. bool CFileList::isDirectory(u32 index) const
  80. {
  81. bool ret = false;
  82. if (index < Files.size())
  83. ret = Files[index].IsDirectory;
  84. _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
  85. return ret;
  86. }
  87. //! Returns the size of a file
  88. u32 CFileList::getFileSize(u32 index) const
  89. {
  90. return index < Files.size() ? Files[index].Size : 0;
  91. }
  92. //! Returns the size of a file
  93. u32 CFileList::getFileOffset(u32 index) const
  94. {
  95. return index < Files.size() ? Files[index].Offset : 0;
  96. }
  97. //! Searches for a file or folder within the list, returns the index
  98. s32 CFileList::findFile(const io::path& filename, bool isDirectory = false) const
  99. {
  100. SFileListEntry entry;
  101. // we only need FullName to be set for the search
  102. entry.FullName = filename;
  103. entry.IsDirectory = isDirectory;
  104. // exchange
  105. entry.FullName.replace('\\', '/');
  106. // remove trailing slash
  107. if (entry.FullName.lastChar() == '/')
  108. {
  109. entry.IsDirectory = true;
  110. entry.FullName[entry.FullName.size()-1] = 0;
  111. entry.FullName.validate();
  112. }
  113. if (IgnoreCase)
  114. entry.FullName.make_lower();
  115. if (IgnorePaths)
  116. core::deletePathFromFilename(entry.FullName);
  117. return Files.binary_search(entry);
  118. }
  119. //! Returns the base path of the file list
  120. const io::path& CFileList::getPath() const
  121. {
  122. return Path;
  123. }
  124. } // end namespace irr
  125. } // end namespace io