juce_FileSearchPath.cpp 4.9 KB

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