ref: refs/heads/ma-tmp-pqt
commit 52c7423ebd053a8189681cebc87f1e5b563f89b0
Author: Michael Andres
Date: Thu Apr 9 16:12:26 2009 +0200
new sat::AttrMatcher: String matching (STRING|SUBSTRING|GLOB|REGEX).
---
zypp/CMakeLists.txt | 2 +
zypp/sat/AttrMatcher.cc | 165 +++++++++++++++++++++++++++++++++++++++++++++++
zypp/sat/AttrMatcher.h | 128 ++++++++++++++++++++++++++++++++++++
3 files changed, 295 insertions(+), 0 deletions(-)
diff --git a/zypp/CMakeLists.txt b/zypp/CMakeLists.txt
index 68c0102..addd92d 100644
--- a/zypp/CMakeLists.txt
+++ b/zypp/CMakeLists.txt
@@ -508,6 +508,7 @@ SET( zypp_sat_SRCS
sat/LocaleSupport.cc
sat/LookupAttr.cc
sat/SolvAttr.cc
+ sat/AttrMatcher.cc
)
SET( zypp_sat_HEADERS
@@ -521,6 +522,7 @@ SET( zypp_sat_HEADERS
sat/LookupAttr.h
sat/LookupAttrTools.h
sat/SolvAttr.h
+ sat/AttrMatcher.h
)
INSTALL( FILES
diff --git a/zypp/sat/AttrMatcher.cc b/zypp/sat/AttrMatcher.cc
new file mode 100644
index 0000000..fd53562
--- /dev/null
+++ b/zypp/sat/AttrMatcher.cc
@@ -0,0 +1,165 @@
+/*---------------------------------------------------------------------\
+| ____ _ __ __ ___ |
+| |__ / \ / / . \ . \ |
+| / / \ V /| _/ _/ |
+| / /__ | | | | | | |
+| /_____||_| |_| |_| |
+| |
+\---------------------------------------------------------------------*/
+/** \file zypp/sat/AttrMatcher.cc
+ *
+*/
+extern "C"
+{
+#include "satsolver/repo.h"
+}
+
+#include <iostream>
+#include "zypp/base/LogTools.h"
+
+#include "zypp/sat/AttrMatcher.h"
+
+using std::endl;
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+ ///////////////////////////////////////////////////////////////////
+ namespace sat
+ { /////////////////////////////////////////////////////////////////
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // CLASS NAME : AttrMatcher::Impl
+ //
+ /** AttrMatcher implementation.
+ *
+ * Take care to release any allocated regex by calling
+ * \c ::datamatcher_free.
+ */
+ struct AttrMatcher::Impl
+ {
+ Impl()
+ : _flags( 0 )
+ {}
+
+ Impl( const std::string & search_r, int flags_r )
+ : _search( search_r )
+ , _flags( flags_r )
+ {}
+
+ ~Impl()
+ { invalidate(); }
+
+ /** Return whether string matches. */
+ bool doMatch( const char * string_r ) const
+ {
+ if ( ! string_r || ! *string_r )
+ return true; // every string matches nothing.
+
+ if ( !_matcher )
+ {
+ _matcher.reset( new ::_Datamatcher );
+ ::datamatcher_init( _matcher.get(), _search.c_str(), _flags );
+ }
+
+ return ::datamatcher_match( _matcher.get(), string_r );
+ }
+
+ /** The current searchstring. */
+ const std::string & searchstring() const
+ { return _search; }
+
+ /** Set a new searchstring. */
+ void setSearchstring( const std::string & string_r )
+ { invalidate(); _search = string_r; }
+
+ /** The current search flags. */
+ int flags() const
+ { return _flags; }
+
+ /** Set new search flags. */
+ void setFlags( int flags_r )
+ { invalidate(); _flags = flags_r; }
+
+ private:
+ /** Has to be called if _search or _flags change. */
+ void invalidate()
+ {
+ if ( _matcher )
+ ::datamatcher_free( _matcher.get() );
+ //_search[0] = 0;
+ _matcher.reset();
+ }
+
+ private:
+ public:
+ std::string _search;
+ int _flags;
+ mutable scoped_ptr< ::_Datamatcher> _matcher;
+
+ private:
+ friend Impl * rwcowClone<Impl>( const Impl * rhs );
+ /** clone for RWCOW_pointer */
+ Impl * clone() const
+ { return new Impl( _search, _flags ); }
+ };
+ ///////////////////////////////////////////////////////////////////
+
+ /** \relates AttrMatcher::Impl Stream output */
+ inline std::ostream & operator<<( std::ostream & str, const AttrMatcher::Impl & obj )
+ {
+ return str << "match(" << obj.searchstring() << "|" << obj.flags() << ")";
+ }
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // CLASS NAME : AttrMatcher
+ //
+ ///////////////////////////////////////////////////////////////////
+
+ AttrMatcher::AttrMatcher()
+ : _pimpl( new Impl )
+ {}
+
+ AttrMatcher::AttrMatcher( const std::string & search_r, int flags_r )
+ : _pimpl( new Impl( search_r, flags_r ) )
+ {}
+
+ bool AttrMatcher::doMatch( const char * string_r ) const
+ { return _pimpl->doMatch( string_r ); }
+
+ const std::string & AttrMatcher::searchstring() const
+ { return _pimpl->searchstring(); }
+
+ void AttrMatcher::setSearchstring( const std::string & string_r )
+ { _pimpl->setSearchstring( string_r ); }
+
+ void AttrMatcher::setSearchstring( const std::string & string_r, int flags_r )
+ {
+ _pimpl->setSearchstring( string_r );
+ _pimpl->setFlags( flags_r );
+ }
+
+ int AttrMatcher::flags() const
+ { return _pimpl->flags(); }
+
+ void AttrMatcher::setFlags( int flags_r )
+ { _pimpl->setFlags( flags_r ); }
+
+ /******************************************************************
+ **
+ ** FUNCTION NAME : operator<<
+ ** FUNCTION TYPE : std::ostream &
+ */
+ std::ostream & operator<<( std::ostream & str, const AttrMatcher & obj )
+ {
+ return str << *obj._pimpl;
+ }
+
+ /////////////////////////////////////////////////////////////////
+ } // namespace sat
+ ///////////////////////////////////////////////////////////////////
+ /////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
diff --git a/zypp/sat/AttrMatcher.h b/zypp/sat/AttrMatcher.h
new file mode 100644
index 0000000..f918f1d
--- /dev/null
+++ b/zypp/sat/AttrMatcher.h
@@ -0,0 +1,128 @@
+/*---------------------------------------------------------------------\
+| ____ _ __ __ ___ |
+| |__ / \ / / . \ . \ |
+| / / \ V /| _/ _/ |
+| / /__ | | | | | | |
+| /_____||_| |_| |_| |
+| |
+\---------------------------------------------------------------------*/
+/** \file zypp/sat/AttrMatcher.h
+ *
+*/
+#ifndef ZYPP_SAT_ATTRMATCHER_H
+#define ZYPP_SAT_ATTRMATCHER_H
+
+extern "C"
+{
+struct _Datamatcher;
+}
+
+#include <iosfwd>
+#include <string>
+
+#include "zypp/base/PtrTypes.h"
+#include "zypp/base/SafeBool.h"
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+ ///////////////////////////////////////////////////////////////////
+ namespace sat
+ { /////////////////////////////////////////////////////////////////
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // CLASS NAME : AttrMatcher
+ //
+ /** String matching (STRING|SUBSTRING|GLOB|REGEX).
+ *
+ * Used by e.g. \ref PoolQuery and \ref LookupAttr for queries,
+ * but it can also be used for matching arbitrary strings.
+ *
+ * Matches \c SUBSTRING per default.
+ *
+ * \code
+ * AttrMatcher matches( "foo" );
+ * for_( it, stringlist.begin(), stringlist().end() )
+ * {
+ * if ( matches( *it ) )
+ * cout << *it << " has substring 'foo'" << endl;
+ * }
+ * \endcode
+ *
+ * \todo Enhance the search flags manip interface; Enums and
+ * Flags are missing.
+ */
+ class AttrMatcher : private base::SafeBool<AttrMatcher>
+ {
+ friend std::ostream & operator<<( std::ostream & str, const AttrMatcher & obj );
+
+ public:
+ /** Implementation */
+ class Impl;
+
+ public:
+ /** Default ctor matches everything. */
+ AttrMatcher();
+
+ /** Low level interface. */
+ AttrMatcher( const std::string & search_r, int flags_r = 0 );
+
+#ifndef SWIG // Swig treats it as syntax error
+ /** Evaluate in a boolean context <tt>( ! searchstring().empty() )</tt>. */
+ using base::SafeBool<AttrMatcher>::operator bool_type;
+#endif
+
+ public:
+ /** Return whether string matches.
+ * You can use it with any class that impements \c c_str.
+ * (\c std::string, \ref Pathname, \ref IdString, ...)
+ */
+ template<class _Tp>
+ bool operator()( const _Tp & string_r ) const
+ { return doMatch( string_r.c_str() ); }
+ /** \overload */
+ bool operator()( const char * string_r ) const
+ { return doMatch( string_r ); }
+
+ public:
+ /** The current searchstring. */
+ const std::string & searchstring() const;
+
+ /** Set a new searchstring. */
+ void setSearchstring( const std::string & string_r );
+
+ /** Set a new searchstring and flags. */
+ void setSearchstring( const std::string & string_r, int flags_r );
+
+ /** The current search flags. */
+ int flags() const;
+
+ /** Set new search flags. */
+ void setFlags( int flags_r );
+
+ private:
+ /** Return whether string matches. */
+ bool doMatch( const char * string_r ) const;
+
+ private:
+ friend base::SafeBool<AttrMatcher>::operator bool_type() const;
+ bool boolTest() const
+ { return !searchstring().empty(); }
+
+ private:
+ /** Pointer to implementation */
+ RWCOW_pointer<Impl> _pimpl;
+ };
+ ///////////////////////////////////////////////////////////////////
+
+ /** \relates AttrMatcher Stream output */
+ std::ostream & operator<<( std::ostream & str, const AttrMatcher & obj );
+
+ /////////////////////////////////////////////////////////////////
+ } // namespace sat
+ ///////////////////////////////////////////////////////////////////
+ /////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
+#endif // ZYPP_SAT_ATTRMATCHER_H
--
To unsubscribe, e-mail: zypp-commit+unsubscribe@opensuse.org
For additional commands, e-mail: zypp-commit+help@opensuse.org