Scid  4.6.5
bytebuf.h
Go to the documentation of this file.
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 
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  ByteBuffer(uint length) : BufferSize(length), AllocatedBuffer(new byte[length]) { Empty(); }
47  ~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);
63  ASSERT(Current != NULL);
64  if (ReadPos >= ByteCount) { Err = ERROR_BufferRead; return 0; }
65  byte b = *Current;
66  Current++; ReadPos++;
67  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  void PutByte (byte value) {
81  ASSERT(Current != NULL);
82  ASSERT(Buffer == AllocatedBuffer);
83  if (Buffer == AllocatedBuffer && ByteCount >= BufferSize) {
84  Err = ERROR_BufferFull; return;
85  }
86  *Current = value;
87  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 //////////////////////////////////////////////////////////////////////
unsigned char byte
Definition: common.h:97
void Skip(uint value)
Definition: bytebuf.cpp:66
byte GetByte()
Definition: bytebuf.h:62
#define ASSERT(f)
Definition: common.h:67
const errorT ERROR_BufferRead
Definition: error.h:79
void BackToStart()
Definition: bytebuf.cpp:37
uint GetByteCount()
Definition: bytebuf.h:59
uint32_t uint
Definition: common.h:99
errorT Status()
Definition: bytebuf.h:58
void PutFixedString(const char *str, uint length)
Definition: bytebuf.cpp:98
void GetFixedString(char *str, uint length)
Definition: bytebuf.cpp:80
unsigned short errorT
Definition: error.h:20
ByteBuffer(uint length)
Definition: bytebuf.h:46
neww?psize?
Definition: board.tcl:318
void ProvideExternal(byte *data, uint length)
Definition: bytebuf.cpp:52
void Empty()
Definition: bytebuf.cpp:23
const errorT ERROR_BufferFull
Definition: error.h:78
void PutTerminatedString(const char *str)
Definition: bytebuf.cpp:140
uint GetTerminatedString(char **str)
Definition: bytebuf.cpp:117
void PutByte(byte value)
Definition: bytebuf.h:80
const byte * getData()
Definition: bytebuf.h:71
~ByteBuffer()
Definition: bytebuf.h:47