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.

169 lines
4.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. FileSearchPath::FileSearchPath()
  19. {
  20. }
  21. FileSearchPath::FileSearchPath (const String& path)
  22. {
  23. init (path);
  24. }
  25. FileSearchPath::FileSearchPath (const FileSearchPath& other)
  26. : directories (other.directories)
  27. {
  28. }
  29. FileSearchPath::~FileSearchPath()
  30. {
  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. void FileSearchPath::addIfNotAlreadyThere (const File& dir)
  67. {
  68. for (int i = 0; i < directories.size(); ++i)
  69. if (File (directories[i]) == dir)
  70. return;
  71. add (dir);
  72. }
  73. void FileSearchPath::remove (const int index)
  74. {
  75. directories.remove (index);
  76. }
  77. void FileSearchPath::addPath (const FileSearchPath& other)
  78. {
  79. for (int i = 0; i < other.getNumPaths(); ++i)
  80. addIfNotAlreadyThere (other[i]);
  81. }
  82. void FileSearchPath::removeRedundantPaths()
  83. {
  84. for (int i = directories.size(); --i >= 0;)
  85. {
  86. const File d1 (directories[i]);
  87. for (int j = directories.size(); --j >= 0;)
  88. {
  89. const File d2 (directories[j]);
  90. if ((i != j) && (d1.isAChildOf (d2) || d1 == d2))
  91. {
  92. directories.remove (i);
  93. break;
  94. }
  95. }
  96. }
  97. }
  98. void FileSearchPath::removeNonExistentPaths()
  99. {
  100. for (int i = directories.size(); --i >= 0;)
  101. if (! File (directories[i]).isDirectory())
  102. directories.remove (i);
  103. }
  104. int FileSearchPath::findChildFiles (Array<File>& results,
  105. const int whatToLookFor,
  106. const bool searchRecursively,
  107. const String& wildCardPattern) const
  108. {
  109. int total = 0;
  110. for (int i = 0; i < directories.size(); ++i)
  111. total += operator[] (i).findChildFiles (results,
  112. whatToLookFor,
  113. searchRecursively,
  114. wildCardPattern);
  115. return total;
  116. }
  117. bool FileSearchPath::isFileInPath (const File& fileToCheck,
  118. const bool checkRecursively) const
  119. {
  120. for (int i = directories.size(); --i >= 0;)
  121. {
  122. const File d (directories[i]);
  123. if (checkRecursively)
  124. {
  125. if (fileToCheck.isAChildOf (d))
  126. return true;
  127. }
  128. else
  129. {
  130. if (fileToCheck.getParentDirectory() == d)
  131. return true;
  132. }
  133. }
  134. return false;
  135. }