aboutsummaryrefslogtreecommitdiffstats
path: root/libpatch_stream_diff2_printer__.c
blob: 6c6bc1481a1b13dda832fd09c12f9def54ad5bb3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* See LICENSE file for copyright and license details. */
#include "common.h"


static void
put(struct libpatch_diff2_printer *this, const char *text, size_t len)
{
	size_t off, r;
	FILE *f = ((struct printer_internals *)this->user_data)->f.stream;
	if (ferror(f))
		return;
	for (off = 0; off < len; off += r) {
		r = fwrite(&text[off], 1, len - off, f);
		if (!r) {
			if (errno != EINTR) {
				this->error = errno;
				break;
			}
			clearerr(f);
		}
	}
}


struct libpatch_diff2_printer *
libpatch_stream_diff2_printer__(FILE *output)
{
	struct libpatch_diff2_printer *printer;
	struct printer_internals *internals;
	printer = calloc(1, sizeof(*printer) + sizeof(struct printer_internals));
	if (!printer)
		return NULL;
	internals = (void *)&((char *)printer)[sizeof(*printer)];
	printer->user_data = internals;
	internals->put = put;
	internals->f.stream = output;
	return printer;
	
}