private-search.cc 2.9 KB

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