diff -r 000000000000 -r 707d51fedbe0 test/harness/testcaseparser.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/harness/testcaseparser.c Tue Jan 24 23:57:11 2012 +0000 @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include +#include "testcaseparser.h" + +/* + * Get the flag (the string of characters which bracket tags in test cases). + */ +const char *testcase_parser_get_flag(TestcaseParser *parser) +{ + char *s=parser->contents; + if (!parser->flag) + { + parser->flag=string_new(NULL); + while(*s>' ' && *s<='~') + string_append_c(parser->flag,*s++); + } + return parser->flag->str; +} + +/* + * Test if the parser has reached the end of the input file + */ +boolean testcase_parser_at_eof(TestcaseParser *parser) +{ + return !parser->contents[parser->pos]; +} + +/* + * Get the next tag (and its associated text, if any) from a test case. + * Returns: TRUE if successful and FALSE if no more valid tags are present. + * Callers can call testcase_parser_at_eof() when testcase_parser_get_next_tag() + * to distinguish EOF and text which isn't a valid tag. + */ +boolean testcase_parser_get_next_tag(TestcaseParser *parser,const char **tag, + const char **text) +{ + size_t n; + char *eol,*endp; + String *string; + mem_free(parser->tag); + parser->tag=NULL; + mem_free(parser->tag_text); + parser->tag_text=NULL; + (void)testcase_parser_get_flag(parser); + if (strncmp(parser->contents+parser->pos,parser->flag->str, + parser->flag->len)) + return FALSE; + eol=strchr(parser->contents+parser->pos,'\n'); + if (!eol) + return FALSE; + endp=eol-parser->flag->len; + if (strncmp(endp,parser->flag->str,parser->flag->len)) + return FALSE; + while(endp>parser->contents && isspace(endp[-1])) + endp--; + parser->pos+=parser->flag->len; + while(isspace(parser->contents[parser->pos])) + parser->pos++; + parser->tag=str_ndup(parser->contents+parser->pos, + endp-(parser->contents+parser->pos)); + parser->pos=eol-parser->contents+1; + string=string_new(NULL); + while (!testcase_parser_at_eof(parser) && + strncmp(parser->contents+parser->pos,parser->flag->str,parser->flag->len)) + { + eol=strchr(parser->contents+parser->pos,'\n'); + if (eol) + n=eol-(parser->contents+parser->pos)+1; + else + n=strlen(parser->contents+parser->pos); + string_append_len(string,parser->contents+parser->pos,n); + parser->pos+=n; + } + parser->tag_text=string_free(string,FALSE); + if (!parser->tag_text) + parser->tag_text=str_dup(""); + if (tag) + *tag=parser->tag; + if (text) + *text=parser->tag_text; + return TRUE; +} + +/* + * Create a testcase parser to read a regular file. + */ +TestcaseParser *testcase_parser_new_from_file(const char *filename) +{ + TestcaseParser *parser; + parser=mem_new0(TestcaseParser,1); + if (!file_get_contents_text(filename,&parser->contents,NULL)) + { + mem_free(parser); + return NULL; + } + parser->filename=str_dup(filename); + return parser; +} + +/* + * Free a testcase parser. + */ +void testcase_parser_free(TestcaseParser *parser) +{ + mem_free(parser->filename); + mem_free(parser->contents); + if (parser->flag) + string_free(parser->flag,TRUE); + mem_free(parser->tag); + mem_free(parser->tag_text); + mem_free(parser); +}