OpenShot Library | libopenshot-audio  0.1.9
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
FileSearchPath()
Creates an empty search path.
void addPath(const FileSearchPath &)
Merges another search path into this one.
void removeNonExistentPaths()
Removes any directories that don&#39;t actually exist.
String toString() const
Returns the search path as a semicolon-separated list of directories.
bool isAChildOf(const File &potentialParentDirectory) const
Checks whether a file is somewhere inside a directory.
Definition: juce_File.cpp:362
Represents a set of folders that make up a search path.
File getParentDirectory() const
Returns the directory that contains this file or directory.
Definition: juce_File.cpp:340
int getNumPaths() const
Returns the number of folders in this search path.
void removeEmptyStrings(bool removeWhitespaceStrings=true)
Removes empty strings from the array.
void remove(int index)
Removes a string from the array.
Array< File > findChildFiles(int whatToLookFor, bool searchRecursively, const String &wildCardPattern="*") const
Searches the path for a wildcard.
The JUCE String class!
Definition: juce_String.h:42
bool isFileInPath(const File &fileToCheck, bool checkRecursively) const
Finds out whether a file is inside one of the path&#39;s directories.
void trim()
Deletes any whitespace characters from the starts and ends of all the strings.
int addTokens(StringRef stringToTokenise, bool preserveQuotedStrings)
Breaks up a string into tokens and adds them to this array.
void removeRedundantPaths()
Removes any directories that are actually subdirectories of one of the other directories in the searc...
void remove(int indexToRemove)
Removes a directory from the search path.
bool addIfNotAlreadyThere(const File &directoryToAdd)
Adds a new directory to the search path if it&#39;s not already in there.
void clear()
Removes all elements from the array.
Array< File > findChildFiles(int whatToLookFor, bool searchRecursively, const String &wildCardPattern="*") const
Searches this directory for files matching a wildcard pattern.
Definition: juce_File.cpp:546
Represents a local file or directory.
Definition: juce_File.h:44
Holds a resizable array of primitive or copy-by-value objects.
Definition: juce_Array.h:59
File operator[](int index) const
Returns one of the folders in this search path.
String joinIntoString(StringRef separatorString, int startIndex=0, int numberOfElements=-1) const
Joins the strings in the array together into one string.
void insert(int index, String stringToAdd)
Inserts a string into the array.
int size() const noexcept
Returns the number of strings in the array.
FileSearchPath & operator=(const FileSearchPath &)
Copies another search path.
bool isDirectory() const
Checks whether the file is a directory that exists.
const String & getFullPathName() const noexcept
Returns the complete, absolute path of this file.
Definition: juce_File.h:153
void add(const File &directoryToAdd, int insertIndex=-1)
Adds a new directory to the search path.