1 // -*- mode: cpp; mode: fold -*-
3 // $Id: tagfile.cc,v 1.8 1998/07/16 06:08: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>
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
--);