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