From 7e29ca4e5abcbc212a4630dd80b133f9ed96a32a Mon Sep 17 00:00:00 2001 From: Gabor Adam Toth Date: Tue, 26 Apr 2011 03:43:49 +0200 Subject: [PATCH] parser: fix for empty data, testParser: verbose mode --- src/parser.c | 12 ++++++----- test/Makefile | 2 +- test/packets/test-4-circuit | 2 +- test/packets/test-4-circuit-n | 4 ---- test/testParser.c | 38 +++++++++++++++++++++-------------- 5 files changed, 32 insertions(+), 26 deletions(-) delete mode 100644 test/packets/test-4-circuit-n diff --git a/src/parser.c b/src/parser.c index 8025b07..6785514 100644 --- a/src/parser.c +++ b/src/parser.c @@ -354,18 +354,20 @@ psycParseRC psyc_parse(psycParseState* state, char* oper, psycString* name, psyc { 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; return PSYC_PARSE_INSUFFICIENT; } - if (state->buffer.ptr[state->cursor+1] == '|' && - state->buffer.ptr[state->cursor+2] == '\n') // packet ends here + if (state->buffer.ptr[state->cursor+nl] == '|' && + state->buffer.ptr[state->cursor+1+nl] == '\n') // packet ends here { - state->cursor++; + state->cursor += nl; state->part = PSYC_PART_END; return PSYC_PARSE_BODY; } diff --git a/test/Makefile b/test/Makefile index 58118ee..0503d44 100644 --- a/test/Makefile +++ b/test/Makefile @@ -9,7 +9,7 @@ all: $(TARGETS) ./isRoutingVar test: $(TARGETS) - for f in packets/*; do echo "\n>> $$f"; ./testParser $$f; done + for f in packets/*; do echo ">> $$f"; ./testParser $$f; done clean: rm -f $(TARGETS) diff --git a/test/packets/test-4-circuit b/test/packets/test-4-circuit index a9d3aec..5905f2b 100644 --- a/test/packets/test-4-circuit +++ b/test/packets/test-4-circuit @@ -1,4 +1,4 @@ -:_list_understand_modules _state|_fragments|_context +:_list_understand_modules |_state|_fragments|_context _request_features | diff --git a/test/packets/test-4-circuit-n b/test/packets/test-4-circuit-n deleted file mode 100644 index a9d3aec..0000000 --- a/test/packets/test-4-circuit-n +++ /dev/null @@ -1,4 +0,0 @@ -:_list_understand_modules _state|_fragments|_context - -_request_features -| diff --git a/test/testParser.c b/test/testParser.c index 9d86126..e3b0a0d 100644 --- a/test/testParser.c +++ b/test/testParser.c @@ -6,7 +6,7 @@ int main(int argc, char** argv) { - int indx, ret; + int indx, ret, verbose = argc > 2; char buffer[2048], oper; psycString name, value, elem; psycParseState state; @@ -17,10 +17,11 @@ int main(int argc, char** argv) return -1; indx = read(file,(void*)buffer,sizeof(buffer)); -// write(1, ">> INPUT\n", 9); -// write(1, buffer, indx); -// write(1, ">> PARSE\n", 9); - + if (verbose) { + write(1, ">> INPUT\n", 9); + write(1, buffer, indx); + write(1, ">> PARSE\n", 9); + } psyc_initParseState(&state); psyc_nextParseBuffer(&state, psyc_newString(buffer, indx)); @@ -33,16 +34,20 @@ int main(int argc, char** argv) { case PSYC_PARSE_ROUTING: case PSYC_PARSE_ENTITY: - write(1, &oper, 1); + if (verbose) + write(1, &oper, 1); case PSYC_PARSE_BODY: // printf("the string is '%.*s'\n", name); - write(1, name.ptr, name.length); - write(1, " = ", 3); - write(1, value.ptr, value.length); - write(1, "\n", 1); + if (verbose) { + write(1, name.ptr, name.length); + write(1, " = ", 3); + write(1, value.ptr, value.length); + write(1, "\n", 1); + } 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_nextParseListBuffer(&listState, value); 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_ELEM: - write(1, "|", 1); - write(1, elem.ptr, elem.length); - write(1, "\n", 1); + if (verbose) { + write(1, "|", 1); + write(1, elem.ptr, elem.length); + write(1, "\n", 1); + } break; default: printf("Error while parsing list: %i\n", ret); @@ -62,7 +69,8 @@ int main(int argc, char** argv) if (ret == PSYC_PARSE_LIST_END) { - write(1, ">>> LIST END\n", 13); + if (verbose) + write(1, ">>> LIST END\n", 13); break; } }