]> git.saurik.com Git - apt.git/blame - apt-private/private-search.cc
make GetLocalitySortedVersionSet more generic
[apt.git] / apt-private / private-search.cc
CommitLineData
ee0167c4 1// Includes /*{{{*/
453b82a3
DK
2#include <config.h>
3
b9179170 4#include <apt-pkg/cachefile.h>
b9179170 5#include <apt-pkg/cacheset.h>
b9179170 6#include <apt-pkg/cmndline.h>
b9179170 7#include <apt-pkg/pkgrecords.h>
b9179170 8#include <apt-pkg/policy.h>
453b82a3
DK
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>
b9179170 15
453b82a3
DK
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>
b9179170 21#include <iostream>
453b82a3 22#include <sstream>
b9179170 23#include <map>
453b82a3
DK
24#include <string>
25#include <utility>
b9179170 26
453b82a3 27#include <apti18n.h>
ee0167c4 28 /*}}}*/
b9179170 29
ee0167c4 30bool FullTextSearch(CommandLine &CmdL) /*{{{*/
b9179170
MV
31{
32 pkgCacheFile CacheFile;
33 pkgCache *Cache = CacheFile.GetPkgCache();
34 pkgDepCache::Policy *Plcy = CacheFile.GetPolicy();
b9179170
MV
35 if (unlikely(Cache == NULL || Plcy == NULL))
36 return false;
37
859093da
DK
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);
b9179170
MV
60
61 std::map<std::string, std::string> output_map;
b9179170
MV
62
63 LocalitySortedVersionSet bag;
1bef0dd5 64 OpTextProgress progress(*_config);
b9179170 65 progress.OverallProgress(0, 100, 50, _("Sorting"));
25594bb5 66 GetLocalitySortedVersionSet(CacheFile, &bag, &progress);
b9179170
MV
67 LocalitySortedVersionSet::iterator V = bag.begin();
68
69 progress.OverallProgress(50, 100, 50, _("Full Text Search"));
70 progress.SubProgress(bag.size());
206b6bb3 71 pkgRecords records(CacheFile);
1a68655d
DK
72
73 std::string format = "${color:highlight}${Package}${color:neutral}/${Origin} ${Version} ${Architecture}${ }${apt:Status}\n";
74 if (_config->FindB("APT::Cache::ShowFull",false) == false)
75 format += " ${Description}\n";
76 else
77 format += " ${LongDescription}\n";
78
b9179170 79 int Done = 0;
25594bb5 80 std::vector<bool> PkgsDone(Cache->Head().PackageCount, false);
9ce3cfc9 81 for ( ;V != bag.end(); ++V)
b9179170
MV
82 {
83 if (Done%500 == 0)
84 progress.Progress(Done);
9ce3cfc9 85 ++Done;
859093da 86
206b6bb3 87 // we want to list each package only once
25594bb5
DK
88 pkgCache::PkgIterator const P = V.ParentPkg();
89 if (PkgsDone[P->ID] == true)
206b6bb3
DK
90 continue;
91
25594bb5 92 char const * const PkgName = P.Name();
b9179170
MV
93 pkgCache::DescIterator Desc = V.TranslatedDescription();
94 pkgRecords::Parser &parser = records.Lookup(Desc.FileList());
206b6bb3 95 std::string const LongDesc = parser.LongDesc();
859093da 96
b9179170 97 bool all_found = true;
859093da
DK
98 for (std::vector<regex_t>::const_iterator pattern = Patterns.begin();
99 pattern != Patterns.end(); ++pattern)
b9179170 100 {
206b6bb3 101 if (regexec(&(*pattern), PkgName, 0, 0, 0) == 0)
859093da 102 continue;
206b6bb3 103 else if (NamesOnly == false && regexec(&(*pattern), LongDesc.c_str(), 0, 0, 0) == 0)
859093da
DK
104 continue;
105 // search patterns are AND, so one failing fails all
106 all_found = false;
107 break;
b9179170 108 }
859093da 109 if (all_found == true)
b9179170 110 {
25594bb5
DK
111 PkgsDone[P->ID] = true;
112 std::stringstream outs;
113 ListSingleVersion(CacheFile, records, V, outs, format);
114 output_map.insert(std::make_pair<std::string, std::string>(
115 PkgName, outs.str()));
b9179170
MV
116 }
117 }
859093da 118 APT_FREE_PATTERNS();
b9179170
MV
119 progress.Done();
120
121 // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status)
122 // output the sorted map
206b6bb3 123 std::map<std::string, std::string>::const_iterator K;
9ce3cfc9 124 for (K = output_map.begin(); K != output_map.end(); ++K)
b9179170
MV
125 std::cout << (*K).second << std::endl;
126
127 return true;
128}
ee0167c4 129 /*}}}*/