Blob


1 /* Common parts for printing diff output */
2 /*
3 * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <errno.h>
19 #include <inttypes.h>
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
25 #include <diff/arraylist.h>
26 #include <diff/diff_main.h>
27 #include <diff/diff_output.h>
29 #include "diff_internal.h"
31 static int
32 get_atom_byte(int *ch, struct diff_atom *atom, off_t off)
33 {
34 off_t cur;
36 if (atom->at != NULL) {
37 *ch = atom->at[off];
38 return 0;
39 }
41 cur = ftello(atom->d->root->f);
42 if (cur == -1)
43 return errno;
45 if (cur != atom->pos + off &&
46 fseeko(atom->d->root->f, atom->pos + off, SEEK_SET) == -1)
47 return errno;
49 *ch = fgetc(atom->d->root->f);
50 if (*ch == EOF && ferror(atom->d->root->f))
51 return errno;
53 return 0;
54 }
56 int
57 diff_output_lines(FILE *dest, const char *prefix, struct diff_atom *start_atom,
58 unsigned int count)
59 {
60 struct diff_atom *atom;
61 int rc;
62 foreach_diff_atom(atom, start_atom, count) {
63 fprintf(dest, "%s", prefix);
64 int i, ch;
65 unsigned int len = atom->len;
66 if (len) {
67 rc = get_atom_byte(&ch, atom, len - 1);
68 if (rc)
69 return rc;
70 if (ch == '\n')
71 len--;
72 if (len) {
73 rc = get_atom_byte(&ch, atom, len - 1);
74 if (rc)
75 return rc;
76 if (ch == '\r')
77 len--;
78 }
79 }
81 for (i = 0; i < len; i++) {
82 rc = get_atom_byte(&ch, atom, i);
83 if (rc)
84 return rc;
85 if ((ch < 0x20 || ch >= 0x7f) && ch != '\t')
86 fprintf(dest, "\\x%02x", (unsigned char)ch);
87 else
88 fprintf(dest, "%c", ch);
89 }
90 fprintf(dest, "\n");
91 }
93 return 0;
94 }