tagfile.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: tagfile.h,v 1.20 2003/05/19 17:13:57 doogie Exp $
  4. /* ######################################################################
  5. Fast scanner for RFC-822 type header information
  6. This parser handles Debian package files (and others). Their form is
  7. RFC-822 type header fields in groups separated by a blank line.
  8. The parser reads the file and provides methods to step linearly
  9. over it or to jump to a pre-recorded start point and read that record.
  10. A second class is used to perform pre-parsing of the record. It works
  11. by indexing the start of each header field and providing lookup
  12. functions for header fields.
  13. ##################################################################### */
  14. /*}}}*/
  15. #ifndef PKGLIB_TAGFILE_H
  16. #define PKGLIB_TAGFILE_H
  17. #include <apt-pkg/fileutl.h>
  18. #include <stdio.h>
  19. class pkgTagSection
  20. {
  21. const char *Section;
  22. const char *Stop;
  23. // We have a limit of 256 tags per section.
  24. unsigned int Indexes[256];
  25. unsigned int AlphaIndexes[0x100];
  26. unsigned int TagCount;
  27. public:
  28. inline bool operator ==(const pkgTagSection &rhs) {return Section == rhs.Section;};
  29. inline bool operator !=(const pkgTagSection &rhs) {return Section != rhs.Section;};
  30. bool Find(const char *Tag,const char *&Start, const char *&End) const;
  31. bool Find(const char *Tag,unsigned &Pos) const;
  32. string FindS(const char *Tag) const;
  33. signed int FindI(const char *Tag,signed long Default = 0) const ;
  34. bool FindFlag(const char *Tag,unsigned long &Flags,
  35. unsigned long Flag) const;
  36. bool Scan(const char *Start,unsigned long MaxLength);
  37. inline unsigned long size() const {return Stop - Section;};
  38. void Trim();
  39. inline unsigned int Count() const {return TagCount;};
  40. inline void Get(const char *&Start,const char *&Stop,unsigned int I) const
  41. {Start = Section + Indexes[I]; Stop = Section + Indexes[I+1];}
  42. inline void GetSection(const char *&Start,const char *&Stop) const
  43. {
  44. Start = Section;
  45. Stop = this->Stop;
  46. };
  47. pkgTagSection() : Section(0), Stop(0) {};
  48. };
  49. class pkgTagFile
  50. {
  51. FileFd &Fd;
  52. char *Buffer;
  53. char *Start;
  54. char *End;
  55. bool Done;
  56. unsigned long iOffset;
  57. unsigned long Size;
  58. bool Fill();
  59. bool Resize();
  60. public:
  61. bool Step(pkgTagSection &Section);
  62. inline unsigned long Offset() {return iOffset;};
  63. bool Jump(pkgTagSection &Tag,unsigned long Offset);
  64. pkgTagFile(FileFd *F,unsigned long Size = 32*1024);
  65. ~pkgTagFile();
  66. };
  67. /* This is the list of things to rewrite. The rewriter
  68. goes through and changes or adds each of these headers
  69. to suit. A zero forces the header to be erased, an empty string
  70. causes the old value to be used. (rewrite rule ignored) */
  71. struct TFRewriteData
  72. {
  73. const char *Tag;
  74. const char *Rewrite;
  75. const char *NewTag;
  76. };
  77. extern const char **TFRewritePackageOrder;
  78. extern const char **TFRewriteSourceOrder;
  79. bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
  80. TFRewriteData *Rewrite);
  81. #endif