Audio plugin host https://kx.studio/carla
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.

176 lines
6.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #include "DirectoryIterator.h"
  24. #include "../text/StringArray.h"
  25. namespace water {
  26. DirectoryIterator::DirectoryIterator (const File& directory, bool recursive,
  27. const String& pattern, const int type)
  28. : wildCards (parseWildcards (pattern)),
  29. fileFinder (directory, (recursive || wildCards.size() > 1) ? "*" : pattern),
  30. wildCard (pattern),
  31. path (File::addTrailingSeparator (directory.getFullPathName())),
  32. index (-1),
  33. totalNumFiles (-1),
  34. whatToLookFor (type),
  35. isRecursive (recursive),
  36. hasBeenAdvanced (false)
  37. {
  38. // you have to specify the type of files you're looking for!
  39. jassert ((type & (File::findFiles | File::findDirectories)) != 0);
  40. jassert (type > 0 && type <= 7);
  41. }
  42. DirectoryIterator::~DirectoryIterator()
  43. {
  44. }
  45. StringArray DirectoryIterator::parseWildcards (const String& pattern)
  46. {
  47. StringArray s;
  48. s.addTokens (pattern, ";,", "\"'");
  49. s.trim();
  50. s.removeEmptyStrings();
  51. return s;
  52. }
  53. bool DirectoryIterator::fileMatches (const StringArray& wildCards, const String& filename)
  54. {
  55. for (int i = 0; i < wildCards.size(); ++i)
  56. if (filename.matchesWildcard (wildCards[i], ! File::areFileNamesCaseSensitive()))
  57. return true;
  58. return false;
  59. }
  60. bool DirectoryIterator::next()
  61. {
  62. return next (nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
  63. }
  64. bool DirectoryIterator::next (bool* const isDirResult, bool* const isHiddenResult, int64* const fileSize,
  65. Time* const modTime, Time* const creationTime, bool* const isReadOnly)
  66. {
  67. for (;;)
  68. {
  69. hasBeenAdvanced = true;
  70. if (subIterator != nullptr)
  71. {
  72. if (subIterator->next (isDirResult, isHiddenResult, fileSize, modTime, creationTime, isReadOnly))
  73. return true;
  74. subIterator = nullptr;
  75. }
  76. String filename;
  77. bool isDirectory, isHidden = false, shouldContinue = false;
  78. while (fileFinder.next (filename, &isDirectory,
  79. (isHiddenResult != nullptr || (whatToLookFor & File::ignoreHiddenFiles) != 0) ? &isHidden : nullptr,
  80. fileSize, modTime, creationTime, isReadOnly))
  81. {
  82. ++index;
  83. if (! filename.containsOnly ("."))
  84. {
  85. bool matches = false;
  86. if (isDirectory)
  87. {
  88. if (isRecursive && ((whatToLookFor & File::ignoreHiddenFiles) == 0 || ! isHidden))
  89. subIterator = new DirectoryIterator (File::createFileWithoutCheckingPath (path + filename),
  90. true, wildCard, whatToLookFor);
  91. matches = (whatToLookFor & File::findDirectories) != 0;
  92. }
  93. else
  94. {
  95. matches = (whatToLookFor & File::findFiles) != 0;
  96. }
  97. // if we're not relying on the OS iterator to do the wildcard match, do it now..
  98. if (matches && (isRecursive || wildCards.size() > 1))
  99. matches = fileMatches (wildCards, filename);
  100. if (matches && (whatToLookFor & File::ignoreHiddenFiles) != 0)
  101. matches = ! isHidden;
  102. if (matches)
  103. {
  104. currentFile = File::createFileWithoutCheckingPath (path + filename);
  105. if (isHiddenResult != nullptr) *isHiddenResult = isHidden;
  106. if (isDirResult != nullptr) *isDirResult = isDirectory;
  107. return true;
  108. }
  109. if (subIterator != nullptr)
  110. {
  111. shouldContinue = true;
  112. break;
  113. }
  114. }
  115. }
  116. if (! shouldContinue)
  117. return false;
  118. }
  119. }
  120. const File& DirectoryIterator::getFile() const
  121. {
  122. if (subIterator != nullptr && subIterator->hasBeenAdvanced)
  123. return subIterator->getFile();
  124. // You need to call DirectoryIterator::next() before asking it for the file that it found!
  125. jassert (hasBeenAdvanced);
  126. return currentFile;
  127. }
  128. float DirectoryIterator::getEstimatedProgress() const
  129. {
  130. if (totalNumFiles < 0)
  131. totalNumFiles = File (path).getNumberOfChildFiles (File::findFilesAndDirectories);
  132. if (totalNumFiles <= 0)
  133. return 0.0f;
  134. const float detailedIndex = (subIterator != nullptr) ? index + subIterator->getEstimatedProgress()
  135. : (float) index;
  136. return detailedIndex / totalNumFiles;
  137. }
  138. }