OpenShot Audio Library | OpenShotAudio: juce_DirectoryIterator.cpp Source File

OpenShot Audio Library | OpenShotAudio 0.3.2

Loading...
Searching...
No Matches
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
23namespace juce
24{
25
26DirectoryIterator::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
40DirectoryIterator::~DirectoryIterator()
41{
42}
43
44 StringArray DirectoryIterator::parseWildcards (const String& pattern)
45{
46 StringArray s;
47 s.addTokens (pattern, ";,", "\"'");
48 s.trim();
49 s.removeEmptyStrings();
50 return s;
51}
52
53bool 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
62 bool DirectoryIterator::next()
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
137 const File& DirectoryIterator::getFile() const
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
148 float DirectoryIterator::getEstimatedProgress() const
149{
150 if (totalNumFiles < 0)
151 totalNumFiles = File (path).getNumberOfChildFiles (File::findFilesAndDirectories);
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::DirectoryIterator
Definition: juce_DirectoryIterator.h:47
juce::DirectoryIterator::getFile
const File & getFile() const
Definition: juce_DirectoryIterator.cpp:137
juce::DirectoryIterator::next
bool next()
Definition: juce_DirectoryIterator.cpp:62
juce::DirectoryIterator::getEstimatedProgress
float getEstimatedProgress() const
Definition: juce_DirectoryIterator.cpp:148
juce::DirectoryIterator::DirectoryIterator
DirectoryIterator(const File &directory, bool isRecursive, const String &wildCard="*", int whatToLookFor=File::findFiles)
Definition: juce_DirectoryIterator.cpp:26
juce::DirectoryIterator::~DirectoryIterator
~DirectoryIterator()
Definition: juce_DirectoryIterator.cpp:40
juce::File
Definition: juce_File.h:41
juce::File::getNumberOfChildFiles
int getNumberOfChildFiles(int whatToLookFor, const String &wildCardPattern="*") const
Definition: juce_File.cpp:586
juce::File::ignoreHiddenFiles
@ ignoreHiddenFiles
Definition: juce_File.h:552
juce::File::findDirectories
@ findDirectories
Definition: juce_File.h:549
juce::File::findFilesAndDirectories
@ findFilesAndDirectories
Definition: juce_File.h:551
juce::File::findFiles
@ findFiles
Definition: juce_File.h:550
juce::File::areFileNamesCaseSensitive
static bool areFileNamesCaseSensitive()
Definition: juce_File.cpp:238
juce::File::createFileWithoutCheckingPath
static File createFileWithoutCheckingPath(const String &absolutePath) noexcept
Definition: juce_File.cpp:31
juce::StringArray
Definition: juce_StringArray.h:35
juce::StringArray::removeEmptyStrings
void removeEmptyStrings(bool removeWhitespaceStrings=true)
Definition: juce_StringArray.cpp:249
juce::StringArray::size
int size() const noexcept
Definition: juce_StringArray.h:135
juce::StringArray::trim
void trim()
Definition: juce_StringArray.cpp:265
juce::StringArray::addTokens
int addTokens(StringRef stringToTokenise, bool preserveQuotedStrings)
Definition: juce_StringArray.cpp:328
juce::String
Definition: juce_String.h:39
juce::String::containsOnly
bool containsOnly(StringRef charactersItMightContain) const noexcept
Definition: juce_String.cpp:1807
juce::Time
Definition: juce_Time.h:37