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.

168 lines
4.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. FileSearchPath::FileSearchPath() {}
  18. FileSearchPath::~FileSearchPath() {}
  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 (int i = directories.size(); --i >= 0;)
  44. directories.set (i, directories[i].unquoted());
  45. }
  46. int FileSearchPath::getNumPaths() const
  47. {
  48. return directories.size();
  49. }
  50. File FileSearchPath::operator[] (const int index) const
  51. {
  52. return File (directories [index]);
  53. }
  54. String FileSearchPath::toString() const
  55. {
  56. StringArray directories2 (directories);
  57. for (int i = directories2.size(); --i >= 0;)
  58. if (directories2[i].containsChar (';'))
  59. directories2.set (i, directories2[i].quoted());
  60. return directories2.joinIntoString (";");
  61. }
  62. void FileSearchPath::add (const File& dir, const int insertIndex)
  63. {
  64. directories.insert (insertIndex, dir.getFullPathName());
  65. }
  66. bool FileSearchPath::addIfNotAlreadyThere (const File& dir)
  67. {
  68. for (int i = 0; i < directories.size(); ++i)
  69. if (File (directories[i]) == dir)
  70. return false;
  71. add (dir);
  72. return true;
  73. }
  74. void FileSearchPath::remove (const int index)
  75. {
  76. directories.remove (index);
  77. }
  78. void FileSearchPath::addPath (const FileSearchPath& other)
  79. {
  80. for (int i = 0; i < other.getNumPaths(); ++i)
  81. addIfNotAlreadyThere (other[i]);
  82. }
  83. void FileSearchPath::removeRedundantPaths()
  84. {
  85. for (int i = directories.size(); --i >= 0;)
  86. {
  87. const File d1 (directories[i]);
  88. for (int j = directories.size(); --j >= 0;)
  89. {
  90. const File d2 (directories[j]);
  91. if ((i != j) && (d1.isAChildOf (d2) || d1 == d2))
  92. {
  93. directories.remove (i);
  94. break;
  95. }
  96. }
  97. }
  98. }
  99. void FileSearchPath::removeNonExistentPaths()
  100. {
  101. for (int i = directories.size(); --i >= 0;)
  102. if (! File (directories[i]).isDirectory())
  103. directories.remove (i);
  104. }
  105. int FileSearchPath::findChildFiles (Array<File>& results,
  106. const int whatToLookFor,
  107. const bool searchRecursively,
  108. const String& wildCardPattern) const
  109. {
  110. int total = 0;
  111. for (int i = 0; i < directories.size(); ++i)
  112. total += operator[] (i).findChildFiles (results,
  113. whatToLookFor,
  114. searchRecursively,
  115. wildCardPattern);
  116. return total;
  117. }
  118. bool FileSearchPath::isFileInPath (const File& fileToCheck,
  119. const bool checkRecursively) const
  120. {
  121. for (int i = directories.size(); --i >= 0;)
  122. {
  123. const File d (directories[i]);
  124. if (checkRecursively)
  125. {
  126. if (fileToCheck.isAChildOf (d))
  127. return true;
  128. }
  129. else
  130. {
  131. if (fileToCheck.getParentDirectory() == d)
  132. return true;
  133. }
  134. }
  135. return false;
  136. }