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(unsigned int position
, bool relative
)
92 if (position
> datasize
)
94 this->position
= position
;
98 if (this->position
+ position
> datasize
)
99 position
= datasize
- this->position
;
100 this->position
+= position
;
104 int Buffer::readData(void *dest
, unsigned int size
)
106 if (datasize
- position
< size
)
108 size
= datasize
- position
;
110 memcpy(dest
, data
+ position
, size
);
114 int Buffer::writeData(const void *source
, unsigned int size
, bool resize
)
116 if (this->size
- datasize
< size
)
120 setSize(datasize
+ size
);
124 size
= this->size
- datasize
;
127 memcpy(data
+ datasize
, source
, size
);
132 void Buffer::writeInt(int value
)
134 writeData(&value
, sizeof(int), true);
136 int Buffer::readInt(void)
139 readData(&value
, sizeof(int));
142 void Buffer::writeFloat(float value
)
144 writeData(&value
, sizeof(float), true);
146 float Buffer::readFloat(void)
149 readData(&value
, sizeof(float));
152 void Buffer::writeString(std::string str
)
154 writeData(str
.c_str(), str
.length() + 1, true);
156 std::string
Buffer::readString(void)
158 std::string str
= data
+ position
;
163 void Buffer::writeByte(unsigned char value
)
165 writeData(&value
, sizeof(unsigned char), true);
167 unsigned char Buffer::readByte(void)
170 readData(&value
, sizeof(unsigned char));
173 void Buffer::writeWord(short value
)
175 writeData(&value
, sizeof(short), true);
177 short Buffer::readWord(void)
180 readData(&value
, sizeof(short));
183 void Buffer::writeBool(bool flag
)
187 bool Buffer::readBool(void)
189 bool flag
= readByte();
192 void Buffer::writeVector2D(Vector2D v
)
197 Vector2D
Buffer::readVector2D(void)
204 void Buffer::writeVector3D(Vector3D v
)
210 Vector3D
Buffer::readVector3D(void)
218 void Buffer::writeQuaternion(Quaternion q
)
220 writeWord((unsigned short)(q
.x
* (1<<15)));
221 writeWord((unsigned short)(q
.y
* (1<<15)));
222 writeWord((unsigned short)(q
.z
* (1<<15)));
223 writeWord((unsigned short)(q
.w
* (1<<15)));
225 Quaternion
Buffer::readQuaternion(void)
228 q
.x
= (float)readWord() / (1<<15);
229 q
.y
= (float)readWord() / (1<<15);
230 q
.z
= (float)readWord() / (1<<15);
231 q
.w
= (float)readWord() / (1<<15);
236 char *Buffer::getData(void)
241 void Buffer::clear(void)
251 Buffer
&Buffer::operator=(const Buffer
&buf
)
257 Buffer
&Buffer::operator+=(const Buffer
&buf
)
259 writeData(buf
.data
, buf
.datasize
, true);
262 Buffer
&Buffer::operator<<(const Buffer
&buf
)
264 writeData(buf
.data
, buf
.datasize
, true);
267 Buffer
&Buffer::operator<<(int &data
)
269 writeData(&data
, sizeof(int), true);
272 Buffer
&Buffer::operator<<(float &data
)
274 writeData(&data
, sizeof(float), true);