Blob


1 /* Output all lines of a diff_result. */
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 <stdint.h>
20 #include <stdio.h>
21 #include <stdbool.h>
22 #include <stdlib.h>
24 #include <arraylist.h>
25 #include <diff_main.h>
26 #include <diff_output.h>
28 #include "diff_internal.h"
30 int
31 diff_output_plain(struct diff_output_info **output_info, FILE *dest,
32 const struct diff_input_info *info,
33 const struct diff_result *result)
34 {
35 struct diff_output_info *outinfo = NULL;
36 int i, rc;
38 if (!result)
39 return EINVAL;
40 if (result->rc != DIFF_RC_OK)
41 return result->rc;
43 if (output_info) {
44 *output_info = diff_output_info_alloc();
45 if (*output_info == NULL)
46 return errno;
47 outinfo = *output_info;
48 }
50 for (i = 0; i < result->chunks.len; i++) {
51 struct diff_chunk *c = &result->chunks.head[i];
52 if (c->left_count && c->right_count)
53 rc = diff_output_lines(outinfo, dest,
54 c->solved ? " " : "?",
55 c->left_start, c->left_count);
56 else if (c->left_count && !c->right_count)
57 rc = diff_output_lines(outinfo, dest,
58 c->solved ? "-" : "?",
59 c->left_start, c->left_count);
60 else if (c->right_count && !c->left_count)
61 rc = diff_output_lines(outinfo, dest,
62 c->solved ? "+" : "?",
63 c->right_start, c->right_count);
64 if (rc)
65 return rc;
66 }
67 return DIFF_RC_OK;
68 }