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 t=unix2dos(input->contents);
80 s=g_convert(t,-1,testcase->encoding,"UTF-8",NULL,&n,&tmp_err);
84 g_propagate_prefixed_error(error,tmp_err,
85 "Conversion to %s failed: ",testcase->encoding);
91 s=unix2dos(input->contents);
100 g_free(input->name_used);
101 input->name_used=NULL;
102 if (testcase->tmpdir)
103 filename=g_build_filename(testcase->tmpdir,input->name,NULL);
105 filename=g_strdup(input->name);
106 if (strstr(input->name,"XXXXXX"))
107 fd=g_mkstemp(filename);
109 fd=g_open(filename,O_WRONLY|O_CREAT|O_EXCL|O_BINARY,0600);
112 g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno),
113 "%s: %s",filename,g_strerror(errno));
117 input->name_used=g_strdup(filename+strlen(filename)-strlen(input->name));
118 if (n && write_file(fd,s,n,error)!=n)
122 (void)g_unlink(filename);
124 g_free(input->name_used);
125 input->name_used=NULL;
131 g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno),
132 "%s: %s",filename,g_strerror(errno));
133 (void)g_unlink(filename);
135 g_free(input->name_used);
136 input->name_used=NULL;
144 * Remove an input file created with testcase_input_create()
146 gboolean testcase_input_remove(Testcase *testcase,TestcaseInput *input,
150 if (input->name_used)
152 if (testcase->tmpdir)
153 filename=g_build_filename(testcase->tmpdir,input->name_used,NULL);
155 filename=g_strdup(input->name_used);
156 if (g_unlink(filename)<0)
158 g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno),
159 "%s: %s",filename,g_strerror(errno));
163 g_free(input->name_used);
164 input->name_used=NULL;
169 /* Create a new description of an input file needed for a testcase */
170 TestcaseInput *testcase_input_new(const char *name,const char *contents)
172 TestcaseInput *input;
173 input=g_new0(TestcaseInput,1);
174 input->name=g_strdup(name);
175 input->contents=g_strdup(contents);
179 /* Free the description of a testcase input file */
180 void testcase_input_free(TestcaseInput *input)
183 g_free(input->name_used);
184 g_free(input->contents);