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