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 <diff/diff_output.h>
20 void diff_output_lines(FILE *dest, const char *prefix, struct diff_atom *start_atom, unsigned int count)
21 {
22 struct diff_atom *atom;
23 foreach_diff_atom(atom, start_atom, count) {
24 fprintf(dest, "%s", prefix);
25 int i;
26 unsigned int len = atom->len;
27 if (len && atom->at[len - 1] == '\n') {
28 len--;
29 if (len && atom->at[len - 1] == '\r')
30 len--;
31 }
33 for (i = 0; i < len; i++) {
34 char c = atom->at[i];
35 if ((c < 0x20 || c >= 0x7f) && c != '\t')
36 fprintf(dest, "\\x%02x", (unsigned char)c);
37 else
38 fprintf(dest, "%c", c);
39 }
40 fprintf(dest, "\n");
41 }
42 }
44 enum diff_rc diff_output_info(FILE *dest, const struct diff_input_info *info)
45 {
46 if (info->arbitrary_info && *info->arbitrary_info)
47 fprintf(dest, "%s", info->arbitrary_info);
48 fprintf(dest, "--- %s\n+++ %s\n",
49 info->left_path ? : "a",
50 info->right_path ? : "b");
51 return DIFF_RC_OK;
52 }