Cleanup
[carla.git] / source / modules / water / files / DirectoryIterator.cpp
blob1b616cc35dc9376dbe3e92b625635145c1ca97f8
1 /*
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
21 OF THIS SOFTWARE.
23 ==============================================================================
26 #include "DirectoryIterator.h"
28 namespace water {
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),
34 wildCard (pattern),
35 path (File::addTrailingSeparator (directory.getFullPathName())),
36 index (-1),
37 totalNumFiles (-1),
38 whatToLookFor (type),
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)
53 StringArray s;
54 s.addTokens (pattern, ";,", "\"'");
55 s.trim();
56 s.removeEmptyStrings();
57 return s;
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()))
64 return true;
66 return false;
69 bool DirectoryIterator::next()
71 return next (nullptr, nullptr, nullptr);
74 bool DirectoryIterator::next (bool* const isDirResult, int64* const fileSize, bool* const isReadOnly)
76 for (;;)
78 hasBeenAdvanced = true;
80 if (subIterator != nullptr)
82 if (subIterator->next (isDirResult, fileSize, isReadOnly))
83 return true;
85 subIterator = nullptr;
88 String filename;
89 bool isDirectory, shouldContinue = false;
91 while (fileFinder.next (filename, &isDirectory, fileSize, isReadOnly))
93 ++index;
95 if (! filename.containsOnly ("."))
97 bool matches = false;
99 if (isDirectory)
101 if (isRecursive)
102 subIterator = new DirectoryIterator (File::createFileWithoutCheckingPath (path + filename),
103 true, wildCard, whatToLookFor);
105 matches = (whatToLookFor & File::findDirectories) != 0;
107 else
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);
116 if (matches)
118 currentFile = File::createFileWithoutCheckingPath (path + filename);
119 if (isDirResult != nullptr) *isDirResult = isDirectory;
121 return true;
124 if (subIterator != nullptr)
126 shouldContinue = true;
127 break;
132 if (! shouldContinue)
133 return false;
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);
145 return currentFile;