From c4bc70fd358d94eb14d71292db1484529ad8a17c Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 3 Sep 2014 14:34:53 +0000 Subject: [PATCH] Use text stream for parsing. --- common/camera.cpp | 32 ++++++++++++++------------------ common/camera.h | 2 +- common/lc_model.cpp | 27 +++++++-------------------- common/object.h | 12 +++--------- common/piece.cpp | 26 +++++++++++++------------- common/piece.h | 2 +- 6 files changed, 39 insertions(+), 62 deletions(-) diff --git a/common/camera.cpp b/common/camera.cpp index d34c8c23..96904e86 100644 --- a/common/camera.cpp +++ b/common/camera.cpp @@ -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; diff --git a/common/camera.h b/common/camera.h index 2cbeabcf..cd24c793 100644 --- a/common/camera.h +++ b/common/camera.h @@ -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); diff --git a/common/lc_model.cpp b/common/lc_model.cpp index 2364e20a..f3dfc089 100644 --- a/common/lc_model.cpp +++ b/common/lc_model.cpp @@ -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) diff --git a/common/object.h b/common/object.h index b64c4e5d..794fb49f 100644 --- a/common/object.h +++ b/common/object.h @@ -91,20 +91,14 @@ protected: void SaveKeysLDraw(const lcArray>& Keys, const char* KeyName, lcFile& File) const; template - void LoadKeyLDraw(lcArray>& Keys, QString& Line) + void LoadKeyLDraw(lcArray>& 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]; diff --git a/common/piece.cpp b/common/piece.cpp index 5974e7c4..d5274a1c 100644 --- a/common/piece.cpp +++ b/common/piece.cpp @@ -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; } diff --git a/common/piece.h b/common/piece.h index bbd1e4d3..d2d630b0 100644 --- a/common/piece.h +++ b/common/piece.h @@ -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);