FileEnum.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "EditorDefs.h"
  9. #include "FileEnum.h"
  10. CFileEnum::CFileEnum()
  11. : m_hEnumFile(nullptr)
  12. {
  13. }
  14. CFileEnum::~CFileEnum()
  15. {
  16. if (m_hEnumFile)
  17. {
  18. delete m_hEnumFile;
  19. m_hEnumFile = nullptr;
  20. }
  21. }
  22. bool CFileEnum::StartEnumeration(
  23. const QString& szEnumPath,
  24. const QString& szEnumPattern,
  25. QFileInfo* pFile)
  26. {
  27. //////////////////////////////////////////////////////////////////////
  28. // Take path and search pattern as separate arguments
  29. //////////////////////////////////////////////////////////////////////
  30. // Build enumeration path
  31. QString szPath = szEnumPath;
  32. if (!szPath.endsWith('\\') && !szPath.endsWith('/'))
  33. {
  34. szPath += QDir::separator();
  35. }
  36. szPath += szEnumPattern;
  37. return StartEnumeration(szPath, pFile);
  38. }
  39. bool CFileEnum::StartEnumeration(const QString& szEnumPathAndPattern, QFileInfo* pFile)
  40. {
  41. // End any previous enumeration
  42. if (m_hEnumFile)
  43. {
  44. delete m_hEnumFile;
  45. m_hEnumFile = nullptr;
  46. }
  47. QStringList parts = szEnumPathAndPattern.split(QRegularExpression(R"([\\/])"));
  48. QString pattern = parts.takeLast();
  49. QString path = parts.join(QDir::separator());
  50. // Start the enumeration
  51. m_hEnumFile = new QDirIterator(path, QStringList(pattern));
  52. if (!m_hEnumFile->hasNext())
  53. {
  54. // No files found
  55. delete m_hEnumFile;
  56. m_hEnumFile = nullptr;
  57. return false;
  58. }
  59. m_hEnumFile->next();
  60. *pFile = m_hEnumFile->fileInfo();
  61. return true;
  62. }
  63. bool CFileEnum::GetNextFile(QFileInfo* pFile)
  64. {
  65. // Fill file strcuture
  66. if (!m_hEnumFile->hasNext())
  67. {
  68. // No more files left
  69. delete m_hEnumFile;
  70. m_hEnumFile = nullptr;
  71. return false;
  72. }
  73. m_hEnumFile->next();
  74. *pFile = m_hEnumFile->fileInfo();
  75. // At least one file left
  76. return true;
  77. }
  78. inline bool ScanDirectoryRecursive(
  79. const QString& root,
  80. const QString& path,
  81. const QString& file,
  82. QStringList& files,
  83. bool bRecursive)
  84. {
  85. bool bFoundAny = false;
  86. QDirIterator hFile(root + path, QStringList(file));
  87. if (hFile.hasNext())
  88. {
  89. // Find the rest of the files.
  90. do
  91. {
  92. hFile.next();
  93. QFileInfo foundFile = hFile.fileInfo();
  94. bFoundAny = true;
  95. files.push_back(path + foundFile.fileName());
  96. } while (hFile.hasNext());
  97. }
  98. if (bRecursive)
  99. {
  100. QDirIterator hFile2(root + path, QStringList("*.*"));
  101. if (hFile2.hasNext())
  102. {
  103. // Find directories.
  104. do
  105. {
  106. hFile2.next();
  107. QFileInfo foundFile = hFile2.fileInfo();
  108. if (foundFile.isDir())
  109. {
  110. // If recursive.
  111. if (!foundFile.fileName().startsWith('.'))
  112. {
  113. if (ScanDirectoryRecursive(
  114. root,
  115. path + foundFile.fileName() + QDir::separator(),
  116. file,
  117. files,
  118. bRecursive))
  119. {
  120. bFoundAny = true;
  121. }
  122. }
  123. }
  124. } while (hFile2.hasNext());
  125. }
  126. }
  127. return bFoundAny;
  128. }
  129. bool CFileEnum::ScanDirectory(
  130. const QString& path,
  131. const QString& file,
  132. QStringList& files,
  133. bool bRecursive,
  134. bool /* bSkipPaks - unused, left for backwards API compatibility */)
  135. {
  136. return ScanDirectoryRecursive(path, "", file, files, bRecursive);
  137. }