CernVM-FS  2.9.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 bool regex_compiled_;
170  mutable regex_t *regex_;
171 
173  mutable regex_t *relaxed_regex_;
174 
176  mutable regex_t *prefix_regex_;
177 
178  mutable bool glob_string_compiled_;
179  mutable std::string glob_string_;
180 
183 
184  bool valid_;
185  bool absolute_;
186 };
187 
188 #endif // CVMFS_PATHSPEC_PATHSPEC_H_
bool glob_string_compiled_
Definition: pathspec.h:178
const GlobStringSequence & GetGlobStringSequence() const
Definition: pathspec.cc:301
bool operator==(const Pathspec &other) const
Definition: pathspec.cc:272
regex_t * relaxed_regex_
Definition: pathspec.h:173
regex_t * GetRelaxedRegularExpression() const
Definition: pathspec.cc:193
regex_t * CompileRegularExpression(const std::string &regex) const
Definition: pathspec.cc:243
bool absolute_
Definition: pathspec.h:185
void Parse(const std::string &spec)
Definition: pathspec.cc:76
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:93
bool IsValid() const
Definition: pathspec.h:106
GlobStringSequence glob_string_sequence_
Definition: pathspec.h:182
bool IsPrefixMatching(const std::string &query_path) const
Definition: pathspec.cc:120
regex_t * GetPrefixRegularExpression() const
Definition: pathspec.cc:179
bool glob_string_sequence_compiled_
Definition: pathspec.h:181
void GenerateGlobString() const
Definition: pathspec.cc:330
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:207
bool regex_compiled_
Definition: pathspec.h:169
bool valid_
Definition: pathspec.h:184
bool IsPathspecMatchingRelaxed(const std::string &query_path) const
Definition: pathspec.cc:150
static const char kEscaper
Definition: pathspec.h:50
bool prefix_regex_compiled_
Definition: pathspec.h:175
bool IsAbsolute() const
Definition: pathspec.h:112
regex_t * GetRegularExpression() const
Definition: pathspec.cc:166
void DestroyRegularExpressions()
Definition: pathspec.cc:256
void PrintRegularExpressionError(const int error_code) const
Definition: pathspec.cc:293
bool IsPathspecPrefixMatching(const std::string &query_path) const
Definition: pathspec.cc:146
static const char kSeparator
Definition: pathspec.h:49
void GenerateGlobStringSequence() const
Definition: pathspec.cc:310
bool IsPathspecMatching(const std::string &query_path) const
Definition: pathspec.cc:142
static const char kWildcard
Definition: pathspec.h:51
regex_t * prefix_regex_
Definition: pathspec.h:176
std::vector< std::string > GlobStringSequence
Definition: pathspec.h:58
regex_t * regex_
Definition: pathspec.h:170
bool relaxed_regex_compiled_
Definition: pathspec.h:172
bool operator!=(const Pathspec &other) const
Definition: pathspec.h:133
bool ApplyRegularExpression(const std::string &query_path, regex_t *regex) const
Definition: pathspec.cc:154
Pathspec & operator=(const Pathspec &other)
Definition: pathspec.cc:57
bool IsMatchingRelaxed(const std::string &query_path) const
Definition: pathspec.cc:132
const std::string & GetGlobString() const
Definition: pathspec.cc:321
ElementPatterns patterns_
Definition: pathspec.h:167
bool IsMatching(const std::string &query_path) const
Definition: pathspec.cc:108
std::string glob_string_
Definition: pathspec.h:179
~Pathspec()
Definition: pathspec.cc:53
static const char kPlaceholder
Definition: pathspec.h:52
Pathspec(const std::string &spec)
Definition: pathspec.cc:12