1 /* Common parts for printing diff output */
3 * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
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.
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.
25 #include <diff/arraylist.h>
26 #include <diff/diff_main.h>
27 #include <diff/diff_output.h>
29 #include "diff_internal.h"
32 get_atom_byte(int *ch, struct diff_atom *atom, off_t off)
36 if (atom->at != NULL) {
41 cur = ftello(atom->d->root->f);
45 if (cur != atom->pos + off &&
46 fseeko(atom->d->root->f, atom->pos + off, SEEK_SET) == -1)
49 *ch = fgetc(atom->d->root->f);
50 if (*ch == EOF && ferror(atom->d->root->f))
57 diff_output_lines(FILE *dest, const char *prefix, struct diff_atom *start_atom,
60 struct diff_atom *atom;
62 foreach_diff_atom(atom, start_atom, count) {
63 fprintf(dest, "%s", prefix);
65 unsigned int len = atom->len;
67 rc = get_atom_byte(&ch, atom, len - 1);
73 rc = get_atom_byte(&ch, atom, len - 1);
81 for (i = 0; i < len; i++) {
82 rc = get_atom_byte(&ch, atom, i);
85 if ((ch < 0x20 || ch >= 0x7f) && ch != '\t')
86 fprintf(dest, "\\x%02x", (unsigned char)ch);
88 fprintf(dest, "%c", ch);