libzypp: zypp/Glob.h Source File

libzypp 17.38.14

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
15extern "C"
16{
17#include <glob.h>
18}
19
20#include <iosfwd>
21
22#include <zypp-core/base/Easy.h>
23#include <zypp-core/base/Flags.h>
24#include <zypp-core/base/Iterator.h>
25#include <zypp-core/base/NonCopyable.h>
26#include <zypp-core/base/DefaultIntegral>
27
28#include <zypp-core/Pathname.h>
29
31namespace zypp
32{
33
35 namespace filesystem
36 {
37
39 //
40 // CLASS NAME : Glob
41 //
57 class ZYPP_API Glob : private base::NonCopyable
58 {
59 public:
60 using size_type = size_t;
61 using value_type = const char *;
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:
73 const_iterator()
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:
82 friend class boost::iterator_core_access;
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 };
91
92
93 public:
95 enum Bits {
96 kErr = GLOB_ERR,
97 kMark = GLOB_MARK,
98 kNoSort = GLOB_NOSORT,
99 // unsupported kDoOffs = GLOB_DOOFFS, //!< Insert PGLOB->gl_offs NULLs.
100 kNoCheck = GLOB_NOCHECK,
101 // autoapplied kAppend = GLOB_APPEND, //!< Append to results of a previous call.
102 kNoEscape = GLOB_NOESCAPE,
103 kPeriod = GLOB_PERIOD,
104 // unsupported kMagChar = GLOB_MAGCHAR,//!< Set in gl_flags if any metachars seen.
105 kAltDirFunc = GLOB_ALTDIRFUNC,
106 kBrace = GLOB_BRACE,
107 kNoMagic = GLOB_NOMAGIC,
108 kTilde = GLOB_TILDE,
109 kOnlyDir = GLOB_ONLYDIR,
110 kTildeCheck = 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 ); }
132
133 explicit Glob( const std::string & pattern_r, Flags flags_r = Flags() )
134 : _defaultFlags( flags_r )
135 { add( pattern_r, flags_r ); }
136
137 explicit Glob( const char * pattern_r, Flags flags_r = Flags() )
138 : _defaultFlags( flags_r )
139 { add( pattern_r, flags_r ); }
140
142 ~Glob()
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 ); }
157
158 int add( const std::string & pattern_r, Flags flags_r = Flags() )
159 { return add( pattern_r.c_str(), flags_r ); }
160
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
197 const_iterator begin() const
198 { return( _result ? const_iterator( _result->gl_pathv ) : const_iterator() ); }
199
201 const_iterator end() const
202 { return const_iterator(); }
203
204 public:
205
214
215 template<class TOutputIterator>
216 static int collect( const Pathname & pattern_r, TOutputIterator result_r )
217 { return collect( pattern_r.c_str(), Flags(), result_r ); }
218
219 template<class TOutputIterator>
220 static int collect( const std::string & pattern_r, TOutputIterator result_r )
221 { return collect( pattern_r.c_str(), Flags(), result_r ); }
222
223 template<class TOutputIterator>
224 static int collect( const char * pattern_r, TOutputIterator result_r )
225 { return collect( pattern_r, Flags(), result_r ); }
226
228 template<class TOutputIterator>
229 static int collect( const Pathname & pattern_r, Flags flags_r, TOutputIterator result_r )
230 { return collect( pattern_r.c_str(), flags_r, result_r ); }
231
232 template<class TOutputIterator>
233 static int collect( const std::string & pattern_r, Flags flags_r, TOutputIterator result_r )
234 { return collect( pattern_r.c_str(), flags_r, result_r ); }
235
236 template<class TOutputIterator>
237 static int collect( const char * pattern_r, Flags flags_r, TOutputIterator 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 TOutputIterator::container_type::value_type(*it);
243 return glob.lastGlobReturn();
244 }
245
246
247 private:
248 Flags _defaultFlags;
249 scoped_ptr< ::glob_t> _result;
250 DefaultIntegral<int,0> _lastGlobReturn;
251 };
252
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
Easy.h
for_
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition Easy.h:27
Flags.h
ZYPP_DECLARE_OPERATORS_FOR_FLAGS
#define ZYPP_DECLARE_OPERATORS_FOR_FLAGS(Name)
relates: Flags
Definition Flags.h:177
Iterator.h
NonCopyable.h
Pathname.h
scoped_ptr
zypp::DefaultIntegral
Integral type with defined initial value when default constructed.
Definition defaultintegral.h:42
zypp::filesystem::Glob::const_iterator
Iterate NULL terminated char* array.
Definition Glob.h:71
zypp::filesystem::Glob::const_iterator::increment
void increment()
Definition Glob.h:83
zypp::filesystem::Glob::const_iterator::const_iterator
const_iterator()
Definition Glob.h:73
zypp::filesystem::Glob::const_iterator::dereference
reference dereference() const
Definition Glob.h:88
zypp::filesystem::Glob::const_iterator::const_iterator
const_iterator(char **_idx)
Definition Glob.h:77
zypp::filesystem::Glob
Find pathnames matching a pattern.
Definition Glob.h:58
zypp::filesystem::Glob::collect
static int collect(const std::string &pattern_r, Flags flags_r, TOutputIterator result_r)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Glob.h:233
zypp::filesystem::Glob::value_type
const char * value_type
Definition Glob.h:61
zypp::filesystem::Glob::empty
bool empty() const
Whether matches were found.
Definition Glob.h:189
zypp::filesystem::Glob::_result
scoped_ptr< ::glob_t > _result
Definition Glob.h:249
zypp::filesystem::Glob::setDefaultFlags
void setDefaultFlags(Flags flags_r=Flags())
Set the default flags passed to glob().
Definition Glob.h:177
zypp::filesystem::Glob::add
int add(const std::string &pattern_r, Flags flags_r=Flags())
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Glob.h:158
zypp::filesystem::Glob::Glob
Glob(Flags flags_r=Flags())
Default ctor optionally taking the default flags.
Definition Glob.h:121
zypp::filesystem::Glob::size
size_type size() const
The number of matches found so far.
Definition Glob.h:193
zypp::filesystem::Glob::collect
static int collect(const std::string &pattern_r, TOutputIterator result_r)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Glob.h:220
zypp::filesystem::Glob::ZYPP_DECLARE_FLAGS
ZYPP_DECLARE_FLAGS(Flags, Bits)
type Flags: Type-safe OR-combination of Bits.
zypp::filesystem::Glob::begin
const_iterator begin() const
Iterator pointing to the first result.
Definition Glob.h:197
zypp::filesystem::Glob::add
int add(const Pathname &pattern_r, Flags flags_r=Flags())
Add pathnames matching pattern_r to the current result.
Definition Glob.h:155
zypp::filesystem::Glob::collect
static int collect(const char *pattern_r, TOutputIterator result_r)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Glob.h:224
zypp::filesystem::Glob::Bits
Bits
Individual bits to combine in Flags.
Definition Glob.h:95
zypp::filesystem::Glob::kOnlyDir
@ kOnlyDir
Match only directories.
Definition Glob.h:109
zypp::filesystem::Glob::kBrace
@ kBrace
Expand "{a,b}" to "a" "b".
Definition Glob.h:106
zypp::filesystem::Glob::kPeriod
@ kPeriod
Leading `.' can be matched by metachars.
Definition Glob.h:103
zypp::filesystem::Glob::kNoCheck
@ kNoCheck
If nothing matches, return the pattern.
Definition Glob.h:100
zypp::filesystem::Glob::kNoEscape
@ kNoEscape
Backslashes don't quote metacharacters.
Definition Glob.h:102
zypp::filesystem::Glob::kNoMagic
@ kNoMagic
If no magic chars, return the pattern.
Definition Glob.h:107
zypp::filesystem::Glob::kTilde
@ kTilde
Expand ~user and ~ to home directories.
Definition Glob.h:108
zypp::filesystem::Glob::kMark
@ kMark
Append a slash to each name.
Definition Glob.h:97
zypp::filesystem::Glob::kNoSort
@ kNoSort
Don't sort the names.
Definition Glob.h:98
zypp::filesystem::Glob::kErr
@ kErr
Return on read errors.
Definition Glob.h:96
zypp::filesystem::Glob::kAltDirFunc
@ kAltDirFunc
Use gl_opendir et al functions.
Definition Glob.h:105
zypp::filesystem::Glob::kTildeCheck
@ kTildeCheck
Like GLOB_TILDE but return an error if the user name is not available.
Definition Glob.h:110
zypp::filesystem::Glob::_lastGlobReturn
DefaultIntegral< int, 0 > _lastGlobReturn
Definition Glob.h:250
zypp::filesystem::Glob::lastGlobReturn
int lastGlobReturn() const
Returns the value returned by the last call to glob().
Definition Glob.h:184
zypp::filesystem::Glob::collect
static int collect(const Pathname &pattern_r, Flags flags_r, TOutputIterator result_r)
Definition Glob.h:229
zypp::filesystem::Glob::_defaultFlags
Flags _defaultFlags
Definition Glob.h:248
zypp::filesystem::Glob::reset
void reset(Flags flags_r=Flags())
Clear all results and reset defaultFlags.
Definition Glob.h:167
zypp::filesystem::Glob::clear
void clear()
Clear all results found so far.
Definition Glob.cc:38
zypp::filesystem::Glob::end
const_iterator end() const
Iterator pointing behind the last result.
Definition Glob.h:201
zypp::filesystem::Glob::collect
static int collect(const char *pattern_r, Flags flags_r, TOutputIterator result_r)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Glob.h:237
zypp::filesystem::Glob::collect
static int collect(const Pathname &pattern_r, TOutputIterator result_r)
Write glob result to some OutputIterator.
Definition Glob.h:216
zypp::filesystem::Glob::size_type
size_t size_type
Definition Glob.h:60
zypp::filesystem::Glob::Glob
Glob(const std::string &pattern_r, Flags flags_r=Flags())
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Glob.h:133
zypp::filesystem::Glob::~Glob
~Glob()
Dtor.
Definition Glob.h:142
zypp::filesystem::Glob::Glob
Glob(const Pathname &pattern_r, Flags flags_r=Flags())
Ctor adding pathnames matching pattern_r.
Definition Glob.h:129
zypp::filesystem::Glob::defaultFlags
Flags defaultFlags() const
The default flags passed to glob().
Definition Glob.h:173
zypp::filesystem::Glob::Glob
Glob(const char *pattern_r, Flags flags_r=Flags())
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Glob.h:137
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:48
zypp::filesystem::Pathname::c_str
const char * c_str() const
String representation.
Definition Pathname.h:113
str
String related utilities and Regular expression matching.
zypp::base
Definition Random.cc:7
zypp::base::NonCopyable
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition NonCopyable.h:26
zypp::filesystem
Types and functions for filesystem operations.
Definition Glob.cc:24
zypp::filesystem::operator<<
std::ostream & operator<<(std::ostream &str, const Glob &obj)
relates: Glob Stream output
Definition Glob.cc:53
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::ZYPP_API
const Arch Arch_empty ZYPP_API
relates: Arch This is an empty Arch represented by an empty string.
Definition Arch.h:173