]>
Commit | Line | Data |
---|---|---|
578bfd0a AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
0e66b144 | 3 | // $Id: tagfile.cc,v 1.16 1998/11/28 03:54:29 jgg Exp $ |
578bfd0a AL |
4 | /* ###################################################################### |
5 | ||
6 | Fast scanner for RFC-822 type header information | |
7 | ||
ad00ae81 | 8 | This uses a rotating buffer to load the package information into. |
578bfd0a AL |
9 | The scanner runs over it and isolates and indexes a single section. |
10 | ||
11 | ##################################################################### */ | |
12 | /*}}}*/ | |
13 | // Include Files /*{{{*/ | |
6c139d6e | 14 | #ifdef __GNUG__ |
094a497d | 15 | #pragma implementation "apt-pkg/tagfile.h" |
6c139d6e AL |
16 | #endif |
17 | ||
094a497d AL |
18 | #include <apt-pkg/tagfile.h> |
19 | #include <apt-pkg/error.h> | |
578bfd0a AL |
20 | |
21 | #include <string> | |
22 | #include <stdio.h> | |
23 | /*}}}*/ | |
24 | ||
25 | // TagFile::pkgTagFile - Constructor /*{{{*/ | |
26 | // --------------------------------------------------------------------- | |
27 | /* */ | |
8e06abb2 | 28 | pkgTagFile::pkgTagFile(FileFd &Fd,unsigned long Size) : Fd(Fd), Size(Size) |
578bfd0a | 29 | { |
ad00ae81 AL |
30 | Buffer = new char[Size]; |
31 | Start = End = Buffer; | |
578bfd0a | 32 | Left = Fd.Size(); |
dcb79bae | 33 | iOffset = 0; |
578bfd0a AL |
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 | } | |
dcb79bae AL |
50 | Start += Tag.size(); |
51 | iOffset += Tag.size(); | |
52 | ||
578bfd0a AL |
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 | { | |
ad00ae81 | 62 | unsigned long EndSize = End - Start; |
578bfd0a AL |
63 | |
64 | if (Left == 0) | |
65 | { | |
ad00ae81 | 66 | if (EndSize <= 1) |
578bfd0a AL |
67 | return false; |
68 | return true; | |
69 | } | |
70 | ||
ad00ae81 | 71 | memmove(Buffer,Start,EndSize); |
578bfd0a | 72 | Start = Buffer; |
ad00ae81 | 73 | End = Buffer + EndSize; |
578bfd0a | 74 | |
c88edf1d AL |
75 | // See if only a bit of the file is left |
76 | if (Left < Size - (End - Buffer)) | |
578bfd0a | 77 | { |
ad00ae81 | 78 | if (Fd.Read(End,Left) == false) |
578bfd0a | 79 | return false; |
c88edf1d | 80 | |
ad00ae81 | 81 | End += Left; |
578bfd0a AL |
82 | Left = 0; |
83 | } | |
84 | else | |
85 | { | |
ad00ae81 | 86 | if (Fd.Read(End,Size - (End - Buffer)) == false) |
578bfd0a | 87 | return false; |
c88edf1d | 88 | |
ad00ae81 AL |
89 | Left -= Size - (End - Buffer); |
90 | End = Buffer + Size; | |
578bfd0a AL |
91 | } |
92 | return true; | |
93 | } | |
94 | /*}}}*/ | |
ad00ae81 AL |
95 | // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/ |
96 | // --------------------------------------------------------------------- | |
03e39e59 AL |
97 | /* This jumps to a pre-recorded file location and reads the record |
98 | that is there */ | |
ad00ae81 AL |
99 | bool pkgTagFile::Jump(pkgTagSection &Tag,unsigned long Offset) |
100 | { | |
101 | iOffset = Offset; | |
102 | Left = Fd.Size() - Offset; | |
103 | if (Fd.Seek(Offset) == false) | |
104 | return false; | |
105 | End = Start = Buffer; | |
106 | ||
107 | if (Fill() == false) | |
108 | return false; | |
109 | ||
110 | if (Tag.Scan(Start,End - Start) == false) | |
111 | return _error->Error("Unable to parse package file"); | |
112 | return true; | |
113 | } | |
114 | /*}}}*/ | |
578bfd0a AL |
115 | // TagSection::Scan - Scan for the end of the header information /*{{{*/ |
116 | // --------------------------------------------------------------------- | |
117 | /* This looks for the first double new line in the data stream. It also | |
118 | indexes the tags in the section. */ | |
119 | bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength) | |
120 | { | |
121 | const char *End = Start + MaxLength; | |
122 | Stop = Section = Start; | |
123 | ||
124 | TagCount = 0; | |
125 | Indexes[TagCount++] = Stop - Section; | |
126 | Stop++; | |
127 | for (; Stop < End; Stop++) | |
128 | { | |
129 | if (Stop[-1] != '\n') | |
130 | continue; | |
0a8e3465 AL |
131 | |
132 | // Skip line feeds | |
133 | for (; Stop[0] == '\r' && Stop < End; Stop++); | |
134 | ||
578bfd0a AL |
135 | if (Stop[0] == '\n') |
136 | { | |
137 | // Extra one at the end to simplify find | |
138 | Indexes[TagCount] = Stop - Section; | |
0a8e3465 | 139 | for (; (Stop[0] == '\n' || Stop[0] == '\r') && Stop < End; Stop++); |
578bfd0a | 140 | return true; |
578bfd0a AL |
141 | } |
142 | ||
143 | if (isspace(Stop[0]) == 0) | |
144 | Indexes[TagCount++] = Stop - Section; | |
145 | ||
146 | // Just in case. | |
147 | if (TagCount > sizeof(Indexes)/sizeof(Indexes[0])) | |
148 | TagCount = sizeof(Indexes)/sizeof(Indexes[0]); | |
149 | } | |
150 | return false; | |
151 | } | |
152 | /*}}}*/ | |
153 | // TagSection::Find - Locate a tag /*{{{*/ | |
154 | // --------------------------------------------------------------------- | |
155 | /* This searches the section for a tag that matches the given string. */ | |
156 | bool pkgTagSection::Find(const char *Tag,const char *&Start, | |
157 | const char *&End) | |
158 | { | |
159 | unsigned int Length = strlen(Tag); | |
160 | for (unsigned int I = 0; I != TagCount; I++) | |
161 | { | |
162 | if (strncasecmp(Tag,Section + Indexes[I],Length) != 0) | |
163 | continue; | |
164 | ||
165 | // Make sure the colon is in the right place | |
166 | const char *C = Section + Length + Indexes[I]; | |
167 | for (; isspace(*C) != 0; C++); | |
168 | if (*C != ':') | |
169 | continue; | |
170 | ||
171 | // Strip off the gunk from the start end | |
172 | Start = C; | |
173 | End = Section + Indexes[I+1]; | |
174 | for (; (isspace(*Start) != 0 || *Start == ':') && Start < End; Start++); | |
175 | for (; isspace(End[-1]) != 0 && End > Start; End--); | |
0a8e3465 | 176 | |
578bfd0a AL |
177 | return true; |
178 | } | |
179 | Start = End = 0; | |
180 | return false; | |
181 | } | |
182 | /*}}}*/ | |
0e66b144 | 183 | // TagSection::FindS - Find a string /*{{{*/ |
a05599f1 AL |
184 | // --------------------------------------------------------------------- |
185 | /* */ | |
186 | string pkgTagSection::FindS(const char *Tag) | |
187 | { | |
188 | const char *Start; | |
189 | const char *End; | |
190 | if (Find(Tag,Start,End) == false) | |
191 | return string(); | |
192 | return string(Start,End); | |
193 | } | |
194 | /*}}}*/ | |
195 | // TagSection::FindI - Find an integer /*{{{*/ | |
196 | // --------------------------------------------------------------------- | |
197 | /* */ | |
198 | unsigned int pkgTagSection::FindI(const char *Tag) | |
199 | { | |
200 | const char *Start; | |
201 | const char *End; | |
202 | if (Find(Tag,Start,End) == false) | |
203 | return 0; | |
204 | ||
205 | return atoi(string(Start,End).c_str()); | |
206 | } | |
207 | /*}}}*/ | |
0e66b144 | 208 |