2020-05-24 20:59:49 +02:00
|
|
|
#include "StringParser.h"
|
|
|
|
|
2020-05-25 14:38:18 +02:00
|
|
|
int StringParser::parse(const char * com, int result[], byte maxResults) {
|
|
|
|
byte state=1;
|
2020-05-24 20:59:49 +02:00
|
|
|
byte parameterCount=0;
|
|
|
|
int runningValue=0;
|
2020-05-25 14:38:18 +02:00
|
|
|
const char * remainingCmd=com; // skips the opcode
|
2020-05-24 20:59:49 +02:00
|
|
|
bool signNegative=false;
|
2020-05-25 14:38:18 +02:00
|
|
|
|
|
|
|
// clear all parameters in case not enough found
|
|
|
|
for (int i=0;i<maxResults;i++) result[i]=0;
|
|
|
|
|
|
|
|
while(parameterCount<maxResults) {
|
2020-05-24 20:59:49 +02:00
|
|
|
char hot=*remainingCmd;
|
2020-05-25 14:38:18 +02:00
|
|
|
|
2020-05-24 20:59:49 +02:00
|
|
|
switch (state) {
|
|
|
|
|
|
|
|
case 1: // skipping spaces before a param
|
|
|
|
if (hot==' ') break;
|
2020-05-25 14:38:18 +02:00
|
|
|
if (hot == '\0' || hot=='>') return parameterCount;
|
|
|
|
state=2;
|
2020-05-24 20:59:49 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case 2: // checking sign
|
|
|
|
signNegative=false;
|
|
|
|
runningValue=0;
|
2020-05-25 14:38:18 +02:00
|
|
|
state=3;
|
|
|
|
if (hot!='-') continue;
|
|
|
|
signNegative=true;
|
|
|
|
break;
|
2020-05-24 20:59:49 +02:00
|
|
|
case 3: // building a parameter
|
2020-05-25 14:38:18 +02:00
|
|
|
if (hot>='0' && hot<='9') {
|
2020-05-24 20:59:49 +02:00
|
|
|
runningValue=10*runningValue+(hot-'0');
|
|
|
|
break;
|
|
|
|
}
|
2020-05-25 14:38:18 +02:00
|
|
|
result[parameterCount] = runningValue * (signNegative ?-1:1);
|
2020-05-24 20:59:49 +02:00
|
|
|
parameterCount++;
|
|
|
|
state=1;
|
2020-05-25 14:38:18 +02:00
|
|
|
continue;
|
2020-05-24 20:59:49 +02:00
|
|
|
}
|
|
|
|
remainingCmd++;
|
|
|
|
}
|
|
|
|
return parameterCount;
|
|
|
|
}
|