FolderScanner.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * FolderScanner.cpp
  3. * -----------------
  4. * Purpose: Class for finding files in folders.
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include "stdafx.h"
  10. #include "FolderScanner.h"
  11. #include <tchar.h>
  12. OPENMPT_NAMESPACE_BEGIN
  13. FolderScanner::FolderScanner(const mpt::PathString &path, FlagSet<ScanType> type, mpt::PathString filter)
  14. : m_paths(1, path)
  15. , m_filter(std::move(filter))
  16. , m_hFind(INVALID_HANDLE_VALUE)
  17. , m_type(type)
  18. {
  19. MemsetZero(m_wfd);
  20. }
  21. FolderScanner::~FolderScanner()
  22. {
  23. FindClose(m_hFind);
  24. }
  25. bool FolderScanner::Next(mpt::PathString &file)
  26. {
  27. bool found = false;
  28. do
  29. {
  30. if(m_hFind == INVALID_HANDLE_VALUE)
  31. {
  32. if(m_paths.empty())
  33. {
  34. return false;
  35. }
  36. m_currentPath = m_paths.back();
  37. m_paths.pop_back();
  38. m_currentPath.EnsureTrailingSlash();
  39. m_hFind = FindFirstFile((m_currentPath + m_filter).AsNative().c_str(), &m_wfd);
  40. }
  41. BOOL nextFile = FALSE;
  42. if(m_hFind != INVALID_HANDLE_VALUE)
  43. {
  44. do
  45. {
  46. file = m_currentPath + mpt::PathString::FromNative(m_wfd.cFileName);
  47. if(m_wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  48. {
  49. if(_tcscmp(m_wfd.cFileName, _T("..")) && _tcscmp(m_wfd.cFileName, _T(".")))
  50. {
  51. if(m_type[kFindInSubDirectories])
  52. {
  53. // Add sub directory
  54. m_paths.push_back(file);
  55. }
  56. if(m_type[kOnlyDirectories])
  57. {
  58. found = true;
  59. }
  60. }
  61. } else if(m_type[kOnlyFiles])
  62. {
  63. found = true;
  64. }
  65. } while((nextFile = FindNextFile(m_hFind, &m_wfd)) != FALSE && !found);
  66. }
  67. if(nextFile == FALSE)
  68. {
  69. // Done with this directory, advance to next
  70. if(m_hFind != INVALID_HANDLE_VALUE)
  71. {
  72. FindClose(m_hFind);
  73. }
  74. m_hFind = INVALID_HANDLE_VALUE;
  75. }
  76. } while(!found);
  77. return true;
  78. }
  79. OPENMPT_NAMESPACE_END