6 #include "testcaseparser.h"
7 #include "testcaseinput.h"
8 #include "testcaseio.h"
11 * Read a testcase in from a file.
12 * On error, print a suitable message using g_printerr and return NULL.
13 * The returned testcase should be freed with testcase_free().
15 Testcase *testcase_parse_file(const char *filename)
18 TestcaseParser *parser;
19 TestcaseInput *input=NULL;
21 const char *tag,*text;
22 gboolean found_tag=FALSE;
23 parser=testcase_parser_new_from_file(filename);
26 if (!*testcase_parser_get_flag(parser))
28 g_printerr("%s: Not a valid testcase (flag)\n",filename);
29 testcase_parser_free(parser);
32 testcase=g_new0(Testcase,1);
33 testcase->basename=g_path_get_basename(filename);
34 s=strrchr(testcase->basename,'.');
37 while(testcase_parser_get_next_tag(parser,&tag,&text))
39 if (!input && !strcmp(tag,"INPUT"))
40 input=testcase_input_new("TEST-XXXXXX",text);
41 else if (g_str_has_prefix(tag,"INPUT(") && tag[strlen(tag)-1]==')')
43 arg=g_strndup(tag+6,strlen(tag)-7);
44 s=g_path_get_dirname(arg);
48 * Otherwise it would be possible to overwrite an arbitary
49 * file on somebody's computer by getting them to run a
53 "%s: Input files may not have a directory component\n",arg);
56 testcase_free(testcase);
57 testcase_parser_free(parser);
61 testcase->inputs=g_slist_prepend(testcase->inputs,
62 testcase_input_new(arg,text));
63 if (!strstr(arg,"XXXXXX"))
64 testcase->flags|=TESTCASE_TMP_DIR;
67 else if (!testcase->expected && !strcmp(tag,"EXPECTED"))
68 testcase->expected=g_strdup(text);
69 else if (!testcase->encoding && !strcmp(tag,"ENCODING"))
70 testcase->encoding=g_strchomp(g_strdup(text));
71 else if (!testcase->encoding && !strcmp(tag,"OPTIONS"))
73 testcase->options=g_strsplit(text,"\n",0);
74 g_free(testcase->options[g_strv_length(testcase->options)-1]);
75 testcase->options[g_strv_length(testcase->options)-1]=NULL;
79 g_printerr("%s: Not a valid testcase (%s)\n",filename,tag);
80 testcase_free(testcase);
81 testcase_parser_free(parser);
86 if (!testcase_parser_at_eof(parser))
89 g_printerr("%s: Not a valid testcase (garbage at end)\n",
92 g_printerr("%s: Not a valid testcase (no valid tags)\n",
94 testcase_free(testcase);
95 testcase_parser_free(parser);
99 input=testcase_input_new("TEST-XXXXXX",NULL);
100 testcase->inputs=g_slist_prepend(testcase->inputs,input);
101 testcase_parser_free(parser);