home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / gnucdiff / ed.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-02  |  5.0 KB  |  188 lines

  1. /* Output routines for ed-script format.
  2.    Copyright (C) 1988 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. #include "diff.h"
  22.  
  23. static void print_rcs_hunk ();
  24. static void print_ed_hunk ();
  25. static void print_forward_ed_hunk ();
  26. static void print_range_length ();
  27. void translate_range ();
  28. struct change *find_change ();
  29. struct change *find_reverse_change ();
  30.  
  31. /* Print our script as ed commands.  */
  32.  
  33. void
  34. print_ed_script (script)
  35.     struct change *script;
  36. {
  37.   print_script (script, find_reverse_change, print_ed_hunk);
  38. }
  39.  
  40. /* Print a hunk of an ed diff */
  41.  
  42. static void
  43. print_ed_hunk (hunk)
  44.      struct change *hunk; 
  45. {
  46.   int f0, l0, f1, l1;
  47.   int deletes, inserts;
  48.  
  49. #if 0
  50.   hunk = flip_script (hunk);
  51. #endif
  52. #ifdef DEBUG
  53.   debug_script (hunk);
  54. #endif
  55.  
  56.   /* Determine range of line numbers involved in each file.  */
  57.   analyze_hunk (hunk, &f0, &l0, &f1, &l1, &deletes, &inserts);
  58.   if (!deletes && !inserts)
  59.     return;
  60.  
  61.   /* Print out the line number header for this hunk */
  62.   print_number_range (',', &files[0], f0, l0);
  63.   fprintf (outfile, "%c\n", change_letter (inserts, deletes));
  64.  
  65.   /* Print new/changed lines from second file, if needed */
  66.   if (inserts)
  67.     {
  68.       int i;
  69.       int leading_dot = 0;
  70.       for (i = f1; i <= l1; i++)
  71.     {
  72.       /* If the file's line starts with a dot, it would confuse `ed'.
  73.          So output it with a double dot, and set the flag LEADING_DOT
  74.          so that we will output another ed-command later
  75.          to change the double dot into a single dot.  */
  76.  
  77.       if (files[1].linbuf[i].text[0] == '.')
  78.         {
  79.           leading_dot = 1;
  80.           fprintf (outfile, ".");
  81.         }
  82.       print_1_line ("", &files[1].linbuf[i]);
  83.     }
  84.  
  85.       fprintf (outfile, ".\n");
  86.       if (leading_dot)
  87.     fprintf (outfile, "%d,.s/^\\.\\././\n",
  88.          translate_line_number (&files[0], f0) - 1);
  89.     }
  90. }
  91.  
  92. /* Print change script in the style of ed commands,
  93.    but print the changes in the order they appear in the input files,
  94.    which means that the commands are not truly useful with ed.  */
  95.  
  96. void
  97. print_forward_ed_script (script)
  98.      struct change *script;
  99. {
  100.   print_script (script, find_change, print_forward_ed_hunk);
  101. }
  102.  
  103. static void
  104. print_forward_ed_hunk (hunk)
  105.      struct change *hunk;
  106. {
  107.   int i;
  108.   int f0, l0, f1, l1;
  109.   int deletes, inserts;
  110.  
  111.   /* Determine range of line numbers involved in each file.  */
  112.   analyze_hunk (hunk, &f0, &l0, &f1, &l1, &deletes, &inserts);
  113.   if (!deletes && !inserts)
  114.     return;
  115.  
  116.   fprintf (outfile, "%c", change_letter (inserts, deletes));
  117.   print_number_range (' ', files, f0, l0);
  118.   fprintf (outfile, "\n");
  119.  
  120.   /* If deletion only, print just the number range.  */
  121.  
  122.   if (!inserts)
  123.     return;
  124.  
  125.   /* For insertion (with or without deletion), print the number range
  126.      and the lines from file 2.  */
  127.  
  128.   for (i = f1; i <= l1; i++)
  129.     print_1_line ("", &files[1].linbuf[i]);
  130.  
  131.   fprintf (outfile, ".\n");
  132. }
  133.  
  134. /* Print in a format somewhat like ed commands
  135.    except that each insert command states the number of lines it inserts.
  136.    This format is used for RCS.  */
  137.  
  138. void
  139. print_rcs_script (script)
  140.      struct change *script;
  141. {
  142.   print_script (script, find_change, print_rcs_hunk);
  143. }
  144.  
  145. /* Print a hunk of an RCS diff */
  146.  
  147. static void
  148. print_rcs_hunk (hunk)
  149.      struct change *hunk;
  150. {
  151.   int i;
  152.   int f0, l0, f1, l1;
  153.   int deletes, inserts;
  154.   int tf0, tl0, tf1, tl1;
  155.  
  156.   /* Determine range of line numbers involved in each file.  */
  157.   analyze_hunk (hunk, &f0, &l0, &f1, &l1, &deletes, &inserts);
  158.   if (!deletes && !inserts)
  159.     return;
  160.  
  161.   translate_range (&files[0], f0, l0, &tf0, &tl0);
  162.  
  163.   if (deletes)
  164.     {
  165.       fprintf (outfile, "d");
  166.       /* For deletion, print just the starting line number from file 0
  167.      and the number of lines deleted.  */
  168.       fprintf (outfile, "%d %d\n",
  169.            tf0,
  170.            (tl0 >= tf0 ? tl0 - tf0 + 1 : 1));         
  171.     }
  172.  
  173.   if (inserts)
  174.     {
  175.       fprintf (outfile, "a");
  176.  
  177.       /* Take last-line-number from file 0 and # lines from file 1.  */
  178.       translate_range (&files[1], f1, l1, &tf1, &tl1);
  179.       fprintf (outfile, "%d %d\n",
  180.            tl0,
  181.            (tl1 >= tf1 ? tl1 - tf1 + 1 : 1));         
  182.  
  183.       /* Print the inserted lines.  */
  184.       for (i = f1; i <= l1; i++)
  185.     print_1_line ("", &files[1].linbuf[i]);
  186.     }
  187. }
  188.