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.

162 lines
5.9KB

  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. #ifndef WATER_DIRECTORYITERATOR_H_INCLUDED
  21. #define WATER_DIRECTORYITERATOR_H_INCLUDED
  22. #include "File.h"
  23. #include "../text/StringArray.h"
  24. #include "CarlaScopeUtils.hpp"
  25. namespace water {
  26. //==============================================================================
  27. /**
  28. Searches through the files in a directory, returning each file that is found.
  29. A DirectoryIterator will search through a directory and its subdirectories using
  30. a wildcard filepattern match.
  31. If you may be scanning a large number of files, it's usually smarter to use this
  32. class than File::findChildFiles() because it allows you to stop at any time, rather
  33. than having to wait for the entire scan to finish before getting the results.
  34. It also provides an estimate of its progress, using a (highly inaccurate!) algorithm.
  35. */
  36. class DirectoryIterator
  37. {
  38. public:
  39. //==============================================================================
  40. /** Creates a DirectoryIterator for a given directory.
  41. After creating one of these, call its next() method to get the
  42. first file - e.g. @code
  43. DirectoryIterator iter (File ("/animals/mooses"), true, "*.moose");
  44. while (iter.next())
  45. {
  46. File theFileItFound (iter.getFile());
  47. ... etc
  48. }
  49. @endcode
  50. @param directory the directory to search in
  51. @param isRecursive whether all the subdirectories should also be searched
  52. @param wildCard the file pattern to match. This may contain multiple patterns
  53. separated by a semi-colon or comma, e.g. "*.jpg;*.png"
  54. @param whatToLookFor a value from the File::TypesOfFileToFind enum, specifying
  55. whether to look for files, directories, or both.
  56. */
  57. DirectoryIterator (const File& directory,
  58. bool isRecursive,
  59. const String& wildCard = "*",
  60. int whatToLookFor = File::findFiles);
  61. /** Destructor. */
  62. ~DirectoryIterator();
  63. /** Moves the iterator along to the next file.
  64. @returns true if a file was found (you can then use getFile() to see what it was) - or
  65. false if there are no more matching files.
  66. */
  67. bool next();
  68. /** Moves the iterator along to the next file, and returns various properties of that file.
  69. If you need to find out details about the file, it's more efficient to call this method than
  70. to call the normal next() method and then find out the details afterwards.
  71. All the parameters are optional, so pass null pointers for any items that you're not
  72. interested in.
  73. @returns true if a file was found (you can then use getFile() to see what it was) - or
  74. false if there are no more matching files. If it returns false, then none of the
  75. parameters will be filled-in.
  76. */
  77. bool next (bool* isDirectory,
  78. int64* fileSize,
  79. Time* modTime,
  80. Time* creationTime,
  81. bool* isReadOnly);
  82. /** Returns the file that the iterator is currently pointing at.
  83. The result of this call is only valid after a call to next() has returned true.
  84. */
  85. const File& getFile() const;
  86. /** Returns a guess of how far through the search the iterator has got.
  87. @returns a value 0.0 to 1.0 to show the progress, although this won't be
  88. very accurate.
  89. */
  90. float getEstimatedProgress() const;
  91. private:
  92. //==============================================================================
  93. class NativeIterator
  94. {
  95. public:
  96. NativeIterator (const File& directory, const String& wildCard);
  97. ~NativeIterator();
  98. bool next (String& filenameFound,
  99. bool* isDirectory, int64* fileSize,
  100. Time* modTime, Time* creationTime, bool* isReadOnly);
  101. class Pimpl;
  102. private:
  103. friend class DirectoryIterator;
  104. CarlaScopedPointer<Pimpl> pimpl;
  105. CARLA_DECLARE_NON_COPY_CLASS (NativeIterator)
  106. };
  107. StringArray wildCards;
  108. NativeIterator fileFinder;
  109. String wildCard, path;
  110. int index;
  111. mutable int totalNumFiles;
  112. const int whatToLookFor;
  113. const bool isRecursive;
  114. bool hasBeenAdvanced;
  115. CarlaScopedPointer<DirectoryIterator> subIterator;
  116. File currentFile;
  117. static StringArray parseWildcards (const String& pattern);
  118. static bool fileMatches (const StringArray& wildCards, const String& filename);
  119. CARLA_DECLARE_NON_COPY_CLASS (DirectoryIterator)
  120. };
  121. }
  122. #endif // WATER_DIRECTORYITERATOR_H_INCLUDED