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