Debian Codegraph @

src/normal.c

raw
1 /* Normal-format output routines for GNU DIFF.
2
3 Copyright (C) 1988-1989, 1993, 1995, 1998, 2001, 2006, 2009-2013, 2015-2025
4 Free Software Foundation, Inc.
5
6 This file is part of GNU DIFF.
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "diff.h"
22
23 static void print_normal_hunk (struct change *);
24
25 /* Print the edit-script SCRIPT as a normal diff.
26 INF points to an array of descriptions of the two files. */
27
28 void
29 print_normal_script (struct change *script)
30 {
31 print_script (script, find_change, print_normal_hunk);
32 }
33
34 /* Print a hunk of a normal diff.
35 This is a contiguous portion of a complete edit script,
36 describing changes in consecutive lines. */
37
38 static void
39 print_normal_hunk (struct change *hunk)
40 {
41 /* Determine range of line numbers involved in each file. */
42 lin first0, last0, first1, last1;
43 enum changes changes = analyze_hunk (hunk, &first0, &last0, &first1, &last1);
44 if (!changes)
45 return;
46
47 begin_output ();
48
49 /* Print out the line number header for this hunk */
50 set_color_context (LINE_NUMBER_CONTEXT);
51 print_number_range (',', &curr.file[0], first0, last0);
52 fputc (change_letter[changes], outfile);
53 print_number_range (',', &curr.file[1], first1, last1);
54 set_color_context (RESET_CONTEXT);
55 fputc ('\n', outfile);
56
57 /* Print the lines that the first file has. */
58 if (changes & OLD)
59 {
60 for (lin i = first0; i <= last0; i++)
61 {
62 set_color_context (DELETE_CONTEXT);
63 print_1_line_nl ("<", &curr.file[0].linbuf[i], true);
64 set_color_context (RESET_CONTEXT);
65 if (curr.file[0].linbuf[i + 1][-1] == '\n')
66 putc ('\n', outfile);
67 }
68 }
69
70 if (changes == CHANGED)
71 fputs ("---\n", outfile);
72
73 /* Print the lines that the second file has. */
74 if (changes & NEW)
75 {
76 for (lin i = first1; i <= last1; i++)
77 {
78 set_color_context (ADD_CONTEXT);
79 print_1_line_nl (">", &curr.file[1].linbuf[i], true);
80 set_color_context (RESET_CONTEXT);
81 if (curr.file[1].linbuf[i + 1][-1] == '\n')
82 putc ('\n', outfile);
83 }
84 }
85 }