diff -r 000000000000 -r 70cc629ec1e0 test/harness/testcaseoutput.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/harness/testcaseoutput.c Wed Oct 16 22:51:29 2013 +0100 @@ -0,0 +1,140 @@ +#include +#include +#include +#include +#include +#include "testcase.h" +#include "testcaseoutput.h" + +/* + * Replace \r\n with \n, \n with U+240A (visible symbol for LF) + * and \r with U+240D (visible symbol for CR). + */ +static char *dos2unix(const char *text) +{ + gunichar c; + gboolean cr=FALSE; + const gunichar visible_lf=0x240A; + const gunichar visible_cr=0x240D; + GString *string; + string=g_string_new(NULL); + while(*text) + { + c=g_utf8_get_char(text); + text=g_utf8_next_char(text); + if (cr) + { + cr=FALSE; + if (c=='\n') + { + g_string_append_c(string,'\n'); + continue; + } + else + g_string_append_unichar(string,visible_cr); + } + if (c=='\r') + cr=TRUE; + else if (c=='\n') + g_string_append_unichar(string,visible_lf); + else + g_string_append_unichar(string,c); + } + if (cr) + g_string_append_unichar(string,visible_cr); + return g_string_free(string,FALSE); +} + +/* + * Read an output file needed for a testcase (as specified in ). + * The file is read in the encoding specified for communicating with + * bookloupe. + */ +gboolean testcase_output_read(Testcase *testcase,TestcaseOutput *output, + gchar **contents,gsize *length,GError **error) +{ + char *filename,*s,*t; + gboolean retval; + GError *tmp_err=NULL; + if (!strcmp(output->name,"stdout")) + { + *contents=g_strdup(testcase->test_output); + if (length) + *length=strlen(testcase->test_output); + } + else + { + if (testcase->tmpdir) + filename=g_build_filename(testcase->tmpdir,output->name,NULL); + else + filename=g_strdup(output->name); + if (!g_file_get_contents(filename,&s,NULL,error)) + { + g_free(filename); + return FALSE; + } + g_free(filename); + if (testcase->encoding) + { + t=dos2unix(s); + g_free(s); + s=g_convert(t,-1,"UTF-8",testcase->encoding,NULL,length,&tmp_err); + g_free(t); + if (!s) + { + g_propagate_prefixed_error(error,tmp_err, + "Conversion from %s failed: ",testcase->encoding); + return FALSE; + } + *contents=s; + } + else + { + *contents=dos2unix(s); + if (length) + *length=strlen(*contents); + } + } + return TRUE; +} + +/* + * Remove an output file created by program under test. + */ +gboolean testcase_output_remove(Testcase *testcase,TestcaseOutput *output, + GError **error) +{ + char *filename; + if (!strcmp(output->name,"stdout")) + return TRUE; + if (testcase->tmpdir) + filename=g_build_filename(testcase->tmpdir,output->name,NULL); + else + filename=g_strdup(output->name); + if (g_unlink(filename)<0) + { + g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno), + "%s: %s",filename,g_strerror(errno)); + return FALSE; + } + g_free(filename); + return TRUE; +} + +/* Create a new description of an output file expected by a testcase */ +TestcaseOutput *testcase_output_new(const char *name,const char *contents) +{ + TestcaseOutput *output; + output=g_new0(TestcaseOutput,1); + output->name=g_strdup(name); + output->contents=g_strdup(contents); + return output; +} + +/* Free the description of a testcase output file */ +void testcase_output_free(TestcaseOutput *output) +{ + g_free(output->name); + g_free(output->contents); + g_free(output); +}