CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pathspec.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_PATHSPEC_PATHSPEC_H_
6 #define CVMFS_PATHSPEC_PATHSPEC_H_
7 
8 #include <regex.h>
9 
10 #include <string>
11 #include <vector>
12 
14 
47 class Pathspec {
48  public:
49  static const char kSeparator = '/';
50  static const char kEscaper = '\\';
51  static const char kWildcard = '*';
52  static const char kPlaceholder = '?';
53 
54  protected:
55  typedef std::vector<PathspecElementPattern> ElementPatterns;
56 
57  public:
58  typedef std::vector<std::string> GlobStringSequence;
59 
60  public:
68  explicit Pathspec(const std::string &spec);
69  Pathspec(const Pathspec &other);
70  // TODO(rmeusel): C++11 move constructor
71  ~Pathspec();
72 
80  bool IsMatching(const std::string &query_path) const;
81 
82 
90  bool IsPrefixMatching(const std::string &query_path) const;
91 
100  bool IsMatchingRelaxed(const std::string &query_path) const;
101 
106  bool IsValid() const { return valid_; }
107 
112  bool IsAbsolute() const { return absolute_; }
113 
122 
129  const std::string& GetGlobString() const;
130 
131  Pathspec& operator=(const Pathspec &other);
132  bool operator== (const Pathspec &other) const;
133  bool operator!= (const Pathspec &other) const { return !(*this == other); }
134 
135  static bool IsSpecialChar(const char chr) {
136  return (chr == kWildcard || chr == kPlaceholder);
137  }
138 
139  protected:
140  void Parse(const std::string &spec);
141  void ParsePathElement(const std::string::const_iterator &end,
142  std::string::const_iterator *itr);
143 
144  bool IsPathspecMatching(const std::string &query_path) const;
145  bool IsPathspecPrefixMatching(const std::string &query_path) const;
146  bool IsPathspecMatchingRelaxed(const std::string &query_path) const;
147 
148  bool ApplyRegularExpression(const std::string &query_path,
149  regex_t *regex) const;
150 
151  regex_t* GetRegularExpression() const;
152  regex_t* GetPrefixRegularExpression() const;
153  regex_t* GetRelaxedRegularExpression() const;
154 
155  std::string GenerateRegularExpression(const bool is_relaxed = false,
156  const bool is_prefix = false) const;
157  regex_t* CompileRegularExpression(const std::string &regex) const;
158 
159  void PrintRegularExpressionError(const int error_code) const;
160 
161  void GenerateGlobStringSequence() const;
162  void GenerateGlobString() const;
163 
165 
166  private:
168 
169  mutable regex_t *regex_;
170  mutable regex_t *relaxed_regex_;
171  mutable regex_t *prefix_regex_;
172  mutable std::string glob_string_;
174 
175  mutable bool regex_compiled_;
178  mutable bool glob_string_compiled_;
180 
181  bool valid_;
182  bool absolute_;
183 };
184 
185 #endif // CVMFS_PATHSPEC_PATHSPEC_H_
bool glob_string_compiled_
Definition: pathspec.h:178
const GlobStringSequence & GetGlobStringSequence() const
Definition: pathspec.cc:303
bool operator==(const Pathspec &other) const
Definition: pathspec.cc:274
regex_t * relaxed_regex_
Definition: pathspec.h:170
regex_t * GetRelaxedRegularExpression() const
Definition: pathspec.cc:195
regex_t * CompileRegularExpression(const std::string &regex) const
Definition: pathspec.cc:245
bool absolute_
Definition: pathspec.h:182
void Parse(const std::string &spec)
Definition: pathspec.cc:78
static bool IsSpecialChar(const char chr)
Definition: pathspec.h:135
void ParsePathElement(const std::string::const_iterator &end, std::string::const_iterator *itr)
Definition: pathspec.cc:95
bool IsValid() const
Definition: pathspec.h:106
GlobStringSequence glob_string_sequence_
Definition: pathspec.h:173
bool IsPrefixMatching(const std::string &query_path) const
Definition: pathspec.cc:122
regex_t * GetPrefixRegularExpression() const
Definition: pathspec.cc:181
bool glob_string_sequence_compiled_
Definition: pathspec.h:179
void GenerateGlobString() const
Definition: pathspec.cc:332
std::vector< PathspecElementPattern > ElementPatterns
Definition: pathspec.h:55
std::string GenerateRegularExpression(const bool is_relaxed=false, const bool is_prefix=false) const
Definition: pathspec.cc:209
bool regex_compiled_
Definition: pathspec.h:175
bool valid_
Definition: pathspec.h:181
bool IsPathspecMatchingRelaxed(const std::string &query_path) const
Definition: pathspec.cc:152
static const char kEscaper
Definition: pathspec.h:50
bool prefix_regex_compiled_
Definition: pathspec.h:177
bool IsAbsolute() const
Definition: pathspec.h:112
regex_t * GetRegularExpression() const
Definition: pathspec.cc:168
void DestroyRegularExpressions()
Definition: pathspec.cc:258
void PrintRegularExpressionError(const int error_code) const
Definition: pathspec.cc:295
bool IsPathspecPrefixMatching(const std::string &query_path) const
Definition: pathspec.cc:148
static const char kSeparator
Definition: pathspec.h:49
void GenerateGlobStringSequence() const
Definition: pathspec.cc:312
bool IsPathspecMatching(const std::string &query_path) const
Definition: pathspec.cc:144
static const char kWildcard
Definition: pathspec.h:51
regex_t * prefix_regex_
Definition: pathspec.h:171
std::vector< std::string > GlobStringSequence
Definition: pathspec.h:58
regex_t * regex_
Definition: pathspec.h:169
bool relaxed_regex_compiled_
Definition: pathspec.h:176
bool operator!=(const Pathspec &other) const
Definition: pathspec.h:133
bool ApplyRegularExpression(const std::string &query_path, regex_t *regex) const
Definition: pathspec.cc:156
Pathspec & operator=(const Pathspec &other)
Definition: pathspec.cc:59
bool IsMatchingRelaxed(const std::string &query_path) const
Definition: pathspec.cc:134
const std::string & GetGlobString() const
Definition: pathspec.cc:323
ElementPatterns patterns_
Definition: pathspec.h:167
bool IsMatching(const std::string &query_path) const
Definition: pathspec.cc:110
std::string glob_string_
Definition: pathspec.h:172
~Pathspec()
Definition: pathspec.cc:55
static const char kPlaceholder
Definition: pathspec.h:52
Pathspec(const std::string &spec)
Definition: pathspec.cc:12