2013-11-27 11:01:22 +01:00
|
|
|
#include "OysterByte.h"
|
|
|
|
|
|
|
|
using namespace Oyster::Network;
|
|
|
|
|
|
|
|
OysterByte::OysterByte()
|
|
|
|
{
|
|
|
|
size = 0;
|
|
|
|
capacity = 10;
|
|
|
|
byteArray = new unsigned char[capacity];
|
|
|
|
}
|
|
|
|
|
|
|
|
OysterByte::OysterByte(int cap)
|
|
|
|
{
|
|
|
|
size = 0;
|
|
|
|
capacity = cap;
|
|
|
|
byteArray = new unsigned char[capacity];
|
|
|
|
}
|
|
|
|
|
2013-12-04 14:58:15 +01:00
|
|
|
OysterByte::OysterByte(const OysterByte& obj)
|
|
|
|
{
|
|
|
|
this->byteArray = new unsigned char[obj.capacity];
|
|
|
|
|
2013-12-10 08:32:08 +01:00
|
|
|
for(int i = 0; i < (int)obj.size; i++)
|
2013-12-04 14:58:15 +01:00
|
|
|
{
|
|
|
|
this->byteArray[i] = obj.byteArray[i];
|
|
|
|
}
|
|
|
|
this->size = obj.size;
|
|
|
|
this->capacity = obj.capacity;
|
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
OysterByte::~OysterByte()
|
|
|
|
{
|
|
|
|
delete[] byteArray;
|
|
|
|
}
|
|
|
|
|
2013-12-03 23:12:48 +01:00
|
|
|
void OysterByte::Clear()
|
2013-11-27 11:01:22 +01:00
|
|
|
{
|
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
|
2013-12-03 23:12:48 +01:00
|
|
|
void OysterByte::Resize(unsigned int cap)
|
|
|
|
{
|
|
|
|
if(capacity < cap)
|
|
|
|
{
|
|
|
|
delete[] byteArray;
|
|
|
|
byteArray = new unsigned char[cap];
|
2013-12-17 10:58:07 +01:00
|
|
|
capacity = cap;
|
2013-12-03 23:12:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
int OysterByte::GetSize()
|
|
|
|
{
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char* OysterByte::GetByteArray()
|
|
|
|
{
|
|
|
|
return byteArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OysterByte::AddSize(unsigned int size)
|
|
|
|
{
|
2013-12-18 00:07:24 +01:00
|
|
|
int newCapacity = this->size + size;
|
2013-11-27 11:01:22 +01:00
|
|
|
|
2013-12-18 00:07:24 +01:00
|
|
|
if(newCapacity >= capacity)
|
2013-11-27 11:01:22 +01:00
|
|
|
{
|
2013-12-18 00:07:24 +01:00
|
|
|
IncreaseCapacity(newCapacity);
|
2013-11-27 11:01:22 +01:00
|
|
|
}
|
2013-12-18 00:07:24 +01:00
|
|
|
|
|
|
|
this->size += size;
|
2013-11-27 11:01:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void OysterByte::SetBytes(unsigned char* bytes)
|
|
|
|
{
|
|
|
|
delete[] byteArray;
|
|
|
|
byteArray = bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OysterByte::SetSize(unsigned int size)
|
|
|
|
{
|
|
|
|
this->size = size;
|
|
|
|
}
|
|
|
|
|
2013-12-04 14:58:15 +01:00
|
|
|
OysterByte& OysterByte::operator =(const OysterByte& obj)
|
|
|
|
{
|
|
|
|
delete[] this->byteArray;
|
|
|
|
this->byteArray = new unsigned char[obj.capacity];
|
|
|
|
|
2013-12-10 08:32:08 +01:00
|
|
|
for(int i = 0; i < (int)obj.size; i++)
|
2013-12-04 14:58:15 +01:00
|
|
|
{
|
|
|
|
this->byteArray[i] = obj.byteArray[i];
|
|
|
|
}
|
|
|
|
this->size = obj.size;
|
|
|
|
this->capacity = obj.capacity;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
OysterByte::operator char*()
|
|
|
|
{
|
|
|
|
return (char*)byteArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
OysterByte::operator const char*()
|
|
|
|
{
|
|
|
|
return (const char*)byteArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
OysterByte::operator unsigned char*()
|
|
|
|
{
|
|
|
|
return byteArray;
|
|
|
|
}
|
|
|
|
|
2013-12-17 08:45:47 +01:00
|
|
|
OysterByte& OysterByte::operator +=(const OysterByte& obj)
|
|
|
|
{
|
|
|
|
int newSize = this->size + obj.size;
|
2013-12-18 00:07:24 +01:00
|
|
|
|
2013-12-17 08:45:47 +01:00
|
|
|
if(newSize >= (int)capacity)
|
|
|
|
{
|
2013-12-18 00:07:24 +01:00
|
|
|
IncreaseCapacity(newSize);
|
2013-12-17 08:45:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = size, j = 0; i < newSize; i++, j++)
|
|
|
|
{
|
|
|
|
this->byteArray[i] = obj.byteArray[j];
|
|
|
|
}
|
2013-12-18 00:07:24 +01:00
|
|
|
|
2013-12-17 08:45:47 +01:00
|
|
|
this->size = newSize;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
/////////////
|
|
|
|
// Private //
|
|
|
|
/////////////
|
|
|
|
|
2013-12-18 00:07:24 +01:00
|
|
|
void OysterByte::IncreaseCapacity(unsigned int newCapacity)
|
2013-11-27 11:01:22 +01:00
|
|
|
{
|
2013-12-18 00:07:24 +01:00
|
|
|
capacity = newCapacity * 2;
|
2013-11-27 11:01:22 +01:00
|
|
|
unsigned char* temp = new unsigned char[capacity];
|
|
|
|
|
2013-12-18 00:07:24 +01:00
|
|
|
for(int i = 0; i < (int)this->size; i++)
|
2013-11-27 11:01:22 +01:00
|
|
|
{
|
|
|
|
temp[i] = byteArray[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] byteArray;
|
|
|
|
byteArray = temp;
|
|
|
|
}
|