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.

176 lines
5.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. FileSearchPath::FileSearchPath() {}
  24. FileSearchPath::~FileSearchPath() {}
  25. FileSearchPath::FileSearchPath (const String& path)
  26. {
  27. init (path);
  28. }
  29. FileSearchPath::FileSearchPath (const FileSearchPath& other)
  30. : directories (other.directories)
  31. {
  32. }
  33. FileSearchPath& FileSearchPath::operator= (const FileSearchPath& other)
  34. {
  35. directories = other.directories;
  36. return *this;
  37. }
  38. FileSearchPath& FileSearchPath::operator= (const String& path)
  39. {
  40. init (path);
  41. return *this;
  42. }
  43. void FileSearchPath::init (const String& path)
  44. {
  45. directories.clear();
  46. directories.addTokens (path, ";", "\"");
  47. directories.trim();
  48. directories.removeEmptyStrings();
  49. for (int i = directories.size(); --i >= 0;)
  50. directories.set (i, directories[i].unquoted());
  51. }
  52. int FileSearchPath::getNumPaths() const
  53. {
  54. return directories.size();
  55. }
  56. File FileSearchPath::operator[] (const int index) const
  57. {
  58. return File (directories [index]);
  59. }
  60. String FileSearchPath::toString() const
  61. {
  62. StringArray directories2 (directories);
  63. for (int i = directories2.size(); --i >= 0;)
  64. if (directories2[i].containsChar (';'))
  65. directories2.set (i, directories2[i].quoted());
  66. return directories2.joinIntoString (";");
  67. }
  68. void FileSearchPath::add (const File& dir, const int insertIndex)
  69. {
  70. directories.insert (insertIndex, dir.getFullPathName());
  71. }
  72. bool FileSearchPath::addIfNotAlreadyThere (const File& dir)
  73. {
  74. for (int i = 0; i < directories.size(); ++i)
  75. if (File (directories[i]) == dir)
  76. return false;
  77. add (dir);
  78. return true;
  79. }
  80. void FileSearchPath::remove (const int index)
  81. {
  82. directories.remove (index);
  83. }
  84. void FileSearchPath::addPath (const FileSearchPath& other)
  85. {
  86. for (int i = 0; i < other.getNumPaths(); ++i)
  87. addIfNotAlreadyThere (other[i]);
  88. }
  89. void FileSearchPath::removeRedundantPaths()
  90. {
  91. for (int i = directories.size(); --i >= 0;)
  92. {
  93. const File d1 (directories[i]);
  94. for (int j = directories.size(); --j >= 0;)
  95. {
  96. const File d2 (directories[j]);
  97. if ((i != j) && (d1.isAChildOf (d2) || d1 == d2))
  98. {
  99. directories.remove (i);
  100. break;
  101. }
  102. }
  103. }
  104. }
  105. void FileSearchPath::removeNonExistentPaths()
  106. {
  107. for (int i = directories.size(); --i >= 0;)
  108. if (! File (directories[i]).isDirectory())
  109. directories.remove (i);
  110. }
  111. int FileSearchPath::findChildFiles (Array<File>& results,
  112. const int whatToLookFor,
  113. const bool searchRecursively,
  114. const String& wildCardPattern) const
  115. {
  116. int total = 0;
  117. for (int i = 0; i < directories.size(); ++i)
  118. total += operator[] (i).findChildFiles (results,
  119. whatToLookFor,
  120. searchRecursively,
  121. wildCardPattern);
  122. return total;
  123. }
  124. bool FileSearchPath::isFileInPath (const File& fileToCheck,
  125. const bool checkRecursively) const
  126. {
  127. for (int i = directories.size(); --i >= 0;)
  128. {
  129. const File d (directories[i]);
  130. if (checkRecursively)
  131. {
  132. if (fileToCheck.isAChildOf (d))
  133. return true;
  134. }
  135. else
  136. {
  137. if (fileToCheck.getParentDirectory() == d)
  138. return true;
  139. }
  140. }
  141. return false;
  142. }