]>
git.saurik.com Git - apt.git/blob - apt-pkg/tagfile.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: tagfile.cc,v 1.9 1998/07/19 04:22: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>
25 // TagFile::pkgTagFile - Constructor /*{{{*/
26 // ---------------------------------------------------------------------
28 pkgTagFile::pkgTagFile(File
&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");
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
;
71 memmove(Buffer
,Start
,EndSize
);
73 End
= Buffer
+ EndSize
;
75 // See if only a bit of the file is left
78 if (Fd
.Read(End
,Left
) == false)
85 if (Fd
.Read(End
,Size
- (End
- Buffer
)) == false)
87 Left
-= Size
- (End
- Buffer
);
93 // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/
94 // ---------------------------------------------------------------------
95 /* This jumps to a pre-recorded file location and */
96 bool pkgTagFile::Jump(pkgTagSection
&Tag
,unsigned long Offset
)
99 Left
= Fd
.Size() - Offset
;
100 if (Fd
.Seek(Offset
) == false)
102 End
= Start
= Buffer
;
107 if (Tag
.Scan(Start
,End
- Start
) == false)
108 return _error
->Error("Unable to parse package file");
112 // TagSection::Scan - Scan for the end of the header information /*{{{*/
113 // ---------------------------------------------------------------------
114 /* This looks for the first double new line in the data stream. It also
115 indexes the tags in the section. */
116 bool pkgTagSection::Scan(const char *Start
,unsigned long MaxLength
)
118 const char *End
= Start
+ MaxLength
;
119 Stop
= Section
= Start
;
122 Indexes
[TagCount
++] = Stop
- Section
;
124 for (; Stop
< End
; Stop
++)
126 if (Stop
[-1] != '\n')
130 // Extra one at the end to simplify find
131 Indexes
[TagCount
] = Stop
- Section
;
132 for (; Stop
[0] == '\n' && Stop
< End
; Stop
++);
137 if (isspace(Stop
[0]) == 0)
138 Indexes
[TagCount
++] = Stop
- Section
;
141 if (TagCount
> sizeof(Indexes
)/sizeof(Indexes
[0]))
142 TagCount
= sizeof(Indexes
)/sizeof(Indexes
[0]);
147 // TagSection::Find - Locate a tag /*{{{*/
148 // ---------------------------------------------------------------------
149 /* This searches the section for a tag that matches the given string. */
150 bool pkgTagSection::Find(const char *Tag
,const char *&Start
,
153 unsigned int Length
= strlen(Tag
);
154 for (unsigned int I
= 0; I
!= TagCount
; I
++)
156 if (strncasecmp(Tag
,Section
+ Indexes
[I
],Length
) != 0)
159 // Make sure the colon is in the right place
160 const char *C
= Section
+ Length
+ Indexes
[I
];
161 for (; isspace(*C
) != 0; C
++);
165 // Strip off the gunk from the start end
167 End
= Section
+ Indexes
[I
+1];
168 for (; (isspace(*Start
) != 0 || *Start
== ':') && Start
< End
; Start
++);
169 for (; isspace(End
[-1]) != 0 && End
> Start
; End
--);