]>
git.saurik.com Git - apt.git/blob - apt-pkg/tagfile.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: tagfile.cc,v 1.5 1998/07/07 04:17:06 jgg Exp $
4 /* ######################################################################
6 Fast scanner for RFC-822 type header information
8 This uses a rotating 64K buffer to load the package information into.
9 The scanner runs over it and isolates and indexes a single section.
11 ##################################################################### */
13 // Include Files /*{{{*/
15 #pragma implementation "pkglib/tagfile.h"
18 #include <pkglib/tagfile.h>
19 #include <pkglib/error.h>
25 // TagFile::pkgTagFile - Constructor /*{{{*/
26 // ---------------------------------------------------------------------
28 pkgTagFile::pkgTagFile(File
&Fd
) : Fd(Fd
)
30 Buffer
= new char[64*1024];
31 Start
= End
= Buffer
+ 64*1024;
37 // TagFile::Step - Advance to the next section /*{{{*/
38 // ---------------------------------------------------------------------
39 /* If the Section Scanner fails we refill the buffer and try again. */
40 bool pkgTagFile::Step(pkgTagSection
&Tag
)
42 if (Tag
.Scan(Start
,End
- Start
) == false)
47 if (Tag
.Scan(Start
,End
- Start
) == false)
48 return _error
->Error("Unable to parse package file");
51 iOffset
+= Tag
.size();
56 // TagFile::Fill - Top up the buffer /*{{{*/
57 // ---------------------------------------------------------------------
58 /* This takes the bit at the end of the buffer and puts it at the start
59 then fills the rest from the file */
60 bool pkgTagFile::Fill()
62 unsigned long Size
= End
- Start
;
71 memmove(Buffer
,Start
,Size
);
74 // See if only a bit of the file is left or if
75 if (Left
< End
- Buffer
- Size
)
77 if (Fd
.Read(Buffer
+ Size
,Left
) == false)
79 End
= Buffer
+ Size
+ Left
;
84 if (Fd
.Read(Buffer
+ Size
, End
- Buffer
- Size
) == false)
86 Left
-= End
- Buffer
- Size
;
91 // TagSection::Scan - Scan for the end of the header information /*{{{*/
92 // ---------------------------------------------------------------------
93 /* This looks for the first double new line in the data stream. It also
94 indexes the tags in the section. */
95 bool pkgTagSection::Scan(const char *Start
,unsigned long MaxLength
)
97 const char *End
= Start
+ MaxLength
;
98 Stop
= Section
= Start
;
101 Indexes
[TagCount
++] = Stop
- Section
;
103 for (; Stop
< End
; Stop
++)
105 if (Stop
[-1] != '\n')
109 // Extra one at the end to simplify find
110 Indexes
[TagCount
] = Stop
- Section
;
111 for (; Stop
[0] == '\n' && Stop
< End
; Stop
++);
116 if (isspace(Stop
[0]) == 0)
117 Indexes
[TagCount
++] = Stop
- Section
;
120 if (TagCount
> sizeof(Indexes
)/sizeof(Indexes
[0]))
121 TagCount
= sizeof(Indexes
)/sizeof(Indexes
[0]);
126 // TagSection::Find - Locate a tag /*{{{*/
127 // ---------------------------------------------------------------------
128 /* This searches the section for a tag that matches the given string. */
129 bool pkgTagSection::Find(const char *Tag
,const char *&Start
,
132 unsigned int Length
= strlen(Tag
);
133 for (unsigned int I
= 0; I
!= TagCount
; I
++)
135 if (strncasecmp(Tag
,Section
+ Indexes
[I
],Length
) != 0)
138 // Make sure the colon is in the right place
139 const char *C
= Section
+ Length
+ Indexes
[I
];
140 for (; isspace(*C
) != 0; C
++);
144 // Strip off the gunk from the start end
146 End
= Section
+ Indexes
[I
+1];
147 for (; (isspace(*Start
) != 0 || *Start
== ':') && Start
< End
; Start
++);
148 for (; isspace(End
[-1]) != 0 && End
> Start
; End
--);
156 #include <pkglib/pkgcachegen.h>
157 #include <pkglib/deblistparser.h>
159 int main(int argc
,char *argv
[])
162 File
CacheF("./cache",File::WriteEmpty
);
163 DynamicMMap
Map(CacheF
,MMap::Public
);
164 pkgCacheGenerator
Gen(Map
);
166 for (int I
= 1; I
!= argc
; I
++)
168 cout
<< "Merging in " << argv
[I
] << endl
;
169 File
F(argv
[I
],File::ReadOnly
);
170 Gen
.SelectFile(argv
[I
]);
171 debListParser
Parser(F
);
172 Gen
.MergeList(Parser
);
177 File CacheF("./cache",File::WriteExists);
178 MMap Map(CacheF,MMap::Public | MMap::ReadOnly);
180 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
182 cout << "Package: " << I.Name() << endl;
183 for (pkgCache::VerIterator V = I.VersionList(); V.end() == false; V++)
185 cout << "Version: " << V.VerStr() << endl;
186 cout << "Size: " << V->Size << endl;
187 cout << "Installed-Size: " << V->InstalledSize << endl;
188 cout << "Section: " << V.Section() << endl;
189 cout << "Priority: " << Cache.Priority(V->Priority) << endl;
191 pkgCache::PrvIterator P = V.ProvidesList();
192 if (P.end() == false)
194 cout << "Provides: ";
195 for (; P.end() == false; P++)
196 cout << P.Name() << ", ";
206 while (Test
.Step(I
) == true)
210 if (I
.Find("Package",Start
,End
) == false)
212 cout
<< "Failed" << endl
;
216 cout
<< "Package: " << string(Start
,End
- Start
) << endl
;
218 /* for (const char *I = Start; I < End; I++)
220 const char *Begin = I;
222 while (isspace(*I) == 0 && ispunct(*I) == 0 && I < End)
224 if (isalpha(*I) != 0)
229 cout << string(Begin,I-Begin) << endl;
230 while ((isspace(*I) != 0 || ispunct(*I) != 0) && I < End)
236 _error
->DumpErrors();