squash merge of the feature/apt-binary branch without the changes from experimental
[ntk/apt.git] / apt-private / private-search.cc
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
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
35 #include "private-search.h"
36 #include "private-cacheset.h"
37
38
39 bool FullTextSearch(CommandLine &CmdL)
40 {
41 pkgCacheFile CacheFile;
42 pkgCache *Cache = CacheFile.GetPkgCache();
43 pkgDepCache::Policy *Plcy = CacheFile.GetPolicy();
44 pkgRecords records(CacheFile);
45 if (unlikely(Cache == NULL || Plcy == NULL))
46 return false;
47
48 const char **patterns;
49 patterns = CmdL.FileList + 1;
50
51 std::map<std::string, std::string> output_map;
52 std::map<std::string, std::string>::const_iterator K;
53
54 LocalitySortedVersionSet bag;
55 OpTextProgress progress;
56 progress.OverallProgress(0, 100, 50, _("Sorting"));
57 GetLocalitySortedVersionSet(CacheFile, bag, progress);
58 LocalitySortedVersionSet::iterator V = bag.begin();
59
60 progress.OverallProgress(50, 100, 50, _("Full Text Search"));
61 progress.SubProgress(bag.size());
62 int Done = 0;
63 for ( ;V != bag.end(); V++)
64 {
65 if (Done%500 == 0)
66 progress.Progress(Done);
67 Done++;
68
69 int i;
70 pkgCache::DescIterator Desc = V.TranslatedDescription();
71 pkgRecords::Parser &parser = records.Lookup(Desc.FileList());
72
73 bool all_found = true;
74 for(i=0; patterns[i] != NULL; i++)
75 {
76 // FIXME: use regexp instead of simple find()
77 const char *pattern = patterns[i];
78 all_found &= (
79 strstr(V.ParentPkg().Name(), pattern) != NULL ||
80 parser.ShortDesc().find(pattern) != std::string::npos ||
81 parser.LongDesc().find(pattern) != std::string::npos);
82 }
83 if (all_found)
84 {
85 std::stringstream outs;
86 ListSingleVersion(CacheFile, records, V, outs);
87 output_map.insert(std::make_pair<std::string, std::string>(
88 V.ParentPkg().Name(), outs.str()));
89 }
90 }
91 progress.Done();
92
93 // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status)
94 // output the sorted map
95 for (K = output_map.begin(); K != output_map.end(); K++)
96 std::cout << (*K).second << std::endl;
97
98 return true;
99 }