C++ (Qt) while (!fileStream_.atEnd()){ QString line(fileStream_.readLine()); if (line == "END NODES"){ break; } Q_ASSERT(line.startsWith("*node(")); QTextStream lineStream(&line, QIODevice::ReadOnly); lineStream.seek(6); ParsedNode node; lineStream >> node.number; float x; float y; float z; lineStream.seek(lineStream.pos() + 1); lineStream >> x; lineStream.seek(lineStream.pos() + 1); lineStream >> y; lineStream.seek(lineStream.pos() + 1); lineStream >> z; node.coordinates = QVector3D(x, y, z); nodes << node; }
C++ (Qt)std::tuple<float, float, float> parse_coords(QString text) { const QStringList& parts = text.replace("*node(", "").replace(")", "").split(","); return std::make_tuple(parts[1].toFloat(), parts[2].toFloat(), parts[3].toFloat());} QString text = "*node(363484,232.7625,-113.996,40.69956,0,0,0,0,0)";std::tuple<float, float, float> coord = parse_coords(text);qDebug() << std::get<0>(coord) << std::get<1>(coord) << std::get<2>(coord);
C++ (Qt)#include <QStringRef>#include <QDebug> namespace QStringParser { inline QStringRef BeforeFirst( const QStringRef & src, const QString & splitter ){ return src.left(src.indexOf(splitter));} inline QStringRef AfterFirst( const QStringRef & src, const QString & splitter ){ int pos = src.indexOf(splitter); iif (pos < 0) return src.right(0); return src.mid(pos + splitter.size());} inline bool ReadInt( QStringRef & ref, const QString & splitter, int & val ){ bool ok; val = BeforeFirst(ref, splitter).trimmed().toInt(&ok); if (!ok) return false; ref = AfterFirst(ref, splitter); return true;} inline bool ReadFloat( QStringRef & ref, const QString & splitter, float & val ){ bool ok; val = BeforeFirst(ref, splitter).trimmed().toFloat(&ok); if (!ok) return false; ref = AfterFirst(ref, splitter); return true;} } // namespace using namespace QStringParser; bool Error( const QStringRef & ref, QString & err, const QString & txt ){ err = "\nError at position " + QString::number(ref.position()) + ": " + txt + "\n" + ref.trimmed().toString(); return false;} bool Parse( const QString & srcStr, int & number, float val[3], QString & err ){ static const QString prefix("node"); static const QString comma(","); QStringRef src(&srcStr); if (!src.startsWith(prefix)) return Error(src, err, "string must start with 'node'"); src = AfterFirst(QStringRef(&srcStr), prefix).trimmed(); if (!src.startsWith('(')) return Error(src, err, "'(' expected"); src = src.mid(1); if (!ReadInt(src, comma, number)) return Error(src, err, "integer value expected"); for (int i = 0; i < 3; ++i) if (!ReadFloat(src, comma, val[i])) return Error(src, err, "float value expected"); return true;} int main(int argc, char **argv){ int nodeNum; float val[3] = { 0 }; QString err; QString test("node ( 363484 ,232.7625, -113.996, 40.69x956,0,0,0,0,0)"); bool ok = Parse(test, nodeNum, val, err); qDebug() << ok << nodeNum << val[0] << val[1] << val[2]; if (!ok) qDebug() << err; return 0;}
C++ (Qt) while (!fileStream_.atEnd()){ QString line(fileStream_.readLine()); if (line == endNodesGroupString){ break; } if (line.startsWith(nodeIndicatorString)){ const QStringList &lineParts = line.remove(nodeIndicatorString).remove(" ").remove(")").split(','); constexpr int partCountRequried = 4; if (lineParts.count() < partCountRequried){ qCritical() << "Got bad node line"; } else{ ParsedNode node; node.number = lineParts.at(0).toFloat(); for (int k = 0; k < nodeDimension; ++k){ node.coordinates[k] = lineParts.at(k + 1).toFloat(); } nodes << node; } } }