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.

173 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. namespace juce
  18. {
  19. FileSearchPath::FileSearchPath() {}
  20. FileSearchPath::~FileSearchPath() {}
  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::operator= (const FileSearchPath& other)
  30. {
  31. directories = other.directories;
  32. return *this;
  33. }
  34. FileSearchPath& FileSearchPath::operator= (const String& path)
  35. {
  36. init (path);
  37. return *this;
  38. }
  39. void FileSearchPath::init (const String& path)
  40. {
  41. directories.clear();
  42. directories.addTokens (path, ";", "\"");
  43. directories.trim();
  44. directories.removeEmptyStrings();
  45. for (int i = directories.size(); --i >= 0;)
  46. directories.set (i, directories[i].unquoted());
  47. }
  48. int FileSearchPath::getNumPaths() const
  49. {
  50. return directories.size();
  51. }
  52. File FileSearchPath::operator[] (const int index) const
  53. {
  54. return File (directories [index]);
  55. }
  56. String FileSearchPath::toString() const
  57. {
  58. StringArray directories2 (directories);
  59. for (int i = directories2.size(); --i >= 0;)
  60. if (directories2[i].containsChar (';'))
  61. directories2.set (i, directories2[i].quoted());
  62. return directories2.joinIntoString (";");
  63. }
  64. void FileSearchPath::add (const File& dir, const int insertIndex)
  65. {
  66. directories.insert (insertIndex, dir.getFullPathName());
  67. }
  68. bool FileSearchPath::addIfNotAlreadyThere (const File& dir)
  69. {
  70. for (int i = 0; i < directories.size(); ++i)
  71. if (File (directories[i]) == dir)
  72. return false;
  73. add (dir);
  74. return true;
  75. }
  76. void FileSearchPath::remove (const int index)
  77. {
  78. directories.remove (index);
  79. }
  80. void FileSearchPath::addPath (const FileSearchPath& other)
  81. {
  82. for (int i = 0; i < other.getNumPaths(); ++i)
  83. addIfNotAlreadyThere (other[i]);
  84. }
  85. void FileSearchPath::removeRedundantPaths()
  86. {
  87. for (int i = directories.size(); --i >= 0;)
  88. {
  89. const File d1 (directories[i]);
  90. for (int j = directories.size(); --j >= 0;)
  91. {
  92. const File d2 (directories[j]);
  93. if ((i != j) && (d1.isAChildOf (d2) || d1 == d2))
  94. {
  95. directories.remove (i);
  96. break;
  97. }
  98. }
  99. }
  100. }
  101. void FileSearchPath::removeNonExistentPaths()
  102. {
  103. for (int i = directories.size(); --i >= 0;)
  104. if (! File (directories[i]).isDirectory())
  105. directories.remove (i);
  106. }
  107. int FileSearchPath::findChildFiles (Array<File>& results,
  108. const int whatToLookFor,
  109. const bool searchRecursively,
  110. const String& wildCardPattern) const
  111. {
  112. int total = 0;
  113. for (int i = 0; i < directories.size(); ++i)
  114. total += operator[] (i).findChildFiles (results,
  115. whatToLookFor,
  116. searchRecursively,
  117. wildCardPattern);
  118. return total;
  119. }
  120. bool FileSearchPath::isFileInPath (const File& fileToCheck,
  121. const bool checkRecursively) const
  122. {
  123. for (int i = directories.size(); --i >= 0;)
  124. {
  125. const File d (directories[i]);
  126. if (checkRecursively)
  127. {
  128. if (fileToCheck.isAChildOf (d))
  129. return true;
  130. }
  131. else
  132. {
  133. if (fileToCheck.getParentDirectory() == d)
  134. return true;
  135. }
  136. }
  137. return false;
  138. }
  139. } // namespace juce