The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
6.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. FileSearchPath::FileSearchPath (const String& path)
  20. {
  21. init (path);
  22. }
  23. FileSearchPath::FileSearchPath (const FileSearchPath& other)
  24. : directories (other.directories)
  25. {
  26. }
  27. FileSearchPath& FileSearchPath::operator= (const FileSearchPath& other)
  28. {
  29. directories = other.directories;
  30. return *this;
  31. }
  32. FileSearchPath& FileSearchPath::operator= (const String& path)
  33. {
  34. init (path);
  35. return *this;
  36. }
  37. void FileSearchPath::init (const String& path)
  38. {
  39. directories.clear();
  40. directories.addTokens (path, ";", "\"");
  41. directories.trim();
  42. directories.removeEmptyStrings();
  43. for (auto& d : directories)
  44. d = d.unquoted();
  45. }
  46. int FileSearchPath::getNumPaths() const
  47. {
  48. return directories.size();
  49. }
  50. File FileSearchPath::operator[] (int index) const
  51. {
  52. return File (getRawString (index));
  53. }
  54. String FileSearchPath::getRawString (int index) const
  55. {
  56. return directories[index];
  57. }
  58. String FileSearchPath::toString() const
  59. {
  60. return toStringWithSeparator (";");
  61. }
  62. String FileSearchPath::toStringWithSeparator (StringRef separator) const
  63. {
  64. auto dirs = directories;
  65. for (auto& d : dirs)
  66. if (d.contains (separator))
  67. d = d.quoted();
  68. return dirs.joinIntoString (separator);
  69. }
  70. void FileSearchPath::add (const File& dir, int insertIndex)
  71. {
  72. directories.insert (insertIndex, dir.getFullPathName());
  73. }
  74. bool FileSearchPath::addIfNotAlreadyThere (const File& dir)
  75. {
  76. for (auto& d : directories)
  77. if (File (d) == dir)
  78. return false;
  79. add (dir);
  80. return true;
  81. }
  82. void FileSearchPath::remove (int index)
  83. {
  84. directories.remove (index);
  85. }
  86. void FileSearchPath::addPath (const FileSearchPath& other)
  87. {
  88. for (int i = 0; i < other.getNumPaths(); ++i)
  89. addIfNotAlreadyThere (other[i]);
  90. }
  91. void FileSearchPath::removeRedundantPaths()
  92. {
  93. std::vector<String> reduced;
  94. for (const auto& directory : directories)
  95. {
  96. const auto checkedIsChildOf = [&] (const auto& a, const auto& b)
  97. {
  98. return File::isAbsolutePath (a) && File::isAbsolutePath (b) && File (a).isAChildOf (b);
  99. };
  100. const auto fContainsDirectory = [&] (const auto& f)
  101. {
  102. return f == directory || checkedIsChildOf (directory, f);
  103. };
  104. if (std::find_if (reduced.begin(), reduced.end(), fContainsDirectory) != reduced.end())
  105. continue;
  106. const auto directoryContainsF = [&] (const auto& f) { return checkedIsChildOf (f, directory); };
  107. reduced.erase (std::remove_if (reduced.begin(), reduced.end(), directoryContainsF), reduced.end());
  108. reduced.push_back (directory);
  109. }
  110. directories = StringArray (reduced.data(), (int) reduced.size());
  111. }
  112. void FileSearchPath::removeNonExistentPaths()
  113. {
  114. for (int i = directories.size(); --i >= 0;)
  115. if (! File (directories[i]).isDirectory())
  116. directories.remove (i);
  117. }
  118. Array<File> FileSearchPath::findChildFiles (int whatToLookFor, bool recurse, const String& wildcard) const
  119. {
  120. Array<File> results;
  121. findChildFiles (results, whatToLookFor, recurse, wildcard);
  122. return results;
  123. }
  124. int FileSearchPath::findChildFiles (Array<File>& results, int whatToLookFor,
  125. bool recurse, const String& wildcard) const
  126. {
  127. int total = 0;
  128. for (auto& d : directories)
  129. total += File (d).findChildFiles (results, whatToLookFor, recurse, wildcard);
  130. return total;
  131. }
  132. bool FileSearchPath::isFileInPath (const File& fileToCheck,
  133. const bool checkRecursively) const
  134. {
  135. for (auto& d : directories)
  136. {
  137. if (checkRecursively)
  138. {
  139. if (fileToCheck.isAChildOf (File (d)))
  140. return true;
  141. }
  142. else
  143. {
  144. if (fileToCheck.getParentDirectory() == File (d))
  145. return true;
  146. }
  147. }
  148. return false;
  149. }
  150. //==============================================================================
  151. //==============================================================================
  152. #if JUCE_UNIT_TESTS
  153. class FileSearchPathTests final : public UnitTest
  154. {
  155. public:
  156. FileSearchPathTests() : UnitTest ("FileSearchPath", UnitTestCategories::files) {}
  157. void runTest() override
  158. {
  159. beginTest ("removeRedundantPaths");
  160. {
  161. #if JUCE_WINDOWS
  162. const String prefix = "C:";
  163. #else
  164. const String prefix = "";
  165. #endif
  166. {
  167. FileSearchPath fsp { prefix + "/a/b/c/d;" + prefix + "/a/b/c/e;" + prefix + "/a/b/c" };
  168. fsp.removeRedundantPaths();
  169. expectEquals (fsp.toString(), prefix + "/a/b/c");
  170. }
  171. {
  172. FileSearchPath fsp { prefix + "/a/b/c;" + prefix + "/a/b/c/d;" + prefix + "/a/b/c/e" };
  173. fsp.removeRedundantPaths();
  174. expectEquals (fsp.toString(), prefix + "/a/b/c");
  175. }
  176. {
  177. FileSearchPath fsp { prefix + "/a/b/c/d;" + prefix + "/a/b/c;" + prefix + "/a/b/c/e" };
  178. fsp.removeRedundantPaths();
  179. expectEquals (fsp.toString(), prefix + "/a/b/c");
  180. }
  181. {
  182. FileSearchPath fsp { "%FOO%;" + prefix + "/a/b/c;%FOO%;" + prefix + "/a/b/c/d" };
  183. fsp.removeRedundantPaths();
  184. expectEquals (fsp.toString(), "%FOO%;" + prefix + "/a/b/c");
  185. }
  186. }
  187. }
  188. };
  189. static FileSearchPathTests fileSearchPathTests;
  190. #endif
  191. } // namespace juce