OpenShot Audio Library | OpenShotAudio  0.3.0
juce_DirectoryIterator.cpp
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
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 DirectoryIterator::DirectoryIterator (const File& directory, bool recursive,
27  const String& pattern, int type)
28  : wildCards (parseWildcards (pattern)),
29  fileFinder (directory, (recursive || wildCards.size() > 1) ? "*" : pattern),
30  wildCard (pattern),
31  path (File::addTrailingSeparator (directory.getFullPathName())),
32  whatToLookFor (type),
33  isRecursive (recursive)
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 
41 {
42 }
43 
44 StringArray DirectoryIterator::parseWildcards (const String& pattern)
45 {
46  StringArray s;
47  s.addTokens (pattern, ";,", "\"'");
48  s.trim();
50  return s;
51 }
52 
53 bool DirectoryIterator::fileMatches (const StringArray& wildcards, const String& filename)
54 {
55  for (auto& w : wildcards)
56  if (filename.matchesWildcard (w, ! File::areFileNamesCaseSensitive()))
57  return true;
58 
59  return false;
60 }
61 
63 {
64  return next (nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
65 }
66 
67 bool DirectoryIterator::next (bool* isDirResult, bool* isHiddenResult, int64* fileSize,
68  Time* modTime, Time* creationTime, bool* isReadOnly)
69 {
70  for (;;)
71  {
72  hasBeenAdvanced = true;
73 
74  if (subIterator != nullptr)
75  {
76  if (subIterator->next (isDirResult, isHiddenResult, fileSize, modTime, creationTime, isReadOnly))
77  return true;
78 
79  subIterator.reset();
80  }
81 
82  String filename;
83  bool isDirectory, isHidden = false, shouldContinue = false;
84 
85  while (fileFinder.next (filename, &isDirectory,
86  (isHiddenResult != nullptr || (whatToLookFor & File::ignoreHiddenFiles) != 0) ? &isHidden : nullptr,
87  fileSize, modTime, creationTime, isReadOnly))
88  {
89  ++index;
90 
91  if (! filename.containsOnly ("."))
92  {
93  bool matches = false;
94 
95  if (isDirectory)
96  {
97  if (isRecursive && ((whatToLookFor & File::ignoreHiddenFiles) == 0 || ! isHidden))
98  subIterator.reset (new DirectoryIterator (File::createFileWithoutCheckingPath (path + filename),
99  true, wildCard, whatToLookFor));
100 
101  matches = (whatToLookFor & File::findDirectories) != 0;
102  }
103  else
104  {
105  matches = (whatToLookFor & File::findFiles) != 0;
106  }
107 
108  // if we're not relying on the OS iterator to do the wildcard match, do it now..
109  if (matches && (isRecursive || wildCards.size() > 1))
110  matches = fileMatches (wildCards, filename);
111 
112  if (matches && (whatToLookFor & File::ignoreHiddenFiles) != 0)
113  matches = ! isHidden;
114 
115  if (matches)
116  {
117  currentFile = File::createFileWithoutCheckingPath (path + filename);
118  if (isHiddenResult != nullptr) *isHiddenResult = isHidden;
119  if (isDirResult != nullptr) *isDirResult = isDirectory;
120 
121  return true;
122  }
123 
124  if (subIterator != nullptr)
125  {
126  shouldContinue = true;
127  break;
128  }
129  }
130  }
131 
132  if (! shouldContinue)
133  return false;
134  }
135 }
136 
138 {
139  if (subIterator != nullptr && subIterator->hasBeenAdvanced)
140  return subIterator->getFile();
141 
142  // You need to call DirectoryIterator::next() before asking it for the file that it found!
143  jassert (hasBeenAdvanced);
144 
145  return currentFile;
146 }
147 
149 {
150  if (totalNumFiles < 0)
152 
153  if (totalNumFiles <= 0)
154  return 0.0f;
155 
156  auto detailedIndex = (subIterator != nullptr) ? index + subIterator->getEstimatedProgress()
157  : (float) index;
158 
159  return jlimit (0.0f, 1.0f, detailedIndex / totalNumFiles);
160 }
161 
162 } // namespace juce
DirectoryIterator(const File &directory, bool isRecursive, const String &wildCard="*", int whatToLookFor=File::findFiles)
int getNumberOfChildFiles(int whatToLookFor, const String &wildCardPattern="*") const
Definition: juce_File.cpp:586
@ ignoreHiddenFiles
Definition: juce_File.h:552
@ findDirectories
Definition: juce_File.h:549
@ findFilesAndDirectories
Definition: juce_File.h:551
static bool areFileNamesCaseSensitive()
Definition: juce_File.cpp:238
static File createFileWithoutCheckingPath(const String &absolutePath) noexcept
Definition: juce_File.cpp:31
void removeEmptyStrings(bool removeWhitespaceStrings=true)
int size() const noexcept
int addTokens(StringRef stringToTokenise, bool preserveQuotedStrings)
bool containsOnly(StringRef charactersItMightContain) const noexcept