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 11108 : ByteBuffer::Empty()
24 : {
25 11108 : ReadPos = ByteCount = 0;
26 11108 : ExternalBuffer = NULL;
27 11108 : Buffer = AllocatedBuffer;
28 11108 : Current = Buffer;
29 11108 : Err = OK;
30 11108 : }
31 :
32 : //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33 : // ByteBuffer::BackToStart():
34 : // Sets the ByteBuffer's read position back to the buffer start.
35 : //
36 : void
37 4 : ByteBuffer::BackToStart()
38 : {
39 4 : ReadPos = 0;
40 4 : Current = Buffer;
41 4 : Err = OK;
42 4 : }
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 1018 : ByteBuffer::ProvideExternal (byte * data, size_t length)
53 : {
54 1018 : ExternalBuffer = data;
55 1018 : ByteCount = length;
56 1018 : ReadPos = 0;
57 1018 : Current = Buffer = ExternalBuffer;
58 1018 : Err = OK;
59 1018 : }
60 :
61 :
62 : //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
63 : // ByteBuffer::Skip():
64 : // Skips over a specified number of bytes.
65 : void
66 0 : ByteBuffer::Skip (size_t 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 12 : ByteBuffer::GetFixedString (char * str, size_t length)
81 : {
82 12 : ASSERT(Current != NULL && str != NULL);
83 :
84 12 : if (Err != OK) { return; }
85 12 : if (ReadPos + length > ByteCount) { Err = ERROR_BufferRead; return; }
86 12 : ReadPos += length;
87 228 : while (length > 0) {
88 108 : *str = *Current; Current++; str++;
89 108 : 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 18 : ByteBuffer::PutFixedString (const char * str, size_t length)
99 : {
100 18 : ASSERT(Current != NULL && str != NULL);
101 18 : if (ByteCount + length > BufferSize) { Err = ERROR_BufferFull; return; }
102 18 : ByteCount += length;
103 342 : while (length > 0) {
104 162 : *Current = *str; Current++; str++;
105 162 : 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
117 111977 : ByteBuffer::GetTerminatedString (char ** str)
118 : {
119 111977 : ASSERT(Current != NULL && str != NULL);
120 :
121 111977 : *str = (char *) Current;
122 33727733 : while (*Current) {
123 16807878 : Current++;
124 16807878 : ReadPos++;
125 : }
126 111977 : Current++;
127 111977 : ReadPos++;
128 111977 : if (ReadPos > ByteCount) { Err = ERROR_BufferRead; }
129 111977 : }
130 :
131 : //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
132 : // ByteBuffer::PutTerminatedString():
133 : // Writes a null-terminated string to the buffer, including
134 : // the null character.
135 : void
136 1390894 : ByteBuffer::PutTerminatedString (const char * str)
137 : {
138 1390894 : ASSERT(Current != NULL && str != NULL);
139 422215776 : while (*str) {
140 210412441 : if (ByteCount >= BufferSize) { Err = ERROR_BufferFull; return; }
141 210412441 : *Current = *str; Current++; str++;
142 210412441 : ByteCount++;
143 : }
144 1390894 : *Current = 0; Current++; ByteCount++;
145 : }
146 :
147 : //////////////////////////////////////////////////////////////////////
148 : // EOF: bytebuf.cpp
149 : //////////////////////////////////////////////////////////////////////
|