FreeBSD/contrib/diff/src/context.c
/* Print a portion of an edit script in unidiff format.
   HUNK is the beginning of the portion to be printed.
   The end is marked by a `link' that has been nulled out.

   Prints out lines from both files, and precedes each
   line with the appropriate flag-character.  */
Print a portion of an edit script in unidiff format.
HUNK is the beginning of the portion to be printed.
The end is marked by a `link' that has been nulled out.

Prints out lines from both files, and precedes each
line with the appropriate flag-character.
Print a portion of an edit script in unidiff format . HUNK is the beginning of the portion to be printed . The end is marked by a ` link ' that has been nulled out . Prints out lines from both files and precedes each line with the appropriate flag - character .
static void
pr_unidiff_hunk (struct change *hunk)
{
  lin first0, last0, first1, last1;
  lin i, j, k;
  struct change *next;
  char const *function;
  FILE *out;
  if (! analyze_hunk (hunk, &first0, &last0, &first1, &last1))
    return;
  i = - files[0].prefix_lines;
  first0 = MAX (first0 - context, i);
  first1 = MAX (first1 - context, i);
  if (last0 < files[0].valid_lines - context)
    last0 += context;
  else
    last0 = files[0].valid_lines - 1;
  if (last1 < files[1].valid_lines - context)
    last1 += context;
  else
    last1 = files[1].valid_lines - 1;
  function = 0;
  if (function_regexp.fastmap)
    function = find_function (files[0].linbuf, first0);
  begin_output ();
  out = outfile;
  fprintf (out, "@@ -");
  print_unidiff_number_range (&files[0], first0, last0);
  fprintf (out, " +");
  print_unidiff_number_range (&files[1], first1, last1);
  fprintf (out, " @@");
  if (function)
    print_context_function (out, function);
  putc ('\n', out);
  next = hunk;
  i = first0;
  j = first1;
  while (i <= last0 || j <= last1)
    {
      if (!next || i < next->line0)
	{
	  putc (initial_tab ? '\t' : ' ', out);
	  print_1_line (0, &files[0].linbuf[i++]);
	  j++;
	}
      else
	{
	  k = next->deleted;
	  while (k--)
	    {
	      putc ('-', out);
	      if (initial_tab)
		putc ('\t', out);
	      print_1_line (0, &files[0].linbuf[i++]);
	    }
	  k = next->inserted;
	  while (k--)
	    {
	      putc ('+', out);
	      if (initial_tab)
		putc ('\t', out);
	      print_1_line (0, &files[1].linbuf[j++]);
	    }
	  next = next->link;
	}
    }
}
static void pr_unidiff_hunk ( struct change * hunk ) { lin first0 , last0 , first1 , last1 ; lin i , j , k ; struct change * next ; char const * function ; FILE * out ; if ( ! analyze_hunk ( hunk , & first0 , & last0 , & first1 , & last1 ) ) return ; i = - files [ 0 ] . prefix_lines ; first0 = MAX ( first0 - context , i ) ; first1 = MAX ( first1 - context , i ) ; if ( last0 < files [ 0 ] . valid_lines - context ) last0 += context ; else last0 = files [ 0 ] . valid_lines - 1 ; if ( last1 < files [ 1 ] . valid_lines - context ) last1 += context ; else last1 = files [ 1 ] . valid_lines - 1 ; function = 0 ; if ( function_regexp . fastmap ) function = find_function ( files [ 0 ] . linbuf , first0 ) ; begin_output ( ) ; out = outfile ; fprintf ( out , " " ) ; print_unidiff_number_range ( & files [ 0 ] , first0 , last0 ) ; fprintf ( out , " " ) ; print_unidiff_number_range ( & files [ 1 ] , first1 , last1 ) ; fprintf ( out , " " ) ; if ( function ) print_context_function ( out , function ) ; putc ( ' \n ' , out ) ; next = hunk ; i = first0 ; j = first1 ; while ( i <= last0 || j <= last1 ) { if ( ! next || i < next -> line0 ) { putc ( initial_tab ? ' \t ' : ' ' , out ) ; print_1_line ( 0 , & files [ 0 ] . linbuf [ i ++ ] ) ; j ++ ; } else { k = next -> deleted ; while ( k -- ) { putc ( ' ' , out ) ; if ( initial_tab ) putc ( ' \t ' , out ) ; print_1_line ( 0 , & files [ 0 ] . linbuf [ i ++ ] ) ; } k = next -> inserted ; while ( k -- ) { putc ( ' ' , out ) ; if ( initial_tab ) putc ( ' \t ' , out ) ; print_1_line ( 0 , & files [ 1 ] . linbuf [ j ++ ] ) ; } next = next -> link ; } } }
Print a portion of an edit script in unidiff format.
Print a portion of an edit script in unidiff format .
/* Determine range of line numbers involved in each file.  */ /* Include a context's width before and after.  */ /* If desired, find the preceding function definition line in file 0.  */ /* If the line isn't a difference, output the context from file 0. */ /* For each difference, first output the deleted part. */ /* Then output the inserted part. */ /* We're done with this hunk, so on to the next! */