juce_FileSearchPath.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. //==============================================================================
  20. /**
  21. Represents a set of folders that make up a search path.
  22. @see File
  23. @tags{Core}
  24. */
  25. class JUCE_API FileSearchPath
  26. {
  27. public:
  28. //==============================================================================
  29. /** Creates an empty search path. */
  30. FileSearchPath();
  31. /** Creates a search path from a string of pathnames.
  32. The path can be semicolon- or comma-separated, e.g.
  33. "/foo/bar;/foo/moose;/fish/moose"
  34. The separate folders are tokenised and added to the search path.
  35. */
  36. FileSearchPath (const String& path);
  37. /** Creates a copy of another search path. */
  38. FileSearchPath (const FileSearchPath&);
  39. /** Copies another search path. */
  40. FileSearchPath& operator= (const FileSearchPath&);
  41. /** Destructor. */
  42. ~FileSearchPath();
  43. /** Uses a string containing a list of pathnames to re-initialise this list.
  44. This search path is cleared and the semicolon- or comma-separated folders
  45. in this string are added instead. e.g. "/foo/bar;/foo/moose;/fish/moose"
  46. */
  47. FileSearchPath& operator= (const String& path);
  48. //==============================================================================
  49. /** Returns the number of folders in this search path.
  50. @see operator[]
  51. */
  52. int getNumPaths() const;
  53. /** Returns one of the folders in this search path.
  54. The file returned isn't guaranteed to actually be a valid directory.
  55. @see getNumPaths
  56. */
  57. File operator[] (int index) const;
  58. /** Returns the search path as a semicolon-separated list of directories. */
  59. String toString() const;
  60. //==============================================================================
  61. /** Adds a new directory to the search path.
  62. The new directory is added to the end of the list if the insertIndex parameter is
  63. less than zero, otherwise it is inserted at the given index.
  64. */
  65. void add (const File& directoryToAdd,
  66. int insertIndex = -1);
  67. /** Adds a new directory to the search path if it's not already in there.
  68. @return true if the directory has been added, false otherwise.
  69. */
  70. bool addIfNotAlreadyThere (const File& directoryToAdd);
  71. /** Removes a directory from the search path. */
  72. void remove (int indexToRemove);
  73. /** Merges another search path into this one.
  74. This will remove any duplicate directories.
  75. */
  76. void addPath (const FileSearchPath&);
  77. /** Removes any directories that are actually subdirectories of one of the other directories in the search path.
  78. If the search is intended to be recursive, there's no point having nested folders in the search
  79. path, because they'll just get searched twice and you'll get duplicate results.
  80. e.g. if the path is "c:\abc\de;c:\abc", this method will simplify it to "c:\abc"
  81. */
  82. void removeRedundantPaths();
  83. /** Removes any directories that don't actually exist. */
  84. void removeNonExistentPaths();
  85. //==============================================================================
  86. /** Searches the path for a wildcard.
  87. This will search all the directories in the search path in order and return
  88. an array of the files that were found.
  89. @param whatToLookFor a value from the File::TypesOfFileToFind enum, specifying whether to
  90. return files, directories, or both.
  91. @param searchRecursively whether to recursively search the subdirectories too
  92. @param wildCardPattern a pattern to match against the filenames
  93. @returns the number of files added to the array
  94. @see File::findChildFiles
  95. */
  96. Array<File> findChildFiles (int whatToLookFor,
  97. bool searchRecursively,
  98. const String& wildCardPattern = "*") const;
  99. /** Searches the path for a wildcard.
  100. Note that there's a newer, better version of this method which returns the results
  101. array, and in almost all cases, you should use that one instead! This one is kept around
  102. mainly for legacy code to use.
  103. */
  104. int findChildFiles (Array<File>& results,
  105. int whatToLookFor,
  106. bool searchRecursively,
  107. const String& wildCardPattern = "*") const;
  108. //==============================================================================
  109. /** Finds out whether a file is inside one of the path's directories.
  110. This will return true if the specified file is a child of one of the
  111. directories specified by this path. Note that this doesn't actually do any
  112. searching or check that the files exist - it just looks at the pathnames
  113. to work out whether the file would be inside a directory.
  114. @param fileToCheck the file to look for
  115. @param checkRecursively if true, then this will return true if the file is inside a
  116. subfolder of one of the path's directories (at any depth). If false
  117. it will only return true if the file is actually a direct child
  118. of one of the directories.
  119. @see File::isAChildOf
  120. */
  121. bool isFileInPath (const File& fileToCheck,
  122. bool checkRecursively) const;
  123. private:
  124. //==============================================================================
  125. StringArray directories;
  126. void init (const String&);
  127. JUCE_LEAK_DETECTOR (FileSearchPath)
  128. };
  129. } // namespace juce