juce_FileSearchPath.h 6.7 KB

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