1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/sat/Capability.cc
13 #include "zypp/base/Logger.h"
15 #include "zypp/base/String.h"
16 #include "zypp/base/Regex.h"
17 #include "zypp/base/Gettext.h"
18 #include "zypp/base/Exception.h"
21 #include "zypp/Edition.h"
22 #include "zypp/Capability.h"
24 #include "zypp/sat/detail/PoolImpl.h"
25 #include "zypp/sat/Pool.h"
29 ///////////////////////////////////////////////////////////////////
31 { /////////////////////////////////////////////////////////////////
32 ///////////////////////////////////////////////////////////////////
34 { /////////////////////////////////////////////////////////////////
36 sat::detail::IdType relFromStr( ::_Pool * pool_r,
37 const std::string & name_r, Rel op_r, const Edition & ed_r, const ResKind & kind_r )
39 sat::detail::IdType nid( sat::detail::noId );
40 if ( ! kind_r || kind_r == ResKind::package )
42 nid = IdString( name_r ).id();
46 // non-packages prefixed by kind
47 nid = IdString( str::form( "%s:%s",
49 name_r.c_str() ) ).id();
52 if ( op_r != Rel::ANY && ed_r != Edition::noedition )
54 nid = ::rel2id( pool_r, nid, ed_r.id(), op_r.bits(), /*create*/true );
60 sat::detail::IdType relFromStr( ::_Pool * pool_r,
61 const std::string & str_r, const ResKind & kind_r, Capability::CtorFlag flag_r )
63 // strval_r has at least two words which could make 'op edition'?
65 static const str::regex rx( "(.*[^ \t])([ \t]+)([^ \t]+)([ \t]+)([^ \t]+)" );
66 static str::smatch what;
68 std::string name( str_r );
71 if ( flag_r == Capability::UNPARSED
72 && str_r.find(' ') != std::string::npos
73 && str::regex_match( str_r, what, rx ) )
78 Edition ced( what[5] );
83 catch ( Exception & excpt )
85 // So they don't make valid 'op edition'
87 DBG << "Trying named relation for: " << str_r << endl;
91 // not a versioned relation
93 return relFromStr( pool_r, name, op, ed, kind_r );
96 /////////////////////////////////////////////////////////////////
98 ///////////////////////////////////////////////////////////////////
100 const Capability Capability::Null( STRID_NULL );
102 /////////////////////////////////////////////////////////////////
104 Capability::Capability( const char * str_r, const ResKind & prefix_r, CtorFlag flag_r )
105 : _id( relFromStr( myPool().getPool(), str_r, prefix_r, flag_r ) )
108 Capability::Capability( const std::string & str_r, const ResKind & prefix_r, CtorFlag flag_r )
109 : _id( relFromStr( myPool().getPool(), str_r.c_str(), prefix_r, flag_r ) )
112 Capability::Capability( const char * str_r, CtorFlag flag_r, const ResKind & prefix_r )
113 : _id( relFromStr( myPool().getPool(), str_r, prefix_r, flag_r ) )
116 Capability::Capability( const std::string & str_r, CtorFlag flag_r, const ResKind & prefix_r )
117 : _id( relFromStr( myPool().getPool(), str_r, prefix_r, flag_r ) )
121 Capability::Capability( const std::string & name_r, const std::string & op_r, const std::string & ed_r, const ResKind & prefix_r )
122 : _id( relFromStr( myPool().getPool(), name_r, Rel(op_r), Edition(ed_r), prefix_r ) )
125 Capability::Capability( const std::string & name_r, Rel op_r, const std::string & ed_r, const ResKind & prefix_r )
126 : _id( relFromStr( myPool().getPool(), name_r, op_r, Edition(ed_r), prefix_r ) )
129 Capability::Capability( const std::string & name_r, Rel op_r, const Edition & ed_r, const ResKind & prefix_r )
130 : _id( relFromStr( myPool().getPool(), name_r, op_r, ed_r, prefix_r ) )
133 const char * Capability::c_str() const
134 { return( _id ? ::dep2str( myPool().getPool(), _id ) : "" ); }
136 CapMatch Capability::_doMatch( sat::detail::IdType lhs, sat::detail::IdType rhs )
138 #warning MIGRATE TO SAT
141 return CapMatch::yes;
148 case CapDetail::NOCAP:
149 return( r.kind() == CapDetail::NOCAP ); // NOCAP matches NOCAP only
151 case CapDetail::EXPRESSION:
152 return CapMatch::irrelevant;
154 case CapDetail::NAMED:
155 case CapDetail::VERSIONED:
161 case CapDetail::NOCAP:
162 return CapMatch::no; // match case handled above
164 case CapDetail::EXPRESSION:
165 return CapMatch::irrelevant;
167 case CapDetail::NAMED:
168 case CapDetail::VERSIONED:
171 // comparing two simple caps:
172 if ( l.name() != r.name() )
175 // isNamed matches ANY edition:
176 if ( l.isNamed() || r.isNamed() )
177 return CapMatch::yes;
179 // both are versioned:
180 return overlaps( Edition::MatchRange( l.op(), l.ed() ),
181 Edition::MatchRange( r.op(), r.ed() ) );
184 bool Capability::isInterestingFileSpec( const char * name_r )
186 static str::smatch what;
187 static const str::regex filenameRegex(
188 "/(s?bin|lib(64)?|etc)/|^/usr/(games/|share/(dict/words|magic\\.mime)$)|^/opt/gnome/games/",
189 str::regex::optimize|str::regex::nosubs );
191 return str::regex_match( name_r, what, filenameRegex );
194 /******************************************************************
196 ** FUNCTION NAME : operator<<
197 ** FUNCTION TYPE : std::ostream &
199 std::ostream & operator<<( std::ostream & str, const Capability & obj )
201 return str << obj.detail();
204 std::ostream & dumpOn( std::ostream & str, const Capability & obj )
206 return str << obj.detail();
209 ///////////////////////////////////////////////////////////////////
211 // CLASS NAME : CapDetail
213 ///////////////////////////////////////////////////////////////////
215 void CapDetail::_init()
217 // : _kind( NOCAP ), _lhs( id_r ), _rhs( 0 ), _flag( 0 )
219 if ( _lhs == sat::detail::emptyId || _lhs == sat::detail::noId )
222 if ( ! ISRELDEP(_lhs) )
228 ::Reldep * rd = GETRELDEP( myPool().getPool(), _lhs );
233 _kind = Rel::isRel( _flag ) ? VERSIONED : EXPRESSION;
236 /******************************************************************
238 ** FUNCTION NAME : operator<<
239 ** FUNCTION TYPE : std::ostream &
241 std::ostream & operator<<( std::ostream & str, const CapDetail & obj )
243 switch ( obj.kind() )
245 case CapDetail::NOCAP:
246 return str << "<NoCap>";
248 case CapDetail::NAMED:
249 return str << obj.name();
251 case CapDetail::VERSIONED:
252 return str << obj.name() << " " << obj.op() << " " << obj.ed();
254 case CapDetail::EXPRESSION:
255 switch ( obj.capRel() )
257 case CapDetail::REL_NONE:
258 case CapDetail::CAP_AND:
259 case CapDetail::CAP_OR:
260 case CapDetail::CAP_WITH:
261 return str << obj.lhs().detail() << " " << obj.capRel() << " " << obj.rhs().detail();
263 case CapDetail::CAP_NAMESPACE:
264 return str << obj.lhs().detail() << "(" << obj.rhs().detail() << ")";
268 return str << "<UnknownCap>";
271 std::ostream & operator<<( std::ostream & str, CapDetail::Kind obj )
275 case CapDetail::NOCAP: return str << "NoCap"; break;
276 case CapDetail::NAMED: return str << "NamedCap"; break;
277 case CapDetail::VERSIONED: return str << "VersionedCap"; break;
278 case CapDetail::EXPRESSION: return str << "CapExpression"; break;
280 return str << "UnknownCap";
283 std::ostream & operator<<( std::ostream & str, CapDetail::CapRel obj )
287 case CapDetail::REL_NONE: return str << "NoCapRel"; break;
288 case CapDetail::CAP_AND: return str << "AND"; break;
289 case CapDetail::CAP_OR: return str << "OR"; break;
290 case CapDetail::CAP_WITH: return str << "WITH"; break;
291 case CapDetail::CAP_NAMESPACE: return str << "NAMESPACE"; break;
293 return str << "UnknownCapRel";
296 /////////////////////////////////////////////////////////////////
298 ///////////////////////////////////////////////////////////////////