Author: mlandres Date: Wed Nov 19 13:44:10 2008 New Revision: 11715 URL: http://svn.opensuse.org/viewcvs/zypp?rev=11715&view=rev Log: Enhance Glob and add tests Added: trunk/libzypp/tests/zypp/base/Glob_test.cc trunk/libzypp/tests/zypp/base/Glob_test.dat/ trunk/libzypp/tests/zypp/base/Glob_test.dat/file trunk/libzypp/tests/zypp/base/Glob_test.dat/file.xml trunk/libzypp/tests/zypp/base/Glob_test.dat/file.xml.gz Modified: trunk/libzypp/tests/zypp/base/CMakeLists.txt trunk/libzypp/zypp/Glob.cc trunk/libzypp/zypp/Glob.h Modified: trunk/libzypp/tests/zypp/base/CMakeLists.txt URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/tests/zypp/base/CMakeLists.txt?rev=11715&r1=11714&r2=11715&view=diff ============================================================================== --- trunk/libzypp/tests/zypp/base/CMakeLists.txt (original) +++ trunk/libzypp/tests/zypp/base/CMakeLists.txt Wed Nov 19 13:44:10 2008 @@ -1,4 +1,4 @@ - +ADD_TESTS(Glob ) ADD_TESTS(Sysconfig ) ADD_TESTS(String ) ADD_TESTS( InterProcessMutex InterProcessMutex2 ) Added: trunk/libzypp/tests/zypp/base/Glob_test.cc URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/tests/zypp/base/Glob_test.cc?rev=11715&view=auto ============================================================================== --- trunk/libzypp/tests/zypp/base/Glob_test.cc (added) +++ trunk/libzypp/tests/zypp/base/Glob_test.cc Wed Nov 19 13:44:10 2008 @@ -0,0 +1,59 @@ +#include "TestSetup.h" +#include "zypp/Pathname.h" +#include "zypp/Glob.h" + +#define BOOST_TEST_MODULE Glob + +static Pathname TEST_ROOT( TESTS_SRC_DIR"/zypp/base/Glob_test.dat" ); + +using filesystem::Glob; + +BOOST_AUTO_TEST_CASE(Glob_default) +{ + // enable loging for the scope of this block: + // base::LogControl::TmpLineWriter shutUp( new log::FileLineWriter( "-" ) ); + Glob q; + BOOST_CHECK( q.empty() ); + BOOST_CHECK( q.size() == 0 ); + BOOST_CHECK_EQUAL( q.begin(), q.end() ); + BOOST_CHECK( q.defaultFlags() == Glob::Flags() ); + + q.add( TEST_ROOT/"file" ); + BOOST_CHECK( ! q.empty() ); + BOOST_CHECK( q.size() == 1 ); + BOOST_CHECK_NE( q.begin(), q.end() ); + BOOST_CHECK_EQUAL( *q.begin(), TEST_ROOT/"file" ); + + q.reset( Glob::_BRACE ); + BOOST_CHECK( q.empty() ); + BOOST_CHECK( q.size() == 0 ); + BOOST_CHECK_EQUAL( q.begin(), q.end() ); + BOOST_CHECK( q.defaultFlags() == Glob::_BRACE ); + + q.add( TEST_ROOT/"file*" ); + BOOST_CHECK( q.size() == 3 ); + + q.add( TEST_ROOT/"*{.xml,.xml.gz}" ); + BOOST_CHECK( q.size() == 5 ); + + q.clear(); // no flags reset: Glob::_BRACE active + BOOST_CHECK( q.size() == 0 ); + + q.add( TEST_ROOT/"*{.xml,.xml.gz}" ); + BOOST_CHECK( q.size() == 2 ); + + q.reset(); // flags reset: Glob::_BRACE off + BOOST_CHECK( q.size() == 0 ); + + q.add( TEST_ROOT/"*{.xml,.xml.gz}" ); + BOOST_CHECK( q.size() == 0 ); +} + +BOOST_AUTO_TEST_CASE(Glob_static) +{ + std::set<Pathname> q; + Glob::collect( TEST_ROOT/"*{.xml,.xml.gz}", Glob::_BRACE, std::inserter( q, q.begin() ) ); + BOOST_REQUIRE( q.size() == 2 ); + BOOST_CHECK_EQUAL( *q.begin(), TEST_ROOT/"file.xml" ); + BOOST_CHECK_EQUAL( *++q.begin(), TEST_ROOT/"file.xml.gz" ); +} Added: trunk/libzypp/tests/zypp/base/Glob_test.dat/file URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/tests/zypp/base/Glob_test.dat/file?rev=11715&view=auto ============================================================================== (empty) Added: trunk/libzypp/tests/zypp/base/Glob_test.dat/file.xml URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/tests/zypp/base/Glob_test.dat/file.xml?rev=11715&view=auto ============================================================================== (empty) Added: trunk/libzypp/tests/zypp/base/Glob_test.dat/file.xml.gz URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/tests/zypp/base/Glob_test.dat/file.xml.gz?rev=11715&view=auto ============================================================================== (empty) Modified: trunk/libzypp/zypp/Glob.cc URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/zypp/Glob.cc?rev=11715&r1=11714&r2=11715&view=diff ============================================================================== --- trunk/libzypp/zypp/Glob.cc (original) +++ trunk/libzypp/zypp/Glob.cc Wed Nov 19 13:44:10 2008 @@ -23,7 +23,7 @@ namespace filesystem { ///////////////////////////////////////////////////////////////// - int Glob::add( const std::string & pattern_r, Flags flags_r ) + int Glob::add( const char * pattern_r, Flags flags_r ) { static Flags _APPEND( GLOB_APPEND ); // not published if ( ! flags_r ) @@ -32,7 +32,17 @@ flags_r |= _APPEND; else _result.reset( new ::glob_t ); - return( _lastGlobReturn = ::glob( pattern_r.c_str(), flags_r, NULL, &(*_result) ) ); + return( _lastGlobReturn = ::glob( pattern_r, flags_r, NULL, &(*_result) ) ); + } + + void Glob::clear() + { + if ( _result ) + { + ::globfree( &(*_result) ); + _result.reset(); + _lastGlobReturn = 0; + } } /****************************************************************** Modified: trunk/libzypp/zypp/Glob.h URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/zypp/Glob.h?rev=11715&r1=11714&r2=11715&view=diff ============================================================================== --- trunk/libzypp/zypp/Glob.h (original) +++ trunk/libzypp/zypp/Glob.h Wed Nov 19 13:44:10 2008 @@ -25,6 +25,8 @@ #include "zypp/base/NonCopyable.h" #include "zypp/base/DefaultIntegral.h" +#include "zypp/Pathname.h" + /////////////////////////////////////////////////////////////////// namespace zypp { ///////////////////////////////////////////////////////////////// @@ -61,7 +63,7 @@ , char ** // Base , value_type // Value , boost::forward_traversal_tag // CategoryOrTraversal - , const value_type & // Reference + , const value_type // Reference > { public: @@ -80,6 +82,8 @@ if ( base_reference() && !*(++base_reference()) ) base_reference() = 0; } + reference dereference() const + { return( base() ? *base() : 0 ); } }; /////////////////////////////////////////////////////////////////// @@ -109,6 +113,7 @@ public: /** Default ctor optionally taking the default flags. * The flags passed here are the default for \ref add. + * \see \ref setDefaultFlags */ Glob( Flags flags_r = Flags() ) : _defaultFlags( flags_r ) @@ -116,10 +121,19 @@ /** Ctor adding pathnames matching \a pattern_r. * The flags passed here are the default for \ref add. + * \see \ref setDefaultFlags */ + explicit Glob( const Pathname & pattern_r, Flags flags_r = Flags() ) + : _defaultFlags( flags_r ) + { add( pattern_r, flags_r ); } + /** \overload */ explicit Glob( const std::string & pattern_r, Flags flags_r = Flags() ) : _defaultFlags( flags_r ) { add( pattern_r, flags_r ); } + /** \overload */ + explicit Glob( const char * pattern_r, Flags flags_r = Flags() ) + : _defaultFlags( flags_r ) + { add( pattern_r, flags_r ); } /** Dtor */ ~Glob() @@ -127,14 +141,29 @@ /** Add pathnames matching \a pattern_r to the current result. * - * The flags passed here override the global default passed to - * the ctor. GLOB_APPEND is atomatically added to the flags if needed. + * Any flags passed here override the global default passed to + * the ctor. GLOB_APPEND is atomatically added to the flags + * f needed. * * This invalidates all iterators. * \see \ref setDefaultFlags * \return the value returned by ::glob(). */ - int add( const std::string & pattern_r, Flags flags_r = Flags() ); + int add( const Pathname & pattern_r, Flags flags_r = Flags() ) + { return add( pattern_r.c_str(), flags_r ); } + /** \overload */ + int add( const std::string & pattern_r, Flags flags_r = Flags() ) + { return add( pattern_r.c_str(), flags_r ); } + /** \overload */ + int add( const char * pattern_r, Flags flags_r = Flags() ); + + /** Clear all results found so far. \ref defaultFlags remain active. */ + void clear(); + + /** Clear all results and reset \ref defaultFlags. */ + void reset( Flags flags_r = Flags() ) + { clear(); setDefaultFlags( flags_r ); } + public: /** The default flags passed to \c ::glob(). */ @@ -174,13 +203,35 @@ /** \name Collecting Glob results to some _OutputIterator * \code * std::list<Pathname> p; - * Glob::collect( "/bin/m*", std::back_inserter(p) ); + * Glob::collect( "/bin/a*.dat}", std::back_inserter(p) ); + * Glob::collect( "/bin/a*{.xml,.xml.gz}", Glob::_BRACE, std::back_inserter(p) ); * \endcode */ //@{ /** Write glob result to some \c OutputIterator. */ template<class _OutputIterator> + static int collect( const Pathname & pattern_r, _OutputIterator result_r ) + { return collect( pattern_r.c_str(), Flags(), result_r ); } + /** \overload */ + template<class _OutputIterator> + static int collect( const std::string & pattern_r, _OutputIterator result_r ) + { return collect( pattern_r.c_str(), Flags(), result_r ); } + /** \overload */ + template<class _OutputIterator> + static int collect( const char * & pattern_r, _OutputIterator result_r ) + { return collect( pattern_r, Flags(), result_r ); } + + /** \overload With \ref Flags */ + template<class _OutputIterator> + static int collect( const Pathname & pattern_r, Flags flags_r, _OutputIterator result_r ) + { return collect( pattern_r.c_str(), flags_r, result_r ); } + /** \overload */ + template<class _OutputIterator> static int collect( const std::string & pattern_r, Flags flags_r, _OutputIterator result_r ) + { return collect( pattern_r.c_str(), flags_r, result_r ); } + /** \overload */ + template<class _OutputIterator> + static int collect( const char * pattern_r, Flags flags_r, _OutputIterator result_r ) { Glob glob( pattern_r, flags_r ); if ( glob.lastGlobReturn() == 0 ) @@ -188,10 +239,6 @@ (*result_r)++ = *it; return glob.lastGlobReturn(); } - /** \overload */ - template<class _OutputIterator> - static int collect( const std::string & pattern_r, _OutputIterator result_r ) - { return collect( pattern_r, Flags(), result_r ); } //@} private: @@ -204,6 +251,10 @@ /** \relates Glob Stream output */ std::ostream & operator<<( std::ostream & str, const Glob & obj ); + /** \relates Glob::const_iterator Stream output */ + inline std::ostream & operator<<( std::ostream & str, const Glob::const_iterator & obj ) + { return str << *obj; } + ZYPP_DECLARE_OPERATORS_FOR_FLAGS( Glob::Flags ); /////////////////////////////////////////////////////////////////// -- To unsubscribe, e-mail: zypp-commit+unsubscribe@opensuse.org For additional commands, e-mail: zypp-commit+help@opensuse.org