]> git.saurik.com Git - apt.git/blob - apt-pkg/pkgcachegen.h
pkgCacheGenerator: Use StringView for toString
[apt.git] / apt-pkg / pkgcachegen.h
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4
5 Package Cache Generator - Generator for the cache structure.
6
7 This builds the cache structure from the abstract package list parser.
8 Each archive source has it's own list parser that is instantiated by
9 the caller to provide data for the generator.
10
11 Parts of the cache are created by this generator class while other
12 parts are created by the list parser. The list parser is responsible
13 for creating version, depends and provides structures, and some of
14 their contents
15
16 ##################################################################### */
17 /*}}}*/
18 #ifndef PKGLIB_PKGCACHEGEN_H
19 #define PKGLIB_PKGCACHEGEN_H
20
21 #include <apt-pkg/md5.h>
22 #include <apt-pkg/mmap.h>
23 #include <apt-pkg/pkgcache.h>
24 #include <apt-pkg/cacheiterators.h>
25 #include <apt-pkg/macros.h>
26
27 #include <vector>
28 #include <string>
29 #if __cplusplus >= 201103L
30 #include <unordered_map>
31 #endif
32 #ifdef APT_PKG_EXPOSE_STRING_VIEW
33 #include <apt-pkg/string_view.h>
34 #endif
35
36 class FileFd;
37 class pkgSourceList;
38 class OpProgress;
39 class pkgIndexFile;
40 class pkgCacheListParser;
41
42 class APT_HIDDEN pkgCacheGenerator /*{{{*/
43 {
44 APT_HIDDEN map_stringitem_t WriteStringInMap(std::string const &String) { return WriteStringInMap(String.c_str()); };
45 APT_HIDDEN map_stringitem_t WriteStringInMap(const char *String);
46 APT_HIDDEN map_stringitem_t WriteStringInMap(const char *String, const unsigned long &Len);
47 APT_HIDDEN map_pointer_t AllocateInMap(const unsigned long &size);
48
49 // Dirty hack for public users that do not use C++11 yet
50 #if __cplusplus >= 201103L
51 std::unordered_map<std::string,map_stringitem_t> strMixed;
52 std::unordered_map<std::string,map_stringitem_t> strSections;
53 std::unordered_map<std::string,map_stringitem_t> strPkgNames;
54 std::unordered_map<std::string,map_stringitem_t> strVersions;
55 #endif
56
57 friend class pkgCacheListParser;
58 typedef pkgCacheListParser ListParser;
59
60 public:
61
62 template<typename Iter> class Dynamic {
63 public:
64 static std::vector<Iter*> toReMap;
65 explicit Dynamic(Iter &I) {
66 toReMap.push_back(&I);
67 }
68
69 ~Dynamic() {
70 toReMap.pop_back();
71 }
72 };
73
74 protected:
75
76 DynamicMMap &Map;
77 pkgCache Cache;
78 OpProgress *Progress;
79
80 std::string RlsFileName;
81 pkgCache::ReleaseFile *CurrentRlsFile;
82 std::string PkgFileName;
83 pkgCache::PackageFile *CurrentFile;
84
85 #ifdef APT_PKG_EXPOSE_STRING_VIEW
86 bool NewGroup(pkgCache::GrpIterator &Grp, APT::StringView Name);
87 bool NewPackage(pkgCache::PkgIterator &Pkg, APT::StringView Name, APT::StringView Arch);
88 #endif
89 bool NewFileVer(pkgCache::VerIterator &Ver,ListParser &List);
90 bool NewFileDesc(pkgCache::DescIterator &Desc,ListParser &List);
91 bool NewDepends(pkgCache::PkgIterator &Pkg, pkgCache::VerIterator &Ver,
92 map_pointer_t const Version, uint8_t const Op,
93 uint8_t const Type, map_pointer_t* &OldDepLast);
94 map_pointer_t NewVersion(pkgCache::VerIterator &Ver,const std::string &VerStr,map_pointer_t const Next) APT_DEPRECATED
95 { return NewVersion(Ver, VerStr, 0, 0, Next); }
96 map_pointer_t NewVersion(pkgCache::VerIterator &Ver,const std::string &VerStr,
97 map_pointer_t const ParentPkg, unsigned short const Hash,
98 map_pointer_t const Next);
99 map_pointer_t NewDescription(pkgCache::DescIterator &Desc,const std::string &Lang,const MD5SumValue &md5sum,map_stringitem_t const idxmd5str);
100 bool NewProvides(pkgCache::VerIterator &Ver, pkgCache::PkgIterator &Pkg,
101 map_stringitem_t const ProvidesVersion, uint8_t const Flags);
102
103 public:
104
105 enum StringType { MIXED, PKGNAME, VERSIONNUMBER, SECTION };
106 map_stringitem_t StoreString(StringType const type, const char * S, unsigned int const Size);
107
108 #ifdef APT_PKG_EXPOSE_STRING_VIEW
109 inline map_stringitem_t StoreString(enum StringType const type, APT::StringView S) {return StoreString(type, S.data(),S.length());};
110 #endif
111
112 void DropProgress() {Progress = 0;};
113 bool SelectFile(const std::string &File,pkgIndexFile const &Index, std::string const &Architecture, std::string const &Component, unsigned long Flags = 0);
114 bool SelectReleaseFile(const std::string &File, const std::string &Site, unsigned long Flags = 0);
115 bool MergeList(ListParser &List,pkgCache::VerIterator *Ver = 0);
116 inline pkgCache &GetCache() {return Cache;};
117 inline pkgCache::PkgFileIterator GetCurFile()
118 {return pkgCache::PkgFileIterator(Cache,CurrentFile);};
119 inline pkgCache::RlsFileIterator GetCurRlsFile()
120 {return pkgCache::RlsFileIterator(Cache,CurrentRlsFile);};
121
122 APT_PUBLIC static bool MakeStatusCache(pkgSourceList &List,OpProgress *Progress,
123 MMap **OutMap = 0,bool AllowMem = false);
124 APT_HIDDEN static bool MakeStatusCache(pkgSourceList &List,OpProgress *Progress,
125 MMap **OutMap,pkgCache **OutCache, bool AllowMem = false);
126 APT_PUBLIC static bool MakeOnlyStatusCache(OpProgress *Progress,DynamicMMap **OutMap);
127
128 void ReMap(void const * const oldMap, void const * const newMap);
129
130 pkgCacheGenerator(DynamicMMap *Map,OpProgress *Progress);
131 virtual ~pkgCacheGenerator();
132
133 private:
134 void * const d;
135 APT_HIDDEN bool MergeListGroup(ListParser &List, std::string const &GrpName);
136 APT_HIDDEN bool MergeListPackage(ListParser &List, pkgCache::PkgIterator &Pkg);
137 APT_HIDDEN bool MergeListVersion(ListParser &List, pkgCache::PkgIterator &Pkg,
138 std::string const &Version, pkgCache::VerIterator* &OutVer);
139
140 APT_HIDDEN bool AddImplicitDepends(pkgCache::GrpIterator &G, pkgCache::PkgIterator &P,
141 pkgCache::VerIterator &V);
142 APT_HIDDEN bool AddImplicitDepends(pkgCache::VerIterator &V, pkgCache::PkgIterator &D);
143
144 APT_HIDDEN bool AddNewDescription(ListParser &List, pkgCache::VerIterator &Ver,
145 std::string const &lang, MD5SumValue const &CurMd5, map_stringitem_t &md5idx);
146 };
147 /*}}}*/
148 // This is the abstract package list parser class. /*{{{*/
149 class APT_HIDDEN pkgCacheListParser
150 {
151 pkgCacheGenerator *Owner;
152 friend class pkgCacheGenerator;
153
154 // Some cache items
155 pkgCache::VerIterator OldDepVer;
156 map_pointer_t *OldDepLast;
157
158 void * const d;
159
160 protected:
161
162 inline map_stringitem_t StoreString(pkgCacheGenerator::StringType const type, const char *S,unsigned int Size) {return Owner->StoreString(type, S, Size);};
163 #ifdef APT_PKG_EXPOSE_STRING_VIEW
164 inline map_stringitem_t StoreString(pkgCacheGenerator::StringType const type, APT::StringView S) {return Owner->StoreString(type, S);};
165 #endif
166
167 inline map_stringitem_t WriteString(const std::string &S) {return Owner->WriteStringInMap(S);};
168 inline map_stringitem_t WriteString(const char *S,unsigned int Size) {return Owner->WriteStringInMap(S,Size);};
169 #ifdef APT_PKG_EXPOSE_STRING_VIEW
170 bool NewDepends(pkgCache::VerIterator &Ver,APT::StringView Package, APT::StringView Arch,
171 APT::StringView Version,uint8_t const Op,
172 uint8_t const Type);
173 bool NewProvides(pkgCache::VerIterator &Ver,APT::StringView PkgName,
174 APT::StringView PkgArch, APT::StringView Version,
175 uint8_t const Flags);
176 bool NewProvidesAllArch(pkgCache::VerIterator &Ver, APT::StringView Package,
177 APT::StringView Version, uint8_t const Flags);
178 #endif
179 public:
180
181 // These all operate against the current section
182 virtual std::string Package() = 0;
183 virtual std::string Architecture() = 0;
184 virtual bool ArchitectureAll() = 0;
185 virtual std::string Version() = 0;
186 virtual bool NewVersion(pkgCache::VerIterator &Ver) = 0;
187 virtual std::string Description(std::string const &lang) = 0;
188 virtual std::vector<std::string> AvailableDescriptionLanguages() = 0;
189 virtual MD5SumValue Description_md5() = 0;
190 virtual unsigned short VersionHash() = 0;
191 /** compare currently parsed version with given version
192 *
193 * \param Hash of the currently parsed version
194 * \param Ver to compare with
195 */
196 virtual bool SameVersion(unsigned short const Hash, pkgCache::VerIterator const &Ver);
197 virtual bool UsePackage(pkgCache::PkgIterator &Pkg,
198 pkgCache::VerIterator &Ver) = 0;
199 virtual map_filesize_t Offset() = 0;
200 virtual map_filesize_t Size() = 0;
201
202 virtual bool Step() = 0;
203
204 virtual bool CollectFileProvides(pkgCache &/*Cache*/,
205 pkgCache::VerIterator &/*Ver*/) {return true;};
206
207 pkgCacheListParser();
208 virtual ~pkgCacheListParser();
209 };
210 /*}}}*/
211
212 APT_DEPRECATED_MSG("Use pkgCacheGenerator::MakeStatusCache instead") bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress,
213 MMap **OutMap = 0,bool AllowMem = false);
214 APT_DEPRECATED_MSG("Use pkgCacheGenerator::MakeOnlyStatusCache instead") bool pkgMakeOnlyStatusCache(OpProgress &Progress,DynamicMMap **OutMap);
215
216 #endif