ali@0: #include ali@0: #include ali@0: #include ali@6: #include ali@5: #include ali@0: #include "testcaseparser.h" ali@0: ali@0: /* ali@0: * Get the flag (the string of characters which bracket tags in test cases). ali@0: */ ali@0: const char *testcase_parser_get_flag(TestcaseParser *parser) ali@0: { ali@0: char *s=parser->contents; ali@0: if (!parser->flag) ali@0: { ali@6: parser->flag=g_string_new(NULL); ali@0: while(*s>' ' && *s<='~') ali@6: g_string_append_c(parser->flag,*s++); ali@0: } ali@0: return parser->flag->str; ali@0: } ali@0: ali@0: /* ali@0: * Test if the parser has reached the end of the input file ali@0: */ ali@6: gboolean testcase_parser_at_eof(TestcaseParser *parser) ali@0: { ali@0: return !parser->contents[parser->pos]; ali@0: } ali@0: ali@0: /* ali@0: * Get the next tag (and its associated text, if any) from a test case. ali@0: * Returns: TRUE if successful and FALSE if no more valid tags are present. ali@0: * Callers can call testcase_parser_at_eof() when testcase_parser_get_next_tag() ali@0: * to distinguish EOF and text which isn't a valid tag. ali@0: */ ali@6: gboolean testcase_parser_get_next_tag(TestcaseParser *parser,const char **tag, ali@0: const char **text) ali@0: { ali@0: size_t n; ali@0: char *eol,*endp; ali@6: GString *string; ali@6: g_free(parser->tag); ali@0: parser->tag=NULL; ali@6: g_free(parser->tag_text); ali@0: parser->tag_text=NULL; ali@0: (void)testcase_parser_get_flag(parser); ali@0: if (strncmp(parser->contents+parser->pos,parser->flag->str, ali@0: parser->flag->len)) ali@0: return FALSE; ali@0: eol=strchr(parser->contents+parser->pos,'\n'); ali@0: if (!eol) ali@0: return FALSE; ali@0: endp=eol-parser->flag->len; ali@0: if (strncmp(endp,parser->flag->str,parser->flag->len)) ali@0: return FALSE; ali@6: while(endp>parser->contents && g_ascii_isspace(endp[-1])) ali@0: endp--; ali@0: parser->pos+=parser->flag->len; ali@6: while(g_ascii_isspace(parser->contents[parser->pos])) ali@0: parser->pos++; ali@6: parser->tag=g_strndup(parser->contents+parser->pos, ali@0: endp-(parser->contents+parser->pos)); ali@0: parser->pos=eol-parser->contents+1; ali@6: string=g_string_new(NULL); ali@0: while (!testcase_parser_at_eof(parser) && ali@0: strncmp(parser->contents+parser->pos,parser->flag->str,parser->flag->len)) ali@0: { ali@0: eol=strchr(parser->contents+parser->pos,'\n'); ali@0: if (eol) ali@0: n=eol-(parser->contents+parser->pos)+1; ali@0: else ali@0: n=strlen(parser->contents+parser->pos); ali@6: g_string_append_len(string,parser->contents+parser->pos,n); ali@0: parser->pos+=n; ali@0: } ali@6: parser->tag_text=g_string_free(string,FALSE); ali@0: if (!parser->tag_text) ali@6: parser->tag_text=g_strdup(""); ali@0: if (tag) ali@0: *tag=parser->tag; ali@0: if (text) ali@0: *text=parser->tag_text; ali@0: return TRUE; ali@0: } ali@0: ali@0: /* ali@0: * Create a testcase parser to read a regular file. ali@0: */ ali@0: TestcaseParser *testcase_parser_new_from_file(const char *filename) ali@0: { ali@0: TestcaseParser *parser; ali@6: parser=g_new0(TestcaseParser,1); ali@0: if (!file_get_contents_text(filename,&parser->contents,NULL)) ali@0: { ali@6: g_free(parser); ali@0: return NULL; ali@0: } ali@6: parser->filename=g_strdup(filename); ali@0: return parser; ali@0: } ali@0: ali@0: /* ali@0: * Free a testcase parser. ali@0: */ ali@0: void testcase_parser_free(TestcaseParser *parser) ali@0: { ali@6: g_free(parser->filename); ali@6: g_free(parser->contents); ali@0: if (parser->flag) ali@6: g_string_free(parser->flag,TRUE); ali@6: g_free(parser->tag); ali@6: g_free(parser->tag_text); ali@6: g_free(parser); ali@0: }