]> git.saurik.com Git - apt.git/blame_incremental - apt-private/private-search.cc
search for pkg names in the cache case-sensitive
[apt.git] / apt-private / private-search.cc
... / ...
CommitLineData
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
30bool 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
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
79 int Done = 0;
80 std::vector<bool> PkgsDone(Cache->Head().PackageCount, false);
81 for ( ;V != bag.end(); ++V)
82 {
83 if (Done%500 == 0)
84 progress.Progress(Done);
85 ++Done;
86
87 // we want to list each package only once
88 pkgCache::PkgIterator const P = V.ParentPkg();
89 if (PkgsDone[P->ID] == true)
90 continue;
91
92 char const * const PkgName = P.Name();
93 pkgCache::DescIterator Desc = V.TranslatedDescription();
94 pkgRecords::Parser &parser = records.Lookup(Desc.FileList());
95 std::string const LongDesc = parser.LongDesc();
96
97 bool all_found = true;
98 for (std::vector<regex_t>::const_iterator pattern = Patterns.begin();
99 pattern != Patterns.end(); ++pattern)
100 {
101 if (regexec(&(*pattern), PkgName, 0, 0, 0) == 0)
102 continue;
103 else if (NamesOnly == false && regexec(&(*pattern), LongDesc.c_str(), 0, 0, 0) == 0)
104 continue;
105 // search patterns are AND, so one failing fails all
106 all_found = false;
107 break;
108 }
109 if (all_found == true)
110 {
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()));
116 }
117 }
118 APT_FREE_PATTERNS();
119 progress.Done();
120
121 // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status)
122 // output the sorted map
123 std::map<std::string, std::string>::const_iterator K;
124 for (K = output_map.begin(); K != output_map.end(); ++K)
125 std::cout << (*K).second << std::endl;
126
127 return true;
128}
129 /*}}}*/