tagfile.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/macros.h>
  18. #include <stdio.h>
  19. #include <string>
  20. #include <vector>
  21. #include <list>
  22. #ifndef APT_8_CLEANER_HEADERS
  23. #include <apt-pkg/fileutl.h>
  24. #endif
  25. class FileFd;
  26. class pkgTagSectionPrivate;
  27. class pkgTagSection
  28. {
  29. const char *Section;
  30. // We have a limit of 256 tags per section with the old abi
  31. #if APT_PKG_ABI < 413
  32. APT_DEPRECATED unsigned int Indexes[256];
  33. #endif
  34. unsigned int AlphaIndexes[0x100];
  35. #if APT_PKG_ABI < 413
  36. APT_DEPRECATED unsigned int TagCount;
  37. #endif
  38. // dpointer placeholder (for later in case we need it)
  39. pkgTagSectionPrivate *d;
  40. protected:
  41. const char *Stop;
  42. public:
  43. inline bool operator ==(const pkgTagSection &rhs) {return Section == rhs.Section;};
  44. inline bool operator !=(const pkgTagSection &rhs) {return Section != rhs.Section;};
  45. bool Find(const char *Tag,const char *&Start, const char *&End) const;
  46. bool Find(const char *Tag,unsigned int &Pos) const;
  47. std::string FindS(const char *Tag) const;
  48. std::string FindRawS(const char *Tag) const;
  49. signed int FindI(const char *Tag,signed long Default = 0) const;
  50. bool FindB(const char *Tag, bool const &Default = false) const;
  51. unsigned long long FindULL(const char *Tag, unsigned long long const &Default = 0) const;
  52. bool FindFlag(const char *Tag,unsigned long &Flags,
  53. unsigned long Flag) const;
  54. bool static FindFlag(unsigned long &Flags, unsigned long Flag,
  55. const char* Start, const char* Stop);
  56. /** \brief searches the boundaries of the current section
  57. *
  58. * While parameter Start marks the beginning of the section, this method
  59. * will search for the first double newline in the data stream which marks
  60. * the end of the section. It also does a first pass over the content of
  61. * the section parsing it as encountered for processing later on by Find
  62. *
  63. * @param Start is the beginning of the section
  64. * @param MaxLength is the size of valid data in the stream pointed to by Start
  65. * @param Restart if enabled internal state will be cleared, otherwise it is
  66. * assumed that now more data is available in the stream and the parsing will
  67. * start were it encountered insufficent data the last time.
  68. *
  69. * @return \b true if section end was found, \b false otherwise.
  70. * Beware that internal state will be inconsistent if \b false is returned!
  71. */
  72. #if APT_PKG_ABI >= 413
  73. APT_MUSTCHECK bool Scan(const char *Start, unsigned long MaxLength, bool const Restart = true);
  74. #else
  75. APT_MUSTCHECK bool Scan(const char *Start, unsigned long MaxLength, bool const Restart);
  76. APT_MUSTCHECK bool Scan(const char *Start, unsigned long MaxLength);
  77. #endif
  78. inline unsigned long size() const {return Stop - Section;};
  79. void Trim();
  80. virtual void TrimRecord(bool BeforeRecord, const char* &End);
  81. /** \brief amount of Tags in the current section
  82. *
  83. * Note: if a Tag is mentioned repeatly it will be counted multiple
  84. * times, but only the last occurrence is available via Find methods.
  85. */
  86. unsigned int Count() const;
  87. #if APT_PKG_ABI >= 413
  88. bool Exists(const char* const Tag) const;
  89. #else
  90. bool Exists(const char* const Tag);
  91. #endif
  92. void Get(const char *&Start,const char *&Stop,unsigned int I) const;
  93. inline void GetSection(const char *&Start,const char *&Stop) const
  94. {
  95. Start = Section;
  96. Stop = this->Stop;
  97. };
  98. pkgTagSection();
  99. virtual ~pkgTagSection();
  100. struct Tag
  101. {
  102. enum ActionType { REMOVE, RENAME, REWRITE } Action;
  103. std::string Name;
  104. std::string Data;
  105. static Tag Remove(std::string const &Name);
  106. static Tag Rename(std::string const &OldName, std::string const &NewName);
  107. static Tag Rewrite(std::string const &Name, std::string const &Data);
  108. private:
  109. Tag(ActionType const Action, std::string const &Name, std::string const &Data) :
  110. Action(Action), Name(Name), Data(Data) {}
  111. };
  112. /** Write this section (with optional rewrites) to a file
  113. *
  114. * @param File to write the section to
  115. * @param Order in which tags should appear in the file
  116. * @param Rewrite is a set of tags to be renamed, rewitten and/or removed
  117. * @return \b true if successful, otherwise \b false
  118. */
  119. bool Write(FileFd &File, char const * const * const Order = NULL, std::vector<Tag> const &Rewrite = std::vector<Tag>()) const;
  120. };
  121. class pkgTagFilePrivate;
  122. class pkgTagFile
  123. {
  124. pkgTagFilePrivate *d;
  125. APT_HIDDEN bool Fill();
  126. APT_HIDDEN bool Resize();
  127. APT_HIDDEN bool Resize(unsigned long long const newSize);
  128. public:
  129. bool Step(pkgTagSection &Section);
  130. unsigned long Offset();
  131. bool Jump(pkgTagSection &Tag,unsigned long long Offset);
  132. void Init(FileFd *F,unsigned long long Size = 32*1024);
  133. pkgTagFile(FileFd *F,unsigned long long Size = 32*1024);
  134. virtual ~pkgTagFile();
  135. };
  136. extern const char **TFRewritePackageOrder;
  137. extern const char **TFRewriteSourceOrder;
  138. // Use pkgTagSection::Tag and pkgTagSection::Write() instead
  139. APT_IGNORE_DEPRECATED_PUSH
  140. struct APT_DEPRECATED TFRewriteData
  141. {
  142. const char *Tag;
  143. const char *Rewrite;
  144. const char *NewTag;
  145. };
  146. APT_DEPRECATED bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
  147. TFRewriteData *Rewrite);
  148. APT_IGNORE_DEPRECATED_POP
  149. #endif