Blame src/normal.c

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