CFileList.cpp 3.4 KB

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