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