private-search.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include <apt-pkg/error.h>
  2. #include <apt-pkg/cachefile.h>
  3. #include <apt-pkg/cachefilter.h>
  4. #include <apt-pkg/cacheset.h>
  5. #include <apt-pkg/init.h>
  6. #include <apt-pkg/progress.h>
  7. #include <apt-pkg/sourcelist.h>
  8. #include <apt-pkg/cmndline.h>
  9. #include <apt-pkg/strutl.h>
  10. #include <apt-pkg/fileutl.h>
  11. #include <apt-pkg/pkgrecords.h>
  12. #include <apt-pkg/srcrecords.h>
  13. #include <apt-pkg/version.h>
  14. #include <apt-pkg/policy.h>
  15. #include <apt-pkg/tagfile.h>
  16. #include <apt-pkg/algorithms.h>
  17. #include <apt-pkg/sptr.h>
  18. #include <apt-pkg/pkgsystem.h>
  19. #include <apt-pkg/indexfile.h>
  20. #include <apt-pkg/metaindex.h>
  21. #include <sstream>
  22. #include <utility>
  23. #include <cassert>
  24. #include <locale.h>
  25. #include <iostream>
  26. #include <unistd.h>
  27. #include <errno.h>
  28. #include <regex.h>
  29. #include <stdio.h>
  30. #include <iomanip>
  31. #include <algorithm>
  32. #include <map>
  33. #include "private-search.h"
  34. #include "private-cacheset.h"
  35. bool FullTextSearch(CommandLine &CmdL)
  36. {
  37. pkgCacheFile CacheFile;
  38. pkgCache *Cache = CacheFile.GetPkgCache();
  39. pkgDepCache::Policy *Plcy = CacheFile.GetPolicy();
  40. pkgRecords records(CacheFile);
  41. if (unlikely(Cache == NULL || Plcy == NULL))
  42. return false;
  43. const char **patterns;
  44. patterns = CmdL.FileList + 1;
  45. std::map<std::string, std::string> output_map;
  46. std::map<std::string, std::string>::const_iterator K;
  47. LocalitySortedVersionSet bag;
  48. OpTextProgress progress;
  49. progress.OverallProgress(0, 100, 50, _("Sorting"));
  50. GetLocalitySortedVersionSet(CacheFile, bag, progress);
  51. LocalitySortedVersionSet::iterator V = bag.begin();
  52. progress.OverallProgress(50, 100, 50, _("Full Text Search"));
  53. progress.SubProgress(bag.size());
  54. int Done = 0;
  55. for ( ;V != bag.end(); V++)
  56. {
  57. if (Done%500 == 0)
  58. progress.Progress(Done);
  59. Done++;
  60. int i;
  61. pkgCache::DescIterator Desc = V.TranslatedDescription();
  62. pkgRecords::Parser &parser = records.Lookup(Desc.FileList());
  63. bool all_found = true;
  64. for(i=0; patterns[i] != NULL; i++)
  65. {
  66. // FIXME: use regexp instead of simple find()
  67. const char *pattern = patterns[i];
  68. all_found &= (
  69. strstr(V.ParentPkg().Name(), pattern) != NULL ||
  70. parser.ShortDesc().find(pattern) != std::string::npos ||
  71. parser.LongDesc().find(pattern) != std::string::npos);
  72. }
  73. if (all_found)
  74. {
  75. std::stringstream outs;
  76. ListSingleVersion(CacheFile, records, V, outs);
  77. output_map.insert(std::make_pair<std::string, std::string>(
  78. V.ParentPkg().Name(), outs.str()));
  79. }
  80. }
  81. progress.Done();
  82. // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status)
  83. // output the sorted map
  84. for (K = output_map.begin(); K != output_map.end(); K++)
  85. std::cout << (*K).second << std::endl;
  86. return true;
  87. }