Glob.hxx.in 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
  3. #ifndef @KWSYS_NAMESPACE@_Glob_hxx
  4. #define @KWSYS_NAMESPACE@_Glob_hxx
  5. #include <@KWSYS_NAMESPACE@/Configure.h>
  6. #include <@KWSYS_NAMESPACE@/Configure.hxx>
  7. #include <string>
  8. #include <vector>
  9. namespace @KWSYS_NAMESPACE@ {
  10. class GlobInternals;
  11. /** \class Glob
  12. * \brief Portable globbing searches.
  13. *
  14. * Globbing expressions are much simpler than regular
  15. * expressions. This class will search for files using
  16. * globbing expressions.
  17. *
  18. * Finds all files that match a given globbing expression.
  19. */
  20. class @KWSYS_NAMESPACE@_EXPORT Glob
  21. {
  22. public:
  23. enum MessageType
  24. {
  25. error,
  26. cyclicRecursion
  27. };
  28. struct Message
  29. {
  30. MessageType type;
  31. std::string content;
  32. Message(MessageType t, const std::string& c)
  33. : type(t)
  34. , content(c)
  35. {
  36. }
  37. Message(const Message& msg)
  38. : type(msg.type)
  39. , content(msg.content)
  40. {
  41. }
  42. Message& operator=(Message const& msg)
  43. {
  44. this->type = msg.type;
  45. this->content = msg.content;
  46. return *this;
  47. }
  48. };
  49. typedef std::vector<Message> GlobMessages;
  50. typedef std::vector<Message>::iterator GlobMessagesIterator;
  51. public:
  52. Glob();
  53. ~Glob();
  54. //! Find all files that match the pattern.
  55. bool FindFiles(const std::string& inexpr, GlobMessages* messages = 0);
  56. //! Return the list of files that matched.
  57. std::vector<std::string>& GetFiles();
  58. //! Set recurse to true to match subdirectories.
  59. void RecurseOn() { this->SetRecurse(true); }
  60. void RecurseOff() { this->SetRecurse(false); }
  61. void SetRecurse(bool i) { this->Recurse = i; }
  62. bool GetRecurse() { return this->Recurse; }
  63. //! Set recurse through symlinks to true if recursion should traverse the
  64. // linked-to directories
  65. void RecurseThroughSymlinksOn() { this->SetRecurseThroughSymlinks(true); }
  66. void RecurseThroughSymlinksOff() { this->SetRecurseThroughSymlinks(false); }
  67. void SetRecurseThroughSymlinks(bool i) { this->RecurseThroughSymlinks = i; }
  68. bool GetRecurseThroughSymlinks() { return this->RecurseThroughSymlinks; }
  69. //! Get the number of symlinks followed through recursion
  70. unsigned int GetFollowedSymlinkCount() { return this->FollowedSymlinkCount; }
  71. //! Set relative to true to only show relative path to files.
  72. void SetRelative(const char* dir);
  73. const char* GetRelative();
  74. /** Convert the given globbing pattern to a regular expression.
  75. There is no way to quote meta-characters. The
  76. require_whole_string argument specifies whether the regex is
  77. automatically surrounded by "^" and "$" to match the whole
  78. string. This is on by default because patterns always match
  79. whole strings, but may be disabled to support concatenating
  80. expressions more easily (regex1|regex2|etc). */
  81. static std::string PatternToRegex(const std::string& pattern,
  82. bool require_whole_string = true,
  83. bool preserve_case = false);
  84. /** Getters and setters for enabling and disabling directory
  85. listing in recursive and non recursive globbing mode.
  86. If listing is enabled in recursive mode it also lists
  87. directory symbolic links even if follow symlinks is enabled. */
  88. void SetListDirs(bool list) { this->ListDirs = list; }
  89. bool GetListDirs() const { return this->ListDirs; }
  90. void SetRecurseListDirs(bool list) { this->RecurseListDirs = list; }
  91. bool GetRecurseListDirs() const { return this->RecurseListDirs; }
  92. protected:
  93. //! Process directory
  94. void ProcessDirectory(std::string::size_type start, const std::string& dir,
  95. GlobMessages* messages);
  96. //! Process last directory, but only when recurse flags is on. That is
  97. // effectively like saying: /path/to/file/**/file
  98. bool RecurseDirectory(std::string::size_type start, const std::string& dir,
  99. GlobMessages* messages);
  100. //! Add regular expression
  101. void AddExpression(const std::string& expr);
  102. //! Add a file to the list
  103. void AddFile(std::vector<std::string>& files, const std::string& file);
  104. GlobInternals* Internals;
  105. bool Recurse;
  106. std::string Relative;
  107. bool RecurseThroughSymlinks;
  108. unsigned int FollowedSymlinkCount;
  109. std::vector<std::string> VisitedSymlinks;
  110. bool ListDirs;
  111. bool RecurseListDirs;
  112. private:
  113. Glob(const Glob&); // Not implemented.
  114. void operator=(const Glob&); // Not implemented.
  115. };
  116. } // namespace @KWSYS_NAMESPACE@
  117. #endif