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.

DirectoryIterator.cpp 5.4KB

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