Directory: | cvmfs/ |
---|---|
File: | cvmfs/pathspec/pathspec_pattern.h |
Date: | 2025-02-09 02:34:19 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 19 | 19 | 100.0% |
Branches: | 2 | 4 | 50.0% |
Line | Branch | Exec | Source |
---|---|---|---|
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 | 1619 | SubPattern() {} | |
41 | 3224 | virtual ~SubPattern() {} | |
42 | virtual SubPattern* Clone() const = 0; | ||
43 | 97 | 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 | 7 | 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 | class PlaintextSubPattern : public SubPattern { | ||
57 | public: | ||
58 | 337 | PlaintextSubPattern() : SubPattern() {} | |
59 |
1/2✓ Branch 2 taken 955 times.
✗ Branch 3 not taken.
|
955 | SubPattern* Clone() const { return new PlaintextSubPattern(*this); } |
60 | bool Compare(const SubPattern *other) const; | ||
61 | |||
62 | void AddChar(const char chr); | ||
63 | 337 | bool IsEmpty() const { return chars_.empty(); } | |
64 | 79 | bool IsPlaintext() const { return true; } | |
65 | |||
66 | std::string GenerateRegularExpression(const bool is_relaxed) const; | ||
67 | std::string GenerateGlobString() const; | ||
68 | |||
69 | protected: | ||
70 | 955 | PlaintextSubPattern(const PlaintextSubPattern &other) : | |
71 |
1/2✓ Branch 2 taken 955 times.
✗ Branch 3 not taken.
|
955 | 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 | class WildcardSubPattern : public SubPattern { | ||
80 | public: | ||
81 | 151 | 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 | 7 | bool IsWildcard() const { return true; } | |
86 | }; | ||
87 | |||
88 | class PlaceholderSubPattern : public SubPattern { | ||
89 | public: | ||
90 | 79 | 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 | 547 | bool IsValid() const { return valid_; } | |
112 | |||
113 | bool operator== (const PathspecElementPattern &other) const; | ||
114 | 102 | bool operator!= (const PathspecElementPattern &other) const { | |
115 | 102 | 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_ | ||
132 |