2013-10-30 11:46:18 +00:00
|
|
|
#include "PackStream.h"
|
|
|
|
|
2015-08-13 21:46:50 +00:00
|
|
|
#include "Logs.h"
|
2013-10-30 11:46:18 +00:00
|
|
|
#include <QFile>
|
|
|
|
#include <QDataStream>
|
|
|
|
#include <QString>
|
|
|
|
|
|
|
|
PackStream::PackStream()
|
|
|
|
{
|
|
|
|
file = NULL;
|
2015-08-13 21:46:50 +00:00
|
|
|
buffer = new QByteArray();
|
|
|
|
stream = new QDataStream(buffer, QIODevice::WriteOnly);
|
|
|
|
stream->setVersion(QDataStream::Qt_5_2);
|
2013-10-30 11:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PackStream::~PackStream()
|
|
|
|
{
|
2015-08-13 21:46:50 +00:00
|
|
|
delete buffer;
|
|
|
|
delete stream;
|
2013-10-30 11:46:18 +00:00
|
|
|
if (file)
|
|
|
|
{
|
|
|
|
delete file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-13 22:22:20 +00:00
|
|
|
PackStream::PackStream(const PackStream *other)
|
|
|
|
{
|
|
|
|
file = NULL;
|
|
|
|
buffer = new QByteArray();
|
|
|
|
if (other->file)
|
|
|
|
{
|
|
|
|
Logs::error() << "Try to read from a substream bound to a file: " << other->file->fileName().toStdString() << std::endl;
|
|
|
|
stream = new QDataStream(buffer, QIODevice::ReadOnly);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stream = new QDataStream(other->buffer, QIODevice::ReadOnly);
|
|
|
|
}
|
|
|
|
stream->setVersion(QDataStream::Qt_5_2);
|
|
|
|
}
|
|
|
|
|
2013-12-10 21:32:58 +00:00
|
|
|
bool PackStream::bindToFile(const std::string &filepath, bool write)
|
2013-10-30 11:46:18 +00:00
|
|
|
{
|
2015-08-13 21:46:50 +00:00
|
|
|
if (not file)
|
2013-10-30 11:46:18 +00:00
|
|
|
{
|
2013-12-10 21:32:58 +00:00
|
|
|
file = new QFile(QString::fromStdString(filepath));
|
2013-11-03 12:00:31 +00:00
|
|
|
if (not file->open(write ? QIODevice::WriteOnly : QIODevice::ReadOnly))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-10-30 11:46:18 +00:00
|
|
|
|
2015-08-13 21:46:50 +00:00
|
|
|
QDataStream *new_stream = new QDataStream(file);
|
|
|
|
if (new_stream)
|
|
|
|
{
|
|
|
|
delete stream;
|
|
|
|
stream = new_stream;
|
|
|
|
stream->setVersion(QDataStream::Qt_5_2);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
2013-10-30 11:46:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-10 21:32:58 +00:00
|
|
|
void PackStream::write(const int *value)
|
2013-10-30 11:46:18 +00:00
|
|
|
{
|
2015-08-13 21:46:50 +00:00
|
|
|
if (value)
|
2013-10-30 11:46:18 +00:00
|
|
|
{
|
|
|
|
*stream << *value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-11 12:56:39 +00:00
|
|
|
void PackStream::write(const double *value)
|
2013-10-30 11:46:18 +00:00
|
|
|
{
|
2015-08-13 21:46:50 +00:00
|
|
|
if (value)
|
2013-10-30 11:46:18 +00:00
|
|
|
{
|
|
|
|
*stream << *value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-11 12:56:39 +00:00
|
|
|
void PackStream::write(const char *value, int max_length)
|
2013-10-30 11:46:18 +00:00
|
|
|
{
|
2015-08-13 21:46:50 +00:00
|
|
|
if (value)
|
2013-10-30 11:46:18 +00:00
|
|
|
{
|
|
|
|
int length = qstrlen(value);
|
|
|
|
*stream << QString::fromUtf8(value, length > max_length ? max_length : length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-10 21:32:58 +00:00
|
|
|
void PackStream::write(const std::string &value)
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
2015-08-13 21:46:50 +00:00
|
|
|
*stream << QString::fromStdString(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PackStream::writeFromBuffer(const PackStream &other, bool prepend_size)
|
|
|
|
{
|
|
|
|
if (other.file)
|
|
|
|
{
|
|
|
|
Logs::error() << "Try to write from a substream bound to a file: " << other.file->fileName().toStdString() << std::endl;
|
|
|
|
}
|
|
|
|
else
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
2015-08-13 21:46:50 +00:00
|
|
|
if (prepend_size)
|
|
|
|
{
|
|
|
|
int buffer_size = (int)other.buffer->size();
|
|
|
|
write(&buffer_size);
|
|
|
|
}
|
|
|
|
stream->writeRawData(other.buffer->data(), other.buffer->size());
|
2013-10-31 16:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-30 11:46:18 +00:00
|
|
|
void PackStream::read(int* value)
|
|
|
|
{
|
2015-08-13 21:46:50 +00:00
|
|
|
if (value and not stream->atEnd())
|
2013-10-30 11:46:18 +00:00
|
|
|
{
|
|
|
|
int output;
|
|
|
|
*stream >> output;
|
|
|
|
*value = output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PackStream::read(double* value)
|
|
|
|
{
|
2015-08-13 21:46:50 +00:00
|
|
|
if (value and not stream->atEnd())
|
2013-10-30 11:46:18 +00:00
|
|
|
{
|
|
|
|
double output;
|
|
|
|
*stream >> output;
|
|
|
|
*value = output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PackStream::read(char* value, int max_length)
|
|
|
|
{
|
2015-08-13 21:46:50 +00:00
|
|
|
if (value and not stream->atEnd())
|
2013-10-30 11:46:18 +00:00
|
|
|
{
|
|
|
|
QString output;
|
|
|
|
*stream >> output;
|
|
|
|
QByteArray array = output.toUtf8();
|
|
|
|
qstrncpy(value, array.constData(), max_length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-10 21:32:58 +00:00
|
|
|
std::string PackStream::readString()
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
2015-08-13 21:46:50 +00:00
|
|
|
if (not stream->atEnd())
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
|
|
|
QString output;
|
|
|
|
*stream >> output;
|
2013-12-10 21:32:58 +00:00
|
|
|
return output.toStdString();
|
2013-10-31 16:59:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-10 21:32:58 +00:00
|
|
|
return "";
|
2013-10-31 16:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-21 10:36:28 +00:00
|
|
|
|
|
|
|
void PackStream::skip(const int &value, int count)
|
|
|
|
{
|
|
|
|
stream->skipRawData(sizeof(value) * count);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PackStream::skip(const double &value, int count)
|
|
|
|
{
|
|
|
|
stream->skipRawData(sizeof(value) * count);
|
|
|
|
}
|
2015-08-13 21:46:50 +00:00
|
|
|
|
|
|
|
void paysages::system::PackStream::skipBytes(int bytes)
|
|
|
|
{
|
|
|
|
stream->skipRawData(bytes);
|
|
|
|
}
|