]>
git.saurik.com Git - apt.git/blob - apt-pkg/tagfile.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: tagfile.cc,v 1.24 1999/02/22 03:30:06 jgg Exp $
4 /* ######################################################################
6 Fast scanner for RFC-822 type header information
8 This uses a rotating 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 "apt-pkg/tagfile.h"
18 #include <apt-pkg/tagfile.h>
19 #include <apt-pkg/error.h>
20 #include <apt-pkg/strutl.h>
26 // TagFile::pkgTagFile - Constructor /*{{{*/
27 // ---------------------------------------------------------------------
29 pkgTagFile::pkgTagFile(FileFd
&Fd
,unsigned long Size
) : Fd(Fd
), Size(Size
)
31 Buffer
= new char[Size
];
38 // TagFile::Step - Advance to the next section /*{{{*/
39 // ---------------------------------------------------------------------
40 /* If the Section Scanner fails we refill the buffer and try again. */
41 bool pkgTagFile::Step(pkgTagSection
&Tag
)
43 if (Tag
.Scan(Start
,End
- Start
) == false)
48 if (Tag
.Scan(Start
,End
- Start
) == false)
49 return _error
->Error("Unable to parse package file %s (1)",Fd
.Name().c_str());
52 iOffset
+= Tag
.size();
57 // TagFile::Fill - Top up the buffer /*{{{*/
58 // ---------------------------------------------------------------------
59 /* This takes the bit at the end of the buffer and puts it at the start
60 then fills the rest from the file */
61 bool pkgTagFile::Fill()
63 unsigned long EndSize
= End
- Start
;
65 memmove(Buffer
,Start
,EndSize
);
67 End
= Buffer
+ EndSize
;
73 if (Size
- (End
- Buffer
) < 4)
76 // Append a double new line if one does not exist
77 unsigned int LineCount
= 0;
78 for (const char *E
= End
- 1; E
- End
< 6 && (*E
== '\n' || *E
== '\r'); E
--)
81 for (; LineCount
< 2; LineCount
++)
87 // See if only a bit of the file is left
88 if (Left
< Size
- (End
- Buffer
))
90 if (Fd
.Read(End
,Left
) == false)
98 if (Fd
.Read(End
,Size
- (End
- Buffer
)) == false)
101 Left
-= Size
- (End
- Buffer
);
107 // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/
108 // ---------------------------------------------------------------------
109 /* This jumps to a pre-recorded file location and reads the record
111 bool pkgTagFile::Jump(pkgTagSection
&Tag
,unsigned long Offset
)
114 Left
= Fd
.Size() - Offset
;
115 if (Fd
.Seek(Offset
) == false)
117 End
= Start
= Buffer
;
122 if (Tag
.Scan(Start
,End
- Start
) == true)
125 // This appends a double new line (for the real eof handling)
129 if (Tag
.Scan(Start
,End
- Start
) == false)
131 cout
<< string(Start
,End
) << endl
;
132 return _error
->Error("Unable to parse package file %s (2)",Fd
.Name().c_str());
138 // TagSection::Scan - Scan for the end of the header information /*{{{*/
139 // ---------------------------------------------------------------------
140 /* This looks for the first double new line in the data stream. It also
141 indexes the tags in the section. This very simple hash function for the
142 first 3 letters gives very good performance on the debian package files */
143 bool pkgTagSection::Scan(const char *Start
,unsigned long MaxLength
)
145 const char *End
= Start
+ MaxLength
;
146 Stop
= Section
= Start
;
147 memset(AlphaIndexes
,0,sizeof(AlphaIndexes
));
153 while (TagCount
< sizeof(Indexes
)/sizeof(Indexes
[0]) && Stop
< End
)
155 // Start a new index and add it to the hash
156 if (isspace(Stop
[0]) == 0)
158 Indexes
[TagCount
++] = Stop
- Section
;
159 unsigned char A
= tolower(Stop
[0]) - 'a';
160 unsigned char B
= tolower(Stop
[1]) - 'a';
161 unsigned char C
= tolower(Stop
[3]) - 'a';
162 AlphaIndexes
[((A
+ C
/3)%26
) + 26*((B
+ C
/2)%26
)] = TagCount
;
165 Stop
= (const char *)memchr(Stop
,'\n',End
- Stop
);
170 for (; Stop
[1] == '\r' && Stop
+1 < End
; Stop
++);
172 // Double newline marks the end of the record
173 if (Stop
+1 < End
&& Stop
[1] == '\n')
175 Indexes
[TagCount
] = Stop
- Section
;
176 for (; (Stop
[0] == '\n' || Stop
[0] == '\r') && Stop
< End
; Stop
++);
186 // TagSection::Find - Locate a tag /*{{{*/
187 // ---------------------------------------------------------------------
188 /* This searches the section for a tag that matches the given string. */
189 bool pkgTagSection::Find(const char *Tag
,const char *&Start
,
192 unsigned int Length
= strlen(Tag
);
193 unsigned char A
= tolower(Tag
[0]) - 'a';
194 unsigned char B
= tolower(Tag
[1]) - 'a';
195 unsigned char C
= tolower(Tag
[3]) - 'a';
196 unsigned int I
= AlphaIndexes
[((A
+ C
/3)%26
) + 26*((B
+ C
/2)%26
)];
201 for (unsigned int Counter
= 0; Counter
!= TagCount
; Counter
++,
205 St
= Section
+ Indexes
[I
];
206 if (strncasecmp(Tag
,St
,Length
) != 0)
209 // Make sure the colon is in the right place
210 const char *C
= St
+ Length
;
211 for (; isspace(*C
) != 0; C
++);
215 // Strip off the gunk from the start end
217 End
= Section
+ Indexes
[I
+1];
219 return _error
->Error("Internal parsing error");
221 for (; (isspace(*Start
) != 0 || *Start
== ':') && Start
< End
; Start
++);
222 for (; isspace(End
[-1]) != 0 && End
> Start
; End
--);
231 // TagSection::FindS - Find a string /*{{{*/
232 // ---------------------------------------------------------------------
234 string
pkgTagSection::FindS(const char *Tag
)
238 if (Find(Tag
,Start
,End
) == false)
240 return string(Start
,End
);
243 // TagSection::FindI - Find an integer /*{{{*/
244 // ---------------------------------------------------------------------
246 signed int pkgTagSection::FindI(const char *Tag
,signed long Default
)
250 if (Find(Tag
,Start
,Stop
) == false)
253 // Copy it into a temp buffer so we can use strtol
255 if ((unsigned)(Stop
- Start
) >= sizeof(S
))
257 strncpy(S
,Start
,Stop
-Start
);
261 signed long Result
= strtol(S
,&End
,10);
267 // TagSection::FindFlag - Locate a yes/no type flag /*{{{*/
268 // ---------------------------------------------------------------------
269 /* The bits marked in Flag are masked on/off in Flags */
270 bool pkgTagSection::FindFlag(const char *Tag
,unsigned long &Flags
,
275 if (Find(Tag
,Start
,Stop
) == false)
278 switch (StringToBool(string(Start
,Stop
)))
289 _error
->Warning("Unknown flag value");