7 #include "testcaseoutput.h"
10 * Replace \r\n with \n, \n with U+240A (visible symbol for LF)
11 * and \r with U+240D (visible symbol for CR).
13 static char *dos2unix(const char *text)
17 const gunichar visible_lf=0x240A;
18 const gunichar visible_cr=0x240D;
20 string=g_string_new(NULL);
23 c=g_utf8_get_char(text);
24 text=g_utf8_next_char(text);
30 g_string_append_c(string,'\n');
34 g_string_append_unichar(string,visible_cr);
39 g_string_append_unichar(string,visible_lf);
41 g_string_append_unichar(string,c);
44 g_string_append_unichar(string,visible_cr);
45 return g_string_free(string,FALSE);
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
53 gboolean testcase_output_read(Testcase *testcase,TestcaseOutput *output,
54 gchar **contents,gsize *length,GError **error)
59 if (!strcmp(output->name,"stdout"))
61 *contents=g_strdup(testcase->test_output);
63 *length=strlen(testcase->test_output);
68 filename=g_build_filename(testcase->tmpdir,output->name,NULL);
70 filename=g_strdup(output->name);
71 if (!g_file_get_contents(filename,&s,NULL,error))
77 if (testcase->encoding)
81 s=g_convert(t,-1,"UTF-8",testcase->encoding,NULL,length,&tmp_err);
85 g_propagate_prefixed_error(error,tmp_err,
86 "Conversion from %s failed: ",testcase->encoding);
93 *contents=dos2unix(s);
95 *length=strlen(*contents);
102 * Remove an output file created by program under test.
104 gboolean testcase_output_remove(Testcase *testcase,TestcaseOutput *output,
108 if (!strcmp(output->name,"stdout"))
110 if (testcase->tmpdir)
111 filename=g_build_filename(testcase->tmpdir,output->name,NULL);
113 filename=g_strdup(output->name);
114 if (g_unlink(filename)<0)
116 g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno),
117 "%s: %s",filename,g_strerror(errno));
124 /* Create a new description of an output file expected by a testcase */
125 TestcaseOutput *testcase_output_new(const char *name,const char *contents)
127 TestcaseOutput *output;
128 output=g_new0(TestcaseOutput,1);
129 output->name=g_strdup(name);
130 output->contents=g_strdup(contents);
134 /* Free the description of a testcase output file */
135 void testcase_output_free(TestcaseOutput *output)
137 g_free(output->name);
138 g_free(output->contents);