CFileList.cpp 4.1 KB

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