home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / db02_src.zip / mserv.h < prev    next >
C/C++ Source or Header  |  1994-02-22  |  4KB  |  131 lines

  1. /**************************************************************************
  2.  * Source Id :
  3.  *
  4.  * $Id: mserv.h,v 1.9 1993/10/24 09:14:11 kevinl Exp $
  5.  *-------------------------------------------------------------------------
  6.  * Project Notes :
  7.  *
  8.  *  Diamond Base
  9.  *  ============
  10.  *    A solid database implementation, spurred on by the continuing
  11.  *  Metal (Lead) Base saga.
  12.  *
  13.  *  Project Team :
  14.  *      A. Davison
  15.  *      K. Lentin
  16.  *      D. Platt
  17.  *
  18.  *    Project Commenced : 05-02-1993
  19.  *
  20.  *-------------------------------------------------------------------------
  21.  *  Module Notes :
  22.  *
  23.  *  mserv.h: Header file for...
  24.  *     A memory server. Effectively a malloc style space allocator in
  25.  *     a file to provide variable length records.
  26.  *
  27.  *  Original Author : Kev
  28.  *
  29.  *-------------------------------------------------------------------------
  30.  * Revision History:
  31.  *
  32.  * $Log: mserv.h,v $
  33.  * Revision 1.9  1993/10/24  09:14:11  kevinl
  34.  * Fixed empty string problem
  35.  *
  36.  * Revision 1.8  1993/09/26  06:40:32  kevinl
  37.  * Added dbData support
  38.  *
  39.  * Revision 1.7  1993/08/29  10:46:31  kevinl
  40.  * Include file fixes
  41.  *
  42.  * Revision 1.6  1993/07/19  11:57:13  kevinl
  43.  * Fixed up constant initialisation
  44.  *
  45.  * Revision 1.5  1993/07/11  09:42:05  kevinl
  46.  * Changed String to dbString
  47.  *
  48.  * Revision 1.4  1993/07/02  05:18:37  kevinl
  49.  * delString added
  50.  *
  51.  * Revision 1.3  1993/06/23  05:21:22  kevinl
  52.  * Mallocs are now in angular brackets
  53.  *
  54.  * Revision 1.2  1993/06/20  13:38:11  kevinl
  55.  * strclass now called mystring
  56.  *
  57.  * Revision 1.1  1993/06/18  12:28:43  kevinl
  58.  * Initial revision
  59.  *
  60.  **************************************************************************/
  61.  
  62. #include <fcntl.h>
  63. #if !defined(__SASC)
  64. #    if defined(__BORLANDC__) || defined(__EMX__)
  65. #      include <io.h>
  66. #    else
  67. #        include <unistd.h>
  68. #    endif
  69. #endif
  70. #include <iostream.h>
  71. //#include <stdio.h>
  72. #include <mystring.h>
  73. #include <defs.h>
  74. #include <dberr.h>
  75.  
  76. #define D(x) "  "#x": " << x
  77.  
  78. // The memory server is effectively a file based malloc
  79.  
  80. class memServer {
  81.     static    const    unsigned long GRAN; // = 1<<3;
  82.     static    const    long END_HEADER; // = 12;
  83.                             // Size of header info at front
  84.                             // (2 del pointers + lastEntry)
  85.     static    const    long EXTRA; // = 8;
  86.                             // Amount of info needed in each block
  87.                             // (size, back)
  88.     long    startDelChain;    // First position in delete chain
  89.     long    endDelChain;    // Last position in delete chain
  90.     long    lastEntry;        // Last block in chain
  91.     long    nextFree;        // End of file
  92.     int        fd;
  93.  
  94.     void    readHeader(void);
  95.     void    writeHeader(void);
  96.     void    setNextFree(void);
  97.     void    deleteBlock(long off);
  98.     dbError removeDel(long off);
  99.     long    getChunk(long& newSize, long& back);
  100. public:
  101.     // open constructor
  102.     memServer(const char* name);
  103.  
  104.     // empty constructor
  105.     memServer(void) { fd = -1; }
  106.  
  107.     bool    isOpen(void) { return (fd==-1)?false:true; }
  108.     dbError createMem(const char* name);
  109.     dbError getString(dbData& st, long off);
  110.     dbError putString(dbData& s, long& off);
  111.     dbError delString(dbData& s, long& off);
  112.     bool    checkChain(ostream& o, bool dump = false);
  113.     bool    checkDeleteChain(ostream& o);
  114.     inline    int mSeek(long off)
  115.         {
  116.             return lseek(fd, off, SEEK_SET);
  117.         }
  118.     inline    void mRead(long& data, long off = -1)
  119.         {
  120.             if (off != -1)
  121.                 mSeek(off);
  122.             read(fd, (char*)&data, sizeof(long));
  123.         }
  124.     inline    void mWrite(long& data, long off = -1)
  125.         {
  126.             if (off != -1)
  127.                 mSeek(off);
  128.             write(fd, (char*)&data, sizeof(long));
  129.         }
  130. };
  131.