apt: apt-pkg/tagfile.h Source File

apt 2.7.5 commandline package manager

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 <stdint.h>
25#include <stdio.h>
26
27#include <list>
28#include <string>
29#include <vector>
30#include <apt-pkg/string_view.h>
31
32
33class FileFd;
34class pkgTagSectionPrivate;
35class pkgTagFilePrivate;
36
44 class APT_PUBLIC pkgTagSection
45{
46 const char *Section;
47 unsigned int AlphaIndexes[128];
48 unsigned int BetaIndexes[128];
49
50 pkgTagSectionPrivate * const d;
51
52 APT_HIDDEN bool FindInternal(unsigned int Pos,const char *&Start, const char *&End) const;
53 APT_HIDDEN APT::StringView FindInternal(unsigned int Pos) const;
54 APT_HIDDEN APT::StringView FindRawInternal(unsigned int Pos) const;
55 APT_HIDDEN signed int FindIInternal(unsigned int Pos,signed long Default = 0) const;
56 APT_HIDDEN bool FindBInternal(unsigned int Pos, bool Default = false) const;
57 APT_HIDDEN unsigned long long FindULLInternal(unsigned int Pos, unsigned long long const &Default = 0) const;
58 APT_HIDDEN bool FindFlagInternal(unsigned int Pos,uint8_t &Flags, uint8_t const Flag) const;
59 APT_HIDDEN bool FindFlagInternal(unsigned int Pos,unsigned long &Flags, unsigned long Flag) const;
60
61 protected:
62 const char *Stop;
63
64 public:
65
66 inline bool operator ==(const pkgTagSection &rhs) {return Section == rhs.Section;};
67 inline bool operator !=(const pkgTagSection &rhs) {return Section != rhs.Section;};
68
69 // TODO: Remove internally
70 std::string FindS(APT::StringView sv) const { return Find(sv).to_string(); }
71 std::string FindRawS(APT::StringView sv) const { return FindRaw(sv).to_string(); };
72
73 // Functions for lookup with a perfect hash function
74 enum class Key;
75#ifdef APT_COMPILING_APT
76 bool Find(Key key,const char *&Start, const char *&End) const;
77 bool Find(Key key,unsigned int &Pos) const;
78 signed int FindI(Key key,signed long Default = 0) const;
79 bool FindB(Key key, bool Default = false) const;
80 unsigned long long FindULL(Key key, unsigned long long const &Default = 0) const;
81 bool FindFlag(Key key,uint8_t &Flags, uint8_t const Flag) const;
82 bool FindFlag(Key key,unsigned long &Flags, unsigned long Flag) const;
83 bool Exists(Key key) const;
84 APT::StringView Find(Key key) const;
85 APT::StringView FindRaw(Key key) const;
86#endif
87
88 bool Find(APT::StringView Tag,const char *&Start, const char *&End) const;
89 bool Find(APT::StringView Tag,unsigned int &Pos) const;
90 APT::StringView Find(APT::StringView Tag) const;
91 APT::StringView FindRaw(APT::StringView Tag) const;
92 signed int FindI(APT::StringView Tag,signed long Default = 0) const;
93 bool FindB(APT::StringView, bool Default = false) const;
94 unsigned long long FindULL(APT::StringView Tag, unsigned long long const &Default = 0) const;
95
96 bool FindFlag(APT::StringView Tag,uint8_t &Flags,
97 uint8_t const Flag) const;
98 bool FindFlag(APT::StringView Tag,unsigned long &Flags,
99 unsigned long Flag) const;
100 bool Exists(APT::StringView Tag) const;
101
102 bool static FindFlag(uint8_t &Flags, uint8_t const Flag,
103 const char* const Start, const char* const Stop);
104 bool static FindFlag(unsigned long &Flags, unsigned long Flag,
105 const char* Start, const char* Stop);
106
123 APT_MUSTCHECK bool Scan(const char *Start, unsigned long MaxLength, bool const Restart = true);
124
125 inline unsigned long size() const {return Stop - Section;};
126 void Trim();
127 virtual void TrimRecord(bool BeforeRecord, const char* &End);
128
134 unsigned int Count() const;
135
136 void Get(const char *&Start,const char *&Stop,unsigned int I) const;
137
138 inline void GetSection(const char *&Start,const char *&Stop) const
139 {
140 Start = Section;
141 Stop = this->Stop;
142 };
143
144 pkgTagSection();
145 virtual ~pkgTagSection();
146
147 struct Tag
148 {
149 enum ActionType { REMOVE, RENAME, REWRITE } Action;
150 std::string Name;
151 std::string Data;
152
153 static Tag Remove(std::string const &Name);
154 static Tag Rename(std::string const &OldName, std::string const &NewName);
155 static Tag Rewrite(std::string const &Name, std::string const &Data);
156 private:
157 Tag(ActionType const Action, std::string const &Name, std::string const &Data) :
158 Action(Action), Name(Name), Data(Data) {}
159 };
160
168 bool Write(FileFd &File, char const * const * const Order = NULL, std::vector<Tag> const &Rewrite = std::vector<Tag>()) const;
169};
170
171
176 class APT_PUBLIC pkgTagFile
177{
178 pkgTagFilePrivate * const d;
179
180 APT_HIDDEN bool Fill();
181 APT_HIDDEN bool Resize();
182 APT_HIDDEN bool Resize(unsigned long long const newSize);
183
184public:
185
186 bool Step(pkgTagSection &Section);
187 unsigned long Offset();
188 bool Jump(pkgTagSection &Tag,unsigned long long Offset);
189
190 enum Flags
191 {
192 STRICT = 0,
193 SUPPORT_COMMENTS = 1 << 0,
194 };
195
196 void Init(FileFd * const F, pkgTagFile::Flags const Flags, unsigned long long Size = 32*1024);
197 void Init(FileFd * const F,unsigned long long const Size = 32*1024);
198
199 pkgTagFile(FileFd * const F, pkgTagFile::Flags const Flags, unsigned long long Size = 32*1024);
200 pkgTagFile(FileFd * const F,unsigned long long Size = 32*1024);
201 virtual ~pkgTagFile();
202};
203
204APT_PUBLIC extern const char **TFRewritePackageOrder;
205APT_PUBLIC extern const char **TFRewriteSourceOrder;
206
207#endif
APT::StringView
Simple subset of std::string_view from C++17.
Definition string_view.h:27
FileFd
Definition fileutl.h:41
pkgTagFilePrivate
Definition tagfile.cc:37
pkgTagFile
Definition tagfile.h:177
pkgTagSectionPrivate
Definition tagfile.cc:84
pkgTagSection
Definition tagfile.h:45
pkgTagSection::Tag
Definition tagfile.h:148