LCOV - code coverage report
Current view: top level - src - bytebuf.h (source / functions) Hit Total Coverage
Test: test_coverage.info Lines: 14 15 93.3 %
Date: 2017-06-21 14:32:49 Functions: 3 3 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             :     uint   ReadPos;
      33             :     uint   ByteCount;
      34             :     uint   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        1068 :     ByteBuffer(uint length) : BufferSize(length), AllocatedBuffer(new byte[length]) { Empty(); }
      47        1021 :     ~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, uint length);
      57             : 
      58             :     errorT Status ()      { return Err; }
      59             :     uint   GetByteCount() { return ByteCount; }
      60             :     void   BackToStart ();
      61             :     void   Skip (uint value);
      62     1233323 :     byte   GetByte () {
      63     1233323 :         ASSERT(Current != NULL);
      64     1233323 :         if (ReadPos >= ByteCount) { Err = ERROR_BufferRead; return 0; }
      65     1233323 :         byte b = *Current;
      66     1233323 :         Current++; ReadPos++;
      67     1233323 :         return b;
      68             :     }
      69             :     void   GetFixedString (char *str, uint length);
      70             :     uint   GetTerminatedString (char **str);
      71             :     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    20163410 :     void PutByte (byte value) {
      81    20163410 :         ASSERT(Current != NULL);
      82    20163410 :         ASSERT(Buffer == AllocatedBuffer);
      83    20163410 :         if (Buffer == AllocatedBuffer  && ByteCount >= BufferSize) {
      84           0 :             Err = ERROR_BufferFull; return;
      85             :         }
      86    20163410 :         *Current = value;
      87    20163410 :         Current++; ByteCount++;
      88             :     }
      89             :     void PutFixedString (const char *str, uint 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.12