0.7.1
[fapman] / apt-src / cacheiterators.h
1 /*
2 Apt is copyright 1997, 1998, 1999 Jason Gunthorpe and others.
3 Apt is currently developed by APT Development Team <deity@lists.debian.org>.
4
5 License: GPLv2+
6
7         This program is free software; you can redistribute it and/or modify
8         it under the terms of the GNU General Public License as published by
9         the Free Software Foundation; either version 2 of the License, or
10         (at your option) any later version.
11
12         This program is distributed in the hope that it will be useful,
13         but WITHOUT ANY WARRANTY; without even the implied warranty of
14         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15         GNU General Public License for more details.
16
17         You should have received a copy of the GNU General Public License
18         along with this program; if not, write to the Free Software
19         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21 See /usr/share/common-licenses/GPL-2, or
22 <http://www.gnu.org/copyleft/gpl.txt> for the terms of the latest version
23 of the GNU General Public License.
24 */
25
26 // -*- mode: cpp; mode: fold -*-
27 // Description                                                          /*{{{*/
28 // $Id: cacheiterators.h,v 1.18.2.1 2004/05/08 22:44:27 mdz Exp $
29 /* ######################################################################
30    
31    Cache Iterators - Iterators for navigating the cache structure
32    
33    The iterators all provides ++,==,!=,->,* and end for their type.
34    The end function can be used to tell if the list has been fully
35    traversed.
36    
37    Unlike STL iterators these contain helper functions to access the data
38    that is being iterated over. This is because the data structures can't
39    be formed in a manner that is intuitive to use and also mmapable.
40    
41    For each variable in the target structure that would need a translation
42    to be accessed correctly a translating function of the same name is
43    present in the iterator. If applicable the translating function will
44    return an iterator.
45
46    The DepIterator can iterate over two lists, a list of 'version depends'
47    or a list of 'package reverse depends'. The type is determined by the
48    structure passed to the constructor, which should be the structure
49    that has the depends pointer as a member. The provide iterator has the
50    same system.
51    
52    This header is not user includable, please use apt-pkg/pkgcache.h
53    
54    ##################################################################### */
55                                                                         /*}}}*/
56 #ifndef PKGLIB_CACHEITERATORS_H
57 #define PKGLIB_CACHEITERATORS_H
58
59
60 // Package Iterator
61 class pkgCache::PkgIterator
62 {
63    friend class pkgCache;
64    Package *Pkg;
65    pkgCache *Owner;
66    long HashIndex;
67
68    protected:
69    
70    // This constructor is the 'begin' constructor, never use it.
71    inline PkgIterator(pkgCache &Owner) : Owner(&Owner), HashIndex(-1)
72    {
73       Pkg = Owner.PkgP;
74       operator ++(0);
75    };
76    
77    public:
78
79    enum OkState {NeedsNothing,NeedsUnpack,NeedsConfigure};
80       
81    // Iteration
82    void operator ++(int);
83    inline void operator ++() {operator ++(0);};
84    inline bool end() const {return Owner == 0 || Pkg == Owner->PkgP?true:false;};
85
86    // Comparison
87    inline bool operator ==(const PkgIterator &B) const {return Pkg == B.Pkg;};
88    inline bool operator !=(const PkgIterator &B) const {return Pkg != B.Pkg;};
89                            
90    // Accessors
91    inline Package *operator ->() {return Pkg;};
92    inline Package const *operator ->() const {return Pkg;};
93    inline Package const &operator *() const {return *Pkg;};
94    inline operator Package *() {return Pkg == Owner->PkgP?0:Pkg;};
95    inline operator Package const *() const {return Pkg == Owner->PkgP?0:Pkg;};
96    inline pkgCache *Cache() {return Owner;};
97    
98    inline const char *Name() const {return Pkg->Name == 0?0:Owner->StrP + Pkg->Name;};
99    inline const char *Section() const {return Pkg->Section == 0?0:Owner->StrP + Pkg->Section;};
100    inline bool Purge() const {return Pkg->CurrentState == pkgCache::State::Purge ||
101          (Pkg->CurrentVer == 0 && Pkg->CurrentState == pkgCache::State::NotInstalled);};
102    inline VerIterator VersionList() const;
103    inline VerIterator CurrentVer() const;
104    inline DepIterator RevDependsList() const;
105    inline PrvIterator ProvidesList() const;
106    inline unsigned long Index() const {return Pkg - Owner->PkgP;};
107    OkState State() const;
108    
109    // Constructors
110    inline PkgIterator(pkgCache &Owner,Package *Trg) : Pkg(Trg), Owner(&Owner),
111           HashIndex(0) 
112    { 
113       if (Pkg == 0)
114          Pkg = Owner.PkgP;
115    };
116    inline PkgIterator() : Pkg(0), Owner(0), HashIndex(0) {};
117 };
118
119 // Version Iterator
120 class pkgCache::VerIterator
121 {
122    Version *Ver;
123    pkgCache *Owner;
124
125    void _dummy();
126    
127    public:
128
129    // Iteration
130    void operator ++(int) {if (Ver != Owner->VerP) Ver = Owner->VerP + Ver->NextVer;};
131    inline void operator ++() {operator ++(0);};
132    inline bool end() const {return Owner == NULL || (Ver == Owner->VerP?true:false);};
133    inline void operator =(const VerIterator &B) {Ver = B.Ver; Owner = B.Owner;};
134    
135    // Comparison
136    inline bool operator ==(const VerIterator &B) const {return Ver == B.Ver;};
137    inline bool operator !=(const VerIterator &B) const {return Ver != B.Ver;};
138    int CompareVer(const VerIterator &B) const;
139    
140    // Accessors
141    inline Version *operator ->() {return Ver;};
142    inline Version const *operator ->() const {return Ver;};
143    inline Version &operator *() {return *Ver;};
144    inline Version const &operator *() const {return *Ver;};
145    inline operator Version *() {return Ver == Owner->VerP?0:Ver;};
146    inline operator Version const *() const {return Ver == Owner->VerP?0:Ver;};
147    inline pkgCache *Cache() {return Owner;};
148       
149    inline const char *VerStr() const {return Ver->VerStr == 0?0:Owner->StrP + Ver->VerStr;};
150    inline const char *Section() const {return Ver->Section == 0?0:Owner->StrP + Ver->Section;};
151    inline const char *Arch() const {return Ver->Arch == 0?0:Owner->StrP + Ver->Arch;};
152    inline PkgIterator ParentPkg() const {return PkgIterator(*Owner,Owner->PkgP + Ver->ParentPkg);};
153    inline DescIterator DescriptionList() const;
154    DescIterator TranslatedDescription() const;
155    inline DepIterator DependsList() const;
156    inline PrvIterator ProvidesList() const;
157    inline VerFileIterator FileList() const;
158    inline unsigned long Index() const {return Ver - Owner->VerP;};
159    bool Downloadable() const;
160    inline const char *PriorityType() {return Owner->Priority(Ver->Priority);};
161    string RelStr();
162    
163    bool Automatic() const;
164    VerFileIterator NewestFile() const;
165
166    inline VerIterator() : Ver(0), Owner(0) {};   
167    inline VerIterator(pkgCache &Owner,Version *Trg = 0) : Ver(Trg), 
168               Owner(&Owner) 
169    { 
170       if (Ver == 0)
171          Ver = Owner.VerP;
172    };
173 };
174
175 // Description Iterator
176 class pkgCache::DescIterator
177 {
178    Description *Desc;
179    pkgCache *Owner;
180    
181    void _dummy();
182    
183    public:
184
185    // Iteration
186    void operator ++(int) {if (Desc != Owner->DescP) Desc = Owner->DescP + Desc->NextDesc;};
187    inline void operator ++() {operator ++(0);};
188    inline bool end() const {return Desc == Owner->DescP?true:false;};
189    inline void operator =(const DescIterator &B) {Desc = B.Desc; Owner = B.Owner;};
190    
191    // Comparison
192    inline bool operator ==(const DescIterator &B) const {return Desc == B.Desc;};
193    inline bool operator !=(const DescIterator &B) const {return Desc != B.Desc;};
194    int CompareDesc(const DescIterator &B) const;
195    
196    // Accessors
197    inline Description *operator ->() {return Desc;};
198    inline Description const *operator ->() const {return Desc;};
199    inline Description &operator *() {return *Desc;};
200    inline Description const &operator *() const {return *Desc;};
201    inline operator Description *() {return Desc == Owner->DescP?0:Desc;};
202    inline operator Description const *() const {return Desc == Owner->DescP?0:Desc;};
203    inline pkgCache *Cache() {return Owner;};
204       
205    inline const char *LanguageCode() const {return Owner->StrP + Desc->language_code;};
206    inline const char *md5() const {return Owner->StrP + Desc->md5sum;};
207    inline DescFileIterator FileList() const;
208    inline unsigned long Index() const {return Desc - Owner->DescP;};
209
210    inline DescIterator() : Desc(0), Owner(0) {};   
211    inline DescIterator(pkgCache &Owner,Description *Trg = 0) : Desc(Trg), 
212               Owner(&Owner) 
213    { 
214       if (Desc == 0)
215          Desc = Owner.DescP;
216    };
217 };
218
219 // Dependency iterator
220 class pkgCache::DepIterator
221 {
222    Dependency *Dep;
223    enum {DepVer, DepRev} Type;
224    pkgCache *Owner;
225    
226    void _dummy();
227    
228    public:
229
230    // Iteration
231    void operator ++(int) {if (Dep != Owner->DepP) Dep = Owner->DepP +
232         (Type == DepVer?Dep->NextDepends:Dep->NextRevDepends);};
233    inline void operator ++() {operator ++(0);};
234    inline bool end() const {return Owner == 0 || Dep == Owner->DepP?true:false;};
235    
236    // Comparison
237    inline bool operator ==(const DepIterator &B) const {return Dep == B.Dep;};
238    inline bool operator !=(const DepIterator &B) const {return Dep != B.Dep;};
239
240    // Accessors
241    inline Dependency *operator ->() {return Dep;};
242    inline Dependency const *operator ->() const {return Dep;};
243    inline Dependency &operator *() {return *Dep;};
244    inline Dependency const &operator *() const {return *Dep;};
245    inline operator Dependency *() {return Dep == Owner->DepP?0:Dep;};
246    inline operator Dependency const *() const {return Dep == Owner->DepP?0:Dep;};
247    inline pkgCache *Cache() {return Owner;};
248    
249    inline const char *TargetVer() const {return Dep->Version == 0?0:Owner->StrP + Dep->Version;};
250    inline PkgIterator TargetPkg() {return PkgIterator(*Owner,Owner->PkgP + Dep->Package);};
251    inline PkgIterator SmartTargetPkg() {PkgIterator R(*Owner,0);SmartTargetPkg(R);return R;};
252    inline VerIterator ParentVer() {return VerIterator(*Owner,Owner->VerP + Dep->ParentVer);};
253    inline PkgIterator ParentPkg() {return PkgIterator(*Owner,Owner->PkgP + Owner->VerP[Dep->ParentVer].ParentPkg);};
254    inline bool Reverse() {return Type == DepRev;};
255    inline unsigned long Index() const {return Dep - Owner->DepP;};
256    bool IsCritical();
257    void GlobOr(DepIterator &Start,DepIterator &End);
258    Version **AllTargets();   
259    bool SmartTargetPkg(PkgIterator &Result);
260    inline const char *CompType() {return Owner->CompType(Dep->CompareOp);};
261    inline const char *DepType() {return Owner->DepType(Dep->Type);};
262    
263    inline DepIterator(pkgCache &Owner,Dependency *Trg,Version * = 0) :
264           Dep(Trg), Type(DepVer), Owner(&Owner) 
265    {
266       if (Dep == 0)
267          Dep = Owner.DepP;
268    };
269    inline DepIterator(pkgCache &Owner,Dependency *Trg,Package *) :
270           Dep(Trg), Type(DepRev), Owner(&Owner)
271    {
272       if (Dep == 0)
273          Dep = Owner.DepP;
274    };
275    inline DepIterator() : Dep(0), Type(DepVer), Owner(0) {};
276 };
277
278 // Provides iterator
279 class pkgCache::PrvIterator
280 {
281    Provides *Prv;
282    enum {PrvVer, PrvPkg} Type;
283    pkgCache *Owner;
284    
285    void _dummy();
286    
287    public:
288
289    // Iteration
290    void operator ++(int) {if (Prv != Owner->ProvideP) Prv = Owner->ProvideP +
291         (Type == PrvVer?Prv->NextPkgProv:Prv->NextProvides);};
292    inline void operator ++() {operator ++(0);};
293    inline bool end() const {return Owner == 0 || Prv == Owner->ProvideP?true:false;};
294    
295    // Comparison
296    inline bool operator ==(const PrvIterator &B) const {return Prv == B.Prv;};
297    inline bool operator !=(const PrvIterator &B) const {return Prv != B.Prv;};
298
299    // Accessors
300    inline Provides *operator ->() {return Prv;};
301    inline Provides const *operator ->() const {return Prv;};
302    inline Provides &operator *() {return *Prv;};
303    inline Provides const &operator *() const {return *Prv;};
304    inline operator Provides *() {return Prv == Owner->ProvideP?0:Prv;};
305    inline operator Provides const *() const {return Prv == Owner->ProvideP?0:Prv;};
306    inline pkgCache *Cache() {return Owner;};
307
308    inline const char *Name() const {return Owner->StrP + Owner->PkgP[Prv->ParentPkg].Name;};
309    inline const char *ProvideVersion() const {return Prv->ProvideVersion == 0?0:Owner->StrP + Prv->ProvideVersion;};
310    inline PkgIterator ParentPkg() {return PkgIterator(*Owner,Owner->PkgP + Prv->ParentPkg);};
311    inline VerIterator OwnerVer() {return VerIterator(*Owner,Owner->VerP + Prv->Version);};
312    inline PkgIterator OwnerPkg() {return PkgIterator(*Owner,Owner->PkgP + Owner->VerP[Prv->Version].ParentPkg);};
313    inline unsigned long Index() const {return Prv - Owner->ProvideP;};
314
315    inline PrvIterator() : Prv(0), Type(PrvVer), Owner(0)  {};
316
317    inline PrvIterator(pkgCache &Owner,Provides *Trg,Version *) :
318           Prv(Trg), Type(PrvVer), Owner(&Owner) 
319    {
320       if (Prv == 0)
321          Prv = Owner.ProvideP;
322    };
323    inline PrvIterator(pkgCache &Owner,Provides *Trg,Package *) : 
324           Prv(Trg), Type(PrvPkg), Owner(&Owner)
325    {
326       if (Prv == 0)
327          Prv = Owner.ProvideP;
328    };
329 };
330
331 // Package file 
332 class pkgCache::PkgFileIterator
333 {
334    pkgCache *Owner;
335    PackageFile *File;
336
337    public:
338
339    // Iteration
340    void operator ++(int) {if (File!= Owner->PkgFileP) File = Owner->PkgFileP + File->NextFile;};
341    inline void operator ++() {operator ++(0);};
342    inline bool end() const {return File == Owner->PkgFileP?true:false;};
343
344    // Comparison
345    inline bool operator ==(const PkgFileIterator &B) const {return File == B.File;};
346    inline bool operator !=(const PkgFileIterator &B) const {return File != B.File;};
347                            
348    // Accessors
349    inline PackageFile *operator ->() {return File;};
350    inline PackageFile const *operator ->() const {return File;};
351    inline PackageFile const &operator *() const {return *File;};
352    inline operator PackageFile *() {return File == Owner->PkgFileP?0:File;};
353    inline operator PackageFile const *() const {return File == Owner->PkgFileP?0:File;};
354    inline pkgCache *Cache() {return Owner;};
355
356    inline const char *FileName() const {return File->FileName == 0?0:Owner->StrP + File->FileName;};
357    inline const char *Archive() const {return File->Archive == 0?0:Owner->StrP + File->Archive;};
358    inline const char *Component() const {return File->Component == 0?0:Owner->StrP + File->Component;};
359    inline const char *Version() const {return File->Version == 0?0:Owner->StrP + File->Version;};
360    inline const char *Origin() const {return File->Origin == 0?0:Owner->StrP + File->Origin;};
361    inline const char *Label() const {return File->Label == 0?0:Owner->StrP + File->Label;};
362    inline const char *Site() const {return File->Site == 0?0:Owner->StrP + File->Site;};
363    inline const char *Architecture() const {return File->Architecture == 0?0:Owner->StrP + File->Architecture;};
364    inline const char *IndexType() const {return File->IndexType == 0?0:Owner->StrP + File->IndexType;};
365
366    inline unsigned long Index() const {return File - Owner->PkgFileP;};
367
368    bool IsOk();
369    string RelStr();
370    
371    // Constructors
372    inline PkgFileIterator() : Owner(0), File(0) {};
373    inline PkgFileIterator(pkgCache &Owner) : Owner(&Owner), File(Owner.PkgFileP) {};
374    inline PkgFileIterator(pkgCache &Owner,PackageFile *Trg) : Owner(&Owner), File(Trg) {};
375 };
376
377 // Version File 
378 class pkgCache::VerFileIterator
379 {
380    pkgCache *Owner;
381    VerFile *FileP;
382
383    public:
384
385    // Iteration
386    void operator ++(int) {if (FileP != Owner->VerFileP) FileP = Owner->VerFileP + FileP->NextFile;};
387    inline void operator ++() {operator ++(0);};
388    inline bool end() const {return FileP == Owner->VerFileP?true:false;};
389
390    // Comparison
391    inline bool operator ==(const VerFileIterator &B) const {return FileP == B.FileP;};
392    inline bool operator !=(const VerFileIterator &B) const {return FileP != B.FileP;};
393                            
394    // Accessors
395    inline VerFile *operator ->() {return FileP;};
396    inline VerFile const *operator ->() const {return FileP;};
397    inline VerFile const &operator *() const {return *FileP;};
398    inline operator VerFile *() {return FileP == Owner->VerFileP?0:FileP;};
399    inline operator VerFile const *() const {return FileP == Owner->VerFileP?0:FileP;};
400    inline pkgCache *Cache() {return Owner;};
401   
402    inline PkgFileIterator File() const {return PkgFileIterator(*Owner,FileP->File + Owner->PkgFileP);};
403    inline unsigned long Index() const {return FileP - Owner->VerFileP;};
404       
405    inline VerFileIterator() : Owner(0), FileP(0) {};
406    inline VerFileIterator(pkgCache &Owner,VerFile *Trg) : Owner(&Owner), FileP(Trg) {};
407 };
408
409 // Description File 
410 class pkgCache::DescFileIterator
411 {
412    pkgCache *Owner;
413    DescFile *FileP;
414
415    public:
416
417    // Iteration
418    void operator ++(int) {if (FileP != Owner->DescFileP) FileP = Owner->DescFileP + FileP->NextFile;};
419    inline void operator ++() {operator ++(0);};
420    inline bool end() const {return FileP == Owner->DescFileP?true:false;};
421
422    // Comparison
423    inline bool operator ==(const DescFileIterator &B) const {return FileP == B.FileP;};
424    inline bool operator !=(const DescFileIterator &B) const {return FileP != B.FileP;};
425                            
426    // Accessors
427    inline DescFile *operator ->() {return FileP;};
428    inline DescFile const *operator ->() const {return FileP;};
429    inline DescFile const &operator *() const {return *FileP;};
430    inline operator DescFile *() {return FileP == Owner->DescFileP?0:FileP;};
431    inline operator DescFile const *() const {return FileP == Owner->DescFileP?0:FileP;};
432    inline pkgCache *Cache() {return Owner;};
433   
434    inline PkgFileIterator File() const {return PkgFileIterator(*Owner,FileP->File + Owner->PkgFileP);};
435    inline unsigned long Index() const {return FileP - Owner->DescFileP;};
436       
437    inline DescFileIterator() : Owner(0), FileP(0) {};
438    inline DescFileIterator(pkgCache &Owner,DescFile *Trg) : Owner(&Owner), FileP(Trg) {};
439 };
440
441 // Inlined Begin functions cant be in the class because of order problems
442 inline pkgCache::VerIterator pkgCache::PkgIterator::VersionList() const
443        {return VerIterator(*Owner,Owner->VerP + Pkg->VersionList);};
444 inline pkgCache::VerIterator pkgCache::PkgIterator::CurrentVer() const
445        {return VerIterator(*Owner,Owner->VerP + Pkg->CurrentVer);};
446 inline pkgCache::DepIterator pkgCache::PkgIterator::RevDependsList() const
447        {return DepIterator(*Owner,Owner->DepP + Pkg->RevDepends,Pkg);};
448 inline pkgCache::PrvIterator pkgCache::PkgIterator::ProvidesList() const
449        {return PrvIterator(*Owner,Owner->ProvideP + Pkg->ProvidesList,Pkg);};
450 inline pkgCache::DescIterator pkgCache::VerIterator::DescriptionList() const
451        {return DescIterator(*Owner,Owner->DescP + Ver->DescriptionList);};
452 inline pkgCache::PrvIterator pkgCache::VerIterator::ProvidesList() const
453        {return PrvIterator(*Owner,Owner->ProvideP + Ver->ProvidesList,Ver);};
454 inline pkgCache::DepIterator pkgCache::VerIterator::DependsList() const
455        {return DepIterator(*Owner,Owner->DepP + Ver->DependsList,Ver);};
456 inline pkgCache::VerFileIterator pkgCache::VerIterator::FileList() const
457        {return VerFileIterator(*Owner,Owner->VerFileP + Ver->FileList);};
458 inline pkgCache::DescFileIterator pkgCache::DescIterator::FileList() const
459        {return DescFileIterator(*Owner,Owner->DescFileP + Desc->FileList);};
460
461 #endif