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 #include <stdio.h>
00024 
00025 #include <string>
00026 
00027 class FileFd;
00028 
00029 class pkgTagSection
00030 {
00031    const char *Section;
00032    // We have a limit of 256 tags per section.
00033    unsigned int Indexes[256];
00034    unsigned int AlphaIndexes[0x100];
00035    unsigned int TagCount;
00036    // dpointer placeholder (for later in case we need it)
00037    void *d;
00038 
00039    /* This very simple hash function for the last 8 letters gives
00040       very good performance on the debian package files */
00041    inline static unsigned long AlphaHash(const char *Text, const char *End = 0)
00042    {
00043       unsigned long Res = 0;
00044       for (; Text != End && *Text != ':' && *Text != 0; Text++)
00045          Res = ((unsigned long)(*Text) & 0xDF) ^ (Res << 1);
00046       return Res & 0xFF;
00047    }
00048 
00049    protected:
00050    const char *Stop;
00051 
00052    public:
00053    
00054    inline bool operator ==(const pkgTagSection &rhs) {return Section == rhs.Section;};
00055    inline bool operator !=(const pkgTagSection &rhs) {return Section != rhs.Section;};
00056    
00057    bool Find(const char *Tag,const char *&Start, const char *&End) const;
00058    bool Find(const char *Tag,unsigned &Pos) const;
00059    std::string FindS(const char *Tag) const;
00060    signed int FindI(const char *Tag,signed long Default = 0) const ;
00061    unsigned long long FindULL(const char *Tag, unsigned long long const &Default = 0) const;
00062    bool FindFlag(const char *Tag,unsigned long &Flags,
00063                  unsigned long Flag) const;
00064    bool static const FindFlag(unsigned long &Flags, unsigned long Flag,
00065                                 const char* Start, const char* Stop);
00066    bool Scan(const char *Start,unsigned long MaxLength);
00067    inline unsigned long size() const {return Stop - Section;};
00068    void Trim();
00069    virtual void TrimRecord(bool BeforeRecord, const char* &End);
00070    
00071    inline unsigned int Count() const {return TagCount;};
00072    inline bool Exists(const char* const Tag) {return AlphaIndexes[AlphaHash(Tag)] != 0;}
00073  
00074    inline void Get(const char *&Start,const char *&Stop,unsigned int I) const
00075                    {Start = Section + Indexes[I]; Stop = Section + Indexes[I+1];}
00076             
00077    inline void GetSection(const char *&Start,const char *&Stop) const
00078    {
00079       Start = Section;
00080       Stop = this->Stop;
00081    };
00082    
00083    pkgTagSection() : Section(0), TagCount(0), Stop(0) {};
00084    virtual ~pkgTagSection() {};
00085 };
00086 
00087 class pkgTagFilePrivate;
00088 class pkgTagFile
00089 {
00090    pkgTagFilePrivate *d;
00091 
00092    bool Fill();
00093    bool Resize();
00094 
00095    public:
00096 
00097    bool Step(pkgTagSection &Section);
00098    unsigned long Offset();
00099    bool Jump(pkgTagSection &Tag,unsigned long long Offset);
00100 
00101    pkgTagFile(FileFd *F,unsigned long long Size = 32*1024);
00102    virtual ~pkgTagFile();
00103 };
00104 
00105 /* This is the list of things to rewrite. The rewriter
00106    goes through and changes or adds each of these headers
00107    to suit. A zero forces the header to be erased, an empty string
00108    causes the old value to be used. (rewrite rule ignored) */
00109 struct TFRewriteData
00110 {
00111    const char *Tag;
00112    const char *Rewrite;
00113    const char *NewTag;
00114 };
00115 extern const char **TFRewritePackageOrder;
00116 extern const char **TFRewriteSourceOrder;
00117 
00118 bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
00119                TFRewriteData *Rewrite);
00120 
00121 #endif