The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
5.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "../../core/juce_StandardHeader.h"
  19. BEGIN_JUCE_NAMESPACE
  20. #include "juce_DirectoryIterator.h"
  21. //==============================================================================
  22. DirectoryIterator::DirectoryIterator (const File& directory,
  23. bool isRecursive_,
  24. const String& wildCard_,
  25. const int whatToLookFor_)
  26. : fileFinder (directory, isRecursive_ ? "*" : wildCard_),
  27. wildCard (wildCard_),
  28. path (File::addTrailingSeparator (directory.getFullPathName())),
  29. index (-1),
  30. totalNumFiles (-1),
  31. whatToLookFor (whatToLookFor_),
  32. isRecursive (isRecursive_),
  33. hasBeenAdvanced (false)
  34. {
  35. // you have to specify the type of files you're looking for!
  36. jassert ((whatToLookFor_ & (File::findFiles | File::findDirectories)) != 0);
  37. jassert (whatToLookFor_ > 0 && whatToLookFor_ <= 7);
  38. }
  39. DirectoryIterator::~DirectoryIterator()
  40. {
  41. }
  42. bool DirectoryIterator::next()
  43. {
  44. return next (nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
  45. }
  46. bool DirectoryIterator::next (bool* const isDirResult, bool* const isHiddenResult, int64* const fileSize,
  47. Time* const modTime, Time* const creationTime, bool* const isReadOnly)
  48. {
  49. hasBeenAdvanced = true;
  50. if (subIterator != nullptr)
  51. {
  52. if (subIterator->next (isDirResult, isHiddenResult, fileSize, modTime, creationTime, isReadOnly))
  53. return true;
  54. subIterator = nullptr;
  55. }
  56. String filename;
  57. bool isDirectory, isHidden;
  58. while (fileFinder.next (filename, &isDirectory, &isHidden, fileSize, modTime, creationTime, isReadOnly))
  59. {
  60. ++index;
  61. if (! filename.containsOnly ("."))
  62. {
  63. const File fileFound (path + filename, 0);
  64. bool matches = false;
  65. if (isDirectory)
  66. {
  67. if (isRecursive && ((whatToLookFor & File::ignoreHiddenFiles) == 0 || ! isHidden))
  68. subIterator = new DirectoryIterator (fileFound, true, wildCard, whatToLookFor);
  69. matches = (whatToLookFor & File::findDirectories) != 0;
  70. }
  71. else
  72. {
  73. matches = (whatToLookFor & File::findFiles) != 0;
  74. }
  75. // if recursive, we're not relying on the OS iterator to do the wildcard match, so do it now..
  76. if (matches && isRecursive)
  77. matches = filename.matchesWildcard (wildCard, ! File::areFileNamesCaseSensitive());
  78. if (matches && (whatToLookFor & File::ignoreHiddenFiles) != 0)
  79. matches = ! isHidden;
  80. if (matches)
  81. {
  82. currentFile = fileFound;
  83. if (isHiddenResult != nullptr) *isHiddenResult = isHidden;
  84. if (isDirResult != nullptr) *isDirResult = isDirectory;
  85. return true;
  86. }
  87. else if (subIterator != nullptr)
  88. {
  89. return next();
  90. }
  91. }
  92. }
  93. return false;
  94. }
  95. const File DirectoryIterator::getFile() const
  96. {
  97. if (subIterator != nullptr && subIterator->hasBeenAdvanced)
  98. return subIterator->getFile();
  99. // You need to call DirectoryIterator::next() before asking it for the file that it found!
  100. jassert (hasBeenAdvanced);
  101. return currentFile;
  102. }
  103. float DirectoryIterator::getEstimatedProgress() const
  104. {
  105. if (totalNumFiles < 0)
  106. totalNumFiles = File (path).getNumberOfChildFiles (File::findFilesAndDirectories);
  107. if (totalNumFiles <= 0)
  108. return 0.0f;
  109. const float detailedIndex = (subIterator != nullptr) ? index + subIterator->getEstimatedProgress()
  110. : (float) index;
  111. return detailedIndex / totalNumFiles;
  112. }
  113. END_JUCE_NAMESPACE