apt: apt-pkg/tagfile.h Source File

apt 2.3.15 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 APT_HIDDEN bool Find(Key key,const char *&Start, const char *&End) const;
76 APT_HIDDEN bool Find(Key key,unsigned int &Pos) const;
77 APT_HIDDEN signed int FindI(Key key,signed long Default = 0) const;
78 APT_HIDDEN bool FindB(Key key, bool Default = false) const;
79 APT_HIDDEN unsigned long long FindULL(Key key, unsigned long long const &Default = 0) const;
80 APT_HIDDEN bool FindFlag(Key key,uint8_t &Flags, uint8_t const Flag) const;
81 APT_HIDDEN bool FindFlag(Key key,unsigned long &Flags, unsigned long Flag) const;
82 APT_HIDDEN bool Exists(Key key) const;
83 APT_HIDDEN APT::StringView Find(Key key) const;
84 APT_HIDDEN APT::StringView FindRaw(Key key) const;
85 bool Find(APT::StringView Tag,const char *&Start, const char *&End) const;
86 bool Find(APT::StringView Tag,unsigned int &Pos) const;
87 APT::StringView Find(APT::StringView Tag) const;
88 APT::StringView FindRaw(APT::StringView Tag) const;
89 signed int FindI(APT::StringView Tag,signed long Default = 0) const;
90 bool FindB(APT::StringView, bool Default = false) const;
91 unsigned long long FindULL(APT::StringView Tag, unsigned long long const &Default = 0) const;
92
93 bool FindFlag(APT::StringView Tag,uint8_t &Flags,
94 uint8_t const Flag) const;
95 bool FindFlag(APT::StringView Tag,unsigned long &Flags,
96 unsigned long Flag) const;
97 bool Exists(APT::StringView Tag) const;
98
99 bool static FindFlag(uint8_t &Flags, uint8_t const Flag,
100 const char* const Start, const char* const Stop);
101 bool static FindFlag(unsigned long &Flags, unsigned long Flag,
102 const char* Start, const char* Stop);
103
120 APT_MUSTCHECK bool Scan(const char *Start, unsigned long MaxLength, bool const Restart = true);
121
122 inline unsigned long size() const {return Stop - Section;};
123 void Trim();
124 virtual void TrimRecord(bool BeforeRecord, const char* &End);
125
131 unsigned int Count() const;
132
133 void Get(const char *&Start,const char *&Stop,unsigned int I) const;
134
135 inline void GetSection(const char *&Start,const char *&Stop) const
136 {
137 Start = Section;
138 Stop = this->Stop;
139 };
140
141 pkgTagSection();
142 virtual ~pkgTagSection();
143
144 struct Tag
145 {
146 enum ActionType { REMOVE, RENAME, REWRITE } Action;
147 std::string Name;
148 std::string Data;
149
150 static Tag Remove(std::string const &Name);
151 static Tag Rename(std::string const &OldName, std::string const &NewName);
152 static Tag Rewrite(std::string const &Name, std::string const &Data);
153 private:
154 Tag(ActionType const Action, std::string const &Name, std::string const &Data) :
155 Action(Action), Name(Name), Data(Data) {}
156 };
157
165 bool Write(FileFd &File, char const * const * const Order = NULL, std::vector<Tag> const &Rewrite = std::vector<Tag>()) const;
166};
167
168
173 class APT_PUBLIC pkgTagFile
174{
175 pkgTagFilePrivate * const d;
176
177 APT_HIDDEN bool Fill();
178 APT_HIDDEN bool Resize();
179 APT_HIDDEN bool Resize(unsigned long long const newSize);
180
181public:
182
183 bool Step(pkgTagSection &Section);
184 unsigned long Offset();
185 bool Jump(pkgTagSection &Tag,unsigned long long Offset);
186
187 enum Flags
188 {
189 STRICT = 0,
190 SUPPORT_COMMENTS = 1 << 0,
191 };
192
193 void Init(FileFd * const F, pkgTagFile::Flags const Flags, unsigned long long Size = 32*1024);
194 void Init(FileFd * const F,unsigned long long const Size = 32*1024);
195
196 pkgTagFile(FileFd * const F, pkgTagFile::Flags const Flags, unsigned long long Size = 32*1024);
197 pkgTagFile(FileFd * const F,unsigned long long Size = 32*1024);
198 virtual ~pkgTagFile();
199};
200
201APT_PUBLIC extern const char **TFRewritePackageOrder;
202APT_PUBLIC extern const char **TFRewriteSourceOrder;
203
204#endif
APT::StringView
Simple subset of std::string_view from C++17.
Definition: string_view.h:27
FileFd
Definition: fileutl.h:39
pkgTagFilePrivate
Definition: tagfile.cc:37
pkgTagFile
Definition: tagfile.h:174
pkgTagSectionPrivate
Definition: tagfile.cc:84
pkgTagSection
Definition: tagfile.h:45
pkgTagSection::Tag
Definition: tagfile.h:145