OpenShot Library | libopenshot-audio  0.2.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
juce::StringArray
A special array for holding a list of strings.
Definition: juce_StringArray.h:38
juce::DirectoryIterator::next
bool next()
Moves the iterator along to the next file.
Definition: juce_DirectoryIterator.cpp:62
juce::DirectoryIterator::~DirectoryIterator
~DirectoryIterator()
Destructor.
Definition: juce_DirectoryIterator.cpp:40
juce::File::findDirectories
@ findDirectories
Use this flag to indicate that you want to find directories.
Definition: juce_File.h:553
juce::DirectoryIterator::getFile
const File & getFile() const
Returns the file that the iterator is currently pointing at.
Definition: juce_DirectoryIterator.cpp:137
juce::StringArray::trim
void trim()
Deletes any whitespace characters from the starts and ends of all the strings.
Definition: juce_StringArray.cpp:265
juce::StringArray::removeEmptyStrings
void removeEmptyStrings(bool removeWhitespaceStrings=true)
Removes empty strings from the array.
Definition: juce_StringArray.cpp:249
juce::File::areFileNamesCaseSensitive
static bool areFileNamesCaseSensitive()
Indicates whether filenames are case-sensitive on the current operating system.
Definition: juce_File.cpp:218
juce::File::createFileWithoutCheckingPath
static File createFileWithoutCheckingPath(const String &absolutePath) noexcept
Creates a file that simply contains this string, without doing the sanity-checking that the normal co...
Definition: juce_File.cpp:31
juce::DirectoryIterator
Searches through the files in a directory, returning each file that is found.
Definition: juce_DirectoryIterator.h:50
juce::File::findFilesAndDirectories
@ findFilesAndDirectories
Use this flag to indicate that you want to find both files and directories.
Definition: juce_File.h:555
juce::File
Represents a local file or directory.
Definition: juce_File.h:44
juce::DirectoryIterator::DirectoryIterator
DirectoryIterator(const File &directory, bool isRecursive, const String &wildCard="*", int whatToLookFor=File::findFiles)
Creates a DirectoryIterator for a given directory.
Definition: juce_DirectoryIterator.cpp:26
juce::File::getNumberOfChildFiles
int getNumberOfChildFiles(int whatToLookFor, const String &wildCardPattern="*") const
Searches inside a directory and counts how many files match a wildcard pattern.
Definition: juce_File.cpp:566
juce::StringArray::size
int size() const noexcept
Returns the number of strings in the array.
Definition: juce_StringArray.h:139
juce::String::containsOnly
bool containsOnly(StringRef charactersItMightContain) const noexcept
Looks for a set of characters in the string.
Definition: juce_String.cpp:1802
juce::Time
Holds an absolute date and time.
Definition: juce_Time.h:40
juce::File::ignoreHiddenFiles
@ ignoreHiddenFiles
Add this flag to avoid returning any hidden files in the results.
Definition: juce_File.h:556
juce::DirectoryIterator::getEstimatedProgress
float getEstimatedProgress() const
Returns a guess of how far through the search the iterator has got.
Definition: juce_DirectoryIterator.cpp:148
juce::StringArray::addTokens
int addTokens(StringRef stringToTokenise, bool preserveQuotedStrings)
Breaks up a string into tokens and adds them to this array.
Definition: juce_StringArray.cpp:328
juce::String
The JUCE String class!
Definition: juce_String.h:42
juce::File::findFiles
@ findFiles
Use this flag to indicate that you want to find files.
Definition: juce_File.h:554