JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_FileSearchPath.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
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
27{
28 init (path);
29}
30
32 : directories (other.directories)
33{
34}
35
37{
38 directories = other.directories;
39 return *this;
40}
41
43{
44 init (path);
45 return *this;
46}
47
48void FileSearchPath::init (const String& path)
49{
50 directories.clear();
51 directories.addTokens (path, ";", "\"");
52 directories.trim();
53 directories.removeEmptyStrings();
54
55 for (auto& d : directories)
56 d = d.unquoted();
57}
58
60{
61 return directories.size();
62}
63
65{
66 return File (getRawString (index));
67}
68
70{
71 return directories[index];
72}
73
75{
76 return toStringWithSeparator (";");
77}
78
80{
81 auto dirs = directories;
82
83 for (auto& d : dirs)
84 if (d.contains (separator))
85 d = d.quoted();
86
87 return dirs.joinIntoString (separator);
88}
89
90void FileSearchPath::add (const File& dir, int insertIndex)
91{
92 directories.insert (insertIndex, dir.getFullPathName());
93}
94
96{
97 for (auto& d : directories)
98 if (File (d) == dir)
99 return false;
100
101 add (dir);
102 return true;
103}
104
106{
107 directories.remove (index);
108}
109
111{
112 for (int i = 0; i < other.getNumPaths(); ++i)
114}
115
117{
118 std::vector<String> reduced;
119
120 for (const auto& directory : directories)
121 {
122 const auto checkedIsChildOf = [&] (const auto& a, const auto& b)
123 {
124 return File::isAbsolutePath (a) && File::isAbsolutePath (b) && File (a).isAChildOf (b);
125 };
126
127 const auto fContainsDirectory = [&] (const auto& f)
128 {
129 return f == directory || checkedIsChildOf (directory, f);
130 };
131
132 if (std::find_if (reduced.begin(), reduced.end(), fContainsDirectory) != reduced.end())
133 continue;
134
135 const auto directoryContainsF = [&] (const auto& f) { return checkedIsChildOf (f, directory); };
136
137 reduced.erase (std::remove_if (reduced.begin(), reduced.end(), directoryContainsF), reduced.end());
138 reduced.push_back (directory);
139 }
140
141 directories = StringArray (reduced.data(), (int) reduced.size());
142}
143
145{
146 for (int i = directories.size(); --i >= 0;)
147 if (! File (directories[i]).isDirectory())
148 directories.remove (i);
149}
150
152{
153 Array<File> results;
154 findChildFiles (results, whatToLookFor, recurse, wildcard);
155 return results;
156}
157
159 bool recurse, const String& wildcard) const
160{
161 int total = 0;
162
163 for (auto& d : directories)
164 total += File (d).findChildFiles (results, whatToLookFor, recurse, wildcard);
165
166 return total;
167}
168
170 const bool checkRecursively) const
171{
172 for (auto& d : directories)
173 {
175 {
176 if (fileToCheck.isAChildOf (File (d)))
177 return true;
178 }
179 else
180 {
181 if (fileToCheck.getParentDirectory() == File (d))
182 return true;
183 }
184 }
185
186 return false;
187}
188
189//==============================================================================
190//==============================================================================
191#if JUCE_UNIT_TESTS
192
194{
195public:
196 FileSearchPathTests() : UnitTest ("FileSearchPath", UnitTestCategories::files) {}
197
198 void runTest() override
199 {
200 beginTest ("removeRedundantPaths");
201 {
202 #if JUCE_WINDOWS
203 const String prefix = "C:";
204 #else
205 const String prefix = "";
206 #endif
207
208 {
209 FileSearchPath fsp { prefix + "/a/b/c/d;" + prefix + "/a/b/c/e;" + prefix + "/a/b/c" };
210 fsp.removeRedundantPaths();
211 expectEquals (fsp.toString(), prefix + "/a/b/c");
212 }
213
214 {
215 FileSearchPath fsp { prefix + "/a/b/c;" + prefix + "/a/b/c/d;" + prefix + "/a/b/c/e" };
216 fsp.removeRedundantPaths();
217 expectEquals (fsp.toString(), prefix + "/a/b/c");
218 }
219
220 {
221 FileSearchPath fsp { prefix + "/a/b/c/d;" + prefix + "/a/b/c;" + prefix + "/a/b/c/e" };
222 fsp.removeRedundantPaths();
223 expectEquals (fsp.toString(), prefix + "/a/b/c");
224 }
225
226 {
227 FileSearchPath fsp { "%FOO%;" + prefix + "/a/b/c;%FOO%;" + prefix + "/a/b/c/d" };
228 fsp.removeRedundantPaths();
229 expectEquals (fsp.toString(), "%FOO%;" + prefix + "/a/b/c");
230 }
231 }
232 }
233};
234
236
237#endif
238
239} // namespace juce
T begin(T... args)
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:56
Represents a set of folders that make up a search path.
bool isFileInPath(const File &fileToCheck, bool checkRecursively) const
Finds out whether a file is inside one of the path's directories.
void add(const File &directoryToAdd, int insertIndex=-1)
Adds a new directory to the search path.
void remove(int indexToRemove)
Removes a directory from the search path.
void removeNonExistentPaths()
Removes any directories that don't actually exist.
FileSearchPath & operator=(const FileSearchPath &)
Copies another search path.
void addPath(const FileSearchPath &)
Merges another search path into this one.
String toString() const
Returns the search path as a semicolon-separated list of directories.
int getNumPaths() const
Returns the number of folders in this search path.
File operator[](int index) const
Returns one of the folders in this search path.
void removeRedundantPaths()
Removes any directories that are actually subdirectories of one of the other directories in the searc...
FileSearchPath()=default
Creates an empty search path.
bool addIfNotAlreadyThere(const File &directoryToAdd)
Adds a new directory to the search path if it's not already in there.
String toStringWithSeparator(StringRef separator) const
Returns the search paths, joined with the provided separator.
Array< File > findChildFiles(int whatToLookFor, bool searchRecursively, const String &wildCardPattern="*") const
Searches the path for a wildcard.
String getRawString(int index) const
Returns the unaltered text of the folder at the specified index.
Represents a local file or directory.
Definition juce_File.h:45
bool isDirectory() const
Checks whether the file is a directory that exists.
Array< File > findChildFiles(int whatToLookFor, bool searchRecursively, const String &wildCardPattern="*", FollowSymlinks followSymlinks=FollowSymlinks::yes) const
Searches this directory for files matching a wildcard pattern.
const String & getFullPathName() const noexcept
Returns the complete, absolute path of this file.
Definition juce_File.h:153
static bool isAbsolutePath(StringRef path)
Returns true if the string seems to be a fully-specified absolute path.
bool isAChildOf(const File &potentialParentDirectory) const
Checks whether a file is somewhere inside a directory.
A special array for holding a list of strings.
String joinIntoString(StringRef separatorString, int startIndex=0, int numberOfElements=-1) const
Joins the strings in the array together into one string.
void removeEmptyStrings(bool removeWhitespaceStrings=true)
Removes empty strings from the array.
void insert(int index, String stringToAdd)
Inserts a string into the array.
void clear()
Removes all elements from the array.
int size() const noexcept
Returns the number of strings in the array.
void trim()
Deletes any whitespace characters from the starts and ends of all the strings.
void remove(int index)
Removes a string from the array.
int addTokens(StringRef stringToTokenise, bool preserveQuotedStrings)
Breaks up a string into tokens and adds them to this array.
A simple class for holding temporary references to a string literal or String.
The JUCE String class!
Definition juce_String.h:53
This is a base class for classes that perform a unit test.
T data(T... args)
T end(T... args)
T erase(T... args)
T find_if(T... args)
JUCE Namespace.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
T push_back(T... args)
T remove_if(T... args)
T size(T... args)