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 <diff/diff_output.h>
20 enum diff_rc diff_output_plain(FILE *dest, const struct diff_input_info *info,
21 const struct diff_result *result)
22 {
23 if (!result)
24 return DIFF_RC_EINVAL;
25 if (result->rc != DIFF_RC_OK)
26 return result->rc;
28 diff_output_info(dest, info);
30 int i;
31 for (i = 0; i < result->chunks.len; i++) {
32 struct diff_chunk *c = &result->chunks.head[i];
33 if (c->left_count && c->right_count)
34 diff_output_lines(dest, c->solved ? " " : "?", c->left_start, c->left_count);
35 else if (c->left_count && !c->right_count)
36 diff_output_lines(dest, c->solved ? "-" : "?", c->left_start, c->left_count);
37 else if (c->right_count && !c->left_count)
38 diff_output_lines(dest, c->solved ? "+" : "?", c->right_start, c->right_count);
39 }
40 return DIFF_RC_OK;
41 }
43 enum diff_rc diff_plain(FILE *dest, const struct diff_config *diff_config,
44 const struct diff_input_info *info,
45 const char *left, int left_len, const char *right, int right_len)
46 {
47 enum diff_rc rc;
48 left_len = left_len < 0 ? strlen(left) : left_len;
49 right_len = right_len < 0 ? strlen(right) : right_len;
50 struct diff_result *result = diff_main(diff_config, left, left_len, right, right_len);
51 rc = diff_output_plain(dest, info, result);
52 diff_result_free(result);
53 return rc;
54 }