2 ==============================================================================
4 This file is part of the Water library.
5 Copyright (c) 2016 ROLI Ltd.
6 Copyright (C) 2017-2024 Filipe Coelho <falktx@falktx.com>
8 Permission is granted to use this software under the terms of the ISC license
9 http://www.isc.org/downloads/software-support-policy/isc-license/
11 Permission to use, copy, modify, and/or distribute this software for any
12 purpose with or without fee is hereby granted, provided that the above
13 copyright notice and this permission notice appear in all copies.
15 THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
16 TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
18 OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 ==============================================================================
26 #include "DirectoryIterator.h"
30 DirectoryIterator::DirectoryIterator (const File
& directory
, bool recursive
,
31 const String
& pattern
, const int type
)
32 : wildCards (parseWildcards (pattern
)),
33 fileFinder (directory
, (recursive
|| wildCards
.size() > 1) ? "*" : pattern
),
35 path (File::addTrailingSeparator (directory
.getFullPathName())),
39 isRecursive (recursive
),
40 hasBeenAdvanced (false)
42 // you have to specify the type of files you're looking for!
43 wassert ((type
& (File::findFiles
| File::findDirectories
)) != 0);
44 wassert (type
> 0 && type
<= 7);
47 DirectoryIterator::~DirectoryIterator()
51 StringArray
DirectoryIterator::parseWildcards (const String
& pattern
)
54 s
.addTokens (pattern
, ";,", "\"'");
56 s
.removeEmptyStrings();
60 bool DirectoryIterator::fileMatches (const StringArray
& wildCards
, const String
& filename
)
62 for (int i
= 0; i
< wildCards
.size(); ++i
)
63 if (filename
.matchesWildcard (wildCards
[i
], ! File::areFileNamesCaseSensitive()))
69 bool DirectoryIterator::next()
71 return next (nullptr, nullptr, nullptr);
74 bool DirectoryIterator::next (bool* const isDirResult
, int64
* const fileSize
, bool* const isReadOnly
)
78 hasBeenAdvanced
= true;
80 if (subIterator
!= nullptr)
82 if (subIterator
->next (isDirResult
, fileSize
, isReadOnly
))
85 subIterator
= nullptr;
89 bool isDirectory
, shouldContinue
= false;
91 while (fileFinder
.next (filename
, &isDirectory
, fileSize
, isReadOnly
))
95 if (! filename
.containsOnly ("."))
102 subIterator
= new DirectoryIterator (File::createFileWithoutCheckingPath (path
+ filename
),
103 true, wildCard
, whatToLookFor
);
105 matches
= (whatToLookFor
& File::findDirectories
) != 0;
109 matches
= (whatToLookFor
& File::findFiles
) != 0;
112 // if we're not relying on the OS iterator to do the wildcard match, do it now..
113 if (matches
&& (isRecursive
|| wildCards
.size() > 1))
114 matches
= fileMatches (wildCards
, filename
);
118 currentFile
= File::createFileWithoutCheckingPath (path
+ filename
);
119 if (isDirResult
!= nullptr) *isDirResult
= isDirectory
;
124 if (subIterator
!= nullptr)
126 shouldContinue
= true;
132 if (! shouldContinue
)
137 const File
& DirectoryIterator::getFile() const
139 if (subIterator
!= nullptr && subIterator
->hasBeenAdvanced
)
140 return subIterator
->getFile();
142 // You need to call DirectoryIterator::next() before asking it for the file that it found!
143 wassert (hasBeenAdvanced
);