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

#include <@KWSYS_NAMESPACE@/Configure.h>
#include <@KWSYS_NAMESPACE@/Configure.hxx>

Kitware Robot's avatar
Kitware Robot committed
9 10
#include <string>
#include <vector>
Kitware Robot's avatar
Kitware Robot committed
11

Kitware Robot's avatar
Kitware Robot committed
12
namespace @KWSYS_NAMESPACE@ {
Kitware Robot's avatar
Kitware Robot 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
{
Kitware Robot's avatar
Kitware Robot committed
27 28 29 30 31 32 33 34 35 36
public:
  enum MessageType
  {
    error,
    cyclicRecursion
  };

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

Kitware Robot's avatar
Kitware Robot committed
39 40 41 42 43
    Message(MessageType t, const std::string& c)
      : type(t)
      , content(c)
    {
    }
Kitware Robot's avatar
Kitware Robot committed
44 45 46
    ~Message() = default;
    Message(const Message& msg) = default;
    Message& operator=(Message const& msg) = default;
Kitware Robot's avatar
Kitware Robot committed
47 48
  };

Kitware Robot's avatar
Kitware Robot committed
49 50
  typedef std::vector<Message> GlobMessages;
  typedef std::vector<Message>::iterator GlobMessagesIterator;
Kitware Robot's avatar
Kitware Robot committed
51

Kitware Robot's avatar
Kitware Robot committed
52 53 54 55 56
public:
  Glob();
  ~Glob();

  //! Find all files that match the pattern.
Kitware Robot's avatar
Kitware Robot committed
57
  bool FindFiles(const std::string& inexpr, GlobMessages* messages = 0);
Kitware Robot's avatar
Kitware Robot committed
58 59

  //! Return the list of files that matched.
Kitware Robot's avatar
Kitware Robot committed
60
  std::vector<std::string>& GetFiles();
Kitware Robot's avatar
Kitware Robot committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

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

  //! 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; }

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

  //! Set relative to true to only show relative path to files.
  void SetRelative(const char* dir);
  const char* GetRelative();

  /** 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).  */
Kitware Robot's avatar
Kitware Robot committed
89
  static std::string PatternToRegex(const std::string& pattern,
Kitware Robot's avatar
Kitware Robot committed
90 91
                                    bool require_whole_string = true,
                                    bool preserve_case = false);
Kitware Robot's avatar
Kitware Robot committed
92

Kitware Robot's avatar
Kitware Robot committed
93 94 95 96
  /** 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. */
Kitware Robot's avatar
Kitware Robot committed
97
  void SetListDirs(bool list) { this->ListDirs = list; }
Kitware Robot's avatar
Kitware Robot committed
98
  bool GetListDirs() const { return this->ListDirs; }
Kitware Robot's avatar
Kitware Robot committed
99
  void SetRecurseListDirs(bool list) { this->RecurseListDirs = list; }
Kitware Robot's avatar
Kitware Robot committed
100 101
  bool GetRecurseListDirs() const { return this->RecurseListDirs; }

Kitware Robot's avatar
Kitware Robot committed
102 103
protected:
  //! Process directory
Kitware Robot's avatar
Kitware Robot committed
104 105
  void ProcessDirectory(std::string::size_type start, const std::string& dir,
                        GlobMessages* messages);
Kitware Robot's avatar
Kitware Robot committed
106 107 108

  //! Process last directory, but only when recurse flags is on. That is
  // effectively like saying: /path/to/file/**/file
Kitware Robot's avatar
Kitware Robot committed
109 110
  bool RecurseDirectory(std::string::size_type start, const std::string& dir,
                        GlobMessages* messages);
Kitware Robot's avatar
Kitware Robot committed
111 112

  //! Add regular expression
Kitware Robot's avatar
Kitware Robot committed
113
  void AddExpression(const std::string& expr);
Kitware Robot's avatar
Kitware Robot committed
114 115

  //! Add a file to the list
Kitware Robot's avatar
Kitware Robot committed
116
  void AddFile(std::vector<std::string>& files, const std::string& file);
Kitware Robot's avatar
Kitware Robot committed
117 118 119

  GlobInternals* Internals;
  bool Recurse;
Kitware Robot's avatar
Kitware Robot committed
120
  std::string Relative;
Kitware Robot's avatar
Kitware Robot committed
121 122
  bool RecurseThroughSymlinks;
  unsigned int FollowedSymlinkCount;
Kitware Robot's avatar
Kitware Robot committed
123
  std::vector<std::string> VisitedSymlinks;
Kitware Robot's avatar
Kitware Robot committed
124 125
  bool ListDirs;
  bool RecurseListDirs;
Kitware Robot's avatar
Kitware Robot committed
126 127

private:
Kitware Robot's avatar
Kitware Robot committed
128 129
  Glob(const Glob&);           // Not implemented.
  void operator=(const Glob&); // Not implemented.
Kitware Robot's avatar
Kitware Robot committed
130 131 132 133 134
};

} // namespace @KWSYS_NAMESPACE@

#endif