]> git.saurik.com Git - apt.git/blob - apt-pkg/tagfile.h
implement a more c++-style TFRewrite alternative
[apt.git] / apt-pkg / tagfile.h
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: tagfile.h,v 1.20 2003/05/19 17:13:57 doogie Exp $
4 /* ######################################################################
5
6 Fast scanner for RFC-822 type header information
7
8 This parser handles Debian package files (and others). Their form is
9 RFC-822 type header fields in groups separated by a blank line.
10
11 The parser reads the file and provides methods to step linearly
12 over it or to jump to a pre-recorded start point and read that record.
13
14 A second class is used to perform pre-parsing of the record. It works
15 by indexing the start of each header field and providing lookup
16 functions for header fields.
17
18 ##################################################################### */
19 /*}}}*/
20 #ifndef PKGLIB_TAGFILE_H
21 #define PKGLIB_TAGFILE_H
22
23 #include <apt-pkg/macros.h>
24
25 #include <stdio.h>
26
27 #include <string>
28 #include <vector>
29 #include <list>
30
31 #ifndef APT_8_CLEANER_HEADERS
32 #include <apt-pkg/fileutl.h>
33 #endif
34
35 class FileFd;
36 class pkgTagSectionPrivate;
37
38 class pkgTagSection
39 {
40 const char *Section;
41 // We have a limit of 256 tags per section with the old abi
42 #if APT_PKG_ABI < 413
43 APT_DEPRECATED unsigned int Indexes[256];
44 #endif
45 unsigned int AlphaIndexes[0x100];
46 #if APT_PKG_ABI < 413
47 APT_DEPRECATED unsigned int TagCount;
48 #endif
49
50 // dpointer placeholder (for later in case we need it)
51 pkgTagSectionPrivate *d;
52
53 protected:
54 const char *Stop;
55
56 public:
57
58 inline bool operator ==(const pkgTagSection &rhs) {return Section == rhs.Section;};
59 inline bool operator !=(const pkgTagSection &rhs) {return Section != rhs.Section;};
60
61 bool Find(const char *Tag,const char *&Start, const char *&End) const;
62 bool Find(const char *Tag,unsigned int &Pos) const;
63 std::string FindS(const char *Tag) const;
64 std::string FindRawS(const char *Tag) const;
65 signed int FindI(const char *Tag,signed long Default = 0) const;
66 bool FindB(const char *Tag, bool const &Default = false) const;
67 unsigned long long FindULL(const char *Tag, unsigned long long const &Default = 0) const;
68 bool FindFlag(const char *Tag,unsigned long &Flags,
69 unsigned long Flag) const;
70 bool static FindFlag(unsigned long &Flags, unsigned long Flag,
71 const char* Start, const char* Stop);
72
73 /** \brief searches the boundaries of the current section
74 *
75 * While parameter Start marks the beginning of the section, this method
76 * will search for the first double newline in the data stream which marks
77 * the end of the section. It also does a first pass over the content of
78 * the section parsing it as encountered for processing later on by Find
79 *
80 * @param Start is the beginning of the section
81 * @param MaxLength is the size of valid data in the stream pointed to by Start
82 * @param Restart if enabled internal state will be cleared, otherwise it is
83 * assumed that now more data is available in the stream and the parsing will
84 * start were it encountered insufficent data the last time.
85 *
86 * @return \b true if section end was found, \b false otherwise.
87 * Beware that internal state will be inconsistent if \b false is returned!
88 */
89 #if APT_PKG_ABI >= 413
90 APT_MUSTCHECK bool Scan(const char *Start, unsigned long MaxLength, bool const Restart = true);
91 #else
92 APT_MUSTCHECK bool Scan(const char *Start, unsigned long MaxLength, bool const Restart);
93 APT_MUSTCHECK bool Scan(const char *Start, unsigned long MaxLength);
94 #endif
95
96 inline unsigned long size() const {return Stop - Section;};
97 void Trim();
98 virtual void TrimRecord(bool BeforeRecord, const char* &End);
99
100 /** \brief amount of Tags in the current section
101 *
102 * Note: if a Tag is mentioned repeatly it will be counted multiple
103 * times, but only the last occurrence is available via Find methods.
104 */
105 unsigned int Count() const;
106 #if APT_PKG_ABI >= 413
107 bool Exists(const char* const Tag) const;
108 #else
109 bool Exists(const char* const Tag);
110 #endif
111
112 void Get(const char *&Start,const char *&Stop,unsigned int I) const;
113
114 inline void GetSection(const char *&Start,const char *&Stop) const
115 {
116 Start = Section;
117 Stop = this->Stop;
118 };
119
120 pkgTagSection();
121 virtual ~pkgTagSection();
122
123 struct Tag
124 {
125 enum ActionType { REMOVE, RENAME, REWRITE } Action;
126 std::string Name;
127 std::string Data;
128
129 static Tag Remove(std::string const &Name);
130 static Tag Rename(std::string const &OldName, std::string const &NewName);
131 static Tag Rewrite(std::string const &Name, std::string const &Data);
132 private:
133 Tag(ActionType const Action, std::string const &Name, std::string const &Data) :
134 Action(Action), Name(Name), Data(Data) {}
135 };
136
137 /** Write this section (with optional rewrites) to a file
138 *
139 * @param File to write the section to
140 * @param Order in which tags should appear in the file
141 * @param Rewrite is a set of tags to be renamed, rewitten and/or removed
142 * @return \b true if successful, otherwise \b false
143 */
144 bool Write(FileFd &File, char const * const * const Order = NULL, std::vector<Tag> const &Rewrite = std::vector<Tag>()) const;
145 };
146
147 class pkgTagFilePrivate;
148 class pkgTagFile
149 {
150 pkgTagFilePrivate *d;
151
152 APT_HIDDEN bool Fill();
153 APT_HIDDEN bool Resize();
154 APT_HIDDEN bool Resize(unsigned long long const newSize);
155
156 public:
157
158 bool Step(pkgTagSection &Section);
159 unsigned long Offset();
160 bool Jump(pkgTagSection &Tag,unsigned long long Offset);
161
162 void Init(FileFd *F,unsigned long long Size = 32*1024);
163
164 pkgTagFile(FileFd *F,unsigned long long Size = 32*1024);
165 virtual ~pkgTagFile();
166 };
167
168 extern const char **TFRewritePackageOrder;
169 extern const char **TFRewriteSourceOrder;
170
171 // Use pkgTagSection::Tag and pkgTagSection::Write() instead
172 APT_IGNORE_DEPRECATED_PUSH
173 struct APT_DEPRECATED TFRewriteData
174 {
175 const char *Tag;
176 const char *Rewrite;
177 const char *NewTag;
178 };
179 APT_DEPRECATED bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
180 TFRewriteData *Rewrite);
181 APT_IGNORE_DEPRECATED_POP
182
183 #endif