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.

166 lines
5.4KB

  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. wassert ((type & (File::findFiles | File::findDirectories)) != 0);
  37. wassert (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. wassert (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. }