11 #include <glib/gstdio.h>
14 #include "testcaseinput.h"
21 * As write(), but with error handling.
23 static size_t write_file(int fd,const char *buf,size_t count,GError **error)
25 if (write(fd,buf,count)<count)
27 g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno),
28 "Error writing bookloupe input file: %s",g_strerror(errno));
35 * Replace \n with \r\n, U+240A (visible symbol for LF) with \n
36 * and U+240D (visible symbol for CR) with \r.
38 static char *unix2dos(const char *text)
41 const gunichar visible_lf=0x240A;
42 const gunichar visible_cr=0x240D;
44 string=g_string_new(NULL);
47 c=g_utf8_get_char(text);
48 text=g_utf8_next_char(text);
50 g_string_append(string,"\r\n");
51 else if (c==visible_lf)
52 g_string_append_c(string,'\n');
53 else if (c==visible_cr)
54 g_string_append_c(string,'\r');
56 g_string_append_unichar(string,c);
58 return g_string_free(string,FALSE);
62 * Create an input file needed for a testcase (as specified in <input>).
63 * The file is written in the encoding specified for communicating with
64 * bookloupe. The name_used field of <input> is filled in with the name
65 * of the created file (which may be different than the name specified
66 * if that contained "XXXXXX" to be replaced by a unique string).
68 gboolean testcase_input_create(Testcase *testcase,TestcaseInput *input,
77 if (testcase->encoding)
79 if (testcase->flags&TESTCASE_UNIX_NEWLINES)
80 s=g_convert(input->contents,-1,testcase->encoding,"UTF-8",NULL,
84 t=unix2dos(input->contents);
85 s=g_convert(t,-1,testcase->encoding,"UTF-8",NULL,&n,&tmp_err);
90 g_propagate_prefixed_error(error,tmp_err,
91 "Conversion to %s failed: ",testcase->encoding);
95 else if (testcase->flags&TESTCASE_UNIX_NEWLINES)
97 s=g_strdup(input->contents);
102 s=unix2dos(input->contents);
111 g_free(input->name_used);
112 input->name_used=NULL;
113 if (testcase->tmpdir)
114 filename=g_build_filename(testcase->tmpdir,input->name,NULL);
116 filename=g_strdup(input->name);
117 if (strstr(input->name,"XXXXXX"))
118 fd=g_mkstemp(filename);
120 fd=g_open(filename,O_WRONLY|O_CREAT|O_EXCL|O_BINARY,0600);
123 g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno),
124 "%s: %s",filename,g_strerror(errno));
128 input->name_used=g_strdup(filename+strlen(filename)-strlen(input->name));
129 if (n && write_file(fd,s,n,error)!=n)
133 (void)g_unlink(filename);
135 g_free(input->name_used);
136 input->name_used=NULL;
142 g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno),
143 "%s: %s",filename,g_strerror(errno));
144 (void)g_unlink(filename);
146 g_free(input->name_used);
147 input->name_used=NULL;
155 * Remove an input file created with testcase_input_create()
157 gboolean testcase_input_remove(Testcase *testcase,TestcaseInput *input,
161 if (input->name_used)
163 if (testcase->tmpdir)
164 filename=g_build_filename(testcase->tmpdir,input->name_used,NULL);
166 filename=g_strdup(input->name_used);
167 if (g_unlink(filename)<0)
169 g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno),
170 "%s: %s",filename,g_strerror(errno));
174 g_free(input->name_used);
175 input->name_used=NULL;
180 /* Create a new description of an input file needed for a testcase */
181 TestcaseInput *testcase_input_new(const char *name,const char *contents)
183 TestcaseInput *input;
184 input=g_new0(TestcaseInput,1);
185 input->name=g_strdup(name);
186 input->contents=g_strdup(contents);
190 /* Free the description of a testcase input file */
191 void testcase_input_free(TestcaseInput *input)
194 g_free(input->name_used);
195 g_free(input->contents);