OpenShot Audio Library | OpenShotAudio  0.3.1
juce_FileSearchPath.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 
23 namespace juce
24 {
25 
28 
30 {
31  init (path);
32 }
33 
35  : directories (other.directories)
36 {
37 }
38 
40 {
41  directories = other.directories;
42  return *this;
43 }
44 
46 {
47  init (path);
48  return *this;
49 }
50 
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 
63 {
64  return directories.size();
65 }
66 
68 {
69  return File (directories[index]);
70 }
71 
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 
83 void FileSearchPath::add (const File& dir, int insertIndex)
84 {
85  directories.insert (insertIndex, dir.getFullPathName());
86 }
87 
89 {
90  for (auto& d : directories)
91  if (File (d) == dir)
92  return false;
93 
94  add (dir);
95  return true;
96 }
97 
98 void FileSearchPath::remove (int index)
99 {
100  directories.remove (index);
101 }
102 
104 {
105  for (int i = 0; i < other.getNumPaths(); ++i)
106  addIfNotAlreadyThere (other[i]);
107 }
108 
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 
129 {
130  for (int i = directories.size(); --i >= 0;)
131  if (! File (directories[i]).isDirectory())
132  directories.remove (i);
133 }
134 
135 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 
142 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 
153 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
bool isFileInPath(const File &fileToCheck, bool checkRecursively) const
void add(const File &directoryToAdd, int insertIndex=-1)
void remove(int indexToRemove)
FileSearchPath & operator=(const FileSearchPath &)
void addPath(const FileSearchPath &)
File operator[](int index) const
bool addIfNotAlreadyThere(const File &directoryToAdd)
Array< File > findChildFiles(int whatToLookFor, bool searchRecursively, const String &wildCardPattern="*") const
const String & getFullPathName() const noexcept
Definition: juce_File.h:149
Array< File > findChildFiles(int whatToLookFor, bool searchRecursively, const String &wildCardPattern="*") const
Definition: juce_File.cpp:566
File getParentDirectory() const
Definition: juce_File.cpp:360
bool isAChildOf(const File &potentialParentDirectory) const
Definition: juce_File.cpp:382
void removeEmptyStrings(bool removeWhitespaceStrings=true)
int addTokens(StringRef stringToTokenise, bool preserveQuotedStrings)
String quoted(juce_wchar quoteCharacter='"') const