]>
Commit | Line | Data |
---|---|---|
578bfd0a AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
b3d44315 | 3 | // $Id: tagfile.cc,v 1.37.2.2 2003/12/31 16:02:30 mdz 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 /*{{{*/ | |
ea542140 DK |
14 | #include<config.h> |
15 | ||
094a497d AL |
16 | #include <apt-pkg/tagfile.h> |
17 | #include <apt-pkg/error.h> | |
cdcc6d34 | 18 | #include <apt-pkg/strutl.h> |
578bfd0a AL |
19 | |
20 | #include <string> | |
21 | #include <stdio.h> | |
851a45a8 | 22 | #include <ctype.h> |
ea542140 DK |
23 | |
24 | #include <apti18n.h> | |
578bfd0a AL |
25 | /*}}}*/ |
26 | ||
851a45a8 AL |
27 | using std::string; |
28 | ||
1abbce9e MV |
29 | class pkgTagFilePrivate |
30 | { | |
31 | public: | |
32 | pkgTagFilePrivate(FileFd *pFd, unsigned long Size) : Fd(*pFd), Size(Size) | |
33 | { | |
34 | } | |
35 | FileFd &Fd; | |
36 | char *Buffer; | |
37 | char *Start; | |
38 | char *End; | |
39 | bool Done; | |
40 | unsigned long iOffset; | |
41 | unsigned long Size; | |
42 | }; | |
43 | ||
578bfd0a AL |
44 | // TagFile::pkgTagFile - Constructor /*{{{*/ |
45 | // --------------------------------------------------------------------- | |
46 | /* */ | |
1abbce9e | 47 | pkgTagFile::pkgTagFile(FileFd *pFd,unsigned long Size) |
578bfd0a | 48 | { |
1abbce9e MV |
49 | d = new pkgTagFilePrivate(pFd, Size); |
50 | ||
51 | if (d->Fd.IsOpen() == false) | |
0e72dd52 | 52 | { |
1abbce9e MV |
53 | d->Start = d->End = d->Buffer = 0; |
54 | d->Done = true; | |
55 | d->iOffset = 0; | |
0e72dd52 AL |
56 | return; |
57 | } | |
58 | ||
1abbce9e MV |
59 | d->Buffer = new char[Size]; |
60 | d->Start = d->End = d->Buffer; | |
61 | d->Done = false; | |
62 | d->iOffset = 0; | |
2ca99a0d | 63 | Fill(); |
578bfd0a AL |
64 | } |
65 | /*}}}*/ | |
b2e465d6 | 66 | // TagFile::~pkgTagFile - Destructor /*{{{*/ |
29f7b36c AL |
67 | // --------------------------------------------------------------------- |
68 | /* */ | |
69 | pkgTagFile::~pkgTagFile() | |
70 | { | |
1abbce9e MV |
71 | delete [] d->Buffer; |
72 | delete d; | |
73 | } | |
74 | /*}}}*/ | |
75 | // TagFile::Offset - Return the current offset in the buffer /*{{{*/ | |
76 | unsigned long pkgTagFile::Offset() | |
77 | { | |
78 | return d->iOffset; | |
29f7b36c AL |
79 | } |
80 | /*}}}*/ | |
75c541fd | 81 | // TagFile::Resize - Resize the internal buffer /*{{{*/ |
578bfd0a | 82 | // --------------------------------------------------------------------- |
75c541fd MV |
83 | /* Resize the internal buffer (double it in size). Fail if a maximum size |
84 | * size is reached. | |
85 | */ | |
86 | bool pkgTagFile::Resize() | |
578bfd0a | 87 | { |
75c541fd | 88 | char *tmp; |
1abbce9e | 89 | unsigned long EndSize = d->End - d->Start; |
75c541fd MV |
90 | |
91 | // fail is the buffer grows too big | |
1abbce9e | 92 | if(d->Size > 1024*1024+1) |
432b168c MV |
93 | return false; |
94 | ||
75c541fd | 95 | // get new buffer and use it |
1abbce9e MV |
96 | tmp = new char[2*d->Size]; |
97 | memcpy(tmp, d->Buffer, d->Size); | |
98 | d->Size = d->Size*2; | |
99 | delete [] d->Buffer; | |
100 | d->Buffer = tmp; | |
99c2e5ac | 101 | |
75c541fd | 102 | // update the start/end pointers to the new buffer |
1abbce9e MV |
103 | d->Start = d->Buffer; |
104 | d->End = d->Start + EndSize; | |
75c541fd MV |
105 | return true; |
106 | } | |
81e9789b | 107 | /*}}}*/ |
578bfd0a AL |
108 | // TagFile::Step - Advance to the next section /*{{{*/ |
109 | // --------------------------------------------------------------------- | |
75c541fd MV |
110 | /* If the Section Scanner fails we refill the buffer and try again. |
111 | * If that fails too, double the buffer size and try again until a | |
112 | * maximum buffer is reached. | |
113 | */ | |
578bfd0a AL |
114 | bool pkgTagFile::Step(pkgTagSection &Tag) |
115 | { | |
1abbce9e | 116 | while (Tag.Scan(d->Start,d->End - d->Start) == false) |
0852eaef | 117 | { |
99c2e5ac MV |
118 | if (Fill() == false) |
119 | return false; | |
120 | ||
1abbce9e | 121 | if(Tag.Scan(d->Start,d->End - d->Start)) |
75c541fd MV |
122 | break; |
123 | ||
124 | if (Resize() == false) | |
99c2e5ac | 125 | return _error->Error(_("Unable to parse package file %s (1)"), |
1abbce9e | 126 | d->Fd.Name().c_str()); |
613f9499 | 127 | } |
1abbce9e MV |
128 | d->Start += Tag.size(); |
129 | d->iOffset += Tag.size(); | |
b2e465d6 AL |
130 | |
131 | Tag.Trim(); | |
99c2e5ac MV |
132 | return true; |
133 | } | |
134 | /*}}}*/ | |
135 | // TagFile::Fill - Top up the buffer /*{{{*/ | |
136 | // --------------------------------------------------------------------- | |
137 | /* This takes the bit at the end of the buffer and puts it at the start | |
138 | then fills the rest from the file */ | |
139 | bool pkgTagFile::Fill() | |
140 | { | |
1abbce9e | 141 | unsigned long EndSize = d->End - d->Start; |
99c2e5ac MV |
142 | unsigned long Actual = 0; |
143 | ||
1abbce9e MV |
144 | memmove(d->Buffer,d->Start,EndSize); |
145 | d->Start = d->Buffer; | |
146 | d->End = d->Buffer + EndSize; | |
99c2e5ac | 147 | |
1abbce9e | 148 | if (d->Done == false) |
99c2e5ac MV |
149 | { |
150 | // See if only a bit of the file is left | |
1abbce9e | 151 | if (d->Fd.Read(d->End, d->Size - (d->End - d->Buffer),&Actual) == false) |
99c2e5ac | 152 | return false; |
1abbce9e MV |
153 | if (Actual != d->Size - (d->End - d->Buffer)) |
154 | d->Done = true; | |
155 | d->End += Actual; | |
99c2e5ac MV |
156 | } |
157 | ||
1abbce9e | 158 | if (d->Done == true) |
99c2e5ac MV |
159 | { |
160 | if (EndSize <= 3 && Actual == 0) | |
161 | return false; | |
1abbce9e | 162 | if (d->Size - (d->End - d->Buffer) < 4) |
99c2e5ac MV |
163 | return true; |
164 | ||
165 | // Append a double new line if one does not exist | |
166 | unsigned int LineCount = 0; | |
1abbce9e | 167 | for (const char *E = d->End - 1; E - d->End < 6 && (*E == '\n' || *E == '\r'); E--) |
99c2e5ac MV |
168 | if (*E == '\n') |
169 | LineCount++; | |
170 | for (; LineCount < 2; LineCount++) | |
1abbce9e | 171 | *d->End++ = '\n'; |
99c2e5ac MV |
172 | |
173 | return true; | |
174 | } | |
175 | ||
578bfd0a AL |
176 | return true; |
177 | } | |
178 | /*}}}*/ | |
ad00ae81 AL |
179 | // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/ |
180 | // --------------------------------------------------------------------- | |
03e39e59 AL |
181 | /* This jumps to a pre-recorded file location and reads the record |
182 | that is there */ | |
ad00ae81 AL |
183 | bool pkgTagFile::Jump(pkgTagSection &Tag,unsigned long Offset) |
184 | { | |
b2e465d6 | 185 | // We are within a buffer space of the next hit.. |
1abbce9e | 186 | if (Offset >= d->iOffset && d->iOffset + (d->End - d->Start) > Offset) |
b2e465d6 | 187 | { |
1abbce9e MV |
188 | unsigned long Dist = Offset - d->iOffset; |
189 | d->Start += Dist; | |
190 | d->iOffset += Dist; | |
b2e465d6 AL |
191 | return Step(Tag); |
192 | } | |
193 | ||
2ca99a0d | 194 | // Reposition and reload.. |
1abbce9e MV |
195 | d->iOffset = Offset; |
196 | d->Done = false; | |
197 | if (d->Fd.Seek(Offset) == false) | |
2ca99a0d | 198 | return false; |
1abbce9e | 199 | d->End = d->Start = d->Buffer; |
99c2e5ac | 200 | |
2ca99a0d MV |
201 | if (Fill() == false) |
202 | return false; | |
99c2e5ac | 203 | |
1abbce9e | 204 | if (Tag.Scan(d->Start, d->End - d->Start) == true) |
2ca99a0d MV |
205 | return true; |
206 | ||
207 | // This appends a double new line (for the real eof handling) | |
208 | if (Fill() == false) | |
209 | return false; | |
0852eaef | 210 | |
1abbce9e MV |
211 | if (Tag.Scan(d->Start, d->End - d->Start) == false) |
212 | return _error->Error(_("Unable to parse package file %s (2)"),d->Fd.Name().c_str()); | |
06bba740 | 213 | |
ad00ae81 AL |
214 | return true; |
215 | } | |
216 | /*}}}*/ | |
578bfd0a AL |
217 | // TagSection::Scan - Scan for the end of the header information /*{{{*/ |
218 | // --------------------------------------------------------------------- | |
c176c4d0 DK |
219 | /* This looks for the first double new line in the data stream. |
220 | It also indexes the tags in the section. */ | |
0852eaef | 221 | bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength) |
578bfd0a AL |
222 | { |
223 | const char *End = Start + MaxLength; | |
224 | Stop = Section = Start; | |
c1a22377 | 225 | memset(AlphaIndexes,0,sizeof(AlphaIndexes)); |
c7b5ce1c | 226 | |
2ca99a0d | 227 | if (Stop == 0) |
0852eaef | 228 | return false; |
81e9789b | 229 | |
578bfd0a | 230 | TagCount = 0; |
fd71171a | 231 | while (TagCount+1 < sizeof(Indexes)/sizeof(Indexes[0]) && Stop < End) |
578bfd0a | 232 | { |
81e9789b MV |
233 | TrimRecord(true,End); |
234 | ||
90d64280 | 235 | // Start a new index and add it to the hash |
c1a22377 AL |
236 | if (isspace(Stop[0]) == 0) |
237 | { | |
238 | Indexes[TagCount++] = Stop - Section; | |
b2e465d6 | 239 | AlphaIndexes[AlphaHash(Stop,End)] = TagCount; |
c1a22377 | 240 | } |
0a8e3465 | 241 | |
c1a22377 | 242 | Stop = (const char *)memchr(Stop,'\n',End - Stop); |
0a8e3465 | 243 | |
c1a22377 | 244 | if (Stop == 0) |
0852eaef | 245 | return false; |
81e9789b | 246 | |
b8b7c37d | 247 | for (; Stop+1 < End && Stop[1] == '\r'; Stop++); |
c1a22377 | 248 | |
f3bcc383 AL |
249 | // Double newline marks the end of the record |
250 | if (Stop+1 < End && Stop[1] == '\n') | |
578bfd0a | 251 | { |
578bfd0a | 252 | Indexes[TagCount] = Stop - Section; |
81e9789b | 253 | TrimRecord(false,End); |
0852eaef | 254 | return true; |
578bfd0a AL |
255 | } |
256 | ||
c1a22377 AL |
257 | Stop++; |
258 | } | |
138d4b3d | 259 | |
0852eaef | 260 | return false; |
578bfd0a AL |
261 | } |
262 | /*}}}*/ | |
81e9789b MV |
263 | // TagSection::TrimRecord - Trim off any garbage before/after a record /*{{{*/ |
264 | // --------------------------------------------------------------------- | |
265 | /* There should be exactly 2 newline at the end of the record, no more. */ | |
266 | void pkgTagSection::TrimRecord(bool BeforeRecord, const char*& End) | |
267 | { | |
268 | if (BeforeRecord == true) | |
269 | return; | |
270 | for (; Stop < End && (Stop[0] == '\n' || Stop[0] == '\r'); Stop++); | |
271 | } | |
272 | /*}}}*/ | |
b2e465d6 AL |
273 | // TagSection::Trim - Trim off any trailing garbage /*{{{*/ |
274 | // --------------------------------------------------------------------- | |
275 | /* There should be exactly 1 newline at the end of the buffer, no more. */ | |
276 | void pkgTagSection::Trim() | |
277 | { | |
278 | for (; Stop > Section + 2 && (Stop[-2] == '\n' || Stop[-2] == '\r'); Stop--); | |
279 | } | |
280 | /*}}}*/ | |
578bfd0a AL |
281 | // TagSection::Find - Locate a tag /*{{{*/ |
282 | // --------------------------------------------------------------------- | |
283 | /* This searches the section for a tag that matches the given string. */ | |
b2e465d6 | 284 | bool pkgTagSection::Find(const char *Tag,unsigned &Pos) const |
578bfd0a AL |
285 | { |
286 | unsigned int Length = strlen(Tag); | |
b2e465d6 | 287 | unsigned int I = AlphaIndexes[AlphaHash(Tag)]; |
c1a22377 AL |
288 | if (I == 0) |
289 | return false; | |
290 | I--; | |
291 | ||
292 | for (unsigned int Counter = 0; Counter != TagCount; Counter++, | |
293 | I = (I+1)%TagCount) | |
578bfd0a | 294 | { |
c1a22377 AL |
295 | const char *St; |
296 | St = Section + Indexes[I]; | |
297 | if (strncasecmp(Tag,St,Length) != 0) | |
578bfd0a AL |
298 | continue; |
299 | ||
b2e465d6 AL |
300 | // Make sure the colon is in the right place |
301 | const char *C = St + Length; | |
302 | for (; isspace(*C) != 0; C++); | |
303 | if (*C != ':') | |
304 | continue; | |
305 | Pos = I; | |
306 | return true; | |
307 | } | |
308 | ||
309 | Pos = 0; | |
310 | return false; | |
311 | } | |
312 | /*}}}*/ | |
313 | // TagSection::Find - Locate a tag /*{{{*/ | |
314 | // --------------------------------------------------------------------- | |
315 | /* This searches the section for a tag that matches the given string. */ | |
316 | bool pkgTagSection::Find(const char *Tag,const char *&Start, | |
317 | const char *&End) const | |
318 | { | |
319 | unsigned int Length = strlen(Tag); | |
320 | unsigned int I = AlphaIndexes[AlphaHash(Tag)]; | |
321 | if (I == 0) | |
322 | return false; | |
323 | I--; | |
324 | ||
325 | for (unsigned int Counter = 0; Counter != TagCount; Counter++, | |
326 | I = (I+1)%TagCount) | |
327 | { | |
328 | const char *St; | |
329 | St = Section + Indexes[I]; | |
330 | if (strncasecmp(Tag,St,Length) != 0) | |
331 | continue; | |
332 | ||
578bfd0a | 333 | // Make sure the colon is in the right place |
c1a22377 | 334 | const char *C = St + Length; |
578bfd0a AL |
335 | for (; isspace(*C) != 0; C++); |
336 | if (*C != ':') | |
337 | continue; | |
338 | ||
339 | // Strip off the gunk from the start end | |
340 | Start = C; | |
341 | End = Section + Indexes[I+1]; | |
06bba740 AL |
342 | if (Start >= End) |
343 | return _error->Error("Internal parsing error"); | |
344 | ||
578bfd0a AL |
345 | for (; (isspace(*Start) != 0 || *Start == ':') && Start < End; Start++); |
346 | for (; isspace(End[-1]) != 0 && End > Start; End--); | |
06bba740 | 347 | |
578bfd0a AL |
348 | return true; |
349 | } | |
c1a22377 | 350 | |
578bfd0a AL |
351 | Start = End = 0; |
352 | return false; | |
353 | } | |
354 | /*}}}*/ | |
0e66b144 | 355 | // TagSection::FindS - Find a string /*{{{*/ |
a05599f1 AL |
356 | // --------------------------------------------------------------------- |
357 | /* */ | |
b2e465d6 | 358 | string pkgTagSection::FindS(const char *Tag) const |
a05599f1 AL |
359 | { |
360 | const char *Start; | |
361 | const char *End; | |
362 | if (Find(Tag,Start,End) == false) | |
363 | return string(); | |
364 | return string(Start,End); | |
365 | } | |
366 | /*}}}*/ | |
367 | // TagSection::FindI - Find an integer /*{{{*/ | |
368 | // --------------------------------------------------------------------- | |
369 | /* */ | |
b2e465d6 | 370 | signed int pkgTagSection::FindI(const char *Tag,signed long Default) const |
a05599f1 AL |
371 | { |
372 | const char *Start; | |
b0b4efb9 AL |
373 | const char *Stop; |
374 | if (Find(Tag,Start,Stop) == false) | |
375 | return Default; | |
376 | ||
377 | // Copy it into a temp buffer so we can use strtol | |
378 | char S[300]; | |
379 | if ((unsigned)(Stop - Start) >= sizeof(S)) | |
380 | return Default; | |
381 | strncpy(S,Start,Stop-Start); | |
382 | S[Stop - Start] = 0; | |
383 | ||
384 | char *End; | |
385 | signed long Result = strtol(S,&End,10); | |
386 | if (S == End) | |
387 | return Default; | |
388 | return Result; | |
389 | } | |
390 | /*}}}*/ | |
e2c66de5 DK |
391 | // TagSection::FindULL - Find an unsigned long long integer /*{{{*/ |
392 | // --------------------------------------------------------------------- | |
393 | /* */ | |
394 | unsigned long long pkgTagSection::FindULL(const char *Tag, unsigned long long const &Default) const | |
395 | { | |
396 | const char *Start; | |
397 | const char *Stop; | |
398 | if (Find(Tag,Start,Stop) == false) | |
399 | return Default; | |
400 | ||
401 | // Copy it into a temp buffer so we can use strtoull | |
402 | char S[100]; | |
403 | if ((unsigned)(Stop - Start) >= sizeof(S)) | |
404 | return Default; | |
405 | strncpy(S,Start,Stop-Start); | |
406 | S[Stop - Start] = 0; | |
407 | ||
408 | char *End; | |
409 | unsigned long long Result = strtoull(S,&End,10); | |
410 | if (S == End) | |
411 | return Default; | |
412 | return Result; | |
413 | } | |
414 | /*}}}*/ | |
b0b4efb9 AL |
415 | // TagSection::FindFlag - Locate a yes/no type flag /*{{{*/ |
416 | // --------------------------------------------------------------------- | |
417 | /* The bits marked in Flag are masked on/off in Flags */ | |
418 | bool pkgTagSection::FindFlag(const char *Tag,unsigned long &Flags, | |
b2e465d6 | 419 | unsigned long Flag) const |
b0b4efb9 AL |
420 | { |
421 | const char *Start; | |
422 | const char *Stop; | |
423 | if (Find(Tag,Start,Stop) == false) | |
424 | return true; | |
fe0f7911 DK |
425 | return FindFlag(Flags, Flag, Start, Stop); |
426 | } | |
427 | bool const pkgTagSection::FindFlag(unsigned long &Flags, unsigned long Flag, | |
428 | char const* Start, char const* Stop) | |
429 | { | |
430 | switch (StringToBool(string(Start, Stop))) | |
431 | { | |
b0b4efb9 AL |
432 | case 0: |
433 | Flags &= ~Flag; | |
434 | return true; | |
435 | ||
436 | case 1: | |
437 | Flags |= Flag; | |
438 | return true; | |
439 | ||
440 | default: | |
b2e465d6 | 441 | _error->Warning("Unknown flag value: %s",string(Start,Stop).c_str()); |
b0b4efb9 AL |
442 | return true; |
443 | } | |
444 | return true; | |
a05599f1 AL |
445 | } |
446 | /*}}}*/ | |
b2e465d6 AL |
447 | // TFRewrite - Rewrite a control record /*{{{*/ |
448 | // --------------------------------------------------------------------- | |
449 | /* This writes the control record to stdout rewriting it as necessary. The | |
450 | override map item specificies the rewriting rules to follow. This also | |
451 | takes the time to sort the feild list. */ | |
452 | ||
453 | /* The order of this list is taken from dpkg source lib/parse.c the fieldinfos | |
454 | array. */ | |
455 | static const char *iTFRewritePackageOrder[] = { | |
456 | "Package", | |
457 | "Essential", | |
458 | "Status", | |
459 | "Priority", | |
460 | "Section", | |
461 | "Installed-Size", | |
462 | "Maintainer", | |
47e7ebb3 | 463 | "Original-Maintainer", |
b2e465d6 AL |
464 | "Architecture", |
465 | "Source", | |
466 | "Version", | |
467 | "Revision", // Obsolete | |
468 | "Config-Version", // Obsolete | |
469 | "Replaces", | |
470 | "Provides", | |
471 | "Depends", | |
472 | "Pre-Depends", | |
473 | "Recommends", | |
474 | "Suggests", | |
475 | "Conflicts", | |
308c7d30 | 476 | "Breaks", |
b2e465d6 AL |
477 | "Conffiles", |
478 | "Filename", | |
479 | "Size", | |
480 | "MD5Sum", | |
cde41ae8 MV |
481 | "SHA1", |
482 | "SHA256", | |
d9b9e9e2 | 483 | "SHA512", |
b2e465d6 AL |
484 | "MSDOS-Filename", // Obsolete |
485 | "Description", | |
486 | 0}; | |
487 | static const char *iTFRewriteSourceOrder[] = {"Package", | |
488 | "Source", | |
489 | "Binary", | |
490 | "Version", | |
491 | "Priority", | |
492 | "Section", | |
493 | "Maintainer", | |
47e7ebb3 | 494 | "Original-Maintainer", |
b2e465d6 AL |
495 | "Build-Depends", |
496 | "Build-Depends-Indep", | |
497 | "Build-Conflicts", | |
498 | "Build-Conflicts-Indep", | |
499 | "Architecture", | |
500 | "Standards-Version", | |
501 | "Format", | |
502 | "Directory", | |
503 | "Files", | |
504 | 0}; | |
505 | ||
506 | /* Two levels of initialization are used because gcc will set the symbol | |
507 | size of an array to the length of the array, causing dynamic relinking | |
508 | errors. Doing this makes the symbol size constant */ | |
509 | const char **TFRewritePackageOrder = iTFRewritePackageOrder; | |
510 | const char **TFRewriteSourceOrder = iTFRewriteSourceOrder; | |
511 | ||
512 | bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[], | |
513 | TFRewriteData *Rewrite) | |
514 | { | |
515 | unsigned char Visited[256]; // Bit 1 is Order, Bit 2 is Rewrite | |
516 | for (unsigned I = 0; I != 256; I++) | |
517 | Visited[I] = 0; | |
518 | ||
519 | // Set new tag up as necessary. | |
520 | for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++) | |
521 | { | |
522 | if (Rewrite[J].NewTag == 0) | |
523 | Rewrite[J].NewTag = Rewrite[J].Tag; | |
524 | } | |
525 | ||
526 | // Write all all of the tags, in order. | |
527 | for (unsigned int I = 0; Order[I] != 0; I++) | |
528 | { | |
529 | bool Rewritten = false; | |
530 | ||
531 | // See if this is a field that needs to be rewritten | |
532 | for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++) | |
533 | { | |
534 | if (strcasecmp(Rewrite[J].Tag,Order[I]) == 0) | |
535 | { | |
536 | Visited[J] |= 2; | |
537 | if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0) | |
538 | { | |
539 | if (isspace(Rewrite[J].Rewrite[0])) | |
540 | fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite); | |
541 | else | |
542 | fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite); | |
543 | } | |
544 | ||
545 | Rewritten = true; | |
546 | break; | |
547 | } | |
548 | } | |
549 | ||
550 | // See if it is in the fragment | |
551 | unsigned Pos; | |
552 | if (Tags.Find(Order[I],Pos) == false) | |
553 | continue; | |
554 | Visited[Pos] |= 1; | |
555 | ||
556 | if (Rewritten == true) | |
557 | continue; | |
558 | ||
559 | /* Write out this element, taking a moment to rewrite the tag | |
560 | in case of changes of case. */ | |
561 | const char *Start; | |
562 | const char *Stop; | |
563 | Tags.Get(Start,Stop,Pos); | |
564 | ||
565 | if (fputs(Order[I],Output) < 0) | |
566 | return _error->Errno("fputs","IO Error to output"); | |
567 | Start += strlen(Order[I]); | |
568 | if (fwrite(Start,Stop - Start,1,Output) != 1) | |
569 | return _error->Errno("fwrite","IO Error to output"); | |
570 | if (Stop[-1] != '\n') | |
571 | fprintf(Output,"\n"); | |
572 | } | |
573 | ||
574 | // Now write all the old tags that were missed. | |
575 | for (unsigned int I = 0; I != Tags.Count(); I++) | |
576 | { | |
577 | if ((Visited[I] & 1) == 1) | |
578 | continue; | |
579 | ||
580 | const char *Start; | |
581 | const char *Stop; | |
582 | Tags.Get(Start,Stop,I); | |
583 | const char *End = Start; | |
584 | for (; End < Stop && *End != ':'; End++); | |
585 | ||
586 | // See if this is a field that needs to be rewritten | |
587 | bool Rewritten = false; | |
588 | for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++) | |
589 | { | |
590 | if (stringcasecmp(Start,End,Rewrite[J].Tag) == 0) | |
591 | { | |
592 | Visited[J] |= 2; | |
593 | if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0) | |
594 | { | |
595 | if (isspace(Rewrite[J].Rewrite[0])) | |
596 | fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite); | |
597 | else | |
598 | fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite); | |
599 | } | |
600 | ||
601 | Rewritten = true; | |
602 | break; | |
603 | } | |
604 | } | |
605 | ||
606 | if (Rewritten == true) | |
607 | continue; | |
608 | ||
609 | // Write out this element | |
610 | if (fwrite(Start,Stop - Start,1,Output) != 1) | |
611 | return _error->Errno("fwrite","IO Error to output"); | |
612 | if (Stop[-1] != '\n') | |
613 | fprintf(Output,"\n"); | |
614 | } | |
615 | ||
616 | // Now write all the rewrites that were missed | |
617 | for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++) | |
618 | { | |
619 | if ((Visited[J] & 2) == 2) | |
620 | continue; | |
621 | ||
622 | if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0) | |
623 | { | |
624 | if (isspace(Rewrite[J].Rewrite[0])) | |
625 | fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite); | |
626 | else | |
627 | fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite); | |
628 | } | |
629 | } | |
630 | ||
631 | return true; | |
632 | } | |
633 | /*}}}*/ |