libpsyc/test/testParser.c

105 lines
2.1 KiB
C
Raw Normal View History

2010-02-20 16:41:16 +00:00
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <psyc.h>
#include <psyc/parser.h>
int main (int argc, char **argv)
2010-02-20 16:41:16 +00:00
{
int idx, ret, routing_only = argc > 2, verbose = argc > 3;
2011-04-22 20:59:15 +00:00
char buffer[2048], oper;
psycString name, value, elem;
psycParseState state;
psycParseListState listState;
2010-02-20 16:41:16 +00:00
int file = open(argv[1],O_RDONLY);
if (file < 0)
return -1;
idx = read(file,(void*)buffer,sizeof(buffer));
2010-02-20 16:41:16 +00:00
if (verbose) {
printf(">> INPUT\n");
printf("%.*s\n", (int)idx, buffer);
printf(">> PARSE\n");
}
if (routing_only)
psyc_initParseState2(&state, PSYC_PARSE_ROUTING_ONLY);
2011-04-30 14:42:03 +00:00
else
psyc_initParseState(&state);
psyc_setParseBuffer(&state, psyc_newString(buffer, idx));
2010-02-20 16:41:16 +00:00
// try parsing that now
do
{
2011-04-30 14:42:03 +00:00
oper = 0;
name.length = 0;
value.length = 0;
ret = psyc_parse(&state, &oper, &name, &value);
2011-04-30 14:42:03 +00:00
if (verbose)
printf(">> ret = %d\n", ret);
switch (ret)
{
2011-04-22 15:09:32 +00:00
case PSYC_PARSE_ROUTING:
case PSYC_PARSE_ENTITY:
if (verbose)
printf("%c", oper);
2011-04-22 15:09:32 +00:00
case PSYC_PARSE_BODY:
2011-04-20 16:59:27 +00:00
// printf("the string is '%.*s'\n", name);
if (verbose)
printf("%.*s = %.*s\n",
(int)name.length, name.ptr,
(int)value.length, value.ptr);
2011-04-19 17:41:25 +00:00
if (memcmp(name.ptr, "_list", 5) == 0)
{
if (verbose)
printf(">> LIST START\n");
psyc_initParseListState(&listState);
psyc_setParseListBuffer(&listState, value);
while ((ret = psyc_parseList(&listState, &name, &value, &elem)))
2011-04-19 17:41:25 +00:00
{
switch (ret)
{
2011-04-22 15:09:32 +00:00
case PSYC_PARSE_LIST_END:
case PSYC_PARSE_LIST_ELEM:
if (verbose)
printf("|%.*s\n", (int)elem.length, elem.ptr);
2011-04-19 17:41:25 +00:00
break;
default:
printf("Error while parsing list: %i\n", ret);
return 1;
}
2011-04-22 15:09:32 +00:00
if (ret == PSYC_PARSE_LIST_END)
2011-04-19 17:41:25 +00:00
{
if (verbose)
printf(">> LIST END\n");
2011-04-19 17:41:25 +00:00
break;
}
}
}
break;
2011-04-22 15:09:32 +00:00
case PSYC_PARSE_COMPLETE:
// printf("Done parsing.\n");
ret = 0;
continue;
2011-04-22 15:09:32 +00:00
case PSYC_PARSE_INSUFFICIENT:
printf("Insufficient data.\n");
return 1;
default:
printf("Error while parsing: %i\n", ret);
return 1;
}
}
while (ret);
2010-02-20 16:41:16 +00:00
return 0;
}