test/harness/testcaseio.c
author ali <ali@juiblex.co.uk>
Thu Jan 26 23:35:52 2012 +0000 (2012-01-26)
changeset 3 ec364358631b
child 5 f600b0d1fc5d
permissions -rw-r--r--
Non-win32 platforms might need mkstemp too
ali@0
     1
#include <stdlib.h>
ali@0
     2
#include <stdio.h>
ali@0
     3
#include <string.h>
ali@0
     4
#include <gclib/gclib.h>
ali@0
     5
#include "testcaseparser.h"
ali@0
     6
#include "testcaseio.h"
ali@0
     7
ali@0
     8
/*
ali@0
     9
 * Read a testcase in from a file.
ali@0
    10
 * On error, print a suitable message on stderr and return NULL.
ali@0
    11
 * The returned testcase should be freed with testcase_free().
ali@0
    12
 */
ali@0
    13
Testcase *testcase_parse_file(const char *filename)
ali@0
    14
{
ali@0
    15
    Testcase *testcase;
ali@0
    16
    TestcaseParser *parser;
ali@0
    17
    char *s;
ali@0
    18
    const char *tag,*text;
ali@0
    19
    boolean found_tag=FALSE;
ali@0
    20
    parser=testcase_parser_new_from_file(filename);
ali@0
    21
    if (!parser)
ali@0
    22
	return NULL;
ali@0
    23
    if (!*testcase_parser_get_flag(parser))
ali@0
    24
    {
ali@0
    25
	fprintf(stderr,"%s: Not a valid testcase (flag)\n",filename);
ali@0
    26
	testcase_parser_free(parser);
ali@0
    27
	return NULL;
ali@0
    28
    }
ali@0
    29
    testcase=mem_new0(Testcase,1);
ali@0
    30
    testcase->basename=path_get_basename(filename);
ali@0
    31
    s=strrchr(testcase->basename,'.');
ali@0
    32
    if (s)
ali@0
    33
	*s='\0';
ali@0
    34
    while(testcase_parser_get_next_tag(parser,&tag,&text))
ali@0
    35
    {
ali@0
    36
	if (!testcase->input && !strcmp(tag,"INPUT"))
ali@0
    37
	    testcase->input=str_dup(text);
ali@0
    38
	else if (!testcase->expected && !strcmp(tag,"EXPECTED"))
ali@0
    39
	    testcase->expected=str_dup(text);
ali@0
    40
	else
ali@0
    41
	{
ali@0
    42
	    fprintf(stderr,"%s: Not a valid testcase (%s)\n",filename,tag);
ali@0
    43
	    testcase_free(testcase);
ali@0
    44
	    testcase_parser_free(parser);
ali@0
    45
	    return NULL;
ali@0
    46
	}
ali@0
    47
	found_tag=TRUE;
ali@0
    48
    }
ali@0
    49
    if (!testcase_parser_at_eof(parser))
ali@0
    50
    {
ali@0
    51
	if (found_tag)
ali@0
    52
	    fprintf(stderr,"%s: Not a valid testcase (garbage at end)\n",
ali@0
    53
	      filename);
ali@0
    54
	else
ali@0
    55
	    fprintf(stderr,"%s: Not a valid testcase (no valid tags)\n",
ali@0
    56
	      filename);
ali@0
    57
	testcase_free(testcase);
ali@0
    58
	testcase_parser_free(parser);
ali@0
    59
	return NULL;
ali@0
    60
    }
ali@0
    61
    testcase_parser_free(parser);
ali@0
    62
    return testcase;
ali@0
    63
}