LCOV - code coverage report
Current view: top level - src - bytebuf.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 17 18 94.4 %
Date: 2019-01-29 11:06:41 Functions: 7 7 100.0 %

          Line data    Source code
       1             : //////////////////////////////////////////////////////////////////////
       2             : //
       3             : //  FILE:       bytebuf.h
       4             : //              ByteBuffer class.
       5             : //
       6             : //  Part of:    Scid (Shane's Chess Information Database)
       7             : //  Version:    0.2
       8             : //
       9             : //  Notice:     Copyright (c) 1999  Shane Hudson.  All rights reserved.
      10             : //
      11             : //  Author:     Shane Hudson (sgh@users.sourceforge.net)
      12             : //
      13             : //////////////////////////////////////////////////////////////////////
      14             : 
      15             : // The ByteBuffer class is used to read and write binary buffers.
      16             : // It is primarily used in Scid for storing in-memory the contents of
      17             : // a game as it is represented on-disk in the gamedata file (gfile).
      18             : 
      19             : 
      20             : #ifndef SCID_BYTEBUF_H
      21             : #define SCID_BYTEBUF_H
      22             : 
      23             : #include "common.h"
      24             : 
      25             : class ByteBuffer
      26             : {
      27             :  private:
      28             :     //----------------------------------
      29             :     //  TextBuffer:  Data Structures
      30             :     //----------------------------------
      31             : 
      32             :     size_t ReadPos;
      33             :     size_t ByteCount;
      34             :     size_t BufferSize;
      35             :     byte * Buffer;
      36             :     byte * Current;
      37             :     byte * AllocatedBuffer;
      38             :     byte * ExternalBuffer;
      39             : 
      40             :     errorT Err;
      41             :     
      42             :     //----------------------------------
      43             :     //  TextBuffer:  Public Functions
      44             :     //----------------------------------
      45             :  public:
      46        1090 :     ByteBuffer(size_t length) : BufferSize(length), AllocatedBuffer(new byte[length]) { Empty(); }
      47        1090 :     ~ByteBuffer() { delete[] AllocatedBuffer; }
      48             : 
      49             : /*
      50             :  * ProvideExternal is super dangerous:
      51             :  * - GFile::ReadGame will change "data" when reading another game
      52             :  * - At that point reading or __writing__ to the bytebuffer will create havoc
      53             :  * For example calling GFile::ReadGame(&bytebuffer, ...) followed by GFile::AddGame(&bytebuffer)
      54             :  * is __very__ bad
      55             :  */
      56             :     void ProvideExternal (byte * data, size_t length);
      57             : 
      58     5191895 :     errorT Status ()      { return Err; }
      59       20039 :     size_t GetByteCount() { return ByteCount; }
      60             :     void   BackToStart ();
      61             :     void   Skip (size_t value);
      62     1237931 :     byte   GetByte () {
      63     1237931 :         ASSERT(Current != NULL);
      64     1237931 :         if (ReadPos >= ByteCount) { Err = ERROR_BufferRead; return 0; }
      65     1237931 :         byte b = *Current;
      66     1237931 :         Current++; ReadPos++;
      67     1237931 :         return b;
      68             :     }
      69             :     void   GetFixedString (char *str, size_t length);
      70             :     void   GetTerminatedString (char **str);
      71       11027 :     const byte* getData() { return Buffer; }
      72             : 
      73             : /*
      74             :  * Writing to a bytebuffer is very error-prone
      75             :  * - Empty() must be called to be sure that Buffer points to the internal buffer
      76             :  *   and to clear the Error Flag;
      77             :  * - Status() must be called after every Put
      78             :  */
      79             :     void Empty ();
      80    15017986 :     void PutByte (byte value) {
      81    15017986 :         ASSERT(Current != NULL);
      82    15017986 :         ASSERT(Buffer == AllocatedBuffer);
      83    15017986 :         if (Buffer == AllocatedBuffer  && ByteCount >= BufferSize) {
      84           0 :             Err = ERROR_BufferFull; return;
      85             :         }
      86    15017986 :         *Current = value;
      87    15017986 :         Current++; ByteCount++;
      88             :     }
      89             :     void PutFixedString (const char *str, size_t length);
      90             :     void PutTerminatedString (const char *str);
      91             : 
      92             : 
      93             : private:
      94             :         ByteBuffer(const ByteBuffer&);
      95             :         ByteBuffer& operator=(const ByteBuffer&);
      96             : };
      97             : 
      98             : #endif  // #ifndef SCID_BYTEBUF_H
      99             : 
     100             : //////////////////////////////////////////////////////////////////////
     101             : //  EOF: bytebuf.h
     102             : //////////////////////////////////////////////////////////////////////

Generated by: LCOV version 1.13