test/harness/testcaseio.c
author ali <ali@juiblex.co.uk>
Fri Jan 27 16:18:02 2012 +0000 (2012-01-27)
changeset 6 faab25d520dd
parent 5 f600b0d1fc5d
child 7 721e468c10f3
permissions -rw-r--r--
Rely on glib being available everywhere
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@0
    41
	else
ali@0
    42
	{
ali@0
    43
	    fprintf(stderr,"%s: Not a valid testcase (%s)\n",filename,tag);
ali@0
    44
	    testcase_free(testcase);
ali@0
    45
	    testcase_parser_free(parser);
ali@0
    46
	    return NULL;
ali@0
    47
	}
ali@0
    48
	found_tag=TRUE;
ali@0
    49
    }
ali@0
    50
    if (!testcase_parser_at_eof(parser))
ali@0
    51
    {
ali@0
    52
	if (found_tag)
ali@0
    53
	    fprintf(stderr,"%s: Not a valid testcase (garbage at end)\n",
ali@0
    54
	      filename);
ali@0
    55
	else
ali@0
    56
	    fprintf(stderr,"%s: Not a valid testcase (no valid tags)\n",
ali@0
    57
	      filename);
ali@0
    58
	testcase_free(testcase);
ali@0
    59
	testcase_parser_free(parser);
ali@0
    60
	return NULL;
ali@0
    61
    }
ali@0
    62
    testcase_parser_free(parser);
ali@0
    63
    return testcase;
ali@0
    64
}