]>
git.saurik.com Git - apt.git/blob - apt-pkg/tagfile.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: tagfile.cc,v 1.18 1998/12/08 05:24:41 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>
25 // TagFile::pkgTagFile - Constructor /*{{{*/
26 // ---------------------------------------------------------------------
28 pkgTagFile::pkgTagFile(FileFd
&Fd
,unsigned long Size
) : Fd(Fd
), Size(Size
)
30 Buffer
= new char[Size
];
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 %s",Fd
.Name().c_str());
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 EndSize
= End
- Start
;
64 memmove(Buffer
,Start
,EndSize
);
66 End
= Buffer
+ EndSize
;
72 if (Size
- (End
- Buffer
) < 4)
75 // Append a double new line if one does not exist
76 unsigned int LineCount
= 0;
77 for (const char *E
= End
- 1; E
- End
< 6 && (*E
== '\n' || *E
== '\r'); E
--)
80 for (; LineCount
< 2; LineCount
++)
86 // See if only a bit of the file is left
87 if (Left
< Size
- (End
- Buffer
))
89 if (Fd
.Read(End
,Left
) == false)
97 if (Fd
.Read(End
,Size
- (End
- Buffer
)) == false)
100 Left
-= Size
- (End
- Buffer
);
106 // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/
107 // ---------------------------------------------------------------------
108 /* This jumps to a pre-recorded file location and reads the record
110 bool pkgTagFile::Jump(pkgTagSection
&Tag
,unsigned long Offset
)
113 Left
= Fd
.Size() - Offset
;
114 if (Fd
.Seek(Offset
) == false)
116 End
= Start
= Buffer
;
121 if (Tag
.Scan(Start
,End
- Start
) == false)
122 return _error
->Error("Unable to parse package file");
126 // TagSection::Scan - Scan for the end of the header information /*{{{*/
127 // ---------------------------------------------------------------------
128 /* This looks for the first double new line in the data stream. It also
129 indexes the tags in the section. This very simple hash function for the
130 first 3 letters gives very good performance on the debian package files */
131 bool pkgTagSection::Scan(const char *Start
,unsigned long MaxLength
)
133 const char *End
= Start
+ MaxLength
;
134 Stop
= Section
= Start
;
135 memset(AlphaIndexes
,0,sizeof(AlphaIndexes
));
141 while (TagCount
< sizeof(Indexes
)/sizeof(Indexes
[0]))
143 if (isspace(Stop
[0]) == 0)
145 Indexes
[TagCount
++] = Stop
- Section
;
146 unsigned char A
= tolower(Stop
[0]) - 'a';
147 unsigned char B
= tolower(Stop
[1]) - 'a';
148 unsigned char C
= tolower(Stop
[3]) - 'a';
149 AlphaIndexes
[((A
+ C
/3)%26
) + 26*((B
+ C
/2)%26
)] = TagCount
;
152 Stop
= (const char *)memchr(Stop
,'\n',End
- Stop
);
156 for (; Stop
[1] == '\r' && Stop
< End
; Stop
++);
160 Indexes
[TagCount
] = Stop
- Section
;
161 for (; (Stop
[0] == '\n' || Stop
[0] == '\r') && Stop
< End
; Stop
++);
171 // TagSection::Find - Locate a tag /*{{{*/
172 // ---------------------------------------------------------------------
173 /* This searches the section for a tag that matches the given string. */
174 bool pkgTagSection::Find(const char *Tag
,const char *&Start
,
177 unsigned int Length
= strlen(Tag
);
178 unsigned char A
= tolower(Tag
[0]) - 'a';
179 unsigned char B
= tolower(Tag
[1]) - 'a';
180 unsigned char C
= tolower(Tag
[3]) - 'a';
181 unsigned int I
= AlphaIndexes
[((A
+ C
/3)%26
) + 26*((B
+ C
/2)%26
)];
186 for (unsigned int Counter
= 0; Counter
!= TagCount
; Counter
++,
190 St
= Section
+ Indexes
[I
];
191 if (strncasecmp(Tag
,St
,Length
) != 0)
194 // Make sure the colon is in the right place
195 const char *C
= St
+ Length
;
196 for (; isspace(*C
) != 0; C
++);
200 // Strip off the gunk from the start end
202 End
= Section
+ Indexes
[I
+1];
203 for (; (isspace(*Start
) != 0 || *Start
== ':') && Start
< End
; Start
++);
204 for (; isspace(End
[-1]) != 0 && End
> Start
; End
--);
213 // TagSection::FindS - Find a string /*{{{*/
214 // ---------------------------------------------------------------------
216 string
pkgTagSection::FindS(const char *Tag
)
220 if (Find(Tag
,Start
,End
) == false)
222 return string(Start
,End
);
225 // TagSection::FindI - Find an integer /*{{{*/
226 // ---------------------------------------------------------------------
228 unsigned int pkgTagSection::FindI(const char *Tag
)
232 if (Find(Tag
,Start
,End
) == false)
235 return atoi(string(Start
,End
).c_str());