home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / diff.lzh / diff / side.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-04-24  |  7.0 KB  |  290 lines

  1. static char RCSid[]="$Id: side.c_v 1.1 96/04/23 02:56:20 hiro Exp $";
  2. /* sdiff-format output routines for GNU DIFF.
  3.    Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU DIFF.
  6.  
  7. GNU DIFF is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY.  No author or distributor
  9. accepts responsibility to anyone for the consequences of using it
  10. or for whether it serves any particular purpose or works at all,
  11. unless he says so in writing.  Refer to the GNU DIFF General Public
  12. License for full details.
  13.  
  14. Everyone is granted permission to copy, modify and redistribute
  15. GNU DIFF, but only under the conditions described in the
  16. GNU DIFF General Public License.   A copy of this license is
  17. supposed to have been given to you along with GNU DIFF so you
  18. can know your rights and responsibilities.  It should be in a
  19. file named COPYING.  Among other things, the copyright notice
  20. and this notice must be preserved on all copies.  */
  21.  
  22.  
  23. #include "diff.h"
  24.  
  25. static unsigned print_half_line PARAMS((char const * const *, unsigned, unsigned));
  26. static unsigned tab_from_to PARAMS((unsigned, unsigned));
  27. static void print_1sdiff_line PARAMS((char const * const *, int, char const * const *));
  28. static void print_sdiff_common_lines PARAMS((int, int));
  29. static void print_sdiff_hunk PARAMS((struct change *));
  30.  
  31. /* Next line number to be printed in the two input files.  */
  32. static int next0, next1;
  33.  
  34. /* Print the edit-script SCRIPT as a sdiff style output.  */
  35.  
  36. void
  37. print_sdiff_script (script)
  38.      struct change *script;
  39. {
  40.   begin_output ();
  41.  
  42.   next0 = next1 = - files[0].prefix_lines;
  43.   print_script (script, find_change, print_sdiff_hunk);
  44.  
  45.   print_sdiff_common_lines (files[0].valid_lines, files[1].valid_lines);
  46. }
  47.  
  48. /* Tab from column FROM to column TO, where FROM <= TO.  Yield TO.  */
  49.  
  50. static unsigned
  51. tab_from_to (from, to)
  52.      unsigned from, to;
  53. {
  54.   FILE *out = outfile;
  55.   unsigned tab;
  56.  
  57.   if (! tab_expand_flag)
  58.     for (tab = from + TAB_WIDTH - from % TAB_WIDTH;  tab <= to;  tab += TAB_WIDTH)
  59.       {
  60.     putc ('\t', out);
  61.     from = tab;
  62.       }
  63.   while (from++ < to)
  64.     putc (' ', out);
  65.   return to;
  66. }
  67.  
  68. /*
  69.  * Print the text for half an sdiff line.  This means truncate to width
  70.  * observing tabs, and trim a trailing newline.  Returns the last column
  71.  * written (not the number of chars).
  72.  */
  73. static unsigned
  74. print_half_line (line, indent, out_bound)
  75.      char const * const *line;
  76.      unsigned indent, out_bound;
  77. {
  78.   FILE *out = outfile;
  79.   register unsigned in_position = 0, out_position = 0;
  80.   register char const
  81.     *text_pointer = line[0],
  82.     *text_limit = line[1];
  83.  
  84.   while (text_pointer < text_limit)
  85.     {
  86.       register unsigned char c = *text_pointer++;
  87.  
  88.       switch (c)
  89.     {
  90.     case '\t':
  91.       {
  92.         unsigned spaces = TAB_WIDTH - in_position % TAB_WIDTH;
  93.         if (in_position == out_position)
  94.           {
  95.         unsigned tabstop = out_position + spaces;
  96.         if (tab_expand_flag)
  97.           {
  98.             if (out_bound < tabstop)
  99.               tabstop = out_bound;
  100.             for (;  out_position < tabstop;  out_position++)
  101.               putc (' ', out);
  102.           }
  103.         else
  104.           if (tabstop < out_bound)
  105.             {
  106.               out_position = tabstop;
  107.               putc (c, out);
  108.             }
  109.           }
  110.         in_position += spaces;
  111.       }
  112.       break;
  113.  
  114. #ifdef _OS9000
  115.     case '\012':
  116. #else
  117.     case '\r':
  118. #endif
  119.       {
  120.         putc (c, out);
  121.         tab_from_to (0, indent);
  122.         in_position = out_position = 0;
  123.       }
  124.       break;
  125.  
  126.     case '\b':
  127.       if (in_position != 0 && --in_position < out_bound)
  128.         if (out_position <= in_position)
  129.           /* Add spaces to make up for suppressed tab past out_bound.  */
  130.           for (;  out_position < in_position;  out_position++)
  131.         putc (' ', out);
  132.         else
  133.           {
  134.         out_position = in_position;
  135.         putc (c, out);
  136.           }
  137.       break;
  138.  
  139.     case '\f':
  140.     case '\v':
  141.     control_char:
  142.       if (in_position < out_bound)
  143.         putc (c, out);
  144.       break;
  145.  
  146.     default:
  147.       if (! isprint (c))
  148.         goto control_char;
  149.       /* falls through */
  150.     case ' ':
  151.       if (in_position++ < out_bound)
  152.         {
  153.           out_position = in_position;
  154.           putc (c, out);
  155.         }
  156.       break;
  157.  
  158.     case '\n':
  159.       return out_position;
  160.     }
  161.     }
  162.  
  163.   return out_position;
  164. }
  165.  
  166. /*
  167.  * Print side by side lines with a separator in the middle.
  168.  * 0 parameters are taken to indicate white space text.
  169.  * Blank lines that can easily be caught are reduced to a single newline.
  170.  */
  171.  
  172. static void
  173. print_1sdiff_line (left, sep, right)
  174.      char const * const *left;
  175.      int sep;
  176.      char const * const *right;
  177. {
  178.   FILE *out = outfile;
  179.   unsigned hw = sdiff_half_width, c2o = sdiff_column2_offset;
  180.   unsigned col = 0;
  181.   int put_newline = 0;
  182.   
  183.   if (left)
  184.     {
  185.       if (left[1][-1] == '\n')
  186.     put_newline = 1;
  187.       col = print_half_line (left, 0, hw);
  188.     }
  189.  
  190.   if (sep != ' ')
  191.     {
  192.       col = tab_from_to (col, (hw + c2o - 1) / 2) + 1;
  193.       if (sep == '|' && put_newline != (right[1][-1] == '\n'))
  194.     sep = put_newline ? '/' : '\\';
  195.       putc (sep, out);
  196.     }
  197.  
  198.   if (right)
  199.     {
  200.       if (right[1][-1] == '\n')
  201.     put_newline = 1;
  202.       if (**right != '\n')
  203.     {
  204.       col = tab_from_to (col, c2o);
  205.       print_half_line (right, col, hw);
  206.     }
  207.     }
  208.  
  209.   if (put_newline)
  210.     putc ('\n', out);
  211. }
  212.  
  213. /* Print lines common to both files in side-by-side format.  */
  214. static void
  215. print_sdiff_common_lines (limit0, limit1)
  216.      int limit0, limit1;
  217. {
  218.   int i0 = next0, i1 = next1;
  219.  
  220.   if (! sdiff_skip_common_lines  &&  (i0 != limit0 || i1 != limit1))
  221.     {
  222.       if (sdiff_help_sdiff)
  223.     fprintf (outfile, "i%d,%d\n", limit0 - i0, limit1 - i1);
  224.  
  225.       if (! sdiff_left_only)
  226.     {
  227.       while (i0 != limit0 && i1 != limit1)
  228.         print_1sdiff_line (&files[0].linbuf[i0++], ' ', &files[1].linbuf[i1++]);
  229.       while (i1 != limit1)
  230.         print_1sdiff_line (0, ')', &files[1].linbuf[i1++]);
  231.     }
  232.       while (i0 != limit0)
  233.     print_1sdiff_line (&files[0].linbuf[i0++], '(', 0);
  234.     }
  235.  
  236.   next0 = limit0;
  237.   next1 = limit1;
  238. }
  239.  
  240. /* Print a hunk of an sdiff diff.
  241.    This is a contiguous portion of a complete edit script,
  242.    describing changes in consecutive lines.  */
  243.  
  244. static void
  245. print_sdiff_hunk (hunk)
  246.      struct change *hunk;
  247. {
  248.   int first0, last0, first1, last1, deletes, inserts;
  249.   register int i, j;
  250.  
  251.   /* Determine range of line numbers involved in each file.  */
  252.   analyze_hunk (hunk, &first0, &last0, &first1, &last1, &deletes, &inserts);
  253.   if (!deletes && !inserts)
  254.     return;
  255.  
  256.   /* Print out lines up to this change.  */
  257.   print_sdiff_common_lines (first0, first1);
  258.  
  259.   if (sdiff_help_sdiff)
  260.     fprintf (outfile, "c%d,%d\n", last0 - first0 + 1, last1 - first1 + 1);
  261.  
  262.   /* Print ``xxx  |  xxx '' lines */
  263.   if (inserts && deletes)
  264.     {
  265.       for (i = first0, j = first1;  i <= last0 && j <= last1; ++i, ++j)
  266.     print_1sdiff_line (&files[0].linbuf[i], '|', &files[1].linbuf[j]);
  267.       deletes = i <= last0;
  268.       inserts = j <= last1;
  269.       next0 = first0 = i;
  270.       next1 = first1 = j;
  271.     }
  272.  
  273.  
  274.   /* Print ``     >  xxx '' lines */
  275.   if (inserts)
  276.     {
  277.       for (j = first1; j <= last1; ++j)
  278.     print_1sdiff_line (0, '>', &files[1].linbuf[j]);
  279.       next1 = j;
  280.     }
  281.  
  282.   /* Print ``xxx  <     '' lines */
  283.   if (deletes)
  284.     {
  285.       for (i = first0; i <= last0; ++i)
  286.     print_1sdiff_line (&files[0].linbuf[i], '<', 0);
  287.       next0 = i;
  288.     }
  289. }
  290.