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(struct diff_output_info *outinfo, FILE *dest,
58 const char *prefix, struct diff_atom *start_atom,
59 unsigned int count)
60 {
61 struct diff_atom *atom;
62 off_t outoff = 0, *offp;
63 int rc;
65 if (outinfo && outinfo->line_offsets.len > 0) {
66 unsigned int idx = outinfo->line_offsets.len - 1;
67 outoff = outinfo->line_offsets.head[idx];
68 }
70 foreach_diff_atom(atom, start_atom, count) {
71 off_t outlen = 0;
72 int i, ch;
73 unsigned int len = atom->len;
74 rc = fprintf(dest, "%s", prefix);
75 if (rc < 0)
76 return errno;
77 outlen += rc;
78 if (len) {
79 rc = get_atom_byte(&ch, atom, len - 1);
80 if (rc)
81 return rc;
82 if (ch == '\n')
83 len--;
84 if (len) {
85 rc = get_atom_byte(&ch, atom, len - 1);
86 if (rc)
87 return rc;
88 if (ch == '\r')
89 len--;
90 }
91 }
93 for (i = 0; i < len; i++) {
94 rc = get_atom_byte(&ch, atom, i);
95 if (rc)
96 return rc;
97 if ((ch < 0x20 || ch >= 0x7f) && ch != '\t')
98 rc = fprintf(dest, "\\x%02x", (unsigned char)ch);
99 else
100 rc = fprintf(dest, "%c", ch);
101 if (rc < 0)
102 return errno;
103 outlen += rc;
105 rc = fprintf(dest, "\n");
106 if (rc < 0)
107 return errno;
108 outlen += rc;
109 if (outinfo) {
110 ARRAYLIST_ADD(offp, outinfo->line_offsets);
111 outoff += outlen;
112 *offp = outoff;
116 return DIFF_RC_OK;
119 int
120 diff_output_chunk_left_version(struct diff_output_info **output_info,
121 FILE *dest,
122 const struct diff_input_info *info,
123 const struct diff_result *result,
124 const struct diff_chunk_context *cc)
126 int c_idx;
127 struct diff_output_info *outinfo = NULL;
129 if (diff_range_empty(&cc->left))
130 return DIFF_RC_OK;
132 if (output_info) {
133 *output_info = diff_output_info_alloc();
134 if (*output_info == NULL)
135 return ENOMEM;
136 outinfo = *output_info;
139 /* Write out all chunks on the left side. */
140 for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
141 const struct diff_chunk *c = &result->chunks.head[c_idx];
143 if (c->left_count)
144 diff_output_lines(outinfo, dest, "", c->left_start,
145 c->left_count);
148 return DIFF_RC_OK;
151 int
152 diff_output_chunk_right_version(struct diff_output_info **output_info,
153 FILE *dest,
154 const struct diff_input_info *info,
155 const struct diff_result *result,
156 const struct diff_chunk_context *cc)
158 int c_idx;
159 struct diff_output_info *outinfo = NULL;
161 if (diff_range_empty(&cc->right))
162 return DIFF_RC_OK;
164 if (output_info) {
165 *output_info = diff_output_info_alloc();
166 if (*output_info == NULL)
167 return ENOMEM;
168 outinfo = *output_info;
171 /* Write out all chunks on the right side. */
172 for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
173 const struct diff_chunk *c = &result->chunks.head[c_idx];
175 if (c->right_count)
176 diff_output_lines(outinfo, dest, "", c->right_start,
177 c->right_count);
180 return DIFF_RC_OK;
183 struct diff_output_info *
184 diff_output_info_alloc(void)
186 struct diff_output_info *output_info;
187 off_t *offp;
189 output_info = malloc(sizeof(*output_info));
190 if (output_info != NULL) {
191 ARRAYLIST_INIT(output_info->line_offsets, 128);
192 ARRAYLIST_ADD(offp, output_info->line_offsets);
193 *offp = 0;
195 return output_info;
198 void
199 diff_output_info_free(struct diff_output_info *output_info)
201 ARRAYLIST_FREE(output_info->line_offsets);
202 free(output_info);