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