#include "xrawfile.h"

XRawFile::XRawFile()
    : XAsset()
    , mName(new XString())
    , mLength(0)
    , mBuffer()
{

}

XRawFile::~XRawFile()
{
    delete mName;
}

void XRawFile::SetName(XString* aName)
{
    if (aName == nullptr)
    {
        return;
    }

    mName = aName;
}
void XRawFile::SetName(const QString& aName)
{
    if (mName == nullptr)
    {
        mName = new XString();
    }

    mName->SetString(aName);
}

XString* XRawFile::Name() const
{
    return mName;
}

void XRawFile::SetLength(int aLength)
{
    mBuffer = mBuffer.chopped(aLength);
    mLength = aLength;
}

int XRawFile::Length() const
{
    return mLength;
}

void XRawFile::SetBuffer(const QByteArray& aBuffer)
{
    mBuffer = aBuffer;
    mLength = mBuffer.length();
}

QByteArray XRawFile::Buffer() const
{
    return mBuffer;
}

void XRawFile::Clear()
{
    mName->Clear();
    mLength = 0;
    mBuffer = QByteArray();

    XAsset::Clear();
}

void XRawFile::ParseData(QDataStream *aStream)
{
    mName->ParsePtr(aStream, false);

    qint32 bufferPtr;
    *aStream
        >> mLength
        >> bufferPtr;

    if (mName == nullptr)
    {
        mName = new XString();
    }
    mName->ParseData(aStream);

    if (bufferPtr)
    {
        mBuffer = QByteArray(mLength + 1, Qt::Uninitialized);
        aStream->readRawData(mBuffer.data(), mLength + 1);
    }
}
