1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2020 - 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
23 namespace juce
24 {
25
FileSearchPath()26 FileSearchPath::FileSearchPath() {}
~FileSearchPath()27 FileSearchPath::~FileSearchPath() {}
28
FileSearchPath(const String & path)29 FileSearchPath::FileSearchPath (const String& path)
30 {
31 init (path);
32 }
33
FileSearchPath(const FileSearchPath & other)34 FileSearchPath::FileSearchPath (const FileSearchPath& other)
35 : directories (other.directories)
36 {
37 }
38
operator =(const FileSearchPath & other)39 FileSearchPath& FileSearchPath::operator= (const FileSearchPath& other)
40 {
41 directories = other.directories;
42 return *this;
43 }
44
operator =(const String & path)45 FileSearchPath& FileSearchPath::operator= (const String& path)
46 {
47 init (path);
48 return *this;
49 }
50
init(const String & path)51 void FileSearchPath::init (const String& path)
52 {
53 directories.clear();
54 directories.addTokens (path, ";", "\"");
55 directories.trim();
56 directories.removeEmptyStrings();
57
58 for (auto& d : directories)
59 d = d.unquoted();
60 }
61
getNumPaths() const62 int FileSearchPath::getNumPaths() const
63 {
64 return directories.size();
65 }
66
operator [](int index) const67 File FileSearchPath::operator[] (int index) const
68 {
69 return File (directories[index]);
70 }
71
toString() const72 String FileSearchPath::toString() const
73 {
74 auto dirs = directories;
75
76 for (auto& d : dirs)
77 if (d.containsChar (';'))
78 d = d.quoted();
79
80 return dirs.joinIntoString (";");
81 }
82
add(const File & dir,int insertIndex)83 void FileSearchPath::add (const File& dir, int insertIndex)
84 {
85 directories.insert (insertIndex, dir.getFullPathName());
86 }
87
addIfNotAlreadyThere(const File & dir)88 bool FileSearchPath::addIfNotAlreadyThere (const File& dir)
89 {
90 for (auto& d : directories)
91 if (File (d) == dir)
92 return false;
93
94 add (dir);
95 return true;
96 }
97
remove(int index)98 void FileSearchPath::remove (int index)
99 {
100 directories.remove (index);
101 }
102
addPath(const FileSearchPath & other)103 void FileSearchPath::addPath (const FileSearchPath& other)
104 {
105 for (int i = 0; i < other.getNumPaths(); ++i)
106 addIfNotAlreadyThere (other[i]);
107 }
108
removeRedundantPaths()109 void FileSearchPath::removeRedundantPaths()
110 {
111 for (int i = directories.size(); --i >= 0;)
112 {
113 const File d1 (directories[i]);
114
115 for (int j = directories.size(); --j >= 0;)
116 {
117 const File d2 (directories[j]);
118
119 if (i != j && (d1.isAChildOf (d2) || d1 == d2))
120 {
121 directories.remove (i);
122 break;
123 }
124 }
125 }
126 }
127
removeNonExistentPaths()128 void FileSearchPath::removeNonExistentPaths()
129 {
130 for (int i = directories.size(); --i >= 0;)
131 if (! File (directories[i]).isDirectory())
132 directories.remove (i);
133 }
134
findChildFiles(int whatToLookFor,bool recurse,const String & wildcard) const135 Array<File> FileSearchPath::findChildFiles (int whatToLookFor, bool recurse, const String& wildcard) const
136 {
137 Array<File> results;
138 findChildFiles (results, whatToLookFor, recurse, wildcard);
139 return results;
140 }
141
findChildFiles(Array<File> & results,int whatToLookFor,bool recurse,const String & wildcard) const142 int FileSearchPath::findChildFiles (Array<File>& results, int whatToLookFor,
143 bool recurse, const String& wildcard) const
144 {
145 int total = 0;
146
147 for (auto& d : directories)
148 total += File (d).findChildFiles (results, whatToLookFor, recurse, wildcard);
149
150 return total;
151 }
152
isFileInPath(const File & fileToCheck,const bool checkRecursively) const153 bool FileSearchPath::isFileInPath (const File& fileToCheck,
154 const bool checkRecursively) const
155 {
156 for (auto& d : directories)
157 {
158 if (checkRecursively)
159 {
160 if (fileToCheck.isAChildOf (File (d)))
161 return true;
162 }
163 else
164 {
165 if (fileToCheck.getParentDirectory() == File (d))
166 return true;
167 }
168 }
169
170 return false;
171 }
172
173 } // namespace juce
174