home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / dif115as.zip / ED.C < prev    next >
C/C++ Source or Header  |  1992-02-22  |  6KB  |  213 lines

  1. /* Output routines for ed-script format.
  2.    Copyright (C) 1988, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU DIFF is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU DIFF; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  21. This port is also distributed under the terms of the GNU General
  22. Public License as published by the Free Software Foundation.
  23.  
  24. Please note that this file is not identical to the original GNU release,
  25. you should have received this code as patch to the official release.
  26.  
  27. $Header: e:/gnu/diff/RCS/ed.c 1.15.0.1 91/03/11 22:11:02 tho Exp $  */
  28.  
  29. #include "diff.h"
  30.  
  31. #ifdef __STDC__
  32. static  void print_ed_hunk(struct change *);
  33. static  void pr_forward_ed_hunk(struct change *);
  34. static  void print_rcs_hunk(struct change *);
  35. #else
  36. static void print_rcs_hunk ();
  37. static void print_ed_hunk ();
  38. static void pr_forward_ed_hunk ();
  39. void translate_range ();
  40. struct change *find_change ();
  41. struct change *find_reverse_change ();
  42. #endif /* __STDC__ */
  43.  
  44. /* Print our script as ed commands.  */
  45.  
  46. void
  47. print_ed_script (script)
  48.     struct change *script;
  49. {
  50.   print_script (script, find_reverse_change, print_ed_hunk);
  51. }
  52.  
  53. /* Print a hunk of an ed diff */
  54.  
  55. static void
  56. print_ed_hunk (hunk)
  57.      struct change *hunk; 
  58. {
  59.   int f0, l0, f1, l1;
  60.   int deletes, inserts;
  61.  
  62. #if 0
  63.   hunk = flip_script (hunk);
  64. #endif
  65. #ifdef DEBUG
  66.   debug_script (hunk);
  67. #endif
  68.  
  69.   /* Determine range of line numbers involved in each file.  */
  70.   analyze_hunk (hunk, &f0, &l0, &f1, &l1, &deletes, &inserts);
  71.   if (!deletes && !inserts)
  72.     return;
  73.  
  74.   /* Print out the line number header for this hunk */
  75.   print_number_range (',', &files[0], f0, l0);
  76.   fprintf (outfile, "%c\n", change_letter (inserts, deletes));
  77.  
  78.   /* Print new/changed lines from second file, if needed */
  79.   if (inserts)
  80.     {
  81.       int i;
  82.       int inserting = 1;
  83.       for (i = f1; i <= l1; i++)
  84.     {
  85.       /* Resume the insert, if we stopped.  */
  86.       if (! inserting)
  87.         fprintf (outfile, "%da\n",
  88.              i - f1 + translate_line_number (&files[0], f0) - 1);
  89.       inserting = 1;
  90.  
  91.       /* If the file's line is just a dot, it would confuse `ed'.
  92.          So output it with a double dot, and set the flag LEADING_DOT
  93.          so that we will output another ed-command later
  94.          to change the double dot into a single dot.  */
  95.  
  96.       if (files[1].linbuf[i].text[0] == '.'
  97.           && files[1].linbuf[i].text[1] == '\n')
  98.         {
  99.           fprintf (outfile, "..\n");
  100.           fprintf (outfile, ".\n");
  101.           /* Now change that double dot to the desired single dot.  */
  102.           fprintf (outfile, "%ds/^\\.\\././\n",
  103.                i - f1 + translate_line_number (&files[0], f0));
  104.           inserting = 0;
  105.         }
  106.       else
  107.         /* Line is not `.', so output it unmodified.  */
  108.         print_1_line ("", &files[1].linbuf[i]);
  109.     }
  110.  
  111.       /* End insert mode, if we are still in it.  */
  112.       if (inserting)
  113.     fprintf (outfile, ".\n");
  114.     }
  115. }
  116.  
  117. /* Print change script in the style of ed commands,
  118.    but print the changes in the order they appear in the input files,
  119.    which means that the commands are not truly useful with ed.  */
  120.  
  121. void
  122. pr_forward_ed_script (script)
  123.      struct change *script;
  124. {
  125.   print_script (script, find_change, pr_forward_ed_hunk);
  126. }
  127.  
  128. static void
  129. pr_forward_ed_hunk (hunk)
  130.      struct change *hunk;
  131. {
  132.   int i;
  133.   int f0, l0, f1, l1;
  134.   int deletes, inserts;
  135.  
  136.   /* Determine range of line numbers involved in each file.  */
  137.   analyze_hunk (hunk, &f0, &l0, &f1, &l1, &deletes, &inserts);
  138.   if (!deletes && !inserts)
  139.     return;
  140.  
  141.   fprintf (outfile, "%c", change_letter (inserts, deletes));
  142.   print_number_range (' ', files, f0, l0);
  143.   fprintf (outfile, "\n");
  144.  
  145.   /* If deletion only, print just the number range.  */
  146.  
  147.   if (!inserts)
  148.     return;
  149.  
  150.   /* For insertion (with or without deletion), print the number range
  151.      and the lines from file 2.  */
  152.  
  153.   for (i = f1; i <= l1; i++)
  154.     print_1_line ("", &files[1].linbuf[i]);
  155.  
  156.   fprintf (outfile, ".\n");
  157. }
  158.  
  159. /* Print in a format somewhat like ed commands
  160.    except that each insert command states the number of lines it inserts.
  161.    This format is used for RCS.  */
  162.  
  163. void
  164. print_rcs_script (script)
  165.      struct change *script;
  166. {
  167.   print_script (script, find_change, print_rcs_hunk);
  168. }
  169.  
  170. /* Print a hunk of an RCS diff */
  171.  
  172. static void
  173. print_rcs_hunk (hunk)
  174.      struct change *hunk;
  175. {
  176.   int i;
  177.   int f0, l0, f1, l1;
  178.   int deletes, inserts;
  179.   int tf0, tl0, tf1, tl1;
  180.  
  181.   /* Determine range of line numbers involved in each file.  */
  182.   analyze_hunk (hunk, &f0, &l0, &f1, &l1, &deletes, &inserts);
  183.   if (!deletes && !inserts)
  184.     return;
  185.  
  186.   translate_range (&files[0], f0, l0, &tf0, &tl0);
  187.  
  188.   if (deletes)
  189.     {
  190.       fprintf (outfile, "d");
  191.       /* For deletion, print just the starting line number from file 0
  192.      and the number of lines deleted.  */
  193.       fprintf (outfile, "%d %d\n",
  194.            tf0,
  195.            (tl0 >= tf0 ? tl0 - tf0 + 1 : 1));         
  196.     }
  197.  
  198.   if (inserts)
  199.     {
  200.       fprintf (outfile, "a");
  201.  
  202.       /* Take last-line-number from file 0 and # lines from file 1.  */
  203.       translate_range (&files[1], f1, l1, &tf1, &tl1);
  204.       fprintf (outfile, "%d %d\n",
  205.            tl0,
  206.            (tl1 >= tf1 ? tl1 - tf1 + 1 : 1));         
  207.  
  208.       /* Print the inserted lines.  */
  209.       for (i = f1; i <= l1; i++)
  210.     print_1_line ("", &files[1].linbuf[i]);
  211.     }
  212. }
  213.