c3cc223b253df6acea6c0e2018078ac15ba4ddd6
[platform/upstream/libzypp.git] / zypp / PoolQuery.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/PoolQuery.cc
10  *
11 */
12 #include <iostream>
13 #include <sstream>
14 #include <boost/algorithm/string/replace.hpp>
15
16 #include "zypp/base/Gettext.h"
17 #include "zypp/base/Logger.h"
18 #include "zypp/base/Regex.h"
19 #include "zypp/base/Algorithm.h"
20 #include "zypp/repo/RepoException.h"
21
22 #include "zypp/sat/Pool.h"
23 #include "zypp/sat/Solvable.h"
24
25 #include "zypp/PoolQuery.h"
26
27 extern "C"
28 {
29 #include "satsolver/repo.h"
30 }
31
32 using namespace std;
33 using namespace zypp::sat;
34
35 ///////////////////////////////////////////////////////////////////
36 namespace zypp
37 { /////////////////////////////////////////////////////////////////
38
39   ///////////////////////////////////////////////////////////////////
40   //
41   //  CLASS NAME : PoolQuery::Impl
42   //
43   class PoolQuery::Impl
44   {
45   public:
46     Impl()
47       : _flags( SEARCH_ALL_REPOS | SEARCH_NOCASE | SEARCH_SUBSTRING )
48       , _status_flags(ALL)
49       , _match_word(false) 
50       , _require_all(false)
51       , _compiled(false)
52     {}
53
54     ~Impl()
55     {}
56
57   public:
58     const_iterator begin() const;
59     const_iterator end() const;
60     
61     string asString() const;
62     
63     void compile() const;
64   private:
65     string createRegex(const StrContainer & container) const;
66
67   public:
68     /** Raw search strings. */
69     StrContainer _strings;
70     /** Regex-compiled search strings. */
71     mutable string _rcstrings;
72     mutable regex_t _regex;
73     /** Raw attributes */
74     AttrMap _attrs;
75     /** Regex-compiled attributes */
76     mutable CompiledAttrMap _rcattrs;
77     mutable map<sat::SolvAttr, regex_t> _rattrs;
78
79     /** Repos to search. */
80     StrContainer _repos;
81     /** Kinds to search */
82     Kinds _kinds;
83
84     /** Sat solver search flags */
85     int _flags;
86     /** Backup of search flags. compile() may change the flags if needed, so
87      * in order to reuse the query, the original flags need to be stored
88      * at the start of compile() */
89     mutable int _cflags;
90     /** Sat solver status flags */
91     int _status_flags;
92
93     bool _match_word;
94
95     bool _require_all;
96
97     /** Sat solver Dataiterator structure */
98     mutable ::_Dataiterator _rdit;
99
100     mutable bool _compiled;
101
102     /** Function for processing found solvables. Used in execute(). */
103     mutable PoolQuery::ProcessResolvable _fnc;
104     
105   private:
106     friend Impl * rwcowClone<Impl>( const Impl * rhs );
107     /** clone for RWCOW_pointer */
108     Impl * clone() const
109     { return new Impl( *this ); }
110   };
111
112   static void
113   compileRegex(regex_t * regex, const string & str, bool nocase)
114   {
115     /* We feed multiple lines eventually (e.g. authors or descriptions),
116        so set REG_NEWLINE. */
117     if (regcomp(regex, str.c_str(),
118         REG_EXTENDED | REG_NOSUB | REG_NEWLINE | (nocase ? REG_ICASE : 0)) != 0)
119       ZYPP_THROW(Exception(
120         str::form(_("Invalid regular expression '%s'"), str.c_str())));
121   }
122
123   struct MyInserter
124   {
125     MyInserter(PoolQuery::StrContainer & cont) : _cont(cont) {}
126     
127     bool operator()(const string & str)
128     {
129       _cont.insert(str);
130       return true;
131     }
132     
133     PoolQuery::StrContainer & _cont;
134   };
135
136   
137   struct EmptyFilter
138   {
139     bool operator()(const string & str)
140     {
141       return !str.empty();
142     }
143   };
144
145
146   void PoolQuery::Impl::compile() const
147   {
148     _cflags = _flags;
149
150     // 'different'         - will have to iterate through all and match by ourselves (slow)
151     // 'same'              - will pass the compiled string to dataiterator_init
152     // 'one-attr'          - will pass it to dataiterator_init
153     // 'one-non-regex-str' - will pass to dataiterator_init, set flag to SEARCH_STRING or SEARCH_SUBSTRING
154     
155     // // NO ATTRIBUTE
156     // else
157     //   for all _strings
158     //     create regex; store in _rcstrings; if more strings flag regex;
159     if (_attrs.empty())
160     {
161       _rcstrings = createRegex(_strings);
162       if (_strings.size() > 1)
163         _cflags = (_cflags & ~SEARCH_STRINGMASK) | SEARCH_REGEX;//setMatchRegex();
164     }
165
166     // // ONE ATTRIBUTE 
167     // else if _attrs is not empty but it contains just one attr
168     //   for all _strings and _attr[key] strings
169     //     create regex; store in _rcattrs; flag 'one-attr'; if more strings flag regex;
170     else if (_attrs.size() == 1)
171     {
172       StrContainer joined;
173       invokeOnEach(_strings.begin(), _strings.end(), EmptyFilter(), MyInserter(joined));
174       invokeOnEach(_attrs.begin()->second.begin(), _attrs.begin()->second.end(), EmptyFilter(), MyInserter(joined));
175       _rcstrings = createRegex(joined);
176       _rcattrs.insert(pair<sat::SolvAttr, string>(_attrs.begin()->first, string()));
177     }
178
179     // // MULTIPLE ATTRIBUTES
180     else
181     {
182       // check whether there are any per-attribute strings 
183       bool attrvals_empty = true;
184       for (AttrMap::const_iterator ai = _attrs.begin(); ai != _attrs.end(); ++ai)
185         if (!ai->second.empty())
186           for(StrContainer::const_iterator it = ai->second.begin();
187               it != ai->second.end(); it++)
188             if (!it->empty())
189             {
190               attrvals_empty = false;
191               goto attremptycheckend;
192             }
193 attremptycheckend:
194
195       // chceck whether the per-attribute strings are all the same
196       bool attrvals_thesame = true;
197       AttrMap::const_iterator ai = _attrs.begin();
198       const StrContainer & set1 = ai->second;
199       ++ai;
200       for (; ai != _attrs.end(); ++ai)
201       {
202         StrContainer result;
203         set_difference(
204           set1.begin(), set1.end(),
205           ai->second.begin(), ai->second.end(),
206           inserter(result, result.begin())/*, ltstr()*/);
207         if (!result.empty())
208         {
209           attrvals_thesame = false;
210           break;
211         }
212       }
213
214       // // THE SAME STRINGS FOR DIFFERENT ATTRS
215       // else if _attrs is not empty but it does not contain strings
216       //   for each key in _attrs take all _strings
217       //     create regex; store in _rcattrs and _rcstrings; flag 'same'; if more strings flag regex;
218       if (attrvals_empty || attrvals_thesame)
219       {
220         StrContainer joined;
221         if (attrvals_empty)
222         {
223           invokeOnEach(_strings.begin(), _strings.end(), EmptyFilter(), MyInserter(joined));
224           _rcstrings = createRegex(joined);
225         }
226         else
227         {
228           invokeOnEach(_strings.begin(), _strings.end(), EmptyFilter(), MyInserter(joined));
229           invokeOnEach(_attrs.begin()->second.begin(), _attrs.begin()->second.end(), EmptyFilter(), MyInserter(joined));
230           _rcstrings = createRegex(joined);
231         }
232         // copy the _attrs keys to _rcattrs
233         for_(ai, _attrs.begin(), _attrs.end())
234           _rcattrs.insert(pair<sat::SolvAttr, string>(ai->first, string()));
235
236         if ((_cflags & SEARCH_STRINGMASK) == SEARCH_REGEX)
237           compileRegex(&_regex, _rcstrings, _cflags & SEARCH_NOCASE);
238       }
239
240       // // DIFFERENT STRINGS FOR DIFFERENT ATTRS
241       // if _attrs is not empty and it contains non-empty vectors with non-empty strings
242       //   for each key in _attrs take all _strings + all _attrs[key] strings
243       //     create regex; store in _rcattrs; flag 'different'; if more strings flag regex;
244       else
245       {
246         for_(ai, _attrs.begin(), _attrs.end())
247         {
248           StrContainer joined;
249           invokeOnEach(_strings.begin(), _strings.end(), EmptyFilter(), MyInserter(joined));
250           invokeOnEach(ai->second.begin(), ai->second.end(), EmptyFilter(), MyInserter(joined));
251           string s = createRegex(joined);
252           _rcattrs.insert(pair<sat::SolvAttr, string>(ai->first, s));
253
254           if ((_cflags & SEARCH_STRINGMASK) == SEARCH_REGEX)
255           {
256             regex_t regex;
257             compileRegex(&regex, s, _cflags & SEARCH_NOCASE);
258             _rattrs.insert(pair<sat::SolvAttr, regex_t>(ai->first, regex));
259           }
260         }
261       }
262     }
263
264     // tell the Dataiterator to search only in one repo if only one specified
265     if (_repos.size() == 1)
266       _cflags &= ~SEARCH_ALL_REPOS;
267
268     _compiled = true;
269     
270     DBG << asString() << endl;
271   }
272
273   /**
274    * Converts '*' and '?' wildcards within str into their regex equivalents.
275    */
276   static string wildcards2regex(const string & str)
277   {
278     string regexed = str;
279
280     str::regex all("\\*"); // regex to search for '*'
281     str::regex one("\\?"); // regex to search for '?'
282     string r_all(".*"); // regex equivalent of '*'
283     string r_one(".");  // regex equivalent of '?'
284     string::size_type pos;
285
286     // replace all "*" in input with ".*"
287     for (pos = 0; (pos = regexed.find("*", pos)) != std::string::npos; pos+=2)
288       regexed = regexed.replace(pos, 1, r_all);
289
290     // replace all "?" in input with "."
291     for (pos = 0; (pos = regexed.find('?', pos)) != std::string::npos; ++pos)
292       regexed = regexed.replace(pos, 1, r_one);
293
294     DBG << " -> " << regexed << endl;
295
296     return regexed;
297   }
298
299 //! macro for word boundary tags for regexes
300 #define WB (_match_word ? string("\\b") : string())
301
302   string PoolQuery::Impl::createRegex(const StrContainer & container) const
303   {
304     string rstr;
305
306     if (container.empty())
307       return rstr;
308
309     if (container.size() == 1)
310     {
311       if (_match_word)
312         return ".*" + WB + *container.begin() + WB + ".*";
313
314       return *container.begin();
315     }
316
317     // multiple strings
318
319     bool use_wildcards = (_cflags & SEARCH_STRINGMASK) == SEARCH_GLOB;
320     StrContainer::const_iterator it = container.begin();
321     string tmp;
322
323     if (use_wildcards)
324       tmp = wildcards2regex(*it);
325
326     if (_require_all)
327     {
328       if (!(_cflags & SEARCH_STRING)) // not match exact
329         tmp += ".*" + WB + tmp;
330       rstr = "(?=" + tmp + ")";
331     }
332     else
333     {
334       if (_cflags & SEARCH_STRING) // match exact
335         rstr = "^";
336       else
337         rstr = ".*" + WB;
338
339       rstr += "(" + tmp;
340     }
341
342     ++it;
343
344     for (; it != container.end(); ++it)
345     {
346       if (use_wildcards)
347         tmp = wildcards2regex(*it);
348
349       if (_require_all)
350       {
351         if (!(_cflags & SEARCH_STRING)) // not match exact
352           tmp += ".*" + WB + tmp;
353         rstr += "(?=" + tmp + ")";
354       }
355       else
356       {
357         rstr += "|" + tmp;
358       }
359     }
360
361     if (_require_all)
362     {
363       if (!(_cflags & SEARCH_STRING)) // not match exact
364         rstr += WB + ".*";
365     }
366     else
367     {
368       rstr += ")";
369       if (_cflags & SEARCH_STRING) // match exact
370         rstr += "$";
371       else
372         rstr += WB + ".*";
373     }
374
375     return rstr;
376   }
377
378
379   PoolQuery::const_iterator PoolQuery::Impl::begin() const
380   {
381     compile();
382
383     // if only one repository has been specified, find it in the pool
384     sat::Pool pool(sat::Pool::instance());
385     sat::Pool::RepositoryIterator itr = pool.reposBegin();
386     if (!(_cflags & SEARCH_ALL_REPOS) && _repos.size() == 1)
387     {
388       string theone = *_repos.begin();
389       for (; itr->info().alias() != theone && itr != pool.reposEnd(); ++itr);
390       if (itr == pool.reposEnd())
391       {
392         RepoInfo info; info.setAlias(theone);
393         ERR << "Repository not found in sat pool." <<  endl;
394         ZYPP_THROW(repo::RepoNotFoundException(info));
395       }
396     }
397
398     DBG << "_cflags:" << _cflags << endl;
399
400     if (_rcattrs.empty())
401     {
402     ::dataiterator_init(&_rdit,
403       _cflags & SEARCH_ALL_REPOS ? pool.get()->repos[0] : itr->get(), // repository \todo fix this
404       0,                                           // search all solvables
405       0,                                           // attribute id - only if 1 attr key specified
406       _rcstrings.empty() ? 0 : _rcstrings.c_str(), // compiled search string
407       _cflags);
408     }
409     else if (_rcattrs.size() == 1)
410     {
411       ::dataiterator_init(&_rdit,
412         _cflags & SEARCH_ALL_REPOS ? pool.get()->repos[0] : itr->get(), // repository \todo fix this 
413         0,                                           // search all solvables
414         _rcattrs.begin()->first.id(),                // keyname - attribute id - only if 1 attr key specified
415         _rcstrings.empty() ? 0 : _rcstrings.c_str(), // compiled search string 
416         _cflags);
417     }
418     else
419     {
420       ::dataiterator_init(&_rdit,
421         _cflags & SEARCH_ALL_REPOS ? pool.get()->repos[0] : itr->get(), /* repository - switch to next at the end of current one in increment() */ 
422         0, /*search all resolvables */
423         0, /*keyname - if only 1 attr key specified, pass it here, otherwise do more magic */
424         0, //qs.empty() ? 0 : qs.c_str(), /* create regex, pass it here */
425         _cflags);
426     }
427
428     if ((_cflags & SEARCH_STRINGMASK) == SEARCH_REGEX && _rdit.regex_err != 0)
429       ZYPP_THROW(Exception(
430         str::form(_("Invalid regular expression '%s'"), _rcstrings.c_str())));
431
432     PoolQuery::const_iterator it(this);
433     it.increment();
434     return it;
435   }
436
437   PoolQuery::const_iterator PoolQuery::Impl::end() const
438   {
439     INT << "end" << endl;
440     return PoolQuery::const_iterator();
441   }
442
443
444   string PoolQuery::Impl::asString() const
445   {
446     ostringstream o;
447
448     o << "compiled: " << _compiled << endl;
449     
450     o << "kinds: ";
451     for(Kinds::const_iterator it = _kinds.begin();
452         it != _kinds.end(); ++it)
453       o << *it << " ";
454     o << endl;
455
456     o << "repos: ";
457     for(StrContainer::const_iterator it = _repos.begin();
458         it != _repos.end(); ++it)
459       o << *it << " ";
460     o << endl;
461
462     o << "string match flags:" << endl;
463     o << "* string/substring/glob/regex: " << (_cflags & SEARCH_STRINGMASK) << endl; 
464     o << "* SEARCH_NOCASE: " << ((_cflags & SEARCH_NOCASE) ? "yes" : "no") << endl;
465     o << "* SEARCH_ALL_REPOS: " << ((_cflags & SEARCH_ALL_REPOS) ? "yes" : "no") << endl;
466     o << "status filter flags:" << _status_flags << endl;
467
468     // raw
469
470     o << "strings: ";
471     for(StrContainer::const_iterator it = _strings.begin();
472         it != _strings.end(); ++it)
473       o << *it << " ";
474     o << endl;
475
476     o << "attributes: " << endl;
477     for(AttrMap::const_iterator ai = _attrs.begin(); ai != _attrs.end(); ++ai)
478     {
479       o << "* " << ai->first << ": ";
480       for(StrContainer::const_iterator vi = ai->second.begin();
481           vi != ai->second.end(); ++vi)
482         o << *vi << " ";
483       o << endl;
484     }
485
486     // compiled
487
488     o << "compiled strings: " << _rcstrings << endl;
489     o << "compiled attributes:" << endl;
490     for (CompiledAttrMap::const_iterator ai = _rcattrs.begin(); ai != _rcattrs.end(); ++ai)
491       o << "* " << ai->first << ": " << ai->second << endl;
492
493     return o.str();
494   }
495
496   /** \relates PoolQuery::Impl Stream output *//*
497   inline std::ostream & operator<<( std::ostream & str, const PoolQuery::Impl & obj )
498   {
499     return str << "PoolQuery::Impl";
500   }
501   */
502   ///////////////////////////////////////////////////////////////////
503
504   ///////////////////////////////////////////////////////////////////
505   namespace detail
506   { /////////////////////////////////////////////////////////////////
507
508   ///////////////////////////////////////////////////////////////////
509   //
510   //  CLASS NAME : PoolQuery::ResultIterator
511   //
512   ///////////////////////////////////////////////////////////////////
513
514   PoolQueryIterator::PoolQueryIterator(const PoolQuery::Impl * pqimpl)
515   : PoolQueryIterator::iterator_adaptor_(pqimpl ? &pqimpl->_rdit : 0)
516   , _rdit(pqimpl ? &pqimpl->_rdit : 0)
517   , _pqimpl(pqimpl)
518   , _sid(0)
519   , _has_next(true)
520   , _attrs(pqimpl->_rcattrs)
521   , _do_matching(false)
522   , _pool((sat::Pool::instance()))
523   {
524     if (_attrs.size() > 1)
525       _do_matching = true;
526   }
527
528   void PoolQueryIterator::increment()
529   {
530     if (!_rdit)
531       return;
532
533     bool got_match = false;
534     if (_has_next)
535     {
536       DBG << "last: " << _sid << endl;
537       while (_has_next && !(got_match = matchSolvable()));
538     }
539
540     // no more solvables and the last did not match
541     if (!got_match && !_has_next)
542     {
543       base_reference() = 0;
544       _sid = 0;
545     }
546
547     DBG << "next: " << _sid << endl;
548   }
549
550   bool PoolQueryIterator::matchSolvable()
551   {
552     _sid = _rdit->solvid;
553
554     bool new_solvable = true;
555     bool matches = !_do_matching;
556     bool in_repo;
557     bool drop_by_kind_status = false;
558     bool drop_by_repo = false;
559     do
560     {
561       //! \todo FIXME Dataiterator returning resolvables belonging to current repo?
562       in_repo = _sid >= _rdit->repo->start;
563
564       if (in_repo && new_solvable)
565       {
566         while(1)
567         {
568           drop_by_repo = false;
569           if (!_pqimpl->_repos.empty() && 
570             _pqimpl->_repos.find(_rdit->repo->name) == _pqimpl->_repos.end())
571           {
572             drop_by_repo = true;
573             break;
574           }
575
576           drop_by_kind_status = false;
577
578           // whether to drop an uninstalled (repo) solvable
579           if ( (_pqimpl->_status_flags & PoolQuery::INSTALLED_ONLY) &&
580                _rdit->repo->name != _pool.systemRepoName() )
581           {
582             drop_by_kind_status = true;
583             break;
584           }
585
586           // whether to drop an installed (target) solvable
587           if ((_pqimpl->_status_flags & PoolQuery::UNINSTALLED_ONLY) &&
588                _rdit->repo->name == _pool.systemRepoName())
589           {
590             drop_by_kind_status = true;
591             break;
592           }
593
594           // whether to drop unwanted kind
595           if (!_pqimpl->_kinds.empty())
596           {
597             sat::Solvable s(_sid);
598             // the user wants to filter by kind.
599             if (_pqimpl->_kinds.find(s.kind()) == _pqimpl->_kinds.end())
600               drop_by_kind_status = true;
601           }
602
603           break;
604         }
605
606         matches = matches && !drop_by_kind_status && !drop_by_repo;
607       }
608
609       if (_do_matching && !drop_by_kind_status)
610       {
611         if (!matches && in_repo)
612         {
613           SolvAttr attr(_rdit->key->name);
614           PoolQuery::CompiledAttrMap::const_iterator ai = _attrs.find(attr);
615           if (ai != _attrs.end())
616           {
617             if ((_pqimpl->_cflags & SEARCH_STRINGMASK) == SEARCH_REGEX)
618             {
619               const regex_t * regex;
620               if (_pqimpl->_rcstrings.empty())
621               {
622                 map<sat::SolvAttr, regex_t>::const_iterator rai = _pqimpl->_rattrs.find(attr);
623                 if (rai != _pqimpl->_rattrs.end())
624                   regex = &rai->second;
625                 else
626                 {
627                   ERR << "no compiled regex found for " <<  attr << endl;
628                   continue;
629                 }
630               }
631               else
632                 regex = &_pqimpl->_regex;
633               matches = ::dataiterator_match(_rdit, _pqimpl->_cflags, regex);
634             }
635             else
636             {
637               const string & sstr =
638                 _pqimpl->_rcstrings.empty() ? ai->second : _pqimpl->_rcstrings;
639               matches = ::dataiterator_match(_rdit, _pqimpl->_cflags, sstr.c_str());
640             }
641
642 //            if (matches)
643               /* After calling dataiterator_match (with any string matcher set)
644                  the kv.str member will be filled with something sensible.  */
645   /*            INT << "value: " << _rdit->kv.str << endl
646                   << " mstr: " <<  sstr << endl;*/ 
647           }
648         }
649       }
650
651       if (drop_by_repo)
652       {
653         Repository nextRepo(Repository(_rdit->repo).nextInPool());
654         ::dataiterator_skip_repo(_rdit);
655         if (nextRepo)
656           ::dataiterator_jump_to_repo(_rdit, nextRepo.get());
657         drop_by_repo = false;
658       }
659       else if (drop_by_kind_status)
660       {
661         ::dataiterator_skip_solvable(_rdit);
662         drop_by_kind_status = false;
663       }
664
665       if ((_has_next = ::dataiterator_step(_rdit)))
666       {
667         new_solvable = _rdit->solvid != _sid;
668         if (!in_repo)
669           _sid = _rdit->solvid;
670       }
671       // no more attributes in this repo, return
672       else
673       {
674         // check for more repos to jump to
675         if (!_pqimpl->_repos.empty())
676         {
677           Repository nextRepo(Repository(_rdit->repo).nextInPool());
678           if (nextRepo)
679           {
680             ::dataiterator_jump_to_repo(_rdit, nextRepo.get());
681             _has_next = ::dataiterator_step(_rdit);
682           }
683         }
684
685         // did the last solvable match conditions?
686         return matches && in_repo;
687       }
688     }
689     while (!new_solvable || !in_repo);
690
691     return matches;
692   }
693
694   ///////////////////////////////////////////////////////////////////
695   } //namespace detail
696   ///////////////////////////////////////////////////////////////////
697
698   ///////////////////////////////////////////////////////////////////
699   //
700   //    CLASS NAME : PoolQuery
701   //
702   ///////////////////////////////////////////////////////////////////
703
704   PoolQuery::PoolQuery()
705     : _pimpl(new Impl())
706   {}
707
708
709   PoolQuery::~PoolQuery()
710   {}
711
712
713   void PoolQuery::addRepo(const std::string &repoalias)
714   {
715     _pimpl->_repos.insert(repoalias);
716     _pimpl->_flags &= ~SEARCH_ALL_REPOS;
717   }
718
719
720   void PoolQuery::addKind(const Resolvable::Kind &kind)
721   { _pimpl->_kinds.insert(kind); }
722
723
724   void PoolQuery::addString(const string & value)
725   { _pimpl->_strings.insert(value); }
726
727
728   void PoolQuery::addAttribute(const sat::SolvAttr & attr, const std::string & value)
729   { _pimpl->_attrs[attr].insert(value); }
730
731
732   void PoolQuery::setCaseSensitive(const bool value)
733   {
734     if (value)
735       _pimpl->_flags &= ~SEARCH_NOCASE;
736     else
737       _pimpl->_flags |= SEARCH_NOCASE;
738   }
739
740
741   void PoolQuery::setMatchSubstring()
742   { _pimpl->_flags = (_pimpl->_flags & ~SEARCH_STRINGMASK) | SEARCH_SUBSTRING; }
743   void PoolQuery::setMatchExact()
744   { _pimpl->_flags = (_pimpl->_flags & ~SEARCH_STRINGMASK) | SEARCH_STRING; }
745   void PoolQuery::setMatchRegex()
746   { _pimpl->_flags = (_pimpl->_flags & ~SEARCH_STRINGMASK) | SEARCH_REGEX; }
747   void PoolQuery::setMatchGlob()
748   { _pimpl->_flags = (_pimpl->_flags & ~SEARCH_STRINGMASK) | SEARCH_GLOB; }
749   void PoolQuery::setMatchWord()
750   {
751     _pimpl->_match_word = true;
752     _pimpl->_flags = (_pimpl->_flags & ~SEARCH_STRINGMASK) | SEARCH_REGEX;
753   }
754
755   void PoolQuery::setFlags(int flags)
756   { _pimpl->_flags = flags; }
757
758
759   void PoolQuery::setInstalledOnly()
760   { _pimpl->_status_flags = INSTALLED_ONLY; }
761   void PoolQuery::setUninstalledOnly()
762   { _pimpl->_status_flags = UNINSTALLED_ONLY; }
763   void PoolQuery::setStatusFilterFlags( int flags )
764   { _pimpl->_status_flags = flags; }
765
766
767   void PoolQuery::setRequireAll(const bool require_all)
768   { _pimpl->_require_all = require_all; }
769
770
771   const PoolQuery::StrContainer &
772   PoolQuery::strings() const
773   { return _pimpl->_strings; }
774
775   const PoolQuery::AttrMap &
776   PoolQuery::attributes() const
777   { return _pimpl->_attrs; }
778
779   const PoolQuery::Kinds &
780   PoolQuery::kinds() const
781   { return _pimpl->_kinds; }
782
783   const PoolQuery::StrContainer &
784   PoolQuery::repos() const
785   { return _pimpl->_repos; }
786
787   bool PoolQuery::caseSensitive() const
788   { return _pimpl->_flags & SEARCH_NOCASE; }
789
790   bool PoolQuery::matchExact() const
791   { return (_pimpl->_flags & SEARCH_STRINGMASK) == SEARCH_STRING; }
792   bool PoolQuery::matchSubstring() const
793   { return (_pimpl->_flags & SEARCH_STRINGMASK) == SEARCH_SUBSTRING; }
794   bool PoolQuery::matchGlob() const
795   { return (_pimpl->_flags & SEARCH_STRINGMASK) == SEARCH_GLOB; }
796   bool PoolQuery::matchRegex() const
797   { return (_pimpl->_flags & SEARCH_STRINGMASK) == SEARCH_REGEX; }
798   int PoolQuery::matchType() const
799   { return _pimpl->_flags & SEARCH_STRINGMASK; }
800
801   bool PoolQuery::matchWord() const
802   { return _pimpl->_match_word; }
803
804   bool PoolQuery::requireAll() const
805   { return _pimpl->_require_all; }
806
807
808
809   PoolQuery::const_iterator PoolQuery::begin() const
810   { return _pimpl->begin(); }
811
812
813   PoolQuery::const_iterator PoolQuery::end() const
814   { return _pimpl->end(); }
815
816
817   bool PoolQuery::empty()
818   { return _pimpl->begin() == _pimpl->end(); }
819
820   //! \todo collect the result, reuse if not dirty
821   PoolQuery::size_type PoolQuery::size()
822   {
823     size_type count = 0;
824     for(const_iterator it = _pimpl->begin(); it != _pimpl->end(); ++it, ++count);
825     return count;
826   }
827
828
829   void PoolQuery::execute(ProcessResolvable fnc)
830   { invokeOnEach(_pimpl->begin(), _pimpl->end(), fnc); }
831
832
833   ///////////////////////////////////////////////////////////////////
834   //
835   //  CLASS NAME : PoolQuery::Attr
836   //
837   /**
838    * represents all atributes in PoolQuery except SolvAtributes, which are
839    * used as is (not needed extend anything if someone adds new solv attr)
840    */
841   struct PoolQueryAttr : public IdStringType<PoolQueryAttr>
842   {
843     private:
844       friend class IdStringType<PoolQueryAttr>;
845       IdString _str;
846
847     public:
848     
849     //noAttr
850     PoolQueryAttr():isSolvAttr(false){}
851
852     explicit PoolQueryAttr( const char* cstr_r )
853         : _str( cstr_r ),isSolvAttr(false){}
854
855     explicit PoolQueryAttr( const std::string & str_r )
856         : _str( str_r ),isSolvAttr(false)
857     {
858       if( _str==noAttr ){
859         sat::SolvAttr sa(str_r);
860         if( sa != sat::SolvAttr::noAttr )
861         {
862           isSolvAttr = true; 
863         }
864       }
865     }
866
867     //unknown atributes
868     static const PoolQueryAttr noAttr;
869
870     // own attributes
871     static const PoolQueryAttr repoAttr;
872     static const PoolQueryAttr kindAttr;
873
874     // exported attributes from SolvAtributes
875     bool isSolvAttr;
876   };
877
878   const PoolQueryAttr PoolQueryAttr::noAttr;
879
880   const PoolQueryAttr PoolQueryAttr::repoAttr( "repo" );
881   const PoolQueryAttr PoolQueryAttr::kindAttr( "kind" );
882
883   ///////////////////////////////////////////////////////////////////
884
885
886   //\TODO maybe ctor with stream can be usefull
887   bool PoolQuery::recover( istream &str, char delim )
888   {
889     bool finded_something = false; //indicates some atributes is finded
890     string s;
891     do {
892       if ( str.eof() )
893         break;
894
895       getline( str, s, delim );
896
897       if ((!s.empty()) && s[0]=='#') //comment
898       {
899         continue;
900       }
901
902       string::size_type pos = s.find(':');
903       if (s.empty() || pos == s.npos) // some garbage on line... act like blank line
904       {
905         if (finded_something) //is first blank line after record?
906         {
907           break;
908         }
909         else
910         {
911           continue;
912         }
913       }
914
915       finded_something = true;
916
917       string atrName(str::trim(string(s,0,pos))); // trimmed name of atribute
918       string atrValue(str::trim(string(s,pos+1,s.npos))); //trimmed value
919
920       PoolQueryAttr attribute( atrName );
921
922       if ( attribute==PoolQueryAttr::repoAttr )
923       {
924         addRepo( atrValue );
925       }
926       else if ( attribute==PoolQueryAttr::kindAttr )
927       {
928         addKind( Resolvable::Kind(atrValue) );
929       }
930       else if ( attribute==PoolQueryAttr::noAttr )
931       {
932         if (attribute.isSolvAttr)
933         {
934           //setAtribute
935         }
936         else
937         {
938           //log unknwon atribute
939         }
940       }
941       else
942       {
943         //some forget handle new atribute
944         ;
945       }
946       
947     } while ( true );
948
949     return finded_something;
950   }
951
952   void PoolQuery::serialize( ostream &str, char delim ) const
953   {
954     //separating delim
955     str << delim; 
956     //iterate thrue all settings and write it
957     
958     for_( it, _pimpl->_repos.begin(), _pimpl->_repos.end() )
959     {
960       str << "repo: " << *it << delim ;
961     }
962
963     for_( it, _pimpl->_kinds.begin(), _pimpl->_kinds.end() )
964     {
965       str << "kind: " << it->idStr() << delim ;
966     }
967
968     //separating delim - protection
969     str << delim; 
970
971   }
972
973
974   string PoolQuery::asString() const
975   { return _pimpl->asString(); }
976
977
978   ostream & operator<<( ostream & str, const PoolQuery & obj )
979   { return str << obj.asString(); }
980
981   //internal matching two containers O(n^2)
982   template <class Container>
983   bool equalContainers(const Container& a, const Container& b)
984   {
985     if (a.size()!=b.size())
986       return false;
987
988     for_(it,a.begin(),a.end())
989     {
990       bool finded = false;
991       for_( it2, b.begin(),b.end() )
992       {
993         if (*it==*it2)
994         {
995           finded = true;
996           break;
997         }
998       }
999
1000       if (!finded)
1001         return false;
1002     }
1003     return true;
1004   }
1005
1006   bool PoolQuery::operator==(const PoolQuery& a) const
1007   {
1008     if (!_pimpl->_compiled)
1009       _pimpl->compile();
1010     if (!a._pimpl->_compiled)
1011       a._pimpl->compile();
1012     if( matchType()!=a.matchType() )
1013       return false;
1014     if( a.matchWord()!=matchWord())
1015       return false;
1016     if( a.requireAll()!=requireAll() )
1017       return false;
1018     if(!equalContainers(a.kinds(), kinds()))
1019       return false;
1020     if(!equalContainers(a.repos(), repos()))
1021       return false;
1022     if(a._pimpl->_rcstrings!=_pimpl->_rcstrings)
1023       return false;
1024     if(!equalContainers(a._pimpl->_rcattrs, _pimpl->_rcattrs))
1025       return false;
1026     if(a._pimpl->_cflags!= _pimpl->_cflags)
1027       return false;
1028
1029     return true;
1030   }
1031
1032
1033   /////////////////////////////////////////////////////////////////
1034 } // namespace zypp
1035 ///////////////////////////////////////////////////////////////////
1036