]> git.saurik.com Git - apt.git/blob - apt-pkg/tagfile.cc
merge debian/sid into debian/experimental
[apt.git] / apt-pkg / tagfile.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: tagfile.cc,v 1.37.2.2 2003/12/31 16:02:30 mdz Exp $
4 /* ######################################################################
5
6 Fast scanner for RFC-822 type header information
7
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.
10
11 ##################################################################### */
12 /*}}}*/
13 // Include Files /*{{{*/
14 #include<config.h>
15
16 #include <apt-pkg/tagfile.h>
17 #include <apt-pkg/error.h>
18 #include <apt-pkg/strutl.h>
19 #include <apt-pkg/fileutl.h>
20
21 #include <string>
22 #include <stdio.h>
23 #include <ctype.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <apti18n.h>
28 /*}}}*/
29
30 using std::string;
31
32 class pkgTagFilePrivate
33 {
34 public:
35 pkgTagFilePrivate(FileFd *pFd, unsigned long long Size) : Fd(*pFd), Buffer(NULL),
36 Start(NULL), End(NULL),
37 Done(false), iOffset(0),
38 Size(Size)
39 {
40 }
41 FileFd &Fd;
42 char *Buffer;
43 char *Start;
44 char *End;
45 bool Done;
46 unsigned long long iOffset;
47 unsigned long long Size;
48 };
49
50 class pkgTagSectionPrivate
51 {
52 public:
53 pkgTagSectionPrivate()
54 {
55 }
56 struct TagData {
57 unsigned int StartTag;
58 unsigned int EndTag;
59 unsigned int StartValue;
60 unsigned int NextInBucket;
61
62 TagData(unsigned int const StartTag) : StartTag(StartTag), EndTag(0), StartValue(0), NextInBucket(0) {}
63 };
64 std::vector<TagData> Tags;
65 };
66
67 static unsigned long AlphaHash(const char *Text, size_t Length) /*{{{*/
68 {
69 /* This very simple hash function for the last 8 letters gives
70 very good performance on the debian package files */
71 if (Length > 8)
72 {
73 Text += (Length - 8);
74 Length = 8;
75 }
76 unsigned long Res = 0;
77 for (size_t i = 0; i < Length; ++i)
78 Res = ((unsigned long)(Text[i]) & 0xDF) ^ (Res << 1);
79 return Res & 0xFF;
80 }
81 /*}}}*/
82
83 // TagFile::pkgTagFile - Constructor /*{{{*/
84 // ---------------------------------------------------------------------
85 /* */
86 pkgTagFile::pkgTagFile(FileFd *pFd,unsigned long long Size)
87 : d(NULL)
88 {
89 Init(pFd, Size);
90 }
91
92 void pkgTagFile::Init(FileFd *pFd,unsigned long long Size)
93 {
94 /* The size is increased by 4 because if we start with the Size of the
95 filename we need to try to read 1 char more to see an EOF faster, 1
96 char the end-pointer can be on and maybe 2 newlines need to be added
97 to the end of the file -> 4 extra chars */
98 Size += 4;
99 if(d != NULL)
100 {
101 free(d->Buffer);
102 delete d;
103 }
104 d = new pkgTagFilePrivate(pFd, Size);
105
106 if (d->Fd.IsOpen() == false)
107 d->Start = d->End = d->Buffer = 0;
108 else
109 d->Buffer = (char*)malloc(sizeof(char) * Size);
110
111 if (d->Buffer == NULL)
112 d->Done = true;
113 else
114 d->Done = false;
115
116 d->Start = d->End = d->Buffer;
117 d->iOffset = 0;
118 if (d->Done == false)
119 Fill();
120 }
121 /*}}}*/
122 // TagFile::~pkgTagFile - Destructor /*{{{*/
123 // ---------------------------------------------------------------------
124 /* */
125 pkgTagFile::~pkgTagFile()
126 {
127 free(d->Buffer);
128 delete d;
129 }
130 /*}}}*/
131 // TagFile::Offset - Return the current offset in the buffer /*{{{*/
132 APT_PURE unsigned long pkgTagFile::Offset()
133 {
134 return d->iOffset;
135 }
136 /*}}}*/
137 // TagFile::Resize - Resize the internal buffer /*{{{*/
138 // ---------------------------------------------------------------------
139 /* Resize the internal buffer (double it in size). Fail if a maximum size
140 * size is reached.
141 */
142 bool pkgTagFile::Resize()
143 {
144 // fail is the buffer grows too big
145 if(d->Size > 1024*1024+1)
146 return false;
147
148 return Resize(d->Size * 2);
149 }
150 bool pkgTagFile::Resize(unsigned long long const newSize)
151 {
152 unsigned long long const EndSize = d->End - d->Start;
153
154 // get new buffer and use it
155 char* newBuffer = (char*)realloc(d->Buffer, sizeof(char) * newSize);
156 if (newBuffer == NULL)
157 return false;
158 d->Buffer = newBuffer;
159 d->Size = newSize;
160
161 // update the start/end pointers to the new buffer
162 d->Start = d->Buffer;
163 d->End = d->Start + EndSize;
164 return true;
165 }
166 /*}}}*/
167 // TagFile::Step - Advance to the next section /*{{{*/
168 // ---------------------------------------------------------------------
169 /* If the Section Scanner fails we refill the buffer and try again.
170 * If that fails too, double the buffer size and try again until a
171 * maximum buffer is reached.
172 */
173 bool pkgTagFile::Step(pkgTagSection &Tag)
174 {
175 if(Tag.Scan(d->Start,d->End - d->Start) == false)
176 {
177 do
178 {
179 if (Fill() == false)
180 return false;
181
182 if(Tag.Scan(d->Start,d->End - d->Start, false))
183 break;
184
185 if (Resize() == false)
186 return _error->Error(_("Unable to parse package file %s (1)"),
187 d->Fd.Name().c_str());
188
189 } while (Tag.Scan(d->Start,d->End - d->Start, false) == false);
190 }
191
192 d->Start += Tag.size();
193 d->iOffset += Tag.size();
194
195 Tag.Trim();
196 return true;
197 }
198 /*}}}*/
199 // TagFile::Fill - Top up the buffer /*{{{*/
200 // ---------------------------------------------------------------------
201 /* This takes the bit at the end of the buffer and puts it at the start
202 then fills the rest from the file */
203 bool pkgTagFile::Fill()
204 {
205 unsigned long long EndSize = d->End - d->Start;
206 unsigned long long Actual = 0;
207
208 memmove(d->Buffer,d->Start,EndSize);
209 d->Start = d->Buffer;
210 d->End = d->Buffer + EndSize;
211
212 if (d->Done == false)
213 {
214 // See if only a bit of the file is left
215 unsigned long long const dataSize = d->Size - ((d->End - d->Buffer) + 1);
216 if (d->Fd.Read(d->End, dataSize, &Actual) == false)
217 return false;
218 if (Actual != dataSize)
219 d->Done = true;
220 d->End += Actual;
221 }
222
223 if (d->Done == true)
224 {
225 if (EndSize <= 3 && Actual == 0)
226 return false;
227 if (d->Size - (d->End - d->Buffer) < 4)
228 return true;
229
230 // Append a double new line if one does not exist
231 unsigned int LineCount = 0;
232 for (const char *E = d->End - 1; E - d->End < 6 && (*E == '\n' || *E == '\r'); E--)
233 if (*E == '\n')
234 LineCount++;
235 if (LineCount < 2)
236 {
237 if ((unsigned)(d->End - d->Buffer) >= d->Size)
238 Resize(d->Size + 3);
239 for (; LineCount < 2; LineCount++)
240 *d->End++ = '\n';
241 }
242
243 return true;
244 }
245
246 return true;
247 }
248 /*}}}*/
249 // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/
250 // ---------------------------------------------------------------------
251 /* This jumps to a pre-recorded file location and reads the record
252 that is there */
253 bool pkgTagFile::Jump(pkgTagSection &Tag,unsigned long long Offset)
254 {
255 // We are within a buffer space of the next hit..
256 if (Offset >= d->iOffset && d->iOffset + (d->End - d->Start) > Offset)
257 {
258 unsigned long long Dist = Offset - d->iOffset;
259 d->Start += Dist;
260 d->iOffset += Dist;
261 // if we have seen the end, don't ask for more
262 if (d->Done == true)
263 return Tag.Scan(d->Start, d->End - d->Start);
264 else
265 return Step(Tag);
266 }
267
268 // Reposition and reload..
269 d->iOffset = Offset;
270 d->Done = false;
271 if (d->Fd.Seek(Offset) == false)
272 return false;
273 d->End = d->Start = d->Buffer;
274
275 if (Fill() == false)
276 return false;
277
278 if (Tag.Scan(d->Start, d->End - d->Start) == true)
279 return true;
280
281 // This appends a double new line (for the real eof handling)
282 if (Fill() == false)
283 return false;
284
285 if (Tag.Scan(d->Start, d->End - d->Start, false) == false)
286 return _error->Error(_("Unable to parse package file %s (2)"),d->Fd.Name().c_str());
287
288 return true;
289 }
290 /*}}}*/
291 // pkgTagSection::pkgTagSection - Constructor /*{{{*/
292 // ---------------------------------------------------------------------
293 /* */
294 APT_IGNORE_DEPRECATED_PUSH
295 pkgTagSection::pkgTagSection()
296 : Section(0), d(NULL), Stop(0)
297 {
298 d = new pkgTagSectionPrivate();
299 #if APT_PKG_ABI < 413
300 TagCount = 0;
301 memset(&Indexes, 0, sizeof(Indexes));
302 #endif
303 memset(&AlphaIndexes, 0, sizeof(AlphaIndexes));
304 }
305 APT_IGNORE_DEPRECATED_POP
306 /*}}}*/
307 // TagSection::Scan - Scan for the end of the header information /*{{{*/
308 #if APT_PKG_ABI < 413
309 bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength)
310 {
311 return Scan(Start, MaxLength, true);
312 }
313 #endif
314 bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength, bool const Restart)
315 {
316 Section = Start;
317 const char *End = Start + MaxLength;
318
319 if (Restart == false && d->Tags.empty() == false)
320 {
321 Stop = Section + d->Tags.back().StartTag;
322 if (End <= Stop)
323 return false;
324 Stop = (const char *)memchr(Stop,'\n',End - Stop);
325 if (Stop == NULL)
326 return false;
327 ++Stop;
328 }
329 else
330 {
331 Stop = Section;
332 if (d->Tags.empty() == false)
333 {
334 memset(&AlphaIndexes, 0, sizeof(AlphaIndexes));
335 d->Tags.clear();
336 }
337 d->Tags.reserve(0x100);
338 }
339 #if APT_PKG_ABI >= 413
340 unsigned int TagCount = d->Tags.size();
341 #else
342 APT_IGNORE_DEPRECATED(TagCount = d->Tags.size();)
343 #endif
344
345 if (Stop == 0)
346 return false;
347
348 pkgTagSectionPrivate::TagData lastTagData(0);
349 lastTagData.EndTag = 0;
350 unsigned long lastTagHash = 0;
351 while (Stop < End)
352 {
353 TrimRecord(true,End);
354
355 // this can happen when TrimRecord trims away the entire Record
356 // (e.g. because it just contains comments)
357 if(Stop == End)
358 return true;
359
360 // Start a new index and add it to the hash
361 if (isspace(Stop[0]) == 0)
362 {
363 // store the last found tag
364 if (lastTagData.EndTag != 0)
365 {
366 if (AlphaIndexes[lastTagHash] != 0)
367 lastTagData.NextInBucket = AlphaIndexes[lastTagHash];
368 APT_IGNORE_DEPRECATED_PUSH
369 AlphaIndexes[lastTagHash] = TagCount;
370 #if APT_PKG_ABI < 413
371 if (d->Tags.size() < sizeof(Indexes)/sizeof(Indexes[0]))
372 Indexes[d->Tags.size()] = lastTagData.StartTag;
373 #endif
374 APT_IGNORE_DEPRECATED_POP
375 d->Tags.push_back(lastTagData);
376 }
377
378 APT_IGNORE_DEPRECATED(++TagCount;)
379 lastTagData = pkgTagSectionPrivate::TagData(Stop - Section);
380 // find the colon separating tag and value
381 char const * Colon = (char const *) memchr(Stop, ':', End - Stop);
382 if (Colon == NULL)
383 return false;
384 // find the end of the tag (which might or might not be the colon)
385 char const * EndTag = Colon;
386 --EndTag;
387 for (; EndTag > Stop && isspace(*EndTag) != 0; --EndTag)
388 ;
389 ++EndTag;
390 lastTagData.EndTag = EndTag - Section;
391 lastTagHash = AlphaHash(Stop, EndTag - Stop);
392 // find the beginning of the value
393 Stop = Colon + 1;
394 for (; isspace(*Stop) != 0; ++Stop);
395 if (Stop >= End)
396 return false;
397 lastTagData.StartValue = Stop - Section;
398 }
399
400 Stop = (const char *)memchr(Stop,'\n',End - Stop);
401
402 if (Stop == 0)
403 return false;
404
405 for (; Stop+1 < End && Stop[1] == '\r'; Stop++)
406 /* nothing */
407 ;
408
409 // Double newline marks the end of the record
410 if (Stop+1 < End && Stop[1] == '\n')
411 {
412 if (lastTagData.EndTag != 0)
413 {
414 if (AlphaIndexes[lastTagHash] != 0)
415 lastTagData.NextInBucket = AlphaIndexes[lastTagHash];
416 APT_IGNORE_DEPRECATED(AlphaIndexes[lastTagHash] = TagCount;)
417 #if APT_PKG_ABI < 413
418 APT_IGNORE_DEPRECATED(Indexes[d->Tags.size()] = lastTagData.StartTag;)
419 #endif
420 d->Tags.push_back(lastTagData);
421 }
422
423 pkgTagSectionPrivate::TagData const td(Stop - Section);
424 #if APT_PKG_ABI < 413
425 APT_IGNORE_DEPRECATED(Indexes[d->Tags.size()] = td.StartTag;)
426 #endif
427 d->Tags.push_back(td);
428 TrimRecord(false,End);
429 return true;
430 }
431
432 Stop++;
433 }
434
435 return false;
436 }
437 /*}}}*/
438 // TagSection::TrimRecord - Trim off any garbage before/after a record /*{{{*/
439 // ---------------------------------------------------------------------
440 /* There should be exactly 2 newline at the end of the record, no more. */
441 void pkgTagSection::TrimRecord(bool BeforeRecord, const char*& End)
442 {
443 if (BeforeRecord == true)
444 return;
445 for (; Stop < End && (Stop[0] == '\n' || Stop[0] == '\r'); Stop++);
446 }
447 /*}}}*/
448 // TagSection::Trim - Trim off any trailing garbage /*{{{*/
449 // ---------------------------------------------------------------------
450 /* There should be exactly 1 newline at the end of the buffer, no more. */
451 void pkgTagSection::Trim()
452 {
453 for (; Stop > Section + 2 && (Stop[-2] == '\n' || Stop[-2] == '\r'); Stop--);
454 }
455 /*}}}*/
456 // TagSection::Exists - return True if a tag exists /*{{{*/
457 #if APT_PKG_ABI >= 413
458 bool pkgTagSection::Exists(const char* const Tag) const
459 #else
460 bool pkgTagSection::Exists(const char* const Tag)
461 #endif
462 {
463 unsigned int tmp;
464 return Find(Tag, tmp);
465 }
466 /*}}}*/
467 // TagSection::Find - Locate a tag /*{{{*/
468 // ---------------------------------------------------------------------
469 /* This searches the section for a tag that matches the given string. */
470 bool pkgTagSection::Find(const char *Tag,unsigned int &Pos) const
471 {
472 size_t const Length = strlen(Tag);
473 unsigned int Bucket = AlphaIndexes[AlphaHash(Tag, Length)];
474 if (Bucket == 0)
475 return false;
476
477 for (; Bucket != 0; Bucket = d->Tags[Bucket - 1].NextInBucket)
478 {
479 if ((d->Tags[Bucket - 1].EndTag - d->Tags[Bucket - 1].StartTag) != Length)
480 continue;
481
482 char const * const St = Section + d->Tags[Bucket - 1].StartTag;
483 if (strncasecmp(Tag,St,Length) != 0)
484 continue;
485
486 Pos = Bucket - 1;
487 return true;
488 }
489
490 Pos = 0;
491 return false;
492 }
493 bool pkgTagSection::Find(const char *Tag,const char *&Start,
494 const char *&End) const
495 {
496 unsigned int Pos;
497 if (Find(Tag, Pos) == false)
498 return false;
499
500 Start = Section + d->Tags[Pos].StartValue;
501 // Strip off the gunk from the end
502 End = Section + d->Tags[Pos + 1].StartTag;
503 if (unlikely(Start > End))
504 return _error->Error("Internal parsing error");
505
506 for (; isspace(End[-1]) != 0 && End > Start; --End);
507
508 return true;
509 }
510 /*}}}*/
511 // TagSection::FindS - Find a string /*{{{*/
512 // ---------------------------------------------------------------------
513 /* */
514 string pkgTagSection::FindS(const char *Tag) const
515 {
516 const char *Start;
517 const char *End;
518 if (Find(Tag,Start,End) == false)
519 return string();
520 return string(Start,End);
521 }
522 /*}}}*/
523 // TagSection::FindI - Find an integer /*{{{*/
524 // ---------------------------------------------------------------------
525 /* */
526 signed int pkgTagSection::FindI(const char *Tag,signed long Default) const
527 {
528 const char *Start;
529 const char *Stop;
530 if (Find(Tag,Start,Stop) == false)
531 return Default;
532
533 // Copy it into a temp buffer so we can use strtol
534 char S[300];
535 if ((unsigned)(Stop - Start) >= sizeof(S))
536 return Default;
537 strncpy(S,Start,Stop-Start);
538 S[Stop - Start] = 0;
539
540 char *End;
541 signed long Result = strtol(S,&End,10);
542 if (S == End)
543 return Default;
544 return Result;
545 }
546 /*}}}*/
547 // TagSection::FindULL - Find an unsigned long long integer /*{{{*/
548 // ---------------------------------------------------------------------
549 /* */
550 unsigned long long pkgTagSection::FindULL(const char *Tag, unsigned long long const &Default) const
551 {
552 const char *Start;
553 const char *Stop;
554 if (Find(Tag,Start,Stop) == false)
555 return Default;
556
557 // Copy it into a temp buffer so we can use strtoull
558 char S[100];
559 if ((unsigned)(Stop - Start) >= sizeof(S))
560 return Default;
561 strncpy(S,Start,Stop-Start);
562 S[Stop - Start] = 0;
563
564 char *End;
565 unsigned long long Result = strtoull(S,&End,10);
566 if (S == End)
567 return Default;
568 return Result;
569 }
570 /*}}}*/
571 // TagSection::FindB - Find boolean value /*{{{*/
572 // ---------------------------------------------------------------------
573 /* */
574 bool pkgTagSection::FindB(const char *Tag, bool const &Default) const
575 {
576 const char *Start, *Stop;
577 if (Find(Tag, Start, Stop) == false)
578 return Default;
579 return StringToBool(string(Start, Stop));
580 }
581 /*}}}*/
582 // TagSection::FindFlag - Locate a yes/no type flag /*{{{*/
583 // ---------------------------------------------------------------------
584 /* The bits marked in Flag are masked on/off in Flags */
585 bool pkgTagSection::FindFlag(const char *Tag,unsigned long &Flags,
586 unsigned long Flag) const
587 {
588 const char *Start;
589 const char *Stop;
590 if (Find(Tag,Start,Stop) == false)
591 return true;
592 return FindFlag(Flags, Flag, Start, Stop);
593 }
594 bool pkgTagSection::FindFlag(unsigned long &Flags, unsigned long Flag,
595 char const* Start, char const* Stop)
596 {
597 switch (StringToBool(string(Start, Stop)))
598 {
599 case 0:
600 Flags &= ~Flag;
601 return true;
602
603 case 1:
604 Flags |= Flag;
605 return true;
606
607 default:
608 _error->Warning("Unknown flag value: %s",string(Start,Stop).c_str());
609 return true;
610 }
611 return true;
612 }
613 /*}}}*/
614 void pkgTagSection::Get(const char *&Start,const char *&Stop,unsigned int I) const
615 {
616 Start = Section + d->Tags[I].StartTag;
617 Stop = Section + d->Tags[I+1].StartTag;
618 }
619 APT_PURE unsigned int pkgTagSection::Count() const { /*{{{*/
620 if (d->Tags.empty() == true)
621 return 0;
622 // the last element is just marking the end and isn't a real one
623 return d->Tags.size() - 1;
624 }
625 /*}}}*/
626 // TFRewrite - Rewrite a control record /*{{{*/
627 // ---------------------------------------------------------------------
628 /* This writes the control record to stdout rewriting it as necessary. The
629 override map item specificies the rewriting rules to follow. This also
630 takes the time to sort the feild list. */
631
632 /* The order of this list is taken from dpkg source lib/parse.c the fieldinfos
633 array. */
634 static const char *iTFRewritePackageOrder[] = {
635 "Package",
636 "Essential",
637 "Status",
638 "Priority",
639 "Section",
640 "Installed-Size",
641 "Maintainer",
642 "Original-Maintainer",
643 "Architecture",
644 "Source",
645 "Version",
646 "Revision", // Obsolete
647 "Config-Version", // Obsolete
648 "Replaces",
649 "Provides",
650 "Depends",
651 "Pre-Depends",
652 "Recommends",
653 "Suggests",
654 "Conflicts",
655 "Breaks",
656 "Conffiles",
657 "Filename",
658 "Size",
659 "MD5sum",
660 "SHA1",
661 "SHA256",
662 "SHA512",
663 "MSDOS-Filename", // Obsolete
664 "Description",
665 0};
666 static const char *iTFRewriteSourceOrder[] = {"Package",
667 "Source",
668 "Binary",
669 "Version",
670 "Priority",
671 "Section",
672 "Maintainer",
673 "Original-Maintainer",
674 "Build-Depends",
675 "Build-Depends-Indep",
676 "Build-Conflicts",
677 "Build-Conflicts-Indep",
678 "Architecture",
679 "Standards-Version",
680 "Format",
681 "Directory",
682 "Files",
683 0};
684
685 /* Two levels of initialization are used because gcc will set the symbol
686 size of an array to the length of the array, causing dynamic relinking
687 errors. Doing this makes the symbol size constant */
688 const char **TFRewritePackageOrder = iTFRewritePackageOrder;
689 const char **TFRewriteSourceOrder = iTFRewriteSourceOrder;
690
691 bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
692 TFRewriteData *Rewrite)
693 {
694 unsigned char Visited[256]; // Bit 1 is Order, Bit 2 is Rewrite
695 for (unsigned I = 0; I != 256; I++)
696 Visited[I] = 0;
697
698 // Set new tag up as necessary.
699 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
700 {
701 if (Rewrite[J].NewTag == 0)
702 Rewrite[J].NewTag = Rewrite[J].Tag;
703 }
704
705 // Write all all of the tags, in order.
706 if (Order != NULL)
707 {
708 for (unsigned int I = 0; Order[I] != 0; I++)
709 {
710 bool Rewritten = false;
711
712 // See if this is a field that needs to be rewritten
713 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
714 {
715 if (strcasecmp(Rewrite[J].Tag,Order[I]) == 0)
716 {
717 Visited[J] |= 2;
718 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
719 {
720 if (isspace(Rewrite[J].Rewrite[0]))
721 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
722 else
723 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
724 }
725 Rewritten = true;
726 break;
727 }
728 }
729
730 // See if it is in the fragment
731 unsigned Pos;
732 if (Tags.Find(Order[I],Pos) == false)
733 continue;
734 Visited[Pos] |= 1;
735
736 if (Rewritten == true)
737 continue;
738
739 /* Write out this element, taking a moment to rewrite the tag
740 in case of changes of case. */
741 const char *Start;
742 const char *Stop;
743 Tags.Get(Start,Stop,Pos);
744
745 if (fputs(Order[I],Output) < 0)
746 return _error->Errno("fputs","IO Error to output");
747 Start += strlen(Order[I]);
748 if (fwrite(Start,Stop - Start,1,Output) != 1)
749 return _error->Errno("fwrite","IO Error to output");
750 if (Stop[-1] != '\n')
751 fprintf(Output,"\n");
752 }
753 }
754
755 // Now write all the old tags that were missed.
756 for (unsigned int I = 0; I != Tags.Count(); I++)
757 {
758 if ((Visited[I] & 1) == 1)
759 continue;
760
761 const char *Start;
762 const char *Stop;
763 Tags.Get(Start,Stop,I);
764 const char *End = Start;
765 for (; End < Stop && *End != ':'; End++);
766
767 // See if this is a field that needs to be rewritten
768 bool Rewritten = false;
769 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
770 {
771 if (stringcasecmp(Start,End,Rewrite[J].Tag) == 0)
772 {
773 Visited[J] |= 2;
774 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
775 {
776 if (isspace(Rewrite[J].Rewrite[0]))
777 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
778 else
779 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
780 }
781
782 Rewritten = true;
783 break;
784 }
785 }
786
787 if (Rewritten == true)
788 continue;
789
790 // Write out this element
791 if (fwrite(Start,Stop - Start,1,Output) != 1)
792 return _error->Errno("fwrite","IO Error to output");
793 if (Stop[-1] != '\n')
794 fprintf(Output,"\n");
795 }
796
797 // Now write all the rewrites that were missed
798 for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
799 {
800 if ((Visited[J] & 2) == 2)
801 continue;
802
803 if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
804 {
805 if (isspace(Rewrite[J].Rewrite[0]))
806 fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
807 else
808 fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
809 }
810 }
811
812 return true;
813 }
814 /*}}}*/
815
816 pkgTagSection::~pkgTagSection() { delete d; }