Plotter/hpglparser.cpp
2017-09-14 18:10:46 +02:00

125 lines
2.1 KiB
C++

//
//
//
#include "hpglparser.h"
String buffer = "";
unsigned int bufferPosition = 0;
bool isWhitespace(const char ch)
{
return ch == ' ' || ch == '\n';
}
void addToBuffer(char ch)
{
buffer += ch;
}
void setBuffer(String in)
{
buffer = in;
}
void purgeBuffer()
{
buffer = buffer.substring(bufferPosition);
bufferPosition = 0;
}
bool isBufferEmpty()
{
return bufferPosition<buffer.length();
}
bool isBufferFull()
{
return (buffer.length()>40 && (!buffer.indexOf("LB"))>=0);
}
bool isNumber(char ch)
{
return ch=='-' || isdigit(ch);
}
void skipWhitespaces()
{
//preskoc non-alpha
while(bufferPosition<buffer.length() && isWhitespace(buffer[bufferPosition]))
{
bufferPosition++;
}
}
bool tryReadInt(uint16_t *i)
{
skipWhitespaces();
char ch = buffer[bufferPosition];
if (isNumber(ch))
{
unsigned int p = bufferPosition;
while(bufferPosition<buffer.length() && isNumber(buffer[bufferPosition]))
{
bufferPosition++;
}
String s = buffer.substring(p,bufferPosition);
*i = s.toInt();
return true;
}
return false;
}
bool isSeparator(char ch)
{
return isWhitespace(ch) || ch==',';
}
bool readSeparator()
{
bool b = false;
while(bufferPosition<buffer.length() && isSeparator(buffer[bufferPosition]))
{
b = true;
bufferPosition++;
}
return b;
}
bool tryReadPoint(Point *pt)
{
uint16_t i;
if (!tryReadInt(&i))
return false;
(*pt).x = i;
if (!readSeparator())
return false;
if (!tryReadInt(&i))
return false;
(*pt).y = i;
return true;
}
String readHpglCmd()
{
while(bufferPosition<buffer.length() && !isalpha(buffer[bufferPosition]))
{
bufferPosition++;
}
String cmd = buffer.substring(bufferPosition, bufferPosition + 2);
bufferPosition+=2;
return cmd;
}
String readStringUntil(char ch)
{
unsigned int pos=bufferPosition;
while(bufferPosition<buffer.length() && buffer[bufferPosition]!=ch)
{
bufferPosition++;
}
String result = buffer.substring(pos, bufferPosition);
bufferPosition++;
return result;
}