ali@0: #include ali@0: #include ali@0: #include ali@6: #include ali@5: #include ali@0: #include "testcaseparser.h" ali@0: #include "testcaseio.h" ali@0: ali@0: /* ali@0: * Read a testcase in from a file. ali@0: * On error, print a suitable message on stderr and return NULL. ali@0: * The returned testcase should be freed with testcase_free(). ali@0: */ ali@0: Testcase *testcase_parse_file(const char *filename) ali@0: { ali@0: Testcase *testcase; ali@0: TestcaseParser *parser; ali@0: char *s; ali@0: const char *tag,*text; ali@6: gboolean found_tag=FALSE; ali@0: parser=testcase_parser_new_from_file(filename); ali@0: if (!parser) ali@0: return NULL; ali@0: if (!*testcase_parser_get_flag(parser)) ali@0: { ali@0: fprintf(stderr,"%s: Not a valid testcase (flag)\n",filename); ali@0: testcase_parser_free(parser); ali@0: return NULL; ali@0: } ali@6: testcase=g_new0(Testcase,1); ali@6: testcase->basename=g_path_get_basename(filename); ali@0: s=strrchr(testcase->basename,'.'); ali@0: if (s) ali@0: *s='\0'; ali@0: while(testcase_parser_get_next_tag(parser,&tag,&text)) ali@0: { ali@0: if (!testcase->input && !strcmp(tag,"INPUT")) ali@6: testcase->input=g_strdup(text); ali@0: else if (!testcase->expected && !strcmp(tag,"EXPECTED")) ali@6: testcase->expected=g_strdup(text); ali@7: else if (!testcase->encoding && !strcmp(tag,"ENCODING")) ali@7: testcase->encoding=g_strdup(text); ali@0: else ali@0: { ali@0: fprintf(stderr,"%s: Not a valid testcase (%s)\n",filename,tag); ali@0: testcase_free(testcase); ali@0: testcase_parser_free(parser); ali@0: return NULL; ali@0: } ali@0: found_tag=TRUE; ali@0: } ali@0: if (!testcase_parser_at_eof(parser)) ali@0: { ali@0: if (found_tag) ali@0: fprintf(stderr,"%s: Not a valid testcase (garbage at end)\n", ali@0: filename); ali@0: else ali@0: fprintf(stderr,"%s: Not a valid testcase (no valid tags)\n", ali@0: filename); ali@0: testcase_free(testcase); ali@0: testcase_parser_free(parser); ali@0: return NULL; ali@0: } ali@0: testcase_parser_free(parser); ali@0: return testcase; ali@0: }