]> git.saurik.com Git - apt.git/blob - apt-private/private-search.cc
2d427fa25772f81909b77796a56a2051f9833487
[apt.git] / apt-private / private-search.cc
1 // Includes /*{{{*/
2 #include <config.h>
3
4 #include <apt-pkg/cachefile.h>
5 #include <apt-pkg/cacheset.h>
6 #include <apt-pkg/cmndline.h>
7 #include <apt-pkg/pkgrecords.h>
8 #include <apt-pkg/policy.h>
9 #include <apt-pkg/progress.h>
10 #include <apt-pkg/cacheiterators.h>
11 #include <apt-pkg/configuration.h>
12 #include <apt-pkg/depcache.h>
13 #include <apt-pkg/macros.h>
14 #include <apt-pkg/pkgcache.h>
15
16 #include <apt-private/private-cacheset.h>
17 #include <apt-private/private-output.h>
18 #include <apt-private/private-search.h>
19
20 #include <string.h>
21 #include <iostream>
22 #include <sstream>
23 #include <map>
24 #include <string>
25 #include <utility>
26
27 #include <apti18n.h>
28 /*}}}*/
29
30 bool FullTextSearch(CommandLine &CmdL) /*{{{*/
31 {
32 pkgCacheFile CacheFile;
33 pkgCache *Cache = CacheFile.GetPkgCache();
34 pkgDepCache::Policy *Plcy = CacheFile.GetPolicy();
35 if (unlikely(Cache == NULL || Plcy == NULL))
36 return false;
37
38 // Make sure there is at least one argument
39 unsigned int const NumPatterns = CmdL.FileSize() -1;
40 if (NumPatterns < 1)
41 return _error->Error(_("You must give at least one search pattern"));
42
43 #define APT_FREE_PATTERNS() for (std::vector<regex_t>::iterator P = Patterns.begin(); \
44 P != Patterns.end(); ++P) { regfree(&(*P)); }
45
46 // Compile the regex pattern
47 std::vector<regex_t> Patterns;
48 for (unsigned int I = 0; I != NumPatterns; ++I)
49 {
50 regex_t pattern;
51 if (regcomp(&pattern, CmdL.FileList[I + 1], REG_EXTENDED | REG_ICASE | REG_NOSUB) != 0)
52 {
53 APT_FREE_PATTERNS();
54 return _error->Error("Regex compilation error");
55 }
56 Patterns.push_back(pattern);
57 }
58
59 bool const NamesOnly = _config->FindB("APT::Cache::NamesOnly", false);
60
61 std::map<std::string, std::string> output_map;
62
63 LocalitySortedVersionSet bag;
64 OpTextProgress progress(*_config);
65 progress.OverallProgress(0, 100, 50, _("Sorting"));
66 GetLocalitySortedVersionSet(CacheFile, bag, progress);
67 LocalitySortedVersionSet::iterator V = bag.begin();
68
69 progress.OverallProgress(50, 100, 50, _("Full Text Search"));
70 progress.SubProgress(bag.size());
71 pkgRecords records(CacheFile);
72 int Done = 0;
73 for ( ;V != bag.end(); ++V)
74 {
75 if (Done%500 == 0)
76 progress.Progress(Done);
77 ++Done;
78
79 // we want to list each package only once
80 char const * const PkgName = V.ParentPkg().Name();
81 if (output_map.find(PkgName) != output_map.end())
82 continue;
83
84 pkgCache::DescIterator Desc = V.TranslatedDescription();
85 pkgRecords::Parser &parser = records.Lookup(Desc.FileList());
86 std::string const LongDesc = parser.LongDesc();
87
88 bool all_found = true;
89 for (std::vector<regex_t>::const_iterator pattern = Patterns.begin();
90 pattern != Patterns.end(); ++pattern)
91 {
92 if (regexec(&(*pattern), PkgName, 0, 0, 0) == 0)
93 continue;
94 else if (NamesOnly == false && regexec(&(*pattern), LongDesc.c_str(), 0, 0, 0) == 0)
95 continue;
96 // search patterns are AND, so one failing fails all
97 all_found = false;
98 break;
99 }
100 if (all_found == true)
101 {
102 std::stringstream outs;
103 ListSingleVersion(CacheFile, records, V, outs);
104 output_map.insert(std::make_pair<std::string, std::string>(
105 PkgName, outs.str()));
106 }
107 }
108 APT_FREE_PATTERNS();
109 progress.Done();
110
111 // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status)
112 // output the sorted map
113 std::map<std::string, std::string>::const_iterator K;
114 for (K = output_map.begin(); K != output_map.end(); ++K)
115 std::cout << (*K).second << std::endl;
116
117 return true;
118 }
119 /*}}}*/