private-search.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Includes /*{{{*/
  2. #include <config.h>
  3. #include <apt-pkg/cachefile.h>
  4. #include <apt-pkg/cacheset.h>
  5. #include <apt-pkg/cmndline.h>
  6. #include <apt-pkg/pkgrecords.h>
  7. #include <apt-pkg/policy.h>
  8. #include <apt-pkg/progress.h>
  9. #include <apt-pkg/cacheiterators.h>
  10. #include <apt-pkg/configuration.h>
  11. #include <apt-pkg/depcache.h>
  12. #include <apt-pkg/macros.h>
  13. #include <apt-pkg/pkgcache.h>
  14. #include <apt-private/private-cacheset.h>
  15. #include <apt-private/private-output.h>
  16. #include <apt-private/private-search.h>
  17. #include <string.h>
  18. #include <iostream>
  19. #include <sstream>
  20. #include <map>
  21. #include <string>
  22. #include <utility>
  23. #include <apti18n.h>
  24. /*}}}*/
  25. bool FullTextSearch(CommandLine &CmdL) /*{{{*/
  26. {
  27. pkgCacheFile CacheFile;
  28. pkgCache *Cache = CacheFile.GetPkgCache();
  29. pkgDepCache::Policy *Plcy = CacheFile.GetPolicy();
  30. pkgRecords records(CacheFile);
  31. if (unlikely(Cache == NULL || Plcy == NULL))
  32. return false;
  33. const char **patterns;
  34. patterns = CmdL.FileList + 1;
  35. std::map<std::string, std::string> output_map;
  36. std::map<std::string, std::string>::const_iterator K;
  37. LocalitySortedVersionSet bag;
  38. OpTextProgress progress(*_config);
  39. progress.OverallProgress(0, 100, 50, _("Sorting"));
  40. GetLocalitySortedVersionSet(CacheFile, bag, progress);
  41. LocalitySortedVersionSet::iterator V = bag.begin();
  42. progress.OverallProgress(50, 100, 50, _("Full Text Search"));
  43. progress.SubProgress(bag.size());
  44. int Done = 0;
  45. for ( ;V != bag.end(); ++V)
  46. {
  47. if (Done%500 == 0)
  48. progress.Progress(Done);
  49. ++Done;
  50. int i;
  51. pkgCache::DescIterator Desc = V.TranslatedDescription();
  52. pkgRecords::Parser &parser = records.Lookup(Desc.FileList());
  53. bool all_found = true;
  54. for(i=0; patterns[i] != NULL; ++i)
  55. {
  56. // FIXME: use regexp instead of simple find()
  57. const char *pattern = patterns[i];
  58. all_found &= (
  59. strstr(V.ParentPkg().Name(), pattern) != NULL ||
  60. strcasestr(parser.ShortDesc().c_str(), pattern) != NULL ||
  61. strcasestr(parser.LongDesc().c_str(), pattern) != NULL);
  62. // search patterns are AND by default so we can skip looking further
  63. // on the first mismatch
  64. if(all_found == false)
  65. break;
  66. }
  67. if (all_found)
  68. {
  69. std::stringstream outs;
  70. ListSingleVersion(CacheFile, records, V, outs);
  71. output_map.insert(std::make_pair<std::string, std::string>(
  72. V.ParentPkg().Name(), outs.str()));
  73. }
  74. }
  75. progress.Done();
  76. // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status)
  77. // output the sorted map
  78. for (K = output_map.begin(); K != output_map.end(); ++K)
  79. std::cout << (*K).second << std::endl;
  80. return true;
  81. }
  82. /*}}}*/