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.

174 lines
4.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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() {}
  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 (auto& d : directories)
  46. d = d.unquoted();
  47. }
  48. int FileSearchPath::getNumPaths() const
  49. {
  50. return directories.size();
  51. }
  52. File FileSearchPath::operator[] (int index) const
  53. {
  54. return File (directories[index]);
  55. }
  56. String FileSearchPath::toString() const
  57. {
  58. auto dirs = directories;
  59. for (auto& d : dirs)
  60. if (d.containsChar (';'))
  61. d = d.quoted();
  62. return dirs.joinIntoString (";");
  63. }
  64. void FileSearchPath::add (const File& dir, int insertIndex)
  65. {
  66. directories.insert (insertIndex, dir.getFullPathName());
  67. }
  68. bool FileSearchPath::addIfNotAlreadyThere (const File& dir)
  69. {
  70. for (auto& d : directories)
  71. if (File (d) == dir)
  72. return false;
  73. add (dir);
  74. return true;
  75. }
  76. void FileSearchPath::remove (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. Array<File> FileSearchPath::findChildFiles (int whatToLookFor, bool recurse, const String& wildcard) const
  108. {
  109. Array<File> results;
  110. findChildFiles (results, whatToLookFor, recurse, wildcard);
  111. return results;
  112. }
  113. int FileSearchPath::findChildFiles (Array<File>& results, int whatToLookFor,
  114. bool recurse, const String& wildcard) const
  115. {
  116. int total = 0;
  117. for (auto& d : directories)
  118. total += File (d).findChildFiles (results, whatToLookFor, recurse, wildcard);
  119. return total;
  120. }
  121. bool FileSearchPath::isFileInPath (const File& fileToCheck,
  122. const bool checkRecursively) const
  123. {
  124. for (auto& d : directories)
  125. {
  126. if (checkRecursively)
  127. {
  128. if (fileToCheck.isAChildOf (File (d)))
  129. return true;
  130. }
  131. else
  132. {
  133. if (fileToCheck.getParentDirectory() == File (d))
  134. return true;
  135. }
  136. }
  137. return false;
  138. }
  139. } // namespace juce