43 " -F <footer> Append footer file\n" |
43 " -F <footer> Append footer file\n" |
44 " -p Disable highlighting (plain text)\n" |
44 " -p Disable highlighting (plain text)\n" |
45 " -l Disable line numbers\n" |
45 " -l Disable line numbers\n" |
46 " -V, -v Prints version and exits\n" |
46 " -V, -v Prints version and exits\n" |
47 "\n"); |
47 "\n"); |
48 } |
|
49 |
|
50 int copyfile(char *filename, FILE *dest) { |
|
51 if (!filename) { |
|
52 return 0; |
|
53 } |
|
54 |
|
55 FILE *src = fopen(filename, "r"); |
|
56 if (src) { |
|
57 char buf[4096]; |
|
58 int r; |
|
59 while ((r = fread(buf, 1, 4096, src)) > 0) { |
|
60 fwrite(buf, 1, r, dest); |
|
61 } |
|
62 fclose(src); |
|
63 return 0; |
|
64 } else { |
|
65 return 1; |
|
66 } |
|
67 } |
48 } |
68 |
49 |
69 #define WRITECONST(stream, out, cstr) out(cstr, 1, sizeof(cstr)-1, stream) |
50 #define WRITECONST(stream, out, cstr) out(cstr, 1, sizeof(cstr)-1, stream) |
70 int formatfile( |
51 int formatfile( |
71 highlighter_t *highlighter, |
52 highlighter_t *highlighter, |
238 default: /* should be unreachable */ |
221 default: /* should be unreachable */ |
239 fprintf(stderr, "error in enum source_type\n"); |
222 fprintf(stderr, "error in enum source_type\n"); |
240 retcode = EXIT_FAILURE; |
223 retcode = EXIT_FAILURE; |
241 goto prog_end; |
224 goto prog_end; |
242 } |
225 } |
|
226 |
|
227 { |
|
228 FILE *headerfile = fopen(settings.headerfile, "r"); |
|
229 if (!headerfile) { |
|
230 perror("Error opening header file"); |
|
231 retcode = EXIT_FAILURE; |
|
232 goto prog_end; |
|
233 } |
|
234 ucx_stream_copy(headerfile, fout, |
|
235 (read_func) fread, (write_func) fwrite, |
|
236 filebuf, FILEBUF_SIZE, (size_t)-1); |
|
237 fclose(headerfile); |
|
238 } |
243 |
239 |
244 FILE *inputfile = fopen(settings.infilename, "r"); |
240 FILE *inputfile = fopen(settings.infilename, "r"); |
245 if (inputfile) { |
241 if (inputfile) { |
246 UcxBuffer *filebuf = ucx_buffer_new(NULL, |
242 UcxBuffer *content = ucx_buffer_new(NULL, |
247 8192, UCX_BUFFER_AUTOEXTEND); |
243 FILEBUF_SIZE*2, UCX_BUFFER_AUTOEXTEND); |
248 { |
244 { |
249 const size_t tmpbufsize = 4096; |
245 ucx_stream_copy(inputfile, content, (read_func) fread, |
250 char *tmpbuf = malloc(tmpbufsize); |
|
251 ucx_stream_copy(inputfile, filebuf, (read_func) fread, |
|
252 (write_func) ucx_buffer_write, |
246 (write_func) ucx_buffer_write, |
253 tmpbuf, tmpbufsize, (size_t)-1); |
247 filebuf, FILEBUF_SIZE, (size_t)-1); |
254 free(tmpbuf); |
|
255 } |
248 } |
256 fclose(inputfile); |
249 fclose(inputfile); |
257 |
250 |
258 UcxList *inputlines = ucx_list_append(NULL, filebuf->space); |
251 UcxList *inputlines = ucx_list_append(NULL, content->space); |
259 for (size_t i = 1 ; i < filebuf->size ; i++) { |
252 for (size_t i = 1 ; i < content->size ; i++) { |
260 if (filebuf->space[i] == '\r') { |
253 if (content->space[i] == '\r') { |
261 filebuf->space[i] = '\n'; i++; |
254 content->space[i] = '\n'; i++; |
262 } |
255 } |
263 if (filebuf->space[i] == '\n' && i+1 < filebuf->size) { |
256 if (content->space[i] == '\n' && i+1 < content->size) { |
264 ucx_list_append(inputlines, filebuf->space+i+1); |
257 ucx_list_append(inputlines, content->space+i+1); |
265 } |
258 } |
266 } |
259 } |
267 |
260 |
268 formatfile( |
261 formatfile( |
269 hptr, |
262 hptr, |
270 inputlines, |
263 inputlines, |
271 (write_func) fwrite, |
264 (write_func) fwrite, |
272 fout, |
265 fout, |
273 settings.showlinenumbers); |
266 settings.showlinenumbers); |
274 ucx_buffer_free(filebuf); |
267 ucx_buffer_free(content); |
275 } else { |
268 } else { |
276 perror("Error opening input file"); |
269 perror("Error opening input file"); |
277 retcode = EXIT_FAILURE; |
270 retcode = EXIT_FAILURE; |
278 } |
271 } |
279 |
272 |
280 if (copyfile(settings.footerfile, fout)) { |
273 { |
281 perror("Error opening footer file"); |
274 FILE *footerfile = fopen(settings.footerfile, "r"); |
282 retcode = EXIT_FAILURE; |
275 if (!footerfile) { |
283 } |
276 perror("Error opening footer file"); |
|
277 retcode = EXIT_FAILURE; |
|
278 goto prog_end; |
|
279 } |
|
280 ucx_stream_copy(footerfile, fout, |
|
281 (read_func) fread, (write_func) fwrite, |
|
282 filebuf, FILEBUF_SIZE, (size_t)-1); |
|
283 fclose(footerfile); |
|
284 } |
|
285 |
|
286 free(filebuf); |
284 |
287 |
285 prog_end: |
288 prog_end: |
286 if (fout != stdout) { |
289 if (fout != stdout) { |
287 fclose(fout); |
290 fclose(fout); |
288 } |
291 } |