Glob.hxx.in 4.45 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing#kwsys for details.  */
Andy Cedilnik's avatar
Andy Cedilnik committed
3 4 5
#ifndef @KWSYS_NAMESPACE@_Glob_hxx
#define @KWSYS_NAMESPACE@_Glob_hxx

6 7 8
#include <@KWSYS_NAMESPACE@/Configure.h>
#include <@KWSYS_NAMESPACE@/Configure.hxx>

Brad King's avatar
Brad King committed
9 10
#include <string>
#include <vector>
Andy Cedilnik's avatar
Andy Cedilnik committed
11

12
namespace @KWSYS_NAMESPACE@ {
Andy Cedilnik's avatar
Andy Cedilnik committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26

class GlobInternals;

/** \class Glob
 * \brief Portable globbing searches.
 *
 * Globbing expressions are much simpler than regular
 * expressions. This class will search for files using
 * globbing expressions.
 *
 * Finds all files that match a given globbing expression.
 */
class @KWSYS_NAMESPACE@_EXPORT Glob
{
27 28 29 30 31 32 33 34 35 36
public:
  enum MessageType
  {
    error,
    cyclicRecursion
  };

  struct Message
  {
    MessageType type;
Brad King's avatar
Brad King committed
37
    std::string content;
38

39 40 41 42 43 44 45 46 47 48
    Message(MessageType t, const std::string& c)
      : type(t)
      , content(c)
    {
    }
    Message(const Message& msg)
      : type(msg.type)
      , content(msg.content)
    {
    }
49
    Message& operator=(Message const& msg)
50
    {
51 52 53
      this->type = msg.type;
      this->content = msg.content;
      return *this;
54
    }
55 56
  };

Brad King's avatar
Brad King committed
57 58
  typedef std::vector<Message> GlobMessages;
  typedef std::vector<Message>::iterator GlobMessagesIterator;
59

Andy Cedilnik's avatar
Andy Cedilnik committed
60 61 62 63 64
public:
  Glob();
  ~Glob();

  //! Find all files that match the pattern.
65
  bool FindFiles(const std::string& inexpr, GlobMessages* messages = 0);
Andy Cedilnik's avatar
Andy Cedilnik committed
66 67

  //! Return the list of files that matched.
Brad King's avatar
Brad King committed
68
  std::vector<std::string>& GetFiles();
Andy Cedilnik's avatar
Andy Cedilnik committed
69 70 71 72

  //! Set recurse to true to match subdirectories.
  void RecurseOn() { this->SetRecurse(true); }
  void RecurseOff() { this->SetRecurse(false); }
73 74 75
  void SetRecurse(bool i) { this->Recurse = i; }
  bool GetRecurse() { return this->Recurse; }

76 77 78 79 80 81 82
  //! Set recurse through symlinks to true if recursion should traverse the
  // linked-to directories
  void RecurseThroughSymlinksOn() { this->SetRecurseThroughSymlinks(true); }
  void RecurseThroughSymlinksOff() { this->SetRecurseThroughSymlinks(false); }
  void SetRecurseThroughSymlinks(bool i) { this->RecurseThroughSymlinks = i; }
  bool GetRecurseThroughSymlinks() { return this->RecurseThroughSymlinks; }

83 84 85
  //! Get the number of symlinks followed through recursion
  unsigned int GetFollowedSymlinkCount() { return this->FollowedSymlinkCount; }

86 87 88
  //! Set relative to true to only show relative path to files.
  void SetRelative(const char* dir);
  const char* GetRelative();
Andy Cedilnik's avatar
Andy Cedilnik committed
89

90 91 92 93 94 95 96
  /** Convert the given globbing pattern to a regular expression.
      There is no way to quote meta-characters.  The
      require_whole_string argument specifies whether the regex is
      automatically surrounded by "^" and "$" to match the whole
      string.  This is on by default because patterns always match
      whole strings, but may be disabled to support concatenating
      expressions more easily (regex1|regex2|etc).  */
Brad King's avatar
Brad King committed
97
  static std::string PatternToRegex(const std::string& pattern,
98 99
                                    bool require_whole_string = true,
                                    bool preserve_case = false);
100

101 102 103 104
  /** Getters and setters for enabling and disabling directory
      listing in recursive and non recursive globbing mode.
      If listing is enabled in recursive mode it also lists
      directory symbolic links even if follow symlinks is enabled. */
105
  void SetListDirs(bool list) { this->ListDirs = list; }
106
  bool GetListDirs() const { return this->ListDirs; }
107
  void SetRecurseListDirs(bool list) { this->RecurseListDirs = list; }
108 109
  bool GetRecurseListDirs() const { return this->RecurseListDirs; }

Andy Cedilnik's avatar
Andy Cedilnik committed
110 111
protected:
  //! Process directory
112 113
  void ProcessDirectory(std::string::size_type start, const std::string& dir,
                        GlobMessages* messages);
Andy Cedilnik's avatar
Andy Cedilnik committed
114 115 116

  //! Process last directory, but only when recurse flags is on. That is
  // effectively like saying: /path/to/file/**/file
117 118
  bool RecurseDirectory(std::string::size_type start, const std::string& dir,
                        GlobMessages* messages);
Andy Cedilnik's avatar
Andy Cedilnik committed
119 120

  //! Add regular expression
Brad King's avatar
Brad King committed
121
  void AddExpression(const std::string& expr);
Andy Cedilnik's avatar
Andy Cedilnik committed
122

123
  //! Add a file to the list
Brad King's avatar
Brad King committed
124
  void AddFile(std::vector<std::string>& files, const std::string& file);
125 126 127

  GlobInternals* Internals;
  bool Recurse;
Brad King's avatar
Brad King committed
128
  std::string Relative;
129
  bool RecurseThroughSymlinks;
130
  unsigned int FollowedSymlinkCount;
Brad King's avatar
Brad King committed
131
  std::vector<std::string> VisitedSymlinks;
132 133
  bool ListDirs;
  bool RecurseListDirs;
134 135

private:
136 137
  Glob(const Glob&);           // Not implemented.
  void operator=(const Glob&); // Not implemented.
Andy Cedilnik's avatar
Andy Cedilnik committed
138 139 140 141 142
};

} // namespace @KWSYS_NAMESPACE@

#endif