1 // -*- mode: cpp; mode: fold -*-
3 // $Id: tagfile.cc,v 1.7 1998/07/12 23:58:39 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 "apt-pkg/tagfile.h"
18 #include <apt-pkg/tagfile.h>
19 #include <apt-pkg/error.h>
20 #include <apt-pkg/init.h>
26 // TagFile::pkgTagFile - Constructor /*{{{*/
27 // ---------------------------------------------------------------------
29 pkgTagFile::pkgTagFile(File
&Fd
) : Fd(Fd
)
31 Buffer
= new char[64*1024];
32 Start
= End
= Buffer
+ 64*1024;
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");
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 Size
= End
- Start
;
72 memmove(Buffer
,Start
,Size
);
75 // See if only a bit of the file is left or if
76 if (Left
< End
- Buffer
- Size
)
78 if (Fd
.Read(Buffer
+ Size
,Left
) == false)
80 End
= Buffer
+ Size
+ Left
;
85 if (Fd
.Read(Buffer
+ Size
, End
- Buffer
- Size
) == false)
87 Left
-= End
- Buffer
- Size
;
92 // TagSection::Scan - Scan for the end of the header information /*{{{*/
93 // ---------------------------------------------------------------------
94 /* This looks for the first double new line in the data stream. It also
95 indexes the tags in the section. */
96 bool pkgTagSection::Scan(const char *Start
,unsigned long MaxLength
)
98 const char *End
= Start
+ MaxLength
;
99 Stop
= Section
= Start
;
102 Indexes
[TagCount
++] = Stop
- Section
;
104 for (; Stop
< End
; Stop
++)
106 if (Stop
[-1] != '\n')
110 // Extra one at the end to simplify find
111 Indexes
[TagCount
] = Stop
- Section
;
112 for (; Stop
[0] == '\n' && Stop
< End
; Stop
++);
117 if (isspace(Stop
[0]) == 0)
118 Indexes
[TagCount
++] = Stop
- Section
;
121 if (TagCount
> sizeof(Indexes
)/sizeof(Indexes
[0]))
122 TagCount
= sizeof(Indexes
)/sizeof(Indexes
[0]);
127 // TagSection::Find - Locate a tag /*{{{*/
128 // ---------------------------------------------------------------------
129 /* This searches the section for a tag that matches the given string. */
130 bool pkgTagSection::Find(const char *Tag
,const char *&Start
,
133 unsigned int Length
= strlen(Tag
);
134 for (unsigned int I
= 0; I
!= TagCount
; I
++)
136 if (strncasecmp(Tag
,Section
+ Indexes
[I
],Length
) != 0)
139 // Make sure the colon is in the right place
140 const char *C
= Section
+ Length
+ Indexes
[I
];
141 for (; isspace(*C
) != 0; C
++);
145 // Strip off the gunk from the start end
147 End
= Section
+ Indexes
[I
+1];
148 for (; (isspace(*Start
) != 0 || *Start
== ':') && Start
< End
; Start
++);
149 for (; isspace(End
[-1]) != 0 && End
> Start
; End
--);