libzypp  11.13.5
StrMatcher.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_BASE_STRMATCHER_H
13 #define ZYPP_BASE_STRMATCHER_H
14 
15 #include <iosfwd>
16 #include <string>
17 
18 #include "zypp/base/PtrTypes.h"
19 #include "zypp/base/SafeBool.h"
20 #include "zypp/base/Exception.h"
21 
23 namespace zypp
24 {
33  class Match : private base::SafeBool<Match>
34  {
35  private:
36  static const int _modemask;
37  static const int _flagmask;
38 
39  public:
41  enum Mode
42  {
48  GLOB,
51  };
52 
60  static const Match NOCASE;
61 
62 
70  static const Match NO_STORAGE_SOLVABLE;
71  static const Match SUB;
72  static const Match ARRAYSENTINEL;
73  static const Match DISABLED_REPOS;
74  static const Match COMPLETE_FILELIST;
75  static const Match SKIP_KIND;
76  static const Match FILES;
77  static const Match CHECKSUMS;
78 
79 
80  public:
83  : _val( 0 )
84  {}
85 
87  Match( Mode val_r )
88  : _val( modeval( val_r ) )
89  {}
90 
92  explicit Match( int val_r )
93  : _val( val_r )
94  {}
95 
96 #ifndef SWIG // Swig treats it as syntax error
97 
99 #endif
100 
101  public:
103  bool test( const Match & rhs ) const
104  { return ( ( flagval() & rhs.flagval() ) == rhs.flagval() ) && ( !rhs.modeval() || rhs.modeval() == modeval() ); }
105 
107  bool testAnyOf( const Match & rhs ) const
108  { return ( flagval() & rhs.flagval() ) || ( rhs.modeval() && rhs.modeval() == modeval() ); }
109 
111  void set( const Match & rhs )
112  {
113  if ( rhs.modeval() )
114  _val = rhs._val | flagval(); // also set the rhs mode
115  else
116  _val |= rhs._val; // just set the flags
117  }
118 
120  void unset( const Match & rhs )
121  {
122  if ( modeval() == rhs.modeval() )
123  _val = flagval() & ~rhs.flagval(); // also unset mode
124  else
125  _val &= ~rhs.flagval(); // just unset falgs
126  }
127 
129  void turn( const Match & rhs, bool onoff )
130  { onoff ? set( rhs ) : unset( rhs ); }
131 
133  Match & operator|=( const Match & rhs )
134  { set( rhs ); return *this; }
135 
137  Match & operator-=( const Match & rhs )
138  { unset( rhs ); return *this; }
139 
140  public:
142  Mode mode() const;
143 
145  Match flags() const
146  { return Match( flagval() ); }
147 
148  public:
152  int get() const { return _val; }
154  int modeval() const { return _val & _modemask; }
156  int flagval() const { return _val & _flagmask; }
158 
159  public:
163  bool isMode( Mode rhs ) const
164  { return modeval() == modeval( rhs ); }
166  bool isModeString() const
167  { return isMode( STRING ); }
169  bool isModeStringstart() const
170  { return isMode( STRINGSTART ); }
172  bool isModeStringend() const
173  { return isMode( STRINGEND ); }
175  bool isModeSubstring() const
176  { return isMode( SUBSTRING ); }
178  bool isModeGlob() const
179  { return isMode( GLOB ); }
181  bool isModeRegex() const
182  { return isMode( REGEX ); }
183 
185  void setMode( Mode rhs )
186  { _val = modeval( rhs ) | flagval(); }
189  { setMode( STRING ); }
192  { setMode( STRINGSTART ); }
195  { setMode( STRINGEND ); }
198  { setMode( SUBSTRING ); }
200  void setModeGlob()
201  { setMode( GLOB ); }
204  { setMode( REGEX ); }
206 
208  std::string asString() const;
209 
210  private:
212  bool boolTest() const { return _val; }
213 
215  static int modeval( Mode mode_r );
216 
217  private:
218  int _val;
219  };
220 
222  inline bool operator==( const Match & lhs, const Match & rhs )
223  { return lhs.get() == rhs.get(); }
225  inline bool operator!=( const Match & lhs, const Match & rhs )
226  { return lhs.get() != rhs.get(); }
227 
229  inline Match operator|( const Match & lhs, const Match & rhs )
230  { return Match(lhs) |= rhs; }
232  inline Match operator|( Match::Mode lhs, Match::Mode rhs )
233  { return Match(lhs) |= rhs; }
234 
236  inline Match operator-( const Match & lhs, const Match & rhs )
237  { return Match(lhs) -= rhs; }
239  inline Match operator-( Match::Mode lhs, Match::Mode rhs )
240  { return Match(lhs) -= rhs; }
241 
243  std::ostream & operator<<( std::ostream & str, Match::Mode obj );
244 
246  std::ostream & operator<<( std::ostream & str, const Match & obj );
247 
252  struct MatchException : public Exception
253  {
255  explicit MatchException( const std::string & msg_r ) : Exception( msg_r ) {}
256  };
257 
263  {
265  explicit MatchUnknownModeException( const std::string & msg_r ) : MatchException( msg_r ) {}
266 
268  MatchUnknownModeException( const Match & mode_r, const std::string & msg_r = std::string() );
269  };
270 
276  {
278  explicit MatchInvalidRegexException( const std::string & msg_r ) : MatchException( msg_r ) {}
279 
281  MatchInvalidRegexException( const std::string & regex_r, int regcomp_r );
282  };
283 
302  class StrMatcher : private base::SafeBool<StrMatcher>
303  {
304  friend std::ostream & operator<<( std::ostream & str, const StrMatcher & obj );
305 
306  public:
308 
309  public:
311  class Impl;
312 
313  public:
315  StrMatcher();
316 
318  StrMatcher( const std::string & search_r );
319 
321  StrMatcher( const std::string & search_r, const Match & flags_r );
322 
327  StrMatcher( const std::string & search_r, const Match::Mode & flags_r );
328 
330  StrMatcher( const std::string & search_r, int flags_r );
331 
332  #ifndef SWIG // Swig treats it as syntax error
333 
335  #endif
336 
337  public:
343  template<class _Tp>
344  bool operator()( const _Tp & string_r ) const
345  { return doMatch( string_r.c_str() ); }
347  bool operator()( const char * string_r ) const
348  { return doMatch( string_r ); }
349 
350  public:
352  const std::string & searchstring() const;
353 
355  void setSearchstring( const std::string & string_r );
356 
358  void setSearchstring( const std::string & string_r, const Match & flags_r );
359 
361  const Match & flags() const;
362 
364  void setFlags( const Match & flags_r );
365 
366  public:
373  void compile() const;
374 
376  bool isCompiled() const;
377 
382  bool doMatch( const char * string_r ) const;
383 
384  private:
386  bool boolTest() const
387  { return !searchstring().empty(); }
388 
389  private:
392  };
393 
395  std::ostream & operator<<( std::ostream & str, const StrMatcher & obj );
396 
398  bool operator==( const StrMatcher & lhs, const StrMatcher & rhs );
399 
401  inline bool operator!=( const StrMatcher & lhs, const StrMatcher & rhs )
402  { return !( lhs == rhs ); }
403 
405  bool operator<( const StrMatcher & lhs, const StrMatcher & rhs );
406 
407 } // namespace zypp
409 #endif // ZYPP_BASE_STRMATCHER_H