62f94e255544797eab0392757c311029701c3d16
[apt.git] / apt-pkg / pkgrecords.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description                                                          /*{{{*/
3 // $Id: pkgrecords.cc,v 1.8 2003/09/02 04:52:16 mdz Exp $
4 /* ######################################################################
5    
6    Package Records - Allows access to complete package description records
7                      directly from the file.
8      
9    ##################################################################### */
10                                                                         /*}}}*/
11 // Include Files                                                        /*{{{*/
12 #ifdef __GNUG__
13 #pragma implementation "apt-pkg/pkgrecords.h"
14 #endif
15 #include <apt-pkg/pkgrecords.h>
16 #include <apt-pkg/indexfile.h>
17 #include <apt-pkg/error.h>
18 #include <apt-pkg/configuration.h>
19     
20 #include <apti18n.h>   
21                                                                         /*}}}*/
22 using namespace std;
23
24 // Records::pkgRecords - Constructor                                    /*{{{*/
25 // ---------------------------------------------------------------------
26 /* This will create the necessary structures to access the status files */
27 pkgRecords::pkgRecords(pkgCache &Cache) : Cache(Cache), Files(0)
28 {
29    Files = new Parser *[Cache.HeaderP->PackageFileCount];
30    memset(Files,0,sizeof(*Files)*Cache.HeaderP->PackageFileCount);
31    
32    for (pkgCache::PkgFileIterator I = Cache.FileBegin(); 
33         I.end() == false; I++)
34    {
35       const pkgIndexFile::Type *Type = pkgIndexFile::Type::GetType(I.IndexType());
36       if (Type == 0)
37       {
38          _error->Error(_("Index file type '%s' is not supported"),I.IndexType());
39          return;
40       }
41
42       Files[I->ID] = Type->CreatePkgParser(I);
43       if (Files[I->ID] == 0)
44          return;
45    }   
46    
47    // CNC:2002-11-28
48    // We store that to make sure that the destructor won't segfault,
49    // even if the Cache object was destructed before this instance.
50    PackageFileCount = Cache.HeaderP->PackageFileCount;
51 }
52                                                                         /*}}}*/
53 // Records::~pkgRecords - Destructor                                    /*{{{*/
54 // ---------------------------------------------------------------------
55 /* */
56 pkgRecords::~pkgRecords()
57 {
58    // CNC:2002-11-28
59    // See comments above.
60    for (unsigned I = 0; I != (unsigned)PackageFileCount; I++)
61       delete Files[I];
62    delete [] Files;
63 }
64                                                                         /*}}}*/
65 // Records::Lookup - Get a parser for the package version file          /*{{{*/
66 // ---------------------------------------------------------------------
67 /* */
68 pkgRecords::Parser &pkgRecords::Lookup(pkgCache::VerFileIterator const &Ver)
69 {
70    Files[Ver.File()->ID]->Jump(Ver);
71    return *Files[Ver.File()->ID];
72 }
73                                                                         /*}}}*/