home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 17 / CD_ASCQ_17_101194.iso / dos / prg / todb101 / todb / lib / fblock.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-26  |  2.9 KB  |  125 lines

  1. // ========================================================================
  2. //  TODB LIBRARY
  3. //    fblock.h
  4. //
  5. //    Key set file's data block
  6. //    Block class, ObjHeader structure
  7. //
  8. //    Version: see TODB.H file
  9. //
  10. //    Copyright 1993 Christian Thérien
  11. //    All rights reserved
  12. // ========================================================================
  13.  
  14. #ifndef _FBLOCK_H
  15. #define _FBLOCK_H
  16.  
  17. #include "ptodb.h"
  18. #include "todb.h"
  19. #include <stdio.h>
  20. #include <memory.h>
  21.  
  22. class String;
  23. class ErrReporter;
  24. class TODB;
  25.  
  26. //-------------------------------------------------------------------------
  27. //  ObjHeader structure
  28. //-------------------------------------------------------------------------
  29.  
  30. struct ObjHeader
  31. {
  32.     IdType classid;        // class identification
  33.     BlockNbr blknbr;        // relative address of an object
  34.     ObjHeader( int cid = 0, BlockNbr bn = 0 );
  35. };
  36.  
  37. inline ObjHeader::ObjHeader( int cid, BlockNbr bn ) :
  38.     classid( cid ), blknbr( bn )
  39. {
  40. }
  41.  
  42. //-------------------------------------------------------------------------
  43. //  Block class
  44. //-------------------------------------------------------------------------
  45.  
  46. enum BlockError
  47. {
  48.     BLK_APPEND
  49. };
  50.  
  51. static const int fheadersize = sizeof(BlockNbr) * 2;
  52.  
  53. class Block
  54. {
  55. public:
  56.     Block( TODB * todb = NULL, BlockNbr block = 0 );
  57.     Block( const Block & block );
  58.  
  59.     ~Block();
  60.  
  61.     Block & operator = ( const Block & block );
  62.     void SetNextBlock( BlockNbr block );
  63.     BlockNbr NextBlock() const;
  64.     BlockNbr GetBlockNbr() const;
  65.     void MarkBlockDeleted();
  66.     void Save();
  67.     void Get( void * buf, int length, int pos );
  68.     void Put( void * buf, int length, int pos );
  69.  
  70.     // error control
  71.     static void SetErrOut( ErrReporter * er );
  72.  
  73. private:
  74.     TODB * todb_;        // current database
  75.     FILE * dataf_;        // file control block
  76.     BlockNbr blocknbr_;        // this block number
  77.     long blockaddr_;        // disk address of this block
  78.     Boolean deleteblock_;    // true if this block is being deleted
  79.     BlockNbr nextblock_;    // relative address of the next block
  80.     char buf_[blocklength];    // block contents
  81.  
  82.     // error control
  83.     static ErrReporter * ErrOut;
  84.     static void ErrorHandler( BlockError err );
  85. };
  86.  
  87. inline void Block::SetNextBlock( BlockNbr block )
  88. {
  89.     memcpy( buf_, &block, sizeof( nextblock_ ) );
  90.     nextblock_ = block;
  91. }
  92.  
  93. inline BlockNbr Block::NextBlock() const
  94. {
  95.     return nextblock_;
  96. }
  97.  
  98. inline BlockNbr Block::GetBlockNbr() const
  99. {
  100.     return blocknbr_;
  101. }
  102.  
  103. inline void Block::MarkBlockDeleted()
  104. {
  105.     deleteblock_ = True;
  106. }
  107.  
  108. inline void Block::Save()
  109. {
  110.     fseek( dataf_, blockaddr_, SEEK_SET );
  111.     fwrite( buf_ , 1, blocklength, dataf_ );
  112. }
  113.  
  114. inline void Block::Get( void * buf, int length, int pos )
  115. {
  116.     memcpy( buf, buf_ + pos, length );
  117. }
  118.  
  119. inline void Block::Put( void * buf, int length, int pos )
  120. {
  121.     memcpy( buf_ + pos, buf, length );
  122. }
  123.  
  124. #endif // _FBLOCK_H
  125.