Scid  4.7.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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
66 ByteBuffer::Skip (size_t length)
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, size_t 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, size_t 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 void
118 {
119  ASSERT(Current != NULL && str != NULL);
120 
121  *str = (char *) Current;
122  while (*Current) {
123  Current++;
124  ReadPos++;
125  }
126  Current++;
127  ReadPos++;
128  if (ReadPos > ByteCount) { Err = ERROR_BufferRead; }
129 }
130 
131 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
132 // ByteBuffer::PutTerminatedString():
133 // Writes a null-terminated string to the buffer, including
134 // the null character.
135 void
137 {
138  ASSERT(Current != NULL && str != NULL);
139  while (*str) {
140  if (ByteCount >= BufferSize) { Err = ERROR_BufferFull; return; }
141  *Current = *str; Current++; str++;
142  ByteCount++;
143  }
144  *Current = 0; Current++; ByteCount++;
145 }
146 
147 //////////////////////////////////////////////////////////////////////
148 // EOF: bytebuf.cpp
149 //////////////////////////////////////////////////////////////////////
unsigned char byte
Definition: common.h:89
const errorT OK
Definition: error.h:23
#define ASSERT(f)
Definition: common.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
void PutFixedString(const char *str, size_t length)
Definition: bytebuf.cpp:98
void Empty()
Definition: bytebuf.cpp:23
const errorT ERROR_BufferFull
Definition: error.h:78
void PutTerminatedString(const char *str)
Definition: bytebuf.cpp:136