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