mirror of
git://git.psyc.eu/libpsyc
synced 2024-08-15 03:19:02 +00:00
parser: fix for empty data, testParser: verbose mode
This commit is contained in:
parent
ad415322eb
commit
7e29ca4e5a
5 changed files with 32 additions and 26 deletions
12
src/parser.c
12
src/parser.c
|
@ -354,18 +354,20 @@ psycParseRC psyc_parse(psycParseState* state, char* oper, psycString* name, psyc
|
||||||
{
|
{
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
if (state->buffer.ptr[state->cursor] == '\n')
|
uint8_t nl = state->buffer.ptr[state->cursor] == '\n';
|
||||||
|
// check for |\n if we're at the start of data or we have found a \n
|
||||||
|
if (state->cursor == state->startc || nl)
|
||||||
{
|
{
|
||||||
if (state->cursor+2 >= state->buffer.length) // incremented cursor inside length?
|
if (state->cursor+1+nl >= state->buffer.length) // incremented cursor inside length?
|
||||||
{
|
{
|
||||||
state->cursor = state->startc;
|
state->cursor = state->startc;
|
||||||
return PSYC_PARSE_INSUFFICIENT;
|
return PSYC_PARSE_INSUFFICIENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state->buffer.ptr[state->cursor+1] == '|' &&
|
if (state->buffer.ptr[state->cursor+nl] == '|' &&
|
||||||
state->buffer.ptr[state->cursor+2] == '\n') // packet ends here
|
state->buffer.ptr[state->cursor+1+nl] == '\n') // packet ends here
|
||||||
{
|
{
|
||||||
state->cursor++;
|
state->cursor += nl;
|
||||||
state->part = PSYC_PART_END;
|
state->part = PSYC_PART_END;
|
||||||
return PSYC_PARSE_BODY;
|
return PSYC_PARSE_BODY;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ all: $(TARGETS)
|
||||||
./isRoutingVar
|
./isRoutingVar
|
||||||
|
|
||||||
test: $(TARGETS)
|
test: $(TARGETS)
|
||||||
for f in packets/*; do echo "\n>> $$f"; ./testParser $$f; done
|
for f in packets/*; do echo ">> $$f"; ./testParser $$f; done
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(TARGETS)
|
rm -f $(TARGETS)
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
:_list_understand_modules _state|_fragments|_context
|
:_list_understand_modules |_state|_fragments|_context
|
||||||
|
|
||||||
_request_features
|
_request_features
|
||||||
|
|
|
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
:_list_understand_modules _state|_fragments|_context
|
|
||||||
|
|
||||||
_request_features
|
|
||||||
|
|
|
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
int indx, ret;
|
int indx, ret, verbose = argc > 2;
|
||||||
char buffer[2048], oper;
|
char buffer[2048], oper;
|
||||||
psycString name, value, elem;
|
psycString name, value, elem;
|
||||||
psycParseState state;
|
psycParseState state;
|
||||||
|
@ -17,10 +17,11 @@ int main(int argc, char** argv)
|
||||||
return -1;
|
return -1;
|
||||||
indx = read(file,(void*)buffer,sizeof(buffer));
|
indx = read(file,(void*)buffer,sizeof(buffer));
|
||||||
|
|
||||||
// write(1, ">> INPUT\n", 9);
|
if (verbose) {
|
||||||
// write(1, buffer, indx);
|
write(1, ">> INPUT\n", 9);
|
||||||
// write(1, ">> PARSE\n", 9);
|
write(1, buffer, indx);
|
||||||
|
write(1, ">> PARSE\n", 9);
|
||||||
|
}
|
||||||
psyc_initParseState(&state);
|
psyc_initParseState(&state);
|
||||||
psyc_nextParseBuffer(&state, psyc_newString(buffer, indx));
|
psyc_nextParseBuffer(&state, psyc_newString(buffer, indx));
|
||||||
|
|
||||||
|
@ -33,16 +34,20 @@ int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
case PSYC_PARSE_ROUTING:
|
case PSYC_PARSE_ROUTING:
|
||||||
case PSYC_PARSE_ENTITY:
|
case PSYC_PARSE_ENTITY:
|
||||||
write(1, &oper, 1);
|
if (verbose)
|
||||||
|
write(1, &oper, 1);
|
||||||
case PSYC_PARSE_BODY:
|
case PSYC_PARSE_BODY:
|
||||||
// printf("the string is '%.*s'\n", name);
|
// printf("the string is '%.*s'\n", name);
|
||||||
write(1, name.ptr, name.length);
|
if (verbose) {
|
||||||
write(1, " = ", 3);
|
write(1, name.ptr, name.length);
|
||||||
write(1, value.ptr, value.length);
|
write(1, " = ", 3);
|
||||||
write(1, "\n", 1);
|
write(1, value.ptr, value.length);
|
||||||
|
write(1, "\n", 1);
|
||||||
|
}
|
||||||
if (memcmp(name.ptr, "_list", 5) == 0)
|
if (memcmp(name.ptr, "_list", 5) == 0)
|
||||||
{
|
{
|
||||||
write(1, ">>> LIST START\n", 15);
|
if (verbose)
|
||||||
|
write(1, ">>> LIST START\n", 15);
|
||||||
psyc_initParseListState(&listState);
|
psyc_initParseListState(&listState);
|
||||||
psyc_nextParseListBuffer(&listState, value);
|
psyc_nextParseListBuffer(&listState, value);
|
||||||
while ((ret = psyc_parseList(&listState, &name, &value, &elem)))
|
while ((ret = psyc_parseList(&listState, &name, &value, &elem)))
|
||||||
|
@ -51,9 +56,11 @@ int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
case PSYC_PARSE_LIST_END:
|
case PSYC_PARSE_LIST_END:
|
||||||
case PSYC_PARSE_LIST_ELEM:
|
case PSYC_PARSE_LIST_ELEM:
|
||||||
write(1, "|", 1);
|
if (verbose) {
|
||||||
write(1, elem.ptr, elem.length);
|
write(1, "|", 1);
|
||||||
write(1, "\n", 1);
|
write(1, elem.ptr, elem.length);
|
||||||
|
write(1, "\n", 1);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
printf("Error while parsing list: %i\n", ret);
|
printf("Error while parsing list: %i\n", ret);
|
||||||
|
@ -62,7 +69,8 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
if (ret == PSYC_PARSE_LIST_END)
|
if (ret == PSYC_PARSE_LIST_END)
|
||||||
{
|
{
|
||||||
write(1, ">>> LIST END\n", 13);
|
if (verbose)
|
||||||
|
write(1, ">>> LIST END\n", 13);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue