Scid  4.7.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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  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  ByteBuffer(size_t 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, size_t length);
57 
58  errorT Status () { return Err; }
59  size_t GetByteCount() { return ByteCount; }
60  void BackToStart ();
61  void Skip (size_t 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, size_t length);
70  void 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, 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 //////////////////////////////////////////////////////////////////////
unsigned char byte
Definition: common.h:89
byte GetByte()
Definition: bytebuf.h:62
#define ASSERT(f)
Definition: common.h:59
size_t GetByteCount()
Definition: bytebuf.h:59
void GetFixedString(char *str, size_t length)
Definition: bytebuf.cpp:80
void Skip(size_t value)
Definition: bytebuf.cpp:66
const errorT ERROR_BufferRead
Definition: error.h:79
void BackToStart()
Definition: bytebuf.cpp:37
void ProvideExternal(byte *data, size_t length)
Definition: bytebuf.cpp:52
void GetTerminatedString(char **str)
Definition: bytebuf.cpp:117
errorT Status()
Definition: bytebuf.h:58
unsigned short errorT
Definition: error.h:20
void PutFixedString(const char *str, size_t length)
Definition: bytebuf.cpp:98
ByteBuffer(size_t length)
Definition: bytebuf.h:46
void Empty()
Definition: bytebuf.cpp:23
const errorT ERROR_BufferFull
Definition: error.h:78
neww ?psize?
Definition: board.tcl:318
void PutTerminatedString(const char *str)
Definition: bytebuf.cpp:136
void PutByte(byte value)
Definition: bytebuf.h:80
const byte * getData()
Definition: bytebuf.h:71
~ByteBuffer()
Definition: bytebuf.h:47