src/bytestream.cpp

changeset 10
bc1414343e19
parent 6
67b6ef6917ba
child 13
9bdddd2ccde6
equal deleted inserted replaced
9:f9893eea978b 10:bc1414343e19
5 union { 5 union {
6 uint32 i; 6 uint32 i;
7 float f; 7 float f;
8 } g_floatunion; 8 } g_floatunion;
9 9
10 Bytestream::Bytestream( ulong len ) { 10 Bytestream::Bytestream (ulong len) {
11 m_data = null; 11 m_data = null;
12 resize( len ); 12 resize (len);
13 clear(); 13 clear();
14 } 14 }
15 15
16 Bytestream::Bytestream( const char* data, ulong len ) { 16 Bytestream::Bytestream (const char* data, ulong len) {
17 m_data = null; 17 m_data = null;
18 init( data, len ); 18 init (data, len);
19 } 19 }
20 20
21 void Bytestream::resize( ulong newsize ) { 21 void Bytestream::resize (ulong newsize) {
22 char* olddata = null; 22 char* olddata = null;
23 ulong oldsize; 23 ulong oldsize;
24 24
25 if( m_data ) { 25 if (m_data) {
26 oldsize = m_size; 26 oldsize = m_size;
27 olddata = new char[oldsize]; 27 olddata = new char[oldsize];
28 memcpy( olddata, m_data, oldsize ); 28 memcpy (olddata, m_data, oldsize);
29 } 29 }
30 30
31 delete[] m_data; 31 delete[] m_data;
32 m_data = new uint8[newsize]; 32 m_data = new uint8[newsize];
33 m_size = newsize; 33 m_size = newsize;
34 34
35 if( olddata ) 35 if (olddata)
36 memcpy( m_data, olddata, min<ulong> ( oldsize, newsize )); 36 memcpy (m_data, olddata, min<ulong> (oldsize, newsize));
37 } 37 }
38 38
39 void Bytestream::init( const char* data, ulong len ) { 39 void Bytestream::init (const char* data, ulong len) {
40 resize( len ); 40 resize (len);
41 memcpy( m_data, data, len ); 41 memcpy (m_data, data, len);
42 m_ptr = &m_data[0]; 42 m_ptr = &m_data[0];
43 m_len = len; 43 m_len = len;
44 } 44 }
45 45
46 size_t Bytestream::len() const { 46 size_t Bytestream::len() const {
50 void Bytestream::clear() { 50 void Bytestream::clear() {
51 m_ptr = &m_data[0]; 51 m_ptr = &m_data[0];
52 m_len = 0; 52 m_len = 0;
53 } 53 }
54 54
55 uint8& Bytestream::subscript( ulong idx ) { 55 uint8& Bytestream::subscript (ulong idx) {
56 return m_data[idx]; 56 return m_data[idx];
57 } 57 }
58 58
59 const uint8& Bytestream::const_subscript( ulong idx ) const { 59 const uint8& Bytestream::const_subscript (ulong idx) const {
60 return m_data[idx]; 60 return m_data[idx];
61 } 61 }
62 62
63 void Bytestream::seek( ulong pos ) { 63 void Bytestream::seek (ulong pos) {
64 m_ptr = m_data + pos; 64 m_ptr = m_data + pos;
65 } 65 }
66 66
67 // ============================================================================= 67 // =============================================================================
68 void Bytestream::rewind() { 68 void Bytestream::rewind() {
69 m_ptr = m_data; 69 m_ptr = m_data;
70 } 70 }
71 71
72 ulong Bytestream::bytesLeft() const { 72 ulong Bytestream::bytesLeft() const {
73 return ( m_len - ( m_ptr - &m_data[0] )); 73 return (m_len - (m_ptr - &m_data[0]));
74 } 74 }
75 75
76 ulong Bytestream::spaceLeft() const { 76 ulong Bytestream::spaceLeft() const {
77 return ( m_size - m_len ); 77 return (m_size - m_len);
78 } 78 }
79 79
80 // ============================================================================= 80 // =============================================================================
81 bool Bytestream::readByte( uint8& val ) { 81 bool Bytestream::readByte (uint8& val) {
82 if( bytesLeft() < 1 ) 82 if (bytesLeft() < 1)
83 return false; 83 return false;
84 84
85 val = *m_ptr++; 85 val = *m_ptr++;
86 return true; 86 return true;
87 } 87 }
88 88
89 // ============================================================================= 89 // =============================================================================
90 bool Bytestream::readShort( uint16& val ) { 90 bool Bytestream::readShort (uint16& val) {
91 if( bytesLeft() < 2 ) 91 if (bytesLeft() < 2)
92 return false; 92 return false;
93 93
94 val = 0; 94 val = 0;
95 95
96 for( int i = 0; i < 2; ++i ) 96 for (int i = 0; i < 2; ++i)
97 val |= *m_ptr++ << ( i * 8 ); 97 val |= *m_ptr++ << (i * 8);
98 98
99 return true; 99 return true;
100 } 100 }
101 101
102 // ============================================================================= 102 // =============================================================================
103 bool Bytestream::readLong( uint32& val ) { 103 bool Bytestream::readLong (uint32& val) {
104 if( bytesLeft() < 4 ) 104 if (bytesLeft() < 4)
105 return false; 105 return false;
106 106
107 val = 0; 107 val = 0;
108 108
109 for( int i = 0; i < 4; ++i ) 109 for (int i = 0; i < 4; ++i)
110 val |= *m_ptr++ << ( i * 8 ); 110 val |= *m_ptr++ << (i * 8);
111 111
112 return true; 112 return true;
113 } 113 }
114 114
115 // ============================================================================= 115 // =============================================================================
116 bool Bytestream::readFloat( float& val ) { 116 bool Bytestream::readFloat (float& val) {
117 if( !readLong( g_floatunion.i )) 117 if (!readLong (g_floatunion.i))
118 return false; 118 return false;
119 119
120 val = g_floatunion.f; 120 val = g_floatunion.f;
121 return true; 121 return true;
122 } 122 }
123 123
124 // ============================================================================= 124 // =============================================================================
125 bool Bytestream::readString( str& val ) { 125 bool Bytestream::readString (str& val) {
126 if( bytesLeft() < 1 ) // need at least the null terminator 126 if (bytesLeft() < 1) // need at least the null terminator
127 return false; 127 return false;
128 128
129 uint8_t c; 129 uint8_t c;
130 130
131 while( readByte( c ) && c != '\0' ) 131 while (readByte (c) && c != '\0')
132 val += ( char ) c; 132 val += (char) c;
133 133
134 return true; 134 return true;
135 } 135 }
136 136
137 // ============================================================================= 137 // =============================================================================
138 void Bytestream::doWrite( uint8_t val ) { 138 void Bytestream::doWrite (uint8_t val) {
139 *m_ptr++ = val; 139 *m_ptr++ = val;
140 m_len++; 140 m_len++;
141 } 141 }
142 142
143 void Bytestream::growToFit( ulong bytes ) { 143 void Bytestream::growToFit (ulong bytes) {
144 if( spaceLeft() < bytes ) 144 if (spaceLeft() < bytes)
145 resize( m_size + bytes + 128 ); 145 resize (m_size + bytes + 128);
146 } 146 }
147 147
148 bool Bytestream::readBytes( uint8 numbytes, uint8* val ) { 148 bool Bytestream::readBytes (uint8 numbytes, uint8* val) {
149 while( numbytes-- ) 149 while (numbytes--)
150 if( !readByte( *val++ )) 150 if (!readByte (*val++))
151 return false; 151 return false;
152 152
153 return true; 153 return true;
154 } 154 }
155 155
156 void Bytestream::writeBytes( uint8 numbytes, const uint8* val ) { 156 void Bytestream::writeBytes (uint8 numbytes, const uint8* val) {
157 growToFit( numbytes ); 157 growToFit (numbytes);
158 158
159 while( numbytes-- ) 159 while (numbytes--)
160 writeByte( *val++ ); 160 writeByte (*val++);
161 } 161 }
162 162
163 // ============================================================================= 163 // =============================================================================
164 void Bytestream::writeByte( uint8 val ) { 164 void Bytestream::writeByte (uint8 val) {
165 growToFit( 1 ); 165 growToFit (1);
166 doWrite( val ); 166 doWrite (val);
167 } 167 }
168 168
169 // ============================================================================= 169 // =============================================================================
170 void Bytestream::writeShort( uint16 val ) { 170 void Bytestream::writeShort (uint16 val) {
171 growToFit( 2 ); 171 growToFit (2);
172 172
173 for( int i = 0; i < 2; ++i ) 173 for (int i = 0; i < 2; ++i)
174 doWrite(( val >> ( i * 8 )) & 0xFF ); 174 doWrite ( (val >> (i * 8)) & 0xFF);
175 } 175 }
176 176
177 // ============================================================================= 177 // =============================================================================
178 void Bytestream::writeLong( uint32 val ) { 178 void Bytestream::writeLong (uint32 val) {
179 growToFit( 4 ); 179 growToFit (4);
180 180
181 for( int i = 0; i < 4; ++i ) 181 for (int i = 0; i < 4; ++i)
182 doWrite(( val >> ( i * 8 )) & 0xFF ); 182 doWrite ( (val >> (i * 8)) & 0xFF);
183 } 183 }
184 184
185 // ============================================================================= 185 // =============================================================================
186 void Bytestream::writeFloat( float val ) { 186 void Bytestream::writeFloat (float val) {
187 g_floatunion.f = val; 187 g_floatunion.f = val;
188 writeLong( g_floatunion.i ); 188 writeLong (g_floatunion.i);
189 } 189 }
190 190
191 // ============================================================================= 191 // =============================================================================
192 void Bytestream::writeString( str val ) { 192 void Bytestream::writeString (str val) {
193 growToFit( val.length() + 1 ); 193 growToFit (val.length() + 1);
194 194
195 for( qchar c : val ) 195 for (qchar c : val)
196 doWrite( c.toAscii() ); 196 doWrite (c.toAscii());
197 197
198 doWrite( '\0' ); 198 doWrite ('\0');
199 } 199 }
200 200
201 // ============================================================================= 201 // =============================================================================
202 bool Bytestream::tryMerge( const Bytestream& other ) { 202 bool Bytestream::tryMerge (const Bytestream& other) {
203 if( spaceLeft() < other.len() ) 203 if (spaceLeft() < other.len())
204 return false; 204 return false;
205 205
206 for( ulong i = 0; i < other.len(); ++i ) 206 for (ulong i = 0; i < other.len(); ++i)
207 writeByte( other[i] ); 207 writeByte (other[i]);
208 208
209 return true; 209 return true;
210 } 210 }
211 211
212 void Bytestream::merge( const Bytestream& other ) { 212 void Bytestream::merge (const Bytestream& other) {
213 growToFit( other.len() ); 213 growToFit (other.len());
214 214
215 if( !tryMerge( other )) { 215 if (!tryMerge (other)) {
216 // Shouldn't happen 216 // Shouldn't happen
217 fprint( stderr, "ByteStream: Not enough space for merge (%1 bytes left, need %2)", 217 fprint (stderr, "ByteStream: Not enough space for merge (%1 bytes left, need %2)",
218 spaceLeft(), other.len() ); 218 spaceLeft(), other.len());
219 abort(); 219 abort();
220 } 220 }
221 } 221 }
222 222
223 const uint8* Bytestream::data() const { 223 const uint8* Bytestream::data() const {

mercurial