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(struct diff_output_info *outinfo, FILE *dest,
58 const char *prefix, struct diff_atom *start_atom,
61 struct diff_atom *atom;
62 off_t outoff = 0, *offp;
65 if (outinfo && outinfo->line_offsets.len > 0) {
66 unsigned int idx = outinfo->line_offsets.len - 1;
67 outoff = outinfo->line_offsets.head[idx];
70 foreach_diff_atom(atom, start_atom, count) {
73 unsigned int len = atom->len;
74 rc = fprintf(dest, "%s", prefix);
79 rc = get_atom_byte(&ch, atom, len - 1);
85 rc = get_atom_byte(&ch, atom, len - 1);
93 for (i = 0; i < len; i++) {
94 rc = get_atom_byte(&ch, atom, i);
97 if ((ch < 0x20 || ch >= 0x7f) && ch != '\t')
98 rc = fprintf(dest, "\\x%02x", (unsigned char)ch);
100 rc = fprintf(dest, "%c", ch);
105 rc = fprintf(dest, "\n");
110 ARRAYLIST_ADD(offp, outinfo->line_offsets);
120 diff_output_chunk_left_version(struct diff_output_info **output_info,
122 const struct diff_input_info *info,
123 const struct diff_result *result,
124 const struct diff_chunk_context *cc)
127 struct diff_output_info *outinfo = NULL;
129 if (diff_range_empty(&cc->left))
133 *output_info = diff_output_info_alloc();
134 if (*output_info == NULL)
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];
144 diff_output_lines(outinfo, dest, "", c->left_start,
152 diff_output_chunk_right_version(struct diff_output_info **output_info,
154 const struct diff_input_info *info,
155 const struct diff_result *result,
156 const struct diff_chunk_context *cc)
159 struct diff_output_info *outinfo = NULL;
161 if (diff_range_empty(&cc->right))
165 *output_info = diff_output_info_alloc();
166 if (*output_info == NULL)
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];
176 diff_output_lines(outinfo, dest, "", c->right_start,
183 struct diff_output_info *
184 diff_output_info_alloc(void)
186 struct diff_output_info *output_info;
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);
199 diff_output_info_free(struct diff_output_info *output_info)
201 ARRAYLIST_FREE(output_info->line_offsets);