apt @VERSION@
tagfile.h
00001 // -*- mode: cpp; mode: fold -*-
00002 // Description                                                          /*{{{*/
00003 // $Id: tagfile.h,v 1.20 2003/05/19 17:13:57 doogie Exp $
00004 /* ######################################################################
00005 
00006    Fast scanner for RFC-822 type header information
00007    
00008    This parser handles Debian package files (and others). Their form is
00009    RFC-822 type header fields in groups separated by a blank line.
00010    
00011    The parser reads the file and provides methods to step linearly
00012    over it or to jump to a pre-recorded start point and read that record.
00013    
00014    A second class is used to perform pre-parsing of the record. It works
00015    by indexing the start of each header field and providing lookup 
00016    functions for header fields.
00017    
00018    ##################################################################### */
00019                                                                         /*}}}*/
00020 #ifndef PKGLIB_TAGFILE_H
00021 #define PKGLIB_TAGFILE_H
00022 
00023 
00024 #include <apt-pkg/fileutl.h>
00025 #include <stdio.h>
00026 
00027 class pkgTagSection
00028 {
00029    const char *Section;
00030    // We have a limit of 256 tags per section.
00031    unsigned int Indexes[256];
00032    unsigned int AlphaIndexes[0x100];
00033    unsigned int TagCount;
00034    // dpointer placeholder (for later in case we need it)
00035    void *d;
00036 
00037    /* This very simple hash function for the last 8 letters gives
00038       very good performance on the debian package files */
00039    inline static unsigned long AlphaHash(const char *Text, const char *End = 0)
00040    {
00041       unsigned long Res = 0;
00042       for (; Text != End && *Text != ':' && *Text != 0; Text++)
00043          Res = ((unsigned long)(*Text) & 0xDF) ^ (Res << 1);
00044       return Res & 0xFF;
00045    }
00046 
00047    protected:
00048    const char *Stop;
00049 
00050    public:
00051    
00052    inline bool operator ==(const pkgTagSection &rhs) {return Section == rhs.Section;};
00053    inline bool operator !=(const pkgTagSection &rhs) {return Section != rhs.Section;};
00054    
00055    bool Find(const char *Tag,const char *&Start, const char *&End) const;
00056    bool Find(const char *Tag,unsigned &Pos) const;
00057    string FindS(const char *Tag) const;
00058    signed int FindI(const char *Tag,signed long Default = 0) const ;
00059    unsigned long long FindULL(const char *Tag, unsigned long long const &Default = 0) const;
00060    bool FindFlag(const char *Tag,unsigned long &Flags,
00061                  unsigned long Flag) const;
00062    bool static const FindFlag(unsigned long &Flags, unsigned long Flag,
00063                                 const char* Start, const char* Stop);
00064    bool Scan(const char *Start,unsigned long MaxLength);
00065    inline unsigned long size() const {return Stop - Section;};
00066    void Trim();
00067    virtual void TrimRecord(bool BeforeRecord, const char* &End);
00068    
00069    inline unsigned int Count() const {return TagCount;};
00070    inline bool Exists(const char* const Tag) {return AlphaIndexes[AlphaHash(Tag)] != 0;}
00071  
00072    inline void Get(const char *&Start,const char *&Stop,unsigned int I) const
00073                    {Start = Section + Indexes[I]; Stop = Section + Indexes[I+1];}
00074             
00075    inline void GetSection(const char *&Start,const char *&Stop) const
00076    {
00077       Start = Section;
00078       Stop = this->Stop;
00079    };
00080    
00081    pkgTagSection() : Section(0), TagCount(0), Stop(0) {};
00082    virtual ~pkgTagSection() {};
00083 };
00084 
00085 class pkgTagFilePrivate;
00086 class pkgTagFile
00087 {
00088    pkgTagFilePrivate *d;
00089 
00090    bool Fill();
00091    bool Resize();
00092 
00093    public:
00094 
00095    bool Step(pkgTagSection &Section);
00096    unsigned long Offset();
00097    bool Jump(pkgTagSection &Tag,unsigned long Offset);
00098 
00099    pkgTagFile(FileFd *F,unsigned long Size = 32*1024);
00100    virtual ~pkgTagFile();
00101 };
00102 
00103 /* This is the list of things to rewrite. The rewriter
00104    goes through and changes or adds each of these headers
00105    to suit. A zero forces the header to be erased, an empty string
00106    causes the old value to be used. (rewrite rule ignored) */
00107 struct TFRewriteData
00108 {
00109    const char *Tag;
00110    const char *Rewrite;
00111    const char *NewTag;
00112 };
00113 extern const char **TFRewritePackageOrder;
00114 extern const char **TFRewriteSourceOrder;
00115 
00116 bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
00117                TFRewriteData *Rewrite);
00118 
00119 #endif