libzypp  11.13.5
Glob.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_GLOB_H
13 #define ZYPP_GLOB_H
14 
15 extern "C"
16 {
17 #include <glob.h>
18 }
19 
20 #include <iosfwd>
21 
22 #include "zypp/base/Easy.h"
23 #include "zypp/base/Flags.h"
24 #include "zypp/base/Iterator.h"
25 #include "zypp/base/NonCopyable.h"
27 
28 #include "zypp/Pathname.h"
29 
31 namespace zypp
32 {
33 
35  namespace filesystem
36  {
37 
39  //
40  // CLASS NAME : Glob
41  //
57  class Glob : private base::NonCopyable
58  {
59  public:
60  typedef size_t size_type;
61  typedef const char * value_type;
62 
64  class const_iterator : public boost::iterator_adaptor<
65  const_iterator // Derived
66  , char ** // Base
67  , value_type // Value
68  , boost::forward_traversal_tag // CategoryOrTraversal
69  , const value_type // Reference
70  >
71  {
72  public:
74  : const_iterator::iterator_adaptor_( 0 )
75  {}
76 
77  explicit const_iterator( char ** _idx )
78  : const_iterator::iterator_adaptor_( _idx && *_idx ? _idx : 0 )
79  {}
80 
81  private:
83  void increment()
84  {
85  if ( base_reference() && !*(++base_reference()) )
86  base_reference() = 0;
87  }
88  reference dereference() const
89  { return( base() ? *base() : 0 ); }
90  };
92 
93  public:
95  enum Bits {
96  _ERR = GLOB_ERR,
97  _MARK = GLOB_MARK,
98  _NOSORT = GLOB_NOSORT,
99  // unsupported _DOOFFS = GLOB_DOOFFS, //!< Insert PGLOB->gl_offs NULLs.
100  _NOCHECK = GLOB_NOCHECK,
101  // autoapplied _APPEND = GLOB_APPEND, //!< Append to results of a previous call.
102  _NOESCAPE = GLOB_NOESCAPE,
103  _PERIOD = GLOB_PERIOD,
104  // unsupported _MAGCHAR = GLOB_MAGCHAR,//!< Set in gl_flags if any metachars seen.
105  _ALTDIRFUNC = GLOB_ALTDIRFUNC,
106  _BRACE = GLOB_BRACE,
107  _NOMAGIC = GLOB_NOMAGIC,
108  _TILDE = GLOB_TILDE,
109  _ONLYDIR = GLOB_ONLYDIR,
110  _TILDE_CHECK = GLOB_TILDE_CHECK,
111  };
112 
114  ZYPP_DECLARE_FLAGS( Flags, Bits );
115 
116  public:
121  Glob( Flags flags_r = Flags() )
122  : _defaultFlags( flags_r )
123  {}
124 
129  explicit Glob( const Pathname & pattern_r, Flags flags_r = Flags() )
130  : _defaultFlags( flags_r )
131  { add( pattern_r, flags_r ); }
133  explicit Glob( const std::string & pattern_r, Flags flags_r = Flags() )
134  : _defaultFlags( flags_r )
135  { add( pattern_r, flags_r ); }
137  explicit Glob( const char * pattern_r, Flags flags_r = Flags() )
138  : _defaultFlags( flags_r )
139  { add( pattern_r, flags_r ); }
140 
143  { if ( _result ) ::globfree( &(*_result) ); }
144 
155  int add( const Pathname & pattern_r, Flags flags_r = Flags() )
156  { return add( pattern_r.c_str(), flags_r ); }
158  int add( const std::string & pattern_r, Flags flags_r = Flags() )
159  { return add( pattern_r.c_str(), flags_r ); }
161  int add( const char * pattern_r, Flags flags_r = Flags() );
162 
164  void clear();
165 
167  void reset( Flags flags_r = Flags() )
168  { clear(); setDefaultFlags( flags_r ); }
169 
170 
171  public:
173  Flags defaultFlags() const
174  { return _defaultFlags; }
175 
177  void setDefaultFlags( Flags flags_r = Flags() )
178  { _defaultFlags = flags_r; }
179 
184  int lastGlobReturn() const
185  { return _lastGlobReturn; }
186 
187  public:
189  bool empty() const
190  { return ! ( _result && _result->gl_pathc ); }
191 
193  size_type size() const
194  { return( _result ? _result->gl_pathc : 0 ); }
195 
198  { return( _result ? const_iterator( _result->gl_pathv ) : const_iterator() ); }
199 
202  { return const_iterator(); }
203 
204  public:
205 
215  template<class _OutputIterator>
216  static int collect( const Pathname & pattern_r, _OutputIterator result_r )
217  { return collect( pattern_r.c_str(), Flags(), result_r ); }
219  template<class _OutputIterator>
220  static int collect( const std::string & pattern_r, _OutputIterator result_r )
221  { return collect( pattern_r.c_str(), Flags(), result_r ); }
223  template<class _OutputIterator>
224  static int collect( const char * pattern_r, _OutputIterator result_r )
225  { return collect( pattern_r, Flags(), result_r ); }
226 
228  template<class _OutputIterator>
229  static int collect( const Pathname & pattern_r, Flags flags_r, _OutputIterator result_r )
230  { return collect( pattern_r.c_str(), flags_r, result_r ); }
232  template<class _OutputIterator>
233  static int collect( const std::string & pattern_r, Flags flags_r, _OutputIterator result_r )
234  { return collect( pattern_r.c_str(), flags_r, result_r ); }
236  template<class _OutputIterator>
237  static int collect( const char * pattern_r, Flags flags_r, _OutputIterator result_r )
238  {
239  Glob glob( pattern_r, flags_r );
240  if ( glob.lastGlobReturn() == 0 )
241  for_( it, glob.begin(), glob.end() )
242  (*result_r)++ = typename _OutputIterator::container_type::value_type(*it);
243  return glob.lastGlobReturn();
244  }
246 
247  private:
249  scoped_ptr< ::glob_t> _result;
251  };
253 
255  std::ostream & operator<<( std::ostream & str, const Glob & obj );
256 
258  inline std::ostream & operator<<( std::ostream & str, const Glob::const_iterator & obj )
259  { return str << *obj; }
260 
261  ZYPP_DECLARE_OPERATORS_FOR_FLAGS( Glob::Flags );
262 
264 
266  } // namespace filesystem
269 } // namespace zypp
271 #endif // ZYPP_GLOB_H