Blame src/normal.c

Packit Service fdd496
/* Normal-format output routines for GNU DIFF.
Packit Service fdd496
Packit Service fdd496
   Copyright (C) 1988-1989, 1993, 1995, 1998, 2001, 2006, 2009-2013, 2015-2017
Packit Service fdd496
   Free Software Foundation, Inc.
Packit Service fdd496
Packit Service fdd496
   This file is part of GNU DIFF.
Packit Service fdd496
Packit Service fdd496
   This program is free software: you can redistribute it and/or modify
Packit Service fdd496
   it under the terms of the GNU General Public License as published by
Packit Service fdd496
   the Free Software Foundation, either version 3 of the License, or
Packit Service fdd496
   (at your option) any later version.
Packit Service fdd496
Packit Service fdd496
   This program is distributed in the hope that it will be useful,
Packit Service fdd496
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service fdd496
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service fdd496
   GNU General Public License for more details.
Packit Service fdd496
Packit Service fdd496
   You should have received a copy of the GNU General Public License
Packit Service fdd496
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit Service fdd496
Packit Service fdd496
#include "diff.h"
Packit Service fdd496
Packit Service fdd496
static void print_normal_hunk (struct change *);
Packit Service fdd496
Packit Service fdd496
/* Print the edit-script SCRIPT as a normal diff.
Packit Service fdd496
   INF points to an array of descriptions of the two files.  */
Packit Service fdd496
Packit Service fdd496
void
Packit Service fdd496
print_normal_script (struct change *script)
Packit Service fdd496
{
Packit Service fdd496
  print_script (script, find_change, print_normal_hunk);
Packit Service fdd496
}
Packit Service fdd496
Packit Service fdd496
/* Print a hunk of a normal diff.
Packit Service fdd496
   This is a contiguous portion of a complete edit script,
Packit Service fdd496
   describing changes in consecutive lines.  */
Packit Service fdd496
Packit Service fdd496
static void
Packit Service fdd496
print_normal_hunk (struct change *hunk)
Packit Service fdd496
{
Packit Service fdd496
  lin first0, last0, first1, last1;
Packit Service fdd496
  register lin i;
Packit Service fdd496
Packit Service fdd496
  /* Determine range of line numbers involved in each file.  */
Packit Service fdd496
  enum changes changes = analyze_hunk (hunk, &first0, &last0, &first1, &last1);
Packit Service fdd496
  if (!changes)
Packit Service fdd496
    return;
Packit Service fdd496
Packit Service fdd496
  begin_output ();
Packit Service fdd496
Packit Service fdd496
  /* Print out the line number header for this hunk */
Packit Service fdd496
  set_color_context (LINE_NUMBER_CONTEXT);
Packit Service fdd496
  print_number_range (',', &files[0], first0, last0);
Packit Service fdd496
  fputc (change_letter[changes], outfile);
Packit Service fdd496
  print_number_range (',', &files[1], first1, last1);
Packit Service fdd496
  set_color_context (RESET_CONTEXT);
Packit Service fdd496
  fputc ('\n', outfile);
Packit Service fdd496
Packit Service fdd496
  /* Print the lines that the first file has.  */
Packit Service fdd496
  if (changes & OLD)
Packit Service fdd496
    {
Packit Service fdd496
      if (first0 <= last0)
Packit Service fdd496
        set_color_context (DELETE_CONTEXT);
Packit Service fdd496
      for (i = first0; i <= last0; i++)
Packit Service fdd496
        {
Packit Service fdd496
          print_1_line_nl ("<", &files[0].linbuf[i], true);
Packit Service fdd496
          if (i == last0)
Packit Service fdd496
            set_color_context (RESET_CONTEXT);
Packit Service fdd496
          if (files[0].linbuf[i + 1][-1] == '\n')
Packit Service fdd496
            putc ('\n', outfile);
Packit Service fdd496
        }
Packit Service fdd496
    }
Packit Service fdd496
Packit Service fdd496
  if (changes == CHANGED)
Packit Service fdd496
    fputs ("---\n", outfile);
Packit Service fdd496
Packit Service fdd496
  /* Print the lines that the second file has.  */
Packit Service fdd496
  if (changes & NEW)
Packit Service fdd496
    {
Packit Service fdd496
      if (first1 <= last1)
Packit Service fdd496
        set_color_context (ADD_CONTEXT);
Packit Service fdd496
      for (i = first1; i <= last1; i++)
Packit Service fdd496
        {
Packit Service fdd496
          print_1_line_nl (">", &files[1].linbuf[i], true);
Packit Service fdd496
          if (i == last1)
Packit Service fdd496
            set_color_context (RESET_CONTEXT);
Packit Service fdd496
          if (files[1].linbuf[i + 1][-1] == '\n')
Packit Service fdd496
            putc ('\n', outfile);
Packit Service fdd496
        }
Packit Service fdd496
    }
Packit Service fdd496
}