Scid  4.6.5
bytebuf.cpp
Go to the documentation of this file.
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
24 {
25  ReadPos = ByteCount = 0;
26  ExternalBuffer = NULL;
27  Buffer = AllocatedBuffer;
28  Current = Buffer;
29  Err = OK;
30 }
31 
32 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33 // ByteBuffer::BackToStart():
34 // Sets the ByteBuffer's read position back to the buffer start.
35 //
36 void
38 {
39  ReadPos = 0;
40  Current = Buffer;
41  Err = OK;
42 }
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
53 {
54  ExternalBuffer = data;
55  ByteCount = length;
56  ReadPos = 0;
57  Current = Buffer = ExternalBuffer;
58  Err = OK;
59 }
60 
61 
62 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
63 // ByteBuffer::Skip():
64 // Skips over a specified number of bytes.
65 void
67 {
68  ASSERT (Current != NULL);
69 
70  if (ReadPos + length > ByteCount) { Err = ERROR_BufferRead; return; }
71  ReadPos += length;
72  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 ByteBuffer::GetFixedString (char * str, uint length)
81 {
82  ASSERT(Current != NULL && str != NULL);
83 
84  if (Err != OK) { return; }
85  if (ReadPos + length > ByteCount) { Err = ERROR_BufferRead; return; }
86  ReadPos += length;
87  while (length > 0) {
88  *str = *Current; Current++; str++;
89  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 ByteBuffer::PutFixedString (const char * str, uint length)
99 {
100  ASSERT(Current != NULL && str != NULL);
101  if (ByteCount + length > BufferSize) { Err = ERROR_BufferFull; return; }
102  ByteCount += length;
103  while (length > 0) {
104  *Current = *str; Current++; str++;
105  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
118 {
119  ASSERT(Current != NULL && str != NULL);
120 
121  uint length = 0;
122  *str = (char *) Current;
123  while (*Current) {
124  Current++;
125  length++;
126  ReadPos++;
127  }
128  Current++;
129  length++;
130  ReadPos++;
131  if (ReadPos > ByteCount) { Err = ERROR_BufferRead; }
132  return length;
133 }
134 
135 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
136 // ByteBuffer::PutTerminatedString():
137 // Writes a null-terminated string to the buffer, including
138 // the null character.
139 void
141 {
142  ASSERT(Current != NULL && str != NULL);
143  while (*str) {
144  if (ByteCount >= BufferSize) { Err = ERROR_BufferFull; return; }
145  *Current = *str; Current++; str++;
146  ByteCount++;
147  }
148  *Current = 0; Current++; ByteCount++;
149 }
150 
151 //////////////////////////////////////////////////////////////////////
152 // EOF: bytebuf.cpp
153 //////////////////////////////////////////////////////////////////////
unsigned char byte
Definition: common.h:97
void Skip(uint value)
Definition: bytebuf.cpp:66
const errorT OK
Definition: error.h:23
#define ASSERT(f)
Definition: common.h:67
const errorT ERROR_BufferRead
Definition: error.h:79
void BackToStart()
Definition: bytebuf.cpp:37
uint32_t uint
Definition: common.h:99
void PutFixedString(const char *str, uint length)
Definition: bytebuf.cpp:98
void GetFixedString(char *str, uint length)
Definition: bytebuf.cpp:80
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