OpenShot Library | libopenshot-audio  0.2.0
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
juce::FileSearchPath::operator[]
File operator[](int index) const
Returns one of the folders in this search path.
Definition: juce_FileSearchPath.cpp:67
juce::FileSearchPath::remove
void remove(int indexToRemove)
Removes a directory from the search path.
Definition: juce_FileSearchPath.cpp:98
juce::File::getParentDirectory
File getParentDirectory() const
Returns the directory that contains this file or directory.
Definition: juce_File.cpp:340
juce::StringArray::trim
void trim()
Deletes any whitespace characters from the starts and ends of all the strings.
Definition: juce_StringArray.cpp:265
juce::StringArray::removeEmptyStrings
void removeEmptyStrings(bool removeWhitespaceStrings=true)
Removes empty strings from the array.
Definition: juce_StringArray.cpp:249
juce::File::getFullPathName
const String & getFullPathName() const noexcept
Returns the complete, absolute path of this file.
Definition: juce_File.h:153
juce::File::isAChildOf
bool isAChildOf(const File &potentialParentDirectory) const
Checks whether a file is somewhere inside a directory.
Definition: juce_File.cpp:362
juce::Array
Holds a resizable array of primitive or copy-by-value objects.
Definition: juce_Array.h:59
juce::FileSearchPath::FileSearchPath
FileSearchPath()
Creates an empty search path.
Definition: juce_FileSearchPath.cpp:26
juce::File
Represents a local file or directory.
Definition: juce_File.h:44
juce::FileSearchPath::~FileSearchPath
~FileSearchPath()
Destructor.
Definition: juce_FileSearchPath.cpp:27
juce::FileSearchPath::toString
String toString() const
Returns the search path as a semicolon-separated list of directories.
Definition: juce_FileSearchPath.cpp:72
juce::FileSearchPath::operator=
FileSearchPath & operator=(const FileSearchPath &)
Copies another search path.
Definition: juce_FileSearchPath.cpp:39
juce::FileSearchPath::addPath
void addPath(const FileSearchPath &)
Merges another search path into this one.
Definition: juce_FileSearchPath.cpp:103
juce::FileSearchPath::removeNonExistentPaths
void removeNonExistentPaths()
Removes any directories that don't actually exist.
Definition: juce_FileSearchPath.cpp:128
juce::FileSearchPath::add
void add(const File &directoryToAdd, int insertIndex=-1)
Adds a new directory to the search path.
Definition: juce_FileSearchPath.cpp:83
juce::String::quoted
String quoted(juce_wchar quoteCharacter='"') const
Adds quotation marks around a string.
Definition: juce_String.cpp:1630
juce::StringArray::addTokens
int addTokens(StringRef stringToTokenise, bool preserveQuotedStrings)
Breaks up a string into tokens and adds them to this array.
Definition: juce_StringArray.cpp:328
juce::File::findChildFiles
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
juce::String
The JUCE String class!
Definition: juce_String.h:42
juce::FileSearchPath::getNumPaths
int getNumPaths() const
Returns the number of folders in this search path.
Definition: juce_FileSearchPath.cpp:62
juce::FileSearchPath::removeRedundantPaths
void removeRedundantPaths()
Removes any directories that are actually subdirectories of one of the other directories in the searc...
Definition: juce_FileSearchPath.cpp:109
juce::FileSearchPath::findChildFiles
Array< File > findChildFiles(int whatToLookFor, bool searchRecursively, const String &wildCardPattern="*") const
Searches the path for a wildcard.
Definition: juce_FileSearchPath.cpp:135
juce::FileSearchPath
Represents a set of folders that make up a search path.
Definition: juce_FileSearchPath.h:38
juce::StringArray::clear
void clear()
Removes all elements from the array.
Definition: juce_StringArray.cpp:111
juce::FileSearchPath::addIfNotAlreadyThere
bool addIfNotAlreadyThere(const File &directoryToAdd)
Adds a new directory to the search path if it's not already in there.
Definition: juce_FileSearchPath.cpp:88
juce::FileSearchPath::isFileInPath
bool isFileInPath(const File &fileToCheck, bool checkRecursively) const
Finds out whether a file is inside one of the path's directories.
Definition: juce_FileSearchPath.cpp:153