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    
00031    // We have a limit of 256 tags per section.
00032    unsigned int Indexes[256];
00033    unsigned int AlphaIndexes[0x100];
00034    
00035    unsigned int TagCount;
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 
00048    protected:
00049    const char *Stop;
00050 
00051    public:
00052    
00053    inline bool operator ==(const pkgTagSection &rhs) {return Section == rhs.Section;};
00054    inline bool operator !=(const pkgTagSection &rhs) {return Section != rhs.Section;};
00055    
00056    bool Find(const char *Tag,const char *&Start, const char *&End) const;
00057    bool Find(const char *Tag,unsigned &Pos) const;
00058    string FindS(const char *Tag) const;
00059    signed int FindI(const char *Tag,signed long Default = 0) const ;
00060    unsigned long long FindULL(const char *Tag, unsigned long long const &Default = 0) const;
00061    bool FindFlag(const char *Tag,unsigned long &Flags,
00062                  unsigned long Flag) const;
00063    bool static const FindFlag(unsigned long &Flags, unsigned long Flag,
00064                                 const char* Start, const char* Stop);
00065    bool Scan(const char *Start,unsigned long MaxLength);
00066    inline unsigned long size() const {return Stop - Section;};
00067    void Trim();
00068    virtual void TrimRecord(bool BeforeRecord, const char* &End);
00069    
00070    inline unsigned int Count() const {return TagCount;};
00071    inline bool Exists(const char* const Tag) {return AlphaIndexes[AlphaHash(Tag)] != 0;}
00072  
00073    inline void Get(const char *&Start,const char *&Stop,unsigned int I) const
00074                    {Start = Section + Indexes[I]; Stop = Section + Indexes[I+1];}
00075             
00076    inline void GetSection(const char *&Start,const char *&Stop) const
00077    {
00078       Start = Section;
00079       Stop = this->Stop;
00080    };
00081    
00082    pkgTagSection() : Section(0), Stop(0) {};
00083 };
00084 
00085 class pkgTagFile
00086 {
00087    FileFd &Fd;
00088    char *Buffer;
00089    char *Start;
00090    char *End;
00091    bool Done;
00092    unsigned long iOffset;
00093    unsigned long Size;
00094 
00095    bool Fill();
00096    bool Resize();
00097 
00098    public:
00099 
00100    bool Step(pkgTagSection &Section);
00101    inline unsigned long Offset() {return iOffset;};
00102    bool Jump(pkgTagSection &Tag,unsigned long Offset);
00103 
00104    pkgTagFile(FileFd *F,unsigned long Size = 32*1024);
00105    ~pkgTagFile();
00106 };
00107 
00108 /* This is the list of things to rewrite. The rewriter
00109    goes through and changes or adds each of these headers
00110    to suit. A zero forces the header to be erased, an empty string
00111    causes the old value to be used. (rewrite rule ignored) */
00112 struct TFRewriteData
00113 {
00114    const char *Tag;
00115    const char *Rewrite;
00116    const char *NewTag;
00117 };
00118 extern const char **TFRewritePackageOrder;
00119 extern const char **TFRewriteSourceOrder;
00120 
00121 bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
00122                TFRewriteData *Rewrite);
00123 
00124 #endif