Use text stream for parsing.

This commit is contained in:
leo 2014-09-03 14:34:53 +00:00
parent 661fa68011
commit c4bc70fd35
6 changed files with 39 additions and 62 deletions

View file

@ -159,41 +159,37 @@ void lcCamera::SaveLDraw(lcFile& File) const
File.WriteBuffer(Line, strlen(Line));
}
bool lcCamera::ParseLDrawLine(QString& Line)
bool lcCamera::ParseLDrawLine(QTextStream& Stream)
{
QRegExp TokenExp("\\s*(\\w+)\\s+(.*)");
if (!Line.contains(TokenExp))
return false;
QString Token = TokenExp.cap(1);
Line = TokenExp.cap(2);
QString Token;
Stream >> Token;
if (Token == QStringLiteral("HIDDEN"))
SetHidden(true);
else if (Token == QStringLiteral("ORTHOGRAPHIC"))
SetOrtho(true);
else if (Token == QStringLiteral("FOV"))
m_fovy = Line.toFloat();
Stream >> m_fovy;
else if (Token == QStringLiteral("ZNEAR"))
m_zNear = Line.toFloat();
Stream >> m_zNear;
else if (Token == QStringLiteral("ZFAR"))
m_zFar = Line.toFloat();
Stream >> m_zFar;
else if (Token == QStringLiteral("POSITION"))
QTextStream(&Line) >> mPosition[0] >> mPosition[1] >> mPosition[2];
Stream >> mPosition[0] >> mPosition[1] >> mPosition[2];
else if (Token == QStringLiteral("TARGETPOSITION"))
QTextStream(&Line) >> mTargetPosition[0] >> mTargetPosition[1] >> mTargetPosition[2];
Stream >> mTargetPosition[0] >> mTargetPosition[1] >> mTargetPosition[2];
else if (Token == QStringLiteral("UPVECTOR"))
QTextStream(&Line) >> mUpVector[0] >> mUpVector[1] >> mUpVector[2];
Stream >> mUpVector[0] >> mUpVector[1] >> mUpVector[2];
else if (Token == QStringLiteral("POSITION_KEY"))
LoadKeyLDraw(mPositionKeys, Line);
LoadKeyLDraw(mPositionKeys, Stream);
else if (Token == QStringLiteral("TARGETPOSITION_KEY"))
LoadKeyLDraw(mTargetPositionKeys, Line);
LoadKeyLDraw(mTargetPositionKeys, Stream);
else if (Token == QStringLiteral("UPVECTOR_KEY"))
LoadKeyLDraw(mUpVectorKeys, Line);
LoadKeyLDraw(mUpVectorKeys, Stream);
else if (Token == QStringLiteral("NAME"))
{
QByteArray NameUtf = Line.toUtf8(); // todo: replace with qstring
QString Name = Stream.readAll().trimmed();
QByteArray NameUtf = Name.toUtf8(); // todo: replace with qstring
strncpy(m_strName, NameUtf.constData(), sizeof(m_strName));
m_strName[sizeof(m_strName) - 1] = 0;
return true;

View file

@ -221,7 +221,7 @@ public:
}
void SaveLDraw(lcFile& File) const;
bool ParseLDrawLine(QString& Line);
bool ParseLDrawLine(QTextStream& Stream);
QJsonObject SaveJson() const;
bool LoadJson(const QJsonObject& Camera);

View file

@ -294,7 +294,6 @@ void lcModel::SaveLDraw(lcFile& File) const
void lcModel::LoadLDraw(const QStringList& Lines, const lcMatrix44& CurrentTransform, int DefaultColorCode, int& CurrentStep)
{
QRegExp TokenExp("\\s*(!?\\w+)\\s+(.*)");
lcPiece* Piece = NULL;
lcCamera* Camera = NULL;
lcLight* Light = NULL;
@ -302,20 +301,14 @@ void lcModel::LoadLDraw(const QStringList& Lines, const lcMatrix44& CurrentTrans
for (int LineIdx = 0; LineIdx < Lines.size(); LineIdx++)
{
QString Line = Lines[LineIdx].trimmed();
QTextStream Stream(&Line, QIODevice::ReadOnly);
if (!Line.contains(TokenExp))
continue;
QString Token = TokenExp.cap(1);
Line = TokenExp.cap(2);
QString Token;
Stream >> Token;
if (Token == QStringLiteral("0"))
{
if (!Line.contains(TokenExp))
continue;
Token = TokenExp.cap(1);
Line = TokenExp.cap(2);
Stream >> Token;
if (Token == QStringLiteral("STEP"))
{
@ -326,11 +319,7 @@ void lcModel::LoadLDraw(const QStringList& Lines, const lcMatrix44& CurrentTrans
if (Token != QStringLiteral("!LEOCAD"))
continue;
if (!Line.contains(TokenExp))
continue;
Token = TokenExp.cap(1);
Line = TokenExp.cap(2);
Stream >> Token;
if (Token == QStringLiteral("MODEL"))
{
@ -344,14 +333,14 @@ void lcModel::LoadLDraw(const QStringList& Lines, const lcMatrix44& CurrentTrans
if (!Piece)
Piece = new lcPiece(NULL);
Piece->ParseLDrawLine(Line, this);
Piece->ParseLDrawLine(Stream, this);
}
else if (Token == QStringLiteral("CAMERA"))
{
if (!Camera)
Camera = new lcCamera(false);
if (Camera->ParseLDrawLine(Line))
if (Camera->ParseLDrawLine(Stream))
{
Camera->CreateName(mCameras);
mCameras.Add(Camera);
@ -369,8 +358,6 @@ void lcModel::LoadLDraw(const QStringList& Lines, const lcMatrix44& CurrentTrans
}
else if (Token == QStringLiteral("1"))
{
QTextStream Stream(&Line, QIODevice::ReadOnly);
int ColorCode;
Stream >> ColorCode;
if (ColorCode == 16)

View file

@ -91,20 +91,14 @@ protected:
void SaveKeysLDraw(const lcArray<lcObjectKey<lcVector4>>& Keys, const char* KeyName, lcFile& File) const;
template<typename T>
void LoadKeyLDraw(lcArray<lcObjectKey<T>>& Keys, QString& Line)
void LoadKeyLDraw(lcArray<lcObjectKey<T>>& Keys, QTextStream& Stream)
{
QRegExp TokenExp("\\s*(\\w+)\\s+(.*)");
if (!Line.contains(TokenExp))
return;
QString Token = TokenExp.cap(1);
Line = TokenExp.cap(2);
QString Token;
Stream >> Token;
int Step = Token.toInt();
T Value;
QTextStream Stream(&Line);
const int Count = sizeof(T) / sizeof(float);
for (int ValueIdx = 0; ValueIdx < Count; ValueIdx++)
Stream >> Value[ValueIdx];

View file

@ -98,32 +98,32 @@ void lcPiece::SaveLDraw(lcFile& File) const
File.WriteBuffer(Line, strlen(Line));
}
bool lcPiece::ParseLDrawLine(QString& Line, lcModel* Model)
bool lcPiece::ParseLDrawLine(QTextStream& Stream, lcModel* Model)
{
QRegExp TokenExp("\\s*(\\w+)\\s+(.*)");
if (!Line.contains(TokenExp))
return false;
QString Token = TokenExp.cap(1);
Line = TokenExp.cap(2);
QString Token;
Stream >> Token;
if (Token == QStringLiteral("STEP_HIDE"))
mStepHide = Line.toInt();
Stream >> mStepHide;
else if (Token == QStringLiteral("NAME"))
{
QByteArray NameUtf = Line.toUtf8(); // todo: replace with qstring
QString Name = Stream.readAll().trimmed();
QByteArray NameUtf = Name.toUtf8(); // todo: replace with qstring
strncpy(m_strName, NameUtf.constData(), sizeof(m_strName));
m_strName[sizeof(m_strName) - 1] = 0;
}
else if (Token == QStringLiteral("HIDDEN"))
SetHidden(true);
else if (Token == QStringLiteral("GROUP"))
mGroup = Model->GetGroup(Line.toUtf8(), true);
{
QString Name = Stream.readAll().trimmed();
QByteArray NameUtf = Name.toUtf8(); // todo: replace with qstring
mGroup = Model->GetGroup(NameUtf.constData(), true);
}
else if (Token == QStringLiteral("POSITION_KEY"))
LoadKeyLDraw(mPositionKeys, Line);
LoadKeyLDraw(mPositionKeys, Stream);
else if (Token == QStringLiteral("ROTATION_KEY"))
LoadKeyLDraw(mRotationKeys, Line);
LoadKeyLDraw(mRotationKeys, Stream);
return false;
}

View file

@ -89,7 +89,7 @@ public:
}
void SaveLDraw(lcFile& File) const;
bool ParseLDrawLine(QString& Line, lcModel* Model);
bool ParseLDrawLine(QTextStream& Stream, lcModel* Model);
QJsonObject SaveJson() const;
bool LoadJson(const QJsonObject& Piece);