Add support for false-positives, etc.
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 and U+240A (visible symbol for LF) with \n
37 static char *unix2dos(const char *text)
40 const gunichar visible_lf=0x240A;
42 string=g_string_new(NULL);
45 c=g_utf8_get_char(text);
46 text=g_utf8_next_char(text);
48 g_string_append(string,"\r\n");
49 else if (c==visible_lf)
50 g_string_append_c(string,'\n');
52 g_string_append_unichar(string,c);
54 return g_string_free(string,FALSE);
58 * Create an input file needed for a testcase (as specified in <input>).
59 * The file is written in the encoding specified for communicating with
60 * bookloupe. The name_used field of <input> is filled in with the name
61 * of the created file (which may be different than the name specified
62 * if that contained "XXXXXX" to be replaced by a unique string).
64 gboolean testcase_input_create(Testcase *testcase,TestcaseInput *input,
73 if (testcase->encoding)
75 t=unix2dos(input->contents);
76 s=g_convert(t,-1,testcase->encoding,"UTF-8",NULL,&n,&tmp_err);
80 g_propagate_prefixed_error(error,tmp_err,
81 "Conversion to %s failed: ",testcase->encoding);
87 s=unix2dos(input->contents);
96 g_free(input->name_used);
97 input->name_used=NULL;
99 filename=g_build_filename(testcase->tmpdir,input->name,NULL);
101 filename=g_strdup(input->name);
102 if (strstr(input->name,"XXXXXX"))
103 fd=g_mkstemp(filename);
105 fd=g_open(filename,O_WRONLY|O_CREAT|O_EXCL|O_BINARY,0600);
108 g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno),
109 "%s: %s",filename,g_strerror(errno));
113 input->name_used=g_strdup(filename+strlen(filename)-strlen(input->name));
114 if (n && write_file(fd,s,n,error)!=n)
118 (void)g_unlink(filename);
120 g_free(input->name_used);
121 input->name_used=NULL;
127 g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno),
128 "%s: %s",filename,g_strerror(errno));
129 (void)g_unlink(filename);
131 g_free(input->name_used);
132 input->name_used=NULL;
140 * Remove an input file created with testcase_input_create()
142 gboolean testcase_input_remove(Testcase *testcase,TestcaseInput *input,
146 if (input->name_used)
148 if (testcase->tmpdir)
149 filename=g_build_filename(testcase->tmpdir,input->name_used,NULL);
151 filename=g_strdup(input->name_used);
152 if (g_unlink(filename)<0)
154 g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno),
155 "%s: %s",filename,g_strerror(errno));
159 g_free(input->name_used);
160 input->name_used=NULL;
165 /* Create a new description of an input file needed for a testcase */
166 TestcaseInput *testcase_input_new(const char *name,const char *contents)
168 TestcaseInput *input;
169 input=g_new0(TestcaseInput,1);
170 input->name=g_strdup(name);
171 input->contents=g_strdup(contents);
175 /* Free the description of a testcase input file */
176 void testcase_input_free(TestcaseInput *input)
179 g_free(input->name_used);
180 g_free(input->contents);