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