home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / online / source / c / compilers / DiffUtils.sit.hqx / DiffUtils / src / oldside.c < prev    next >
Text File  |  1993-01-06  |  6KB  |  265 lines

  1. /* sdiff-format output routines for GNU DIFF.
  2.    Copyright (C) 1991, 92 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU DIFF General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU DIFF, but only under the conditions described in the
  15. GNU DIFF General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU DIFF so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21.  
  22. #include "diff.h"
  23.  
  24.  
  25. static void print_sdiff_hunk ();
  26. static void print_sdiff_common_lines ();
  27. static void print_1sdiff_line ();
  28.  
  29. /* Next line number to be printed in the two input files.  */
  30. static int next0, next1;
  31.  
  32. /* Print the edit-script SCRIPT as a sdiff style output.  */
  33.  
  34. void
  35. print_sdiff_script (script)
  36.      struct change *script;
  37. {
  38.   begin_output ();
  39.  
  40.   next0 = next1 = - files[0].prefix_lines;
  41.   print_script (script, find_change, print_sdiff_hunk);
  42.  
  43.   print_sdiff_common_lines (files[0].valid_lines, files[1].valid_lines);
  44.   if (sdiff_help_sdiff)
  45.     fputs ("q\n", outfile);
  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.      const char * const *line;
  76.      unsigned indent, out_bound;
  77. {
  78.   FILE *out = outfile;
  79.   register unsigned in_position = 0, out_position = 0;
  80.   register const char
  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.         in_position += spaces;
  94.         if (tab_expand_flag)
  95.           while (out_position < out_bound && spaces--)
  96.         {
  97.           out_position++;
  98.           putc (' ', out);
  99.         }
  100.         else
  101.           if (out_position + spaces < out_bound)
  102.         {
  103.           out_position += spaces;
  104.           putc (c, out);
  105.         }
  106.       }
  107.       break;
  108.  
  109.     case '\r':
  110.       {
  111.         putc (c, out);
  112.         tab_from_to (0, indent);
  113.         in_position = out_position = 0;
  114.       }
  115.       break;
  116.  
  117.     case '\b':
  118.       if (in_position != 0 && --in_position < out_bound)
  119.         if (out_position <= in_position)
  120.           /* Add spaces to make up for suppressed tab past out_bound.  */
  121.           for (;  out_position < in_position;  out_position++)
  122.         putc (' ', out);
  123.         else
  124.           {
  125.         out_position = in_position;
  126.         putc (c, out);
  127.           }
  128.       break;
  129.  
  130.     case '\f':
  131.     case '\v':
  132.       if (in_position < out_bound)
  133.         putc (c, out);
  134.       break;
  135.  
  136.     default:
  137.       {
  138.         register unsigned p = in_position;
  139.         if (textchar[c])
  140.           in_position++;
  141.         if (p < out_bound)
  142.           {
  143.         out_position = in_position;
  144.         putc (c, out);
  145.           }
  146.       }
  147.       break;
  148.  
  149.     case '\n':
  150.       return out_position;
  151.     }
  152.     }
  153.  
  154.   return out_position;
  155. }
  156.  
  157. /*
  158.  * Print side by side lines with a separator in the middle.
  159.  * NULL parameters are taken to indicate whitespace text.
  160.  * Blank lines that can easily be caught are reduced to a single newline.
  161.  */
  162.  
  163. static void
  164. print_1sdiff_line (left, sep, right)
  165.      const char * const *left;
  166.      int sep;
  167.      const char * const *right;
  168. {
  169.   FILE *out = outfile;
  170.   unsigned hw = sdiff_half_width, c2o = sdiff_column2_offset;
  171.   unsigned col = left ? print_half_line (left, 0, hw) : 0;
  172.  
  173.   if (sep != ' ')
  174.     {
  175.       col = tab_from_to (col, (hw + c2o - 1) / 2) + 1;
  176.       putc (sep, out);
  177.     }
  178.  
  179.   if (right && **right != '\n')
  180.     {
  181.       col = tab_from_to (col, c2o);
  182.       print_half_line (right, col, hw);
  183.     }
  184.  
  185.   putc ('\n', out);
  186. }
  187.  
  188. /* Print lines common to both files in side-by-side format.  */
  189. static void
  190. print_sdiff_common_lines (limit0, limit1)
  191.      int limit0, limit1;
  192. {
  193.   int i0 = next0, i1 = next1;
  194.  
  195.   if (! sdiff_skip_common_lines  &&  (i0 != limit0 || i1 != limit1))
  196.     {
  197.       if (sdiff_help_sdiff)
  198.     fprintf (outfile, "i%d,%d\n", limit0 - i0, limit1 - i1);
  199.  
  200.       if (! sdiff_left_only)
  201.     {
  202.       while (i0 != limit0 && i1 != limit1)
  203.         print_1sdiff_line (&files[0].linbuf[i0++], ' ', &files[1].linbuf[i1++]);
  204.       while (i1 != limit1)
  205.         print_1sdiff_line (0, ')', &files[1].linbuf[i1++]);
  206.     }
  207.       while (i0 != limit0)
  208.     print_1sdiff_line (&files[0].linbuf[i0++], '(', 0);
  209.     }
  210.  
  211.   next0 = limit0;
  212.   next1 = limit1;
  213. }
  214.  
  215. /* Print a hunk of an sdiff diff.
  216.    This is a contiguous portion of a complete edit script,
  217.    describing changes in consecutive lines.  */
  218.  
  219. static void
  220. print_sdiff_hunk (hunk)
  221.      struct change *hunk;
  222. {
  223.   int first0, last0, first1, last1, deletes, inserts;
  224.   register int i, j;
  225.  
  226.   /* Determine range of line numbers involved in each file.  */
  227.   analyze_hunk (hunk, &first0, &last0, &first1, &last1, &deletes, &inserts);
  228.   if (!deletes && !inserts)
  229.     return;
  230.  
  231.   /* Print out lines up to this change.  */
  232.   print_sdiff_common_lines (first0, first1);
  233.  
  234.   if (sdiff_help_sdiff)
  235.     fprintf (outfile, "c%d,%d\n", last0 - first0 + 1, last1 - first1 + 1);
  236.  
  237.   /* Print ``xxx  |  xxx '' lines */
  238.   if (inserts && deletes)
  239.     {
  240.       for (i = first0, j = first1;  i <= last0 && j <= last1; ++i, ++j)
  241.     print_1sdiff_line (&files[0].linbuf[i], '|', &files[1].linbuf[j]);
  242.       deletes = i <= last0;
  243.       inserts = j <= last1;
  244.       next0 = first0 = i;
  245.       next1 = first1 = j;
  246.     }
  247.  
  248.  
  249.   /* Print ``     >  xxx '' lines */
  250.   if (inserts)
  251.     {
  252.       for (j = first1; j <= last1; ++j)
  253.     print_1sdiff_line (0, '>', &files[1].linbuf[j]);
  254.       next1 = j;
  255.     }
  256.  
  257.   /* Print ``xxx  <     '' lines */
  258.   if (deletes)
  259.     {
  260.       for (i = first0; i <= last0; ++i)
  261.     print_1sdiff_line (&files[0].linbuf[i], '<', 0);
  262.       next0 = i;
  263.     }
  264. }
  265.