OpenShot Audio Library | OpenShotAudio  0.6.0
juce_DirectoryIterator.cpp
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2022 - Raw Material Software Limited
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  The code included in this file is provided under the terms of the ISC license
11  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12  To use, copy, modify, and/or distribute this software for any purpose with or
13  without fee is hereby granted provided that the above copyright notice and
14  this permission notice appear in all copies.
15 
16  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18  DISCLAIMED.
19 
20  ==============================================================================
21 */
22 
23 namespace juce
24 {
25 
26 StringArray DirectoryIterator::parseWildcards (const String& pattern)
27 {
28  StringArray s;
29  s.addTokens (pattern, ";,", "\"'");
30  s.trim();
31  s.removeEmptyStrings();
32  return s;
33 }
34 
35 bool DirectoryIterator::fileMatches (const StringArray& wildcards, const String& filename)
36 {
37  for (auto& w : wildcards)
38  if (filename.matchesWildcard (w, ! File::areFileNamesCaseSensitive()))
39  return true;
40 
41  return false;
42 }
43 
45 {
46  return next (nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
47 }
48 
49 JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wdeprecated-declarations")
50 JUCE_BEGIN_IGNORE_WARNINGS_MSVC (4996)
51 
52 bool DirectoryIterator::next (bool* isDirResult, bool* isHiddenResult, int64* fileSize,
53  Time* modTime, Time* creationTime, bool* isReadOnly)
54 {
55  for (;;)
56  {
57  hasBeenAdvanced = true;
58 
59  if (subIterator != nullptr)
60  {
61  if (subIterator->next (isDirResult, isHiddenResult, fileSize, modTime, creationTime, isReadOnly))
62  return true;
63 
64  subIterator.reset();
65  }
66 
67  String filename;
68  bool isDirectory, isHidden = false, shouldContinue = false;
69 
70  while (fileFinder.next (filename, &isDirectory,
71  (isHiddenResult != nullptr || (whatToLookFor & File::ignoreHiddenFiles) != 0) ? &isHidden : nullptr,
72  fileSize, modTime, creationTime, isReadOnly))
73  {
74  ++index;
75 
76  if (! filename.containsOnly ("."))
77  {
78  const auto fullPath = File::createFileWithoutCheckingPath (path + filename);
79  bool matches = false;
80 
81  if (isDirectory)
82  {
83  const auto mayRecurseIntoPossibleHiddenDir = [this, &isHidden]
84  {
85  return (whatToLookFor & File::ignoreHiddenFiles) == 0 || ! isHidden;
86  };
87 
88  const auto mayRecurseIntoPossibleSymlink = [this, &fullPath]
89  {
90  return followSymlinks == File::FollowSymlinks::yes
91  || ! fullPath.isSymbolicLink()
92  || (followSymlinks == File::FollowSymlinks::noCycles
93  && knownPaths->find (fullPath.getLinkedTarget()) == knownPaths->end());
94  };
95 
96  if (isRecursive && mayRecurseIntoPossibleHiddenDir() && mayRecurseIntoPossibleSymlink())
97  subIterator.reset (new DirectoryIterator (fullPath, true, wildCard, whatToLookFor, followSymlinks, knownPaths));
98 
99  matches = (whatToLookFor & File::findDirectories) != 0;
100  }
101  else
102  {
103  matches = (whatToLookFor & File::findFiles) != 0;
104  }
105 
106  // if we're not relying on the OS iterator to do the wildcard match, do it now..
107  if (matches && (isRecursive || wildCards.size() > 1))
108  matches = fileMatches (wildCards, filename);
109 
110  if (matches && (whatToLookFor & File::ignoreHiddenFiles) != 0)
111  matches = ! isHidden;
112 
113  if (matches)
114  {
115  currentFile = fullPath;
116  if (isHiddenResult != nullptr) *isHiddenResult = isHidden;
117  if (isDirResult != nullptr) *isDirResult = isDirectory;
118 
119  return true;
120  }
121 
122  if (subIterator != nullptr)
123  {
124  shouldContinue = true;
125  break;
126  }
127  }
128  }
129 
130  if (! shouldContinue)
131  return false;
132  }
133 }
134 
135 JUCE_END_IGNORE_WARNINGS_GCC_LIKE
136 JUCE_END_IGNORE_WARNINGS_MSVC
137 
139 {
140  if (subIterator != nullptr && subIterator->hasBeenAdvanced)
141  return subIterator->getFile();
142 
143  // You need to call DirectoryIterator::next() before asking it for the file that it found!
144  jassert (hasBeenAdvanced);
145 
146  return currentFile;
147 }
148 
150 {
151  if (totalNumFiles < 0)
153 
154  if (totalNumFiles <= 0)
155  return 0.0f;
156 
157  auto detailedIndex = (subIterator != nullptr) ? (float) index + subIterator->getEstimatedProgress()
158  : (float) index;
159 
160  return jlimit (0.0f, 1.0f, detailedIndex / (float) totalNumFiles);
161 }
162 
163 } // namespace juce
int getNumberOfChildFiles(int whatToLookFor, const String &wildCardPattern="*") const
Definition: juce_File.cpp:592
@ ignoreHiddenFiles
Definition: juce_File.h:568
@ findDirectories
Definition: juce_File.h:565
@ findFilesAndDirectories
Definition: juce_File.h:567
static bool areFileNamesCaseSensitive()
Definition: juce_File.cpp:236
static File createFileWithoutCheckingPath(const String &absolutePath) noexcept
Definition: juce_File.cpp:31
bool containsOnly(StringRef charactersItMightContain) const noexcept