2 Copyright (C) 2008 Mathias Gottschlag
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in the
6 Software without restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #include "core/Buffer.h"
36 Buffer::Buffer(unsigned int size
)
38 data
= (char*)malloc(size
);
43 Buffer::Buffer(void *data
, unsigned int size
)
45 this->data
= (char*)malloc(size
);
49 memcpy(this->data
, data
, size
);
51 Buffer::Buffer(const Buffer
&buf
)
65 void Buffer::setSize(unsigned int size
)
67 data
= (char*)realloc(data
, size
);
70 if (position
> datasize
)
75 unsigned int Buffer::getSize(void)
79 unsigned int Buffer::getDataSize(void)
84 unsigned int Buffer::getPosition(void)
88 void Buffer::setPosition(int position
, bool relative
)
92 if (position
< 0) position
= 0;
93 if (position
> (int)datasize
)
95 this->position
= position
;
99 if (this->position
+ position
> datasize
)
100 position
= datasize
- this->position
;
101 if ((int)this->position
+ position
< 0)
102 position
= -this->position
;
103 this->position
= (int)this->position
+ position
;
107 int Buffer::readData(void *dest
, unsigned int size
)
109 if (datasize
- position
< size
)
111 size
= datasize
- position
;
113 memcpy(dest
, data
+ position
, size
);
117 int Buffer::writeData(const void *source
, unsigned int size
, bool resize
)
119 if (this->size
- datasize
< size
)
123 setSize(datasize
+ size
);
127 size
= this->size
- datasize
;
130 memcpy(data
+ datasize
, source
, size
);
135 void Buffer::writeInt(int value
)
137 writeData(&value
, sizeof(int), true);
139 int Buffer::readInt(void)
142 readData(&value
, sizeof(int));
145 void Buffer::writeFloat(float value
)
147 writeData(&value
, sizeof(float), true);
149 float Buffer::readFloat(void)
152 readData(&value
, sizeof(float));
155 void Buffer::writeString(std::string str
)
157 writeData(str
.c_str(), str
.length() + 1, true);
159 std::string
Buffer::readString(void)
161 std::string str
= data
+ position
;
166 void Buffer::writeByte(unsigned char value
)
168 writeData(&value
, sizeof(unsigned char), true);
170 unsigned char Buffer::readByte(void)
173 readData(&value
, sizeof(unsigned char));
176 void Buffer::writeWord(short value
)
178 writeData(&value
, sizeof(short), true);
180 short Buffer::readWord(void)
183 readData(&value
, sizeof(short));
186 void Buffer::writeBool(bool flag
)
190 bool Buffer::readBool(void)
192 bool flag
= readByte();
195 void Buffer::writeVector2D(Vector2D v
)
200 Vector2D
Buffer::readVector2D(void)
207 void Buffer::writeVector3D(Vector3D v
)
213 Vector3D
Buffer::readVector3D(void)
221 void Buffer::writeQuaternion(Quaternion q
)
223 writeWord((unsigned short)(q
.x
* (1<<15)));
224 writeWord((unsigned short)(q
.y
* (1<<15)));
225 writeWord((unsigned short)(q
.z
* (1<<15)));
226 writeWord((unsigned short)(q
.w
* (1<<15)));
228 Quaternion
Buffer::readQuaternion(void)
231 q
.x
= (float)readWord() / (1<<15);
232 q
.y
= (float)readWord() / (1<<15);
233 q
.z
= (float)readWord() / (1<<15);
234 q
.w
= (float)readWord() / (1<<15);
239 char *Buffer::getData(void)
244 void Buffer::clear(void)
254 Buffer
&Buffer::operator=(const Buffer
&buf
)
260 Buffer
&Buffer::operator+=(const Buffer
&buf
)
262 writeData(buf
.data
, buf
.datasize
, true);
265 Buffer
&Buffer::operator<<(const Buffer
&buf
)
267 writeData(buf
.data
, buf
.datasize
, true);
270 Buffer
&Buffer::operator<<(int &data
)
272 writeData(&data
, sizeof(int), true);
275 Buffer
&Buffer::operator<<(float &data
)
277 writeData(&data
, sizeof(float), true);