1 |
|
|
/** |
2 |
|
|
* This file is part of the CernVM File System. |
3 |
|
|
*/ |
4 |
|
|
|
5 |
|
|
#ifndef CVMFS_PATHSPEC_PATHSPEC_PATTERN_H_ |
6 |
|
|
#define CVMFS_PATHSPEC_PATHSPEC_PATTERN_H_ |
7 |
|
|
|
8 |
|
|
#include <string> |
9 |
|
|
#include <vector> |
10 |
|
|
|
11 |
|
|
/** |
12 |
|
|
* The PathspecElementPattern is used internally by the Pathspec class! |
13 |
|
|
* |
14 |
|
|
* It describes a part of a full Pathspec. Namely only one directory level. |
15 |
|
|
* Each PathspecElementPattern is composed by potentially multiple SubPatterns. |
16 |
|
|
* |
17 |
|
|
* +----------+ +------------------------+ +------------+ |
18 |
|
|
* | Pathspec |----<>----| PathspecElementPattern |----<>----| SubPattern | |
19 |
|
|
* +----------+ +------------------------+ +------------+ |
20 |
|
|
+ ^ ^ ^ |
21 |
|
|
* | | | |
22 |
|
|
* +--------------------------------------------------+ | | |
23 |
|
|
* | +----------------------------+ | |
24 |
|
|
* | | | |
25 |
|
|
* +---------------------+ +--------------------+ +-----------------------+ |
26 |
|
|
* | PlaintextSubPattern | | WildcardSubPattern | | PlaceholderSubPattern | |
27 |
|
|
* +---------------------+ +--------------------+ +-----------------------+ |
28 |
|
|
* |
29 |
|
|
* SubPatterns are implemented as a flat class hierarchy where the different |
30 |
|
|
* patterns are implementing the behaviour of one pattern symbol. |
31 |
|
|
* |
32 |
|
|
* The PathspecElementPattern is taking care of the parsing and creation of |
33 |
|
|
* SubPatterns. |
34 |
|
|
* |
35 |
|
|
*/ |
36 |
|
|
class PathspecElementPattern { |
37 |
|
|
private: |
38 |
|
|
class SubPattern { |
39 |
|
|
public: |
40 |
|
3134 |
SubPattern() {} |
41 |
✗✓ |
3127 |
virtual ~SubPattern() {} |
42 |
|
|
virtual SubPattern* Clone() const = 0; |
43 |
|
184 |
virtual bool IsEmpty() const { return false; } |
44 |
|
|
|
45 |
|
|
virtual bool Compare(const SubPattern *other) const = 0; |
46 |
|
|
|
47 |
|
1 |
virtual bool IsPlaintext() const { return false; } |
48 |
|
19 |
virtual bool IsWildcard() const { return false; } |
49 |
|
2 |
virtual bool IsPlaceholder() const { return false; } |
50 |
|
|
|
51 |
|
|
virtual std::string |
52 |
|
|
GenerateRegularExpression(const bool is_relaxed) const = 0; |
53 |
|
|
virtual std::string GenerateGlobString() const = 0; |
54 |
|
|
}; |
55 |
|
|
|
56 |
✗✗✓ |
4778 |
class PlaintextSubPattern : public SubPattern { |
57 |
|
|
public: |
58 |
|
538 |
PlaintextSubPattern() : SubPattern() {} |
59 |
|
1858 |
SubPattern* Clone() const { return new PlaintextSubPattern(*this); } |
60 |
|
|
bool Compare(const SubPattern *other) const; |
61 |
|
|
|
62 |
|
|
void AddChar(const char chr); |
63 |
|
538 |
bool IsEmpty() const { return chars_.empty(); } |
64 |
|
130 |
bool IsPlaintext() const { return true; } |
65 |
|
|
|
66 |
|
|
std::string GenerateRegularExpression(const bool is_relaxed) const; |
67 |
|
|
std::string GenerateGlobString() const; |
68 |
|
|
|
69 |
|
|
protected: |
70 |
|
1858 |
PlaintextSubPattern(const PlaintextSubPattern &other) : |
71 |
|
1858 |
chars_(other.chars_) {} |
72 |
|
|
PlaintextSubPattern& operator=(const PlaintextSubPattern &other); |
73 |
|
|
bool IsSpecialRegexCharacter(const char chr) const; |
74 |
|
|
|
75 |
|
|
private: |
76 |
|
|
std::string chars_; |
77 |
|
|
}; |
78 |
|
|
|
79 |
✗✓ |
1569 |
class WildcardSubPattern : public SubPattern { |
80 |
|
|
public: |
81 |
|
400 |
SubPattern* Clone() const { return new WildcardSubPattern(); } |
82 |
|
|
bool Compare(const SubPattern *other) const; |
83 |
|
|
std::string GenerateRegularExpression(const bool is_relaxed) const; |
84 |
|
|
std::string GenerateGlobString() const; |
85 |
|
10 |
bool IsWildcard() const { return true; } |
86 |
|
|
}; |
87 |
|
|
|
88 |
✗✓ |
645 |
class PlaceholderSubPattern : public SubPattern { |
89 |
|
|
public: |
90 |
|
154 |
SubPattern* Clone() const { return new PlaceholderSubPattern(); } |
91 |
|
|
bool Compare(const SubPattern *other) const; |
92 |
|
|
std::string GenerateRegularExpression(const bool is_relaxed) const; |
93 |
|
|
std::string GenerateGlobString() const; |
94 |
|
10 |
bool IsPlaceholder() const { return true; } |
95 |
|
|
}; |
96 |
|
|
|
97 |
|
|
private: |
98 |
|
|
typedef std::vector<SubPattern*> SubPatterns; |
99 |
|
|
|
100 |
|
|
public: |
101 |
|
|
PathspecElementPattern(const std::string::const_iterator begin, |
102 |
|
|
const std::string::const_iterator &end); |
103 |
|
|
PathspecElementPattern(const PathspecElementPattern& other); |
104 |
|
|
PathspecElementPattern& operator=(const PathspecElementPattern& other); |
105 |
|
|
// TODO(rmeusel): C++11 - move constructor! |
106 |
|
|
~PathspecElementPattern(); |
107 |
|
|
|
108 |
|
|
std::string GenerateRegularExpression(const bool is_relaxed = false) const; |
109 |
|
|
std::string GenerateGlobString() const; |
110 |
|
|
|
111 |
|
943 |
bool IsValid() const { return valid_; } |
112 |
|
|
|
113 |
|
|
bool operator== (const PathspecElementPattern &other) const; |
114 |
|
180 |
bool operator!= (const PathspecElementPattern &other) const { |
115 |
|
180 |
return !(*this == other); |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
protected: |
119 |
|
|
void Parse(const std::string::const_iterator &begin, |
120 |
|
|
const std::string::const_iterator &end); |
121 |
|
|
SubPattern* ParsePlaintext(const std::string::const_iterator &end, |
122 |
|
|
std::string::const_iterator *i); |
123 |
|
|
SubPattern* ParseSpecialChar(const std::string::const_iterator &end, |
124 |
|
|
std::string::const_iterator *i); |
125 |
|
|
|
126 |
|
|
private: |
127 |
|
|
bool valid_; |
128 |
|
|
SubPatterns subpatterns_; |
129 |
|
|
}; |
130 |
|
|
|
131 |
|
|
#endif // CVMFS_PATHSPEC_PATHSPEC_PATTERN_H_ |