CFileList.cpp 3.1 KB

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