Frontend to the famous GRBL.

gcodeparser.cpp 2.8KB

    /* Part of x2grbl * * Copyright Johann Wilhelm <johann.wilhelm@9mal6.de> 2015 * * see Readme.md for detailed license and usage information! */ #include "gcodeparser.h" #include <QTextStream> #include <QRegExp> #include <QStringList> #include <QDebug> GCodeParser::GCodeParser() { } bool GCodeParser::open(QIODevice &Input) { Lines.clear(); QRegExp RemoveWhiteSpaces(QString("\\s")); QRegExp RemoveInLineComments(QString("\\(.*\\)")); QRegExp RemoveEndComments(QString("(;.*)")); QRegExp LineNumberParse(QString("^N0*([0-9]+)[A-M,O-Z]*")); QRegExp WordParse(QString("^(?:N0*[0-9]+)*([A-M,O-Z]+.*)")); int LineNumber=0; if (Input.isOpen()) { QTextStream InputStream(&Input); while (!InputStream.atEnd()) { QString Line=InputStream.readLine(); Line.replace(RemoveWhiteSpaces,QString("")); Line.replace(RemoveInLineComments, QString("")); Line.replace(RemoveEndComments, QString("")); Line=Line.toUpper(); if (Line.count()) { LineNumberParse.indexIn(Line); QStringList NumberList=LineNumberParse.capturedTexts(); if (NumberList.count()>1) { bool Ok; int Number=NumberList[1].toInt(&Ok); if (Ok) LineNumber=Number; } WordParse.indexIn(Line); QStringList WordList=WordParse.capturedTexts(); if (WordList.count()>1) { if (WordList[1].count()) { if (Lines.contains(LineNumber)) { Lines[LineNumber]=WordList[1]; qDebug()<<"G-Codes has multiple lines "<<LineNumber<<"!\n"; } else { Lines.insert(LineNumber, WordList[1]); } } } LineNumber++; } } return true; } return false; } bool GCodeParser::save(QIODevice &Output) { return false; } bool GCodeParser::adjustZ(QList<QVector3D> ZValues) { return false; } QString GCodeParser::getLine(int Line) { if (Lines.contains(Line)) return Lines[Line]; return QString(); } int GCodeParser::getFirstLine() { return Lines.keys().first(); } int GCodeParser::getLastLine() { return Lines.keys().last(); } int GCodeParser::getNextLine(int Line) { const int LastLine=Lines.keys().last(); if (Line==LastLine) return -1; if (Lines.contains(Line)) { QMap<int, QString>::Iterator n=Lines.find(Line); n++; if (n.key()<Line) { qDebug()<<"AHHH\n"; } return n.key(); } return -1; }