ali@0
|
1 |
#include <stdlib.h>
|
ali@0
|
2 |
#include <stdio.h>
|
ali@0
|
3 |
#include <string.h>
|
ali@6
|
4 |
#include <glib.h>
|
ali@5
|
5 |
#include <bl/bl.h>
|
ali@0
|
6 |
#include "testcaseparser.h"
|
ali@0
|
7 |
#include "testcaseio.h"
|
ali@0
|
8 |
|
ali@0
|
9 |
/*
|
ali@0
|
10 |
* Read a testcase in from a file.
|
ali@0
|
11 |
* On error, print a suitable message on stderr and return NULL.
|
ali@0
|
12 |
* The returned testcase should be freed with testcase_free().
|
ali@0
|
13 |
*/
|
ali@0
|
14 |
Testcase *testcase_parse_file(const char *filename)
|
ali@0
|
15 |
{
|
ali@0
|
16 |
Testcase *testcase;
|
ali@0
|
17 |
TestcaseParser *parser;
|
ali@0
|
18 |
char *s;
|
ali@0
|
19 |
const char *tag,*text;
|
ali@6
|
20 |
gboolean found_tag=FALSE;
|
ali@0
|
21 |
parser=testcase_parser_new_from_file(filename);
|
ali@0
|
22 |
if (!parser)
|
ali@0
|
23 |
return NULL;
|
ali@0
|
24 |
if (!*testcase_parser_get_flag(parser))
|
ali@0
|
25 |
{
|
ali@0
|
26 |
fprintf(stderr,"%s: Not a valid testcase (flag)\n",filename);
|
ali@0
|
27 |
testcase_parser_free(parser);
|
ali@0
|
28 |
return NULL;
|
ali@0
|
29 |
}
|
ali@6
|
30 |
testcase=g_new0(Testcase,1);
|
ali@6
|
31 |
testcase->basename=g_path_get_basename(filename);
|
ali@0
|
32 |
s=strrchr(testcase->basename,'.');
|
ali@0
|
33 |
if (s)
|
ali@0
|
34 |
*s='\0';
|
ali@0
|
35 |
while(testcase_parser_get_next_tag(parser,&tag,&text))
|
ali@0
|
36 |
{
|
ali@0
|
37 |
if (!testcase->input && !strcmp(tag,"INPUT"))
|
ali@6
|
38 |
testcase->input=g_strdup(text);
|
ali@0
|
39 |
else if (!testcase->expected && !strcmp(tag,"EXPECTED"))
|
ali@6
|
40 |
testcase->expected=g_strdup(text);
|
ali@7
|
41 |
else if (!testcase->encoding && !strcmp(tag,"ENCODING"))
|
ali@8
|
42 |
testcase->encoding=g_strchomp(g_strdup(text));
|
ali@0
|
43 |
else
|
ali@0
|
44 |
{
|
ali@0
|
45 |
fprintf(stderr,"%s: Not a valid testcase (%s)\n",filename,tag);
|
ali@0
|
46 |
testcase_free(testcase);
|
ali@0
|
47 |
testcase_parser_free(parser);
|
ali@0
|
48 |
return NULL;
|
ali@0
|
49 |
}
|
ali@0
|
50 |
found_tag=TRUE;
|
ali@0
|
51 |
}
|
ali@0
|
52 |
if (!testcase_parser_at_eof(parser))
|
ali@0
|
53 |
{
|
ali@0
|
54 |
if (found_tag)
|
ali@0
|
55 |
fprintf(stderr,"%s: Not a valid testcase (garbage at end)\n",
|
ali@0
|
56 |
filename);
|
ali@0
|
57 |
else
|
ali@0
|
58 |
fprintf(stderr,"%s: Not a valid testcase (no valid tags)\n",
|
ali@0
|
59 |
filename);
|
ali@0
|
60 |
testcase_free(testcase);
|
ali@0
|
61 |
testcase_parser_free(parser);
|
ali@0
|
62 |
return NULL;
|
ali@0
|
63 |
}
|
ali@0
|
64 |
testcase_parser_free(parser);
|
ali@0
|
65 |
return testcase;
|
ali@0
|
66 |
}
|