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