lib/cdb++/cdb++.h
changeset 0 6f7a81934006
child 2 b3afb9f1e801
equal deleted inserted replaced
-1:000000000000 0:6f7a81934006
       
     1 // Copyright (C) 1999,2000 Bruce Guenter <bruceg@em.ca>
       
     2 //
       
     3 // This program is free software; you can redistribute it and/or modify
       
     4 // it under the terms of the GNU General Public License as published by
       
     5 // the Free Software Foundation; either version 2 of the License, or
       
     6 // (at your option) any later version.
       
     7 //
       
     8 // This program is distributed in the hope that it will be useful,
       
     9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    11 // GNU General Public License for more details.
       
    12 //
       
    13 // You should have received a copy of the GNU General Public License
       
    14 // along with this program; if not, write to the Free Software
       
    15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    16 
       
    17 #ifndef CDBXX__CDBXX__H__
       
    18 #define CDBXX__CDBXX__H__
       
    19 
       
    20 #include "mystring/mystring.h"
       
    21 #include "cdb++/datum.h"
       
    22 #include "fdbuf/fdbuf.h"
       
    23 
       
    24 typedef unsigned long uint32;
       
    25 
       
    26 #define CDBMAKE_HPLIST 1000
       
    27 
       
    28 class cdb_reader
       
    29 {
       
    30 private:
       
    31   fdibuf in;
       
    32   uint32 eod;
       
    33   uint32 pos;
       
    34   bool failed;
       
    35   bool eof;
       
    36 
       
    37   unsigned char header[2048];
       
    38   
       
    39   void abort();
       
    40   int seek(const mystring& key, uint32& len);
       
    41 public:
       
    42   cdb_reader(const mystring& filename);
       
    43   ~cdb_reader();
       
    44 
       
    45   bool firstrec();
       
    46   datum* nextrec();
       
    47   datum* get(const mystring& key);
       
    48   bool operator!() const
       
    49     {
       
    50       return failed;
       
    51     }
       
    52   operator bool() const
       
    53     {
       
    54       return !failed;
       
    55     }
       
    56 };
       
    57 
       
    58 struct cdbmake {
       
    59   struct hp 
       
    60   {
       
    61     uint32 h;
       
    62     uint32 p;
       
    63   };
       
    64   
       
    65   struct hplist 
       
    66   {
       
    67     cdbmake::hp hp[CDBMAKE_HPLIST];
       
    68     hplist* next;
       
    69     int num;
       
    70   };
       
    71   
       
    72   unsigned char final[2048];
       
    73   uint32 count[256];
       
    74   uint32 start[256];
       
    75   hplist *head;
       
    76   hp *split;
       
    77   hp *hash;
       
    78   uint32 numentries;
       
    79 
       
    80   cdbmake();
       
    81   ~cdbmake();
       
    82   int add(uint32 h, uint32 p);
       
    83   int split_hashes();
       
    84   uint32 throw_hash(uint32 pos, int b);
       
    85 };
       
    86 
       
    87 class cdb_writer
       
    88 {
       
    89 private:
       
    90   fdobuf out;
       
    91   cdbmake cdbm;
       
    92   mystring fntemp;
       
    93   uint32 pos;
       
    94   bool failed;
       
    95   
       
    96   void abort();
       
    97 public:
       
    98   cdb_writer(const mystring& filename, int mode = 0666);
       
    99   ~cdb_writer();
       
   100   bool put(const mystring& key, const mystring& data);
       
   101   bool end(const mystring& filename);
       
   102   bool operator!() const
       
   103     {
       
   104       return failed;
       
   105     }
       
   106   operator bool() const
       
   107     {
       
   108       return !failed;
       
   109     }
       
   110 };
       
   111 
       
   112 extern datum* cdb_getrec(const mystring& filename, const mystring& key);
       
   113 
       
   114 #endif // CDBXX__H__