#include "ByteArray.h"
ByteArray::ByteArray(quint8 Mydata)
{
this->length = 0;
this->dataStore = NULL;
this->dataStore = (quint8*)calloc(this->length,sizeof(quint8));
if(this->dataStore != NULL)
{
this->length = 1;
memset(this->dataStore,0,this->length);
this->dataStore[0] = Mydata;
}
else
{
throw;
}
}
ByteArray::ByteArray(quint16 Mydata)
{
this->length = 0;
this->dataStore = NULL;
this->dataStore = (quint8*)calloc(this->length,sizeof(quint8));
if(this->dataStore != NULL)
{
this->length = 2;
memset(this->dataStore,0,this->length);
this->dataStore[0] = (quint8)((quint32)(Mydata & 0xFF00) >> 8);
this->dataStore[1] = (quint8)((Mydata & 0xFF));
}
else
{
throw;
}
}
ByteArray::ByteArray(quint32 Mydata)
{
this->length = 0;
this->dataStore = NULL;
this->dataStore = (quint8*)calloc(this->length,sizeof(quint8));
if(this->dataStore != NULL)
{
this->length = 4;
memset(this->dataStore,0,this->length);
this->dataStore[0] = (quint8)((quint64)(Mydata & 0xFF000000) >> 24);
this->dataStore[1] = (quint8)((quint64)(Mydata & 0xFF0000) >> 16);
this->dataStore[2] = (quint8)((quint64)(Mydata & 0xFF00) >> 8);
this->dataStore[3] = (quint8)((quint64)(Mydata & 0xFF));
}
else
{
throw;
}
}
ByteArray::ByteArray(quint64 Mydata)
{
this->length = 0;
this->dataStore = NULL;
quint32 tmpDataL = (quint32)(Mydata >> 32),
tmpDataR = (quint32)((quint64)(Mydata & 0xFFFFFFFF));
try
{
ByteArray tmpByteArrayL(tmpDataL);
ByteArray tmpByteArrayR(tmpDataR);
this->dataStore = (quint8*)calloc(this->length,sizeof(quint8));
if(this->dataStore != NULL)
{
this -> length = 8;
memset(this->dataStore,0,this->length);
this -> dataStore[0] = tmpByteArrayL.dataStore[0];
this -> dataStore[1] = tmpByteArrayL.dataStore[1];
this -> dataStore[2] = tmpByteArrayL.dataStore[2];
this -> dataStore[3] = tmpByteArrayL.dataStore[3];
this -> dataStore[4] = tmpByteArrayR.dataStore[0];
this -> dataStore[5] = tmpByteArrayR.dataStore[1];
this -> dataStore[6] = tmpByteArrayR.dataStore[2];
this -> dataStore[7] = tmpByteArrayR.dataStore[3];
}
else
{
throw;
}
}
catch(...)
{
throw;
}
}
ByteArray::ByteArray(ByteArray *Mydata)
{
this->length = ((Mydata != NULL)? Mydata -> Length() : 0);
if(this->length > 0)
{
this -> dataStore = (quint8*)calloc(this->length,sizeof(quint8));
if(this->dataStore != NULL)
{
memset(this->dataStore,0,this->length);
memcpy(this->dataStore,Mydata->dataStore,this->length);
}
else
{
this->length = 0;
throw;
}
}
else
{
throw;
}
}
ByteArray::ByteArray(quint8* Mydata ,qint64 Mylength)
{
if(Mylength > 0)
{
this->length = Mylength;
this->dataStore = (quint8*)calloc(this->length,sizeof(quint8));
if(this->dataStore != NULL)
{
memset(this->dataStore,0,this->length);
memcpy(this->dataStore,Mydata,this->length);
}
else
{
this->length = 0;
throw;
}
}
else
{
throw;
}
}
ByteArray::ByteArray(QString Mydata)
{
if(Mydata != "")
{
QByteArray tmpQByteArray = Mydata.toUtf8();
this->length = tmpQByteArray.length();
this->dataStore = (quint8*)calloc(this->length,sizeof(quint8));
if(this->dataStore != NULL)
{
memset(this->dataStore,0,this->length);
const char* tmpArray = tmpQByteArray.constData();
memcpy(this->dataStore,(quint8*)tmpArray,this->length);
}
else
{
this->length = 0;
throw;
}
}
else
{
throw;
}
}
ByteArray::ByteArray(QString Mydata, bool MyuseAscii)
{
if (Mydata != NULL)
{
if(MyuseAscii)
{
QByteArray tmpQByteArray = Mydata.toLocal8Bit();
this->length = tmpQByteArray.length();
this->dataStore = (quint8*)calloc(this->length,sizeof(quint8));
if(this->dataStore != NULL)
{
memset(this->dataStore,0,this->length);
const char* tmpArray = tmpQByteArray.constData();
memcpy(this->dataStore,(quint8*)tmpArray,this->length);
}
else
{
this->length = 0;
throw;
}
}
else
{
QByteArray tmpQByteArray = Mydata.toUtf8();
this->length = tmpQByteArray.length();
this->dataStore= (quint8*)calloc(this->length,sizeof(quint8));
if(this->dataStore != NULL)
{
memset(this->dataStore,0,this->length);
const char* tmpArray = tmpQByteArray.constData();
memcpy(this->dataStore,(quint8*)tmpArray,this->length);
}
else
{
this->length = 0;
throw;
}
}
}
else
{
throw;
}
}
ByteArray::ByteArray(QByteArray* Mydata)
{
if(Mydata != NULL)
{
this->length = Mydata -> length();
this->dataStore= (quint8*)calloc(this->length,sizeof(quint8));
if(this->dataStore != NULL)
{
memset(this->dataStore,0,this->length);
const char* tmpArray = Mydata->constData();
memcpy(this->dataStore,(quint8*)tmpArray,this->length);
}
else
{
this->length = 0;
throw;
}
}
else
{
throw;
}
}
ByteArray::ByteArray(QVector<quint8> Mydata)
{
if(Mydata.size() > 0)
{
this->length = Mydata.size();
this->dataStore = (quint8*)calloc(this->length,sizeof(quint8));
if(this->dataStore != NULL)
{
memset(this->dataStore,0,this->length);
for (int i = 0; i < Mydata.size(); ++i) dataStore[i] = Mydata[i];
}
else
{
this->length = 0;
throw;
}
}
else
{
throw;
}
}
ByteArray::~ByteArray(void)
{
if(this->dataStore != NULL)
{
try
{
memset(this->dataStore,0,this->length);
this->length = 0;
free(this->dataStore);
}
catch(...){throw;}
}
}
qint32 ByteArray::Length(){return (qint32)this->length;}
quint8 ByteArray::operator [](qint32 Myindex)
{
if((this->dataStore != NULL) && (this->length > 0) && (this->length > Myindex)) return dataStore[Myindex];
else
{
throw;
}
}
ByteArray* ByteArray::Copy()
{
if((this->dataStore != NULL) && (this->length > 0)) try{return new ByteArray(this);}catch(...){throw;}
else
{
return NULL;
}
}
ByteArray* ByteArray::Copy(qint32 Mylength)
{
if((this->dataStore != NULL) && (this -> length > 0))
{
if(this -> length > Mylength) try{return new ByteArray(&(this->dataStore[0]),Mylength);}catch(...){throw;}
else return new ByteArray(this);
}
else
{
return NULL;
}
}
ByteArray* ByteArray::Copy(qint32 MystartIndex, qint32 Mylength)
{
if((this->dataStore != NULL) &&
(this -> length > 0) &&
(this -> length > MystartIndex) &&
(this -> length >= MystartIndex + Mylength))
try{return new ByteArray(&(this->dataStore[MystartIndex]), Mylength);}catch(...){throw;}
else
{
return NULL;
}
}
ByteArray* ByteArray::CopyFrom(qint32 MystartIndex)
{
if((this -> length > 0) && (this -> length > MystartInde