test/harness/testcaseoutput.c
author ali <ali@juiblex.co.uk>
Fri Oct 25 11:15:18 2013 +0100 (2013-10-25)
changeset 102 ff0aa9b1397a
permissions -rw-r--r--
Fix bug #14: Add a configuration file
     1 #include <stdlib.h>
     2 #include <string.h>
     3 #include <errno.h>
     4 #include <glib.h>
     5 #include <bl/bl.h>
     6 #include "testcase.h"
     7 #include "testcaseoutput.h"
     8 
     9 /*
    10  * Replace \r\n with \n, \n with U+240A (visible symbol for LF)
    11  * and \r with U+240D (visible symbol for CR).
    12  */
    13 static char *dos2unix(const char *text)
    14 {
    15     gunichar c;
    16     gboolean cr=FALSE;
    17     const gunichar visible_lf=0x240A;
    18     const gunichar visible_cr=0x240D;
    19     GString *string;
    20     string=g_string_new(NULL);
    21     while(*text)
    22     {
    23 	c=g_utf8_get_char(text);
    24 	text=g_utf8_next_char(text);
    25 	if (cr)
    26 	{
    27 	    cr=FALSE;
    28 	    if (c=='\n')
    29 	    {
    30 		g_string_append_c(string,'\n');
    31 		continue;
    32 	    }
    33 	    else
    34 		g_string_append_unichar(string,visible_cr);
    35 	}
    36 	if (c=='\r')
    37 	    cr=TRUE;
    38 	else if (c=='\n')
    39 	    g_string_append_unichar(string,visible_lf);
    40 	else
    41 	    g_string_append_unichar(string,c);
    42     }
    43     if (cr)
    44 	g_string_append_unichar(string,visible_cr);
    45     return g_string_free(string,FALSE);
    46 }
    47 
    48 /*
    49  * Read an output file needed for a testcase (as specified in <output>).
    50  * The file is read in the encoding specified for communicating with
    51  * bookloupe.
    52  */
    53 gboolean testcase_output_read(Testcase *testcase,TestcaseOutput *output,
    54   gchar **contents,gsize *length,GError **error)
    55 {
    56     char *filename,*s,*t;
    57     gboolean retval;
    58     GError *tmp_err=NULL;
    59     if (!strcmp(output->name,"stdout"))
    60     {
    61 	*contents=g_strdup(testcase->test_output);
    62 	if (length)
    63 	    *length=strlen(testcase->test_output);
    64     }
    65     else
    66     {
    67 	if (testcase->tmpdir)
    68 	    filename=g_build_filename(testcase->tmpdir,output->name,NULL);
    69 	else
    70 	    filename=g_strdup(output->name);
    71 	if (!g_file_get_contents(filename,&s,NULL,error))
    72 	{
    73 	    g_free(filename);
    74 	    return FALSE;
    75 	}
    76 	g_free(filename);
    77 	if (testcase->encoding)
    78 	{
    79 	    t=dos2unix(s);
    80 	    g_free(s);
    81 	    s=g_convert(t,-1,"UTF-8",testcase->encoding,NULL,length,&tmp_err);
    82 	    g_free(t);
    83 	    if (!s)
    84 	    {
    85 		g_propagate_prefixed_error(error,tmp_err,
    86 		  "Conversion from %s failed: ",testcase->encoding);
    87 		return FALSE;
    88 	    }
    89 	    *contents=s;
    90 	}
    91 	else
    92 	{
    93 	    *contents=dos2unix(s);
    94 	    if (length)
    95 		*length=strlen(*contents);
    96 	}
    97     }
    98     return TRUE;
    99 }
   100 
   101 /*
   102  * Remove an output file created by program under test.
   103  */
   104 gboolean testcase_output_remove(Testcase *testcase,TestcaseOutput *output,
   105   GError **error)
   106 {
   107     char *filename;
   108     if (!strcmp(output->name,"stdout"))
   109 	return TRUE;
   110     if (testcase->tmpdir)
   111 	filename=g_build_filename(testcase->tmpdir,output->name,NULL);
   112     else
   113 	filename=g_strdup(output->name);
   114     if (g_unlink(filename)<0)
   115     {
   116 	g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno),
   117 	  "%s: %s",filename,g_strerror(errno));
   118 	return FALSE;
   119     }
   120     g_free(filename);
   121     return TRUE;
   122 }
   123 
   124 /* Create a new description of an output file expected by a testcase */
   125 TestcaseOutput *testcase_output_new(const char *name,const char *contents)
   126 {
   127     TestcaseOutput *output;
   128     output=g_new0(TestcaseOutput,1);
   129     output->name=g_strdup(name);
   130     output->contents=g_strdup(contents);
   131     return output;
   132 }
   133 
   134 /* Free the description of a testcase output file */
   135 void testcase_output_free(TestcaseOutput *output)
   136 {
   137     g_free(output->name);
   138     g_free(output->contents);
   139     g_free(output);
   140 }