2 // commandprocessor.cpp: Command processor
4 // Part of the Architektonas Project
5 // (C) 2021 Underground Software
6 // See the README and GPLv3 files for licensing and warranty information
8 // JLH = James Hammons <jlhamm@acm.org>
11 // --- ---------- ------------------------------------------------------------
12 // JLH 12/16/2021 Created this file
15 #include "commandprocessor.h"
18 CommandProcessor::CommandProcessor()
22 CommandProcessor::~CommandProcessor()
26 void CommandProcessor::Error(QString s)
28 AddToResponse("Error", "red", s);
32 void CommandProcessor::Warning(QString s)
34 AddToResponse("Warning", "orange", s);
37 void CommandProcessor::AddToResponse(QString type, QString c, QString msg)
39 if (response.length() > 0)
42 response += QString("<font color=%2>%3: %1</font>").arg(msg).arg(c).arg(type);
45 void CommandProcessor::ClearWhitespace(QString & s)
47 while (s.length() > 0)
49 if (s[0].isSpace() == false)
56 double CommandProcessor::GetDouble(QString & s)
59 N.B.: Still need to add parsing of fractional components, if any (nn/nn)
68 while (s.length() > 0)
70 if ((s[0].isNumber() == false) && (s[0] != QChar('.')) && (s[0] != QChar('+')) && (s[0] != QChar('-')) && (s[0] != QChar('e')))
83 double d = n.toDouble(&ok);
86 Error(QString("could not parse number '%1'").arg(n));
91 int CommandProcessor::GetInt(QString & s)
100 while (s.length() > 0)
102 if (s[0].isNumber() == false)
115 int i = n.toInt(&ok);
118 Error(QString("could not parse number '%1'").arg(n));
123 QString CommandProcessor::GetString(QString & s)
129 while (s.length() > 0)
131 if (s[0].isLetter() == false)
141 int CommandProcessor::GetUnits(QString & s)
144 missingParam = false;
145 QString unit = GetString(s);
147 if (unit.length() == 0)
153 BasicUnit bu = BUInch;
155 if (unit == QString("inch") || unit == QString("in"))
159 else if (unit == QString("foot") || unit == QString("ft"))
163 else if (unit == QString("yard") || unit == QString("yd"))
169 Error(QString("bad unit '%1'").arg(unit));
175 bool CommandProcessor::GetComma(QString & s)
179 if ((s.length() > 0) && (s[0] == QChar(',')))
188 QString CommandProcessor::Process(QString cmd)
193 if (Global::tool == TTParallel)
195 response += QString("Parallel: %1").arg(cmd);
196 bool goodDist = false, goodNum = false, goodBU = false;
198 double d = GetDouble(cmd);
202 if (!missingParam && !parseError)
207 if (!missingParam && !parseError)
213 if (GetComma(cmd) == false)
215 ClearWhitespace(cmd);
217 if (cmd.length() > 0)
218 Error("missing comma");
224 if (!missingParam && !parseError)
228 ClearWhitespace(cmd);
230 if (cmd.length() > 0)
231 Warning(QString("extra junk '%1' at end ignored").arg(cmd));
239 Global::parallelBU = bu;
242 Global::parallelDist = d * buInInches[Global::parallelBU];
245 Global::parallelNum = i;
247 emit(UpdateNeeded());
252 Error(QString("don't know how to '%1'").arg(cmd));