paysages3d/src/system/PackStream.cpp

127 lines
2.2 KiB
C++
Raw Normal View History

#include "PackStream.h"
#include <QFile>
#include <QDataStream>
#include <QString>
PackStream::PackStream()
{
file = NULL;
stream = NULL;
}
PackStream::~PackStream()
{
if (stream)
{
delete stream;
}
if (file)
{
delete file;
}
}
bool PackStream::bindToFile(const std::string &filepath, bool write)
{
if (not file and not stream)
{
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;
}
stream = new QDataStream(file);
}
2013-11-03 12:00:31 +00:00
return stream != NULL;
}
void PackStream::write(const int *value)
{
if (stream and value)
{
*stream << *value;
}
}
2013-11-11 12:56:39 +00:00
void PackStream::write(const double *value)
{
if (stream and value)
{
*stream << *value;
}
}
2013-11-11 12:56:39 +00:00
void PackStream::write(const char *value, int max_length)
{
if (stream and value)
{
int length = qstrlen(value);
*stream << QString::fromUtf8(value, length > max_length ? max_length : length);
}
}
void PackStream::write(const std::string &value)
2013-10-31 16:59:18 +00:00
{
if (stream)
{
*stream << QString::fromStdString(value);
2013-10-31 16:59:18 +00:00
}
}
void PackStream::read(int* value)
{
if (stream and value and not stream->atEnd())
{
int output;
*stream >> output;
*value = output;
}
}
void PackStream::read(double* value)
{
if (stream and value and not stream->atEnd())
{
double output;
*stream >> output;
*value = output;
}
}
void PackStream::read(char* value, int max_length)
{
if (stream and value and not stream->atEnd())
{
QString output;
*stream >> output;
QByteArray array = output.toUtf8();
qstrncpy(value, array.constData(), max_length);
}
}
std::string PackStream::readString()
2013-10-31 16:59:18 +00:00
{
if (stream and not stream->atEnd())
{
QString output;
*stream >> output;
return output.toStdString();
2013-10-31 16:59:18 +00:00
}
else
{
return "";
2013-10-31 16:59:18 +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);
}