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.

161 lines
5.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 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. void* juce_findFileStart (const String& directory, const String& wildCard, String& firstResultFile,
  22. bool* isDirectory, bool* isHidden, int64* fileSize,
  23. Time* modTime, Time* creationTime, bool* isReadOnly);
  24. bool juce_findFileNext (void* handle, String& resultFile,
  25. bool* isDirectory, bool* isHidden, int64* fileSize,
  26. Time* modTime, Time* creationTime, bool* isReadOnly);
  27. void juce_findFileClose (void* handle);
  28. //==============================================================================
  29. DirectoryIterator::DirectoryIterator (const File& directory,
  30. bool isRecursive,
  31. const String& wc,
  32. const int whatToLookFor_)
  33. : wildCard (wc),
  34. index (-1),
  35. whatToLookFor (whatToLookFor_)
  36. {
  37. // you have to specify the type of files you're looking for!
  38. jassert ((whatToLookFor_ & (File::findFiles | File::findDirectories)) != 0);
  39. jassert (whatToLookFor_ > 0 && whatToLookFor_ <= 7);
  40. String path (directory.getFullPathName());
  41. if (! path.endsWithChar (File::separator))
  42. path += File::separator;
  43. String filename;
  44. bool isDirectory, isHidden;
  45. void* const handle = juce_findFileStart (path,
  46. isRecursive ? "*" : wc,
  47. filename, &isDirectory, &isHidden, 0, 0, 0, 0);
  48. if (handle != 0)
  49. {
  50. do
  51. {
  52. if (! filename.containsOnly ("."))
  53. {
  54. bool addToList = false;
  55. if (isDirectory)
  56. {
  57. if (isRecursive
  58. && ((whatToLookFor_ & File::ignoreHiddenFiles) == 0
  59. || ! isHidden))
  60. {
  61. dirsFound.add (File (path + filename, 0));
  62. }
  63. addToList = (whatToLookFor_ & File::findDirectories) != 0;
  64. }
  65. else
  66. {
  67. addToList = (whatToLookFor_ & File::findFiles) != 0;
  68. }
  69. // if it's recursive, we're not relying on the OS iterator
  70. // to do the wildcard match, so do it now..
  71. if (isRecursive && addToList)
  72. addToList = filename.matchesWildcard (wc, true);
  73. if (addToList && (whatToLookFor_ & File::ignoreHiddenFiles) != 0)
  74. addToList = ! isHidden;
  75. if (addToList)
  76. filesFound.add (File (path + filename, 0));
  77. }
  78. } while (juce_findFileNext (handle, filename, &isDirectory, &isHidden, 0, 0, 0, 0));
  79. juce_findFileClose (handle);
  80. }
  81. }
  82. DirectoryIterator::~DirectoryIterator()
  83. {
  84. }
  85. bool DirectoryIterator::next()
  86. {
  87. if (subIterator != 0)
  88. {
  89. if (subIterator->next())
  90. return true;
  91. subIterator = 0;
  92. }
  93. if (index >= filesFound.size() + dirsFound.size() - 1)
  94. return false;
  95. ++index;
  96. if (index >= filesFound.size())
  97. {
  98. subIterator = new DirectoryIterator (dirsFound.getReference (index - filesFound.size()),
  99. true, wildCard, whatToLookFor);
  100. return next();
  101. }
  102. return true;
  103. }
  104. const File DirectoryIterator::getFile() const
  105. {
  106. if (subIterator != 0)
  107. return subIterator->getFile();
  108. return filesFound [index];
  109. }
  110. float DirectoryIterator::getEstimatedProgress() const
  111. {
  112. if (filesFound.size() + dirsFound.size() == 0)
  113. {
  114. return 0.0f;
  115. }
  116. else
  117. {
  118. const float detailedIndex = (subIterator != 0) ? index + subIterator->getEstimatedProgress()
  119. : (float) index;
  120. return detailedIndex / (filesFound.size() + dirsFound.size());
  121. }
  122. }
  123. END_JUCE_NAMESPACE