Working cache generator
[ntk/apt.git] / apt-pkg / tagfile.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: tagfile.cc,v 1.8 1998/07/16 06:08:39 jgg Exp $
4 /* ######################################################################
5
6 Fast scanner for RFC-822 type header information
7
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.
10
11 ##################################################################### */
12 /*}}}*/
13 // Include Files /*{{{*/
14 #ifdef __GNUG__
15 #pragma implementation "apt-pkg/tagfile.h"
16 #endif
17
18 #include <apt-pkg/tagfile.h>
19 #include <apt-pkg/error.h>
20
21 #include <string>
22 #include <stdio.h>
23 /*}}}*/
24
25 // TagFile::pkgTagFile - Constructor /*{{{*/
26 // ---------------------------------------------------------------------
27 /* */
28 pkgTagFile::pkgTagFile(File &Fd) : Fd(Fd)
29 {
30 Buffer = new char[64*1024];
31 Start = End = Buffer + 64*1024;
32 Left = Fd.Size();
33 iOffset = 0;
34 Fill();
35 }
36 /*}}}*/
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)
41 {
42 if (Tag.Scan(Start,End - Start) == false)
43 {
44 if (Fill() == false)
45 return false;
46
47 if (Tag.Scan(Start,End - Start) == false)
48 return _error->Error("Unable to parse package file");
49 }
50 Start += Tag.size();
51 iOffset += Tag.size();
52
53 return true;
54 }
55 /*}}}*/
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()
61 {
62 unsigned long Size = End - Start;
63
64 if (Left == 0)
65 {
66 if (Size <= 1)
67 return false;
68 return true;
69 }
70
71 memmove(Buffer,Start,Size);
72 Start = Buffer;
73
74 // See if only a bit of the file is left or if
75 if (Left < End - Buffer - Size)
76 {
77 if (Fd.Read(Buffer + Size,Left) == false)
78 return false;
79 End = Buffer + Size + Left;
80 Left = 0;
81 }
82 else
83 {
84 if (Fd.Read(Buffer + Size, End - Buffer - Size) == false)
85 return false;
86 Left -= End - Buffer - Size;
87 }
88 return true;
89 }
90 /*}}}*/
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)
96 {
97 const char *End = Start + MaxLength;
98 Stop = Section = Start;
99
100 TagCount = 0;
101 Indexes[TagCount++] = Stop - Section;
102 Stop++;
103 for (; Stop < End; Stop++)
104 {
105 if (Stop[-1] != '\n')
106 continue;
107 if (Stop[0] == '\n')
108 {
109 // Extra one at the end to simplify find
110 Indexes[TagCount] = Stop - Section;
111 for (; Stop[0] == '\n' && Stop < End; Stop++);
112 return true;
113 break;
114 }
115
116 if (isspace(Stop[0]) == 0)
117 Indexes[TagCount++] = Stop - Section;
118
119 // Just in case.
120 if (TagCount > sizeof(Indexes)/sizeof(Indexes[0]))
121 TagCount = sizeof(Indexes)/sizeof(Indexes[0]);
122 }
123 return false;
124 }
125 /*}}}*/
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,
130 const char *&End)
131 {
132 unsigned int Length = strlen(Tag);
133 for (unsigned int I = 0; I != TagCount; I++)
134 {
135 if (strncasecmp(Tag,Section + Indexes[I],Length) != 0)
136 continue;
137
138 // Make sure the colon is in the right place
139 const char *C = Section + Length + Indexes[I];
140 for (; isspace(*C) != 0; C++);
141 if (*C != ':')
142 continue;
143
144 // Strip off the gunk from the start end
145 Start = C;
146 End = Section + Indexes[I+1];
147 for (; (isspace(*Start) != 0 || *Start == ':') && Start < End; Start++);
148 for (; isspace(End[-1]) != 0 && End > Start; End--);
149 return true;
150 }
151 Start = End = 0;
152 return false;
153 }
154 /*}}}*/