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 <arraylist.h>
26 #include <diff_main.h>
27 #include <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);
122 diff_output_chunk_left_version(struct diff_output_info **output_info,
124 const struct diff_input_info *info,
125 const struct diff_result *result,
126 const struct diff_chunk_context *cc)
129 struct diff_output_info *outinfo = NULL;
131 if (diff_range_empty(&cc->left))
135 *output_info = diff_output_info_alloc();
136 if (*output_info == NULL)
138 outinfo = *output_info;
141 /* Write out all chunks on the left side. */
142 for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
143 const struct diff_chunk *c = &result->chunks.head[c_idx];
146 rc = diff_output_lines(outinfo, dest, "",
147 c->left_start, c->left_count);
157 diff_output_chunk_right_version(struct diff_output_info **output_info,
159 const struct diff_input_info *info,
160 const struct diff_result *result,
161 const struct diff_chunk_context *cc)
164 struct diff_output_info *outinfo = NULL;
166 if (diff_range_empty(&cc->right))
170 *output_info = diff_output_info_alloc();
171 if (*output_info == NULL)
173 outinfo = *output_info;
176 /* Write out all chunks on the right side. */
177 for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
178 const struct diff_chunk *c = &result->chunks.head[c_idx];
180 if (c->right_count) {
181 rc = diff_output_lines(outinfo, dest, "", c->right_start,
191 struct diff_output_info *
192 diff_output_info_alloc(void)
194 struct diff_output_info *output_info;
197 output_info = malloc(sizeof(*output_info));
198 if (output_info != NULL) {
199 ARRAYLIST_INIT(output_info->line_offsets, 128);
200 ARRAYLIST_ADD(offp, output_info->line_offsets);
202 diff_output_info_free(output_info);
211 diff_output_info_free(struct diff_output_info *output_info)
213 ARRAYLIST_FREE(output_info->line_offsets);