6 #include "testcaseparser.h"
9 * Get the flag (the string of characters which bracket tags in test cases).
11 const char *testcase_parser_get_flag(TestcaseParser *parser)
13 char *s=parser->contents;
16 parser->flag=string_new(NULL);
17 while(*s>' ' && *s<='~')
18 string_append_c(parser->flag,*s++);
20 return parser->flag->str;
24 * Test if the parser has reached the end of the input file
26 boolean testcase_parser_at_eof(TestcaseParser *parser)
28 return !parser->contents[parser->pos];
32 * Get the next tag (and its associated text, if any) from a test case.
33 * Returns: TRUE if successful and FALSE if no more valid tags are present.
34 * Callers can call testcase_parser_at_eof() when testcase_parser_get_next_tag()
35 * to distinguish EOF and text which isn't a valid tag.
37 boolean testcase_parser_get_next_tag(TestcaseParser *parser,const char **tag,
43 mem_free(parser->tag);
45 mem_free(parser->tag_text);
46 parser->tag_text=NULL;
47 (void)testcase_parser_get_flag(parser);
48 if (strncmp(parser->contents+parser->pos,parser->flag->str,
51 eol=strchr(parser->contents+parser->pos,'\n');
54 endp=eol-parser->flag->len;
55 if (strncmp(endp,parser->flag->str,parser->flag->len))
57 while(endp>parser->contents && isspace(endp[-1]))
59 parser->pos+=parser->flag->len;
60 while(isspace(parser->contents[parser->pos]))
62 parser->tag=str_ndup(parser->contents+parser->pos,
63 endp-(parser->contents+parser->pos));
64 parser->pos=eol-parser->contents+1;
65 string=string_new(NULL);
66 while (!testcase_parser_at_eof(parser) &&
67 strncmp(parser->contents+parser->pos,parser->flag->str,parser->flag->len))
69 eol=strchr(parser->contents+parser->pos,'\n');
71 n=eol-(parser->contents+parser->pos)+1;
73 n=strlen(parser->contents+parser->pos);
74 string_append_len(string,parser->contents+parser->pos,n);
77 parser->tag_text=string_free(string,FALSE);
78 if (!parser->tag_text)
79 parser->tag_text=str_dup("");
83 *text=parser->tag_text;
88 * Create a testcase parser to read a regular file.
90 TestcaseParser *testcase_parser_new_from_file(const char *filename)
92 TestcaseParser *parser;
93 parser=mem_new0(TestcaseParser,1);
94 if (!file_get_contents_text(filename,&parser->contents,NULL))
99 parser->filename=str_dup(filename);
104 * Free a testcase parser.
106 void testcase_parser_free(TestcaseParser *parser)
108 mem_free(parser->filename);
109 mem_free(parser->contents);
111 string_free(parser->flag,TRUE);
112 mem_free(parser->tag);
113 mem_free(parser->tag_text);