LCOV - code coverage report
Current view: top level - src - bytebuf.cpp (source / functions) Hit Total Coverage
Test: test_coverage.info Lines: 34 59 57.6 %
Date: 2017-06-21 14:32:49 Functions: 4 8 50.0 %

          Line data    Source code
       1             : //////////////////////////////////////////////////////////////////////
       2             : //
       3             : //  FILE:       bytebuf.cpp
       4             : //              ByteBuffer class for Scid.
       5             : //
       6             : //  Part of:    Scid (Shane's Chess Information Database)
       7             : //  Version:    0.3
       8             : //
       9             : //  Notice:     Copyright (c) 1999  Shane Hudson.  All rights reserved.
      10             : //
      11             : //  Author:     Shane Hudson (sgh@users.sourceforge.net)
      12             : //
      13             : //////////////////////////////////////////////////////////////////////
      14             : 
      15             : 
      16             : #include "bytebuf.h"
      17             : 
      18             : //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      19             : // ByteBuffer::Empty():
      20             : //      Empties the ByteBuffer.
      21             : //
      22             : void
      23       13077 : ByteBuffer::Empty()
      24             : {
      25       13077 :     ReadPos = ByteCount = 0;
      26       13077 :     ExternalBuffer = NULL;
      27       13077 :     Buffer = AllocatedBuffer;
      28       13077 :     Current = Buffer;
      29       13077 :     Err = OK;
      30       13077 : }
      31             : 
      32             : //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      33             : // ByteBuffer::BackToStart():
      34             : //      Sets the ByteBuffer's read position back to the buffer start.
      35             : //
      36             : void
      37           0 : ByteBuffer::BackToStart()
      38             : {
      39           0 :     ReadPos = 0;
      40           0 :     Current = Buffer;
      41           0 :     Err = OK;
      42           0 : }
      43             : 
      44             : //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      45             : // ByteBuffer::ProvideExternal():
      46             : //      Provides an external buffer to use instead of the allocated
      47             : //      buffer. This is used when the buffer is only going to be read
      48             : //      and it would waste time (and degrade performance) to copy the
      49             : //      data to the buffer's allocated space first.
      50             : //
      51             : void
      52        1012 : ByteBuffer::ProvideExternal (byte * data, uint length)
      53             : {
      54        1012 :     ExternalBuffer = data;
      55        1012 :     ByteCount = length;
      56        1012 :     ReadPos = 0;
      57        1012 :     Current = Buffer = ExternalBuffer;
      58        1012 :     Err = OK;
      59        1012 : }
      60             : 
      61             : 
      62             : //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      63             : // ByteBuffer::Skip():
      64             : //      Skips over a specified number of bytes.
      65             : void
      66           0 : ByteBuffer::Skip (uint length)
      67             : {
      68           0 :     ASSERT (Current != NULL);
      69             : 
      70           0 :     if (ReadPos + length > ByteCount) { Err = ERROR_BufferRead; return; }
      71           0 :     ReadPos += length;
      72           0 :     Current += length;
      73             : }
      74             : 
      75             : //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      76             : // ByteBuffer::GetFixedString():
      77             : //      Reads a fixed-length string from the buffer. A terminating
      78             : //      null character is not added.
      79             : void
      80           0 : ByteBuffer::GetFixedString (char * str, uint length)
      81             : {
      82           0 :     ASSERT(Current != NULL  &&  str != NULL);
      83             : 
      84           0 :     if (Err != OK) { return; }
      85           0 :     if (ReadPos + length > ByteCount) { Err = ERROR_BufferRead; return; }
      86           0 :     ReadPos += length;
      87           0 :     while (length > 0) {
      88           0 :         *str = *Current; Current++; str++;
      89           0 :         length--;
      90             :     }
      91             : }
      92             : 
      93             : //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      94             : // ByteBuffer::PutFixedString():
      95             : //      Writes a fixed-length string to the buffer. A terminating null
      96             : //      character is not written, unless it was part of the string.
      97             : void
      98           0 : ByteBuffer::PutFixedString (const char * str, uint length)
      99             : {
     100           0 :     ASSERT(Current != NULL  &&  str != NULL);
     101           0 :     if (ByteCount + length > BufferSize) { Err = ERROR_BufferFull; return; }
     102           0 :     ByteCount += length;
     103           0 :     while (length > 0) {
     104           0 :         *Current = *str; Current++; str++;
     105           0 :         length--;
     106             :     }
     107             : }
     108             : 
     109             : //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     110             : // ByteBuffer::GetTerminatedString():
     111             : //    Get a null-terminated string.
     112             : //    Just sets str to point to current, and then moves current
     113             : //    to the end of the string, so the calling function can to
     114             : //    duplicate the string itself if it needs to.
     115             : //    The length returned does not include the trailing '\0'.
     116             : uint
     117      111653 : ByteBuffer::GetTerminatedString (char ** str)
     118             : {
     119      111653 :     ASSERT(Current != NULL  &&  str != NULL);
     120             : 
     121      111653 :     uint length = 0;
     122      111653 :     *str = (char *) Current;
     123    33670281 :     while (*Current) {
     124    16779314 :         Current++;
     125    16779314 :         length++;
     126    16779314 :         ReadPos++;
     127             :     }
     128      111653 :     Current++;
     129      111653 :     length++;
     130      111653 :     ReadPos++;
     131      111653 :     if (ReadPos > ByteCount) { Err = ERROR_BufferRead; }
     132      111653 :     return length;
     133             : }
     134             : 
     135             : //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     136             : // ByteBuffer::PutTerminatedString():
     137             : //      Writes a null-terminated string to the buffer, including
     138             : //      the null character.
     139             : void
     140     1888408 : ByteBuffer::PutTerminatedString (const char * str)
     141             : {
     142     1888408 :     ASSERT(Current != NULL  &&  str != NULL);
     143   574955598 :     while (*str) {
     144   286533595 :         if (ByteCount >= BufferSize) { Err = ERROR_BufferFull; return; }
     145   286533595 :         *Current = *str; Current++; str++;
     146   286533595 :         ByteCount++;
     147             :     }
     148     1888408 :     *Current = 0; Current++; ByteCount++;
     149             : }
     150             : 
     151             : //////////////////////////////////////////////////////////////////////
     152             : //  EOF: bytebuf.cpp
     153             : //////////////////////////////////////////////////////////////////////

Generated by: LCOV version 1.12