re-work separator parsing. Now first byte of separator is replaced with a 0x00 byte to allow e.g. simple %s parsing for an element (instead of e.g. %[^,] )
This commit is contained in:
@ -1286,8 +1286,8 @@ matchInput()
|
|||||||
inputLine.length()-consumedInput > 20 ? "..." : "",
|
inputLine.length()-consumedInput > 20 ? "..." : "",
|
||||||
formatstring);
|
formatstring);
|
||||||
else
|
else
|
||||||
error("%s: Can't scan value with format %%%s\n",
|
error("%s: Format %%%s has data type %s which does not match this variable.\n",
|
||||||
name(), formatstring);
|
name(), formatstring, StreamFormatTypeStr[fmt.type] );
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -1372,26 +1372,54 @@ matchInput()
|
|||||||
bool StreamCore::
|
bool StreamCore::
|
||||||
matchSeparator()
|
matchSeparator()
|
||||||
{
|
{
|
||||||
if (!(flags & Separator))
|
// called before value is read, first value has Separator flag cleared
|
||||||
{
|
// for second and next value set Separator flag
|
||||||
flags |= Separator;
|
// first jump over leading separator (all but first value)
|
||||||
return true;
|
// then find next separator and terminate with null byte to
|
||||||
}
|
// help parsing one value (e.g. %s)
|
||||||
|
|
||||||
if (!separator) return true;
|
if (!separator) return true;
|
||||||
long i = 0;
|
if (flags & Separator)
|
||||||
if (separator[0] == ' ')
|
{
|
||||||
|
// not first element in array: expect to be at the separator
|
||||||
|
|
||||||
|
if (separatorStart == -1) return false;
|
||||||
|
|
||||||
|
// restore deleted char for proper debug and error output
|
||||||
|
inputLine[separatorStart] = separatorStore;
|
||||||
|
|
||||||
|
if (consumedInput != separatorStart)
|
||||||
|
{
|
||||||
|
long i = separatorStart - consumedInput;
|
||||||
|
error("%s: %ld byte%s input left before separator: \"%s%s\" after \"%s%s\"\n",
|
||||||
|
name(), i, i != 1 ? "s" : "",
|
||||||
|
inputLine.expand(consumedInput, i > 20 ? 20 : i)(),
|
||||||
|
i > 20 ? "..." : "",
|
||||||
|
consumedInput > 20 ? "..." : "",
|
||||||
|
inputLine.expand(consumedInput, -20)());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
consumedInput = separatorEnd;
|
||||||
|
}
|
||||||
|
flags |= Separator;
|
||||||
|
|
||||||
|
// find next Separator and replace first byte with '\0' to help parsing elements
|
||||||
|
if (separator[0] == ' ' && separator[1] != ' ')
|
||||||
{
|
{
|
||||||
i++;
|
|
||||||
// skip leading whitespaces
|
// skip leading whitespaces
|
||||||
while (isspace(inputLine[consumedInput++]));
|
separatorStart = inputLine.find(separator(1), separator.length() - 1, consumedInput);
|
||||||
|
if (separatorStart == -1) return true;
|
||||||
|
separatorEnd = separatorStart + separator.length() - 1;
|
||||||
|
while (separatorStart > consumedInput && isspace(inputLine[separatorStart - 1])) separatorStart--;
|
||||||
}
|
}
|
||||||
for (; i < separator.length(); i++,consumedInput++)
|
else
|
||||||
{
|
{
|
||||||
if (!inputLine[consumedInput]) return false;
|
separatorStart = inputLine.find(separator, consumedInput);
|
||||||
if (separator[i] == StreamProtocolParser::skip) continue; // wildcard
|
if (separatorStart == -1) return true;
|
||||||
if (separator[i] == esc) i++; // escaped literal byte
|
separatorEnd = separatorStart + separator.length();
|
||||||
if (separator[i] != inputLine[consumedInput]) return false;
|
|
||||||
}
|
}
|
||||||
|
separatorStore = inputLine[separatorStart];
|
||||||
|
inputLine[separatorStart] = '\0';
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -170,6 +170,9 @@ protected:
|
|||||||
|
|
||||||
StreamIoStatus lastInputStatus;
|
StreamIoStatus lastInputStatus;
|
||||||
bool unparsedInput;
|
bool unparsedInput;
|
||||||
|
long separatorStart;
|
||||||
|
long separatorEnd;
|
||||||
|
char separatorStore;
|
||||||
|
|
||||||
StreamCore(const StreamCore&); // undefined
|
StreamCore(const StreamCore&); // undefined
|
||||||
bool compile(StreamProtocolParser::Protocol*);
|
bool compile(StreamProtocolParser::Protocol*);
|
||||||
|
Reference in New Issue
Block a user