changeset 39986:c9411c9fab91

(put_char): New function. (read_c_string_or_comment): Strip trailing spaces and newlines.
author Miles Bader <miles@gnu.org>
date Wed, 17 Oct 2001 02:53:57 +0000
parents 3a906538d254
children 9c096889e4c6
files lib-src/make-docfile.c
diffstat 1 files changed, 48 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/lib-src/make-docfile.c	Wed Oct 17 02:00:20 2001 +0000
+++ b/lib-src/make-docfile.c	Wed Oct 17 02:53:57 2001 +0000
@@ -198,6 +198,42 @@
 
 char buf[128];
 
+/* Add CH to either outfile, if PRINTFLAG is positive, or to the buffer
+   whose end is pointed to by BUFP, if PRINTFLAG is negative.
+   If the counters pointed to by PENDING_NEWLINES and PENDING_SPACES are
+   non-zero, that many newlines and spaces are output before CH, and
+   the counters are zeroed.  */
+
+static INLINE void
+put_char (ch, printflag, bufp, pending_newlines, pending_spaces)
+     int ch, printflag;
+     char **bufp;
+     unsigned *pending_newlines, *pending_spaces;
+{
+  int out_ch;
+  do
+    {
+      if (*pending_newlines > 0)
+	{
+	  (*pending_newlines)--;
+	  out_ch = '\n';
+	}
+      else if (*pending_spaces > 0)
+	{
+	  (*pending_spaces)--;
+	  out_ch = ' ';
+	}
+      else
+	out_ch = ch;
+
+      if (printflag > 0)
+	putc (out_ch, outfile);
+      else if (printflag < 0)
+	*(*bufp)++ = out_ch;
+    }
+  while (out_ch != ch);
+}
+
 /* Skip a C string or C-style comment from INFILE, and return the
    character that follows.  COMMENT non-zero means skip a comment.  If
    PRINTFLAG is positive, output string contents to outfile.  If it is
@@ -210,6 +246,7 @@
      int printflag;
 {
   register int c;
+  unsigned pending_spaces = 0, pending_newlines = 0;
   char *p = buf;
 
   if (comment)
@@ -239,10 +276,16 @@
 		c = '\t';
 	    }
 	  
-	  if (printflag > 0)
-	    putc (c, outfile);
-	  else if (printflag < 0)
-	    *p++ = c;
+	  if (c == ' ')
+	    pending_spaces++;
+	  else if (c == '\n')
+	    {
+	      pending_newlines++;
+	      pending_spaces = 0;
+	    }
+	  else
+	    put_char (c, printflag, &p, &pending_newlines, &pending_spaces);
+
 	  c = getc (infile);
 	}
 
@@ -257,10 +300,7 @@
 	      break;
 	    }
 	  
-	  if (printflag > 0)
-	    putc ('*', outfile);
-	  else if (printflag < 0)
-	    *p++ = '*';
+	  put_char ('*', printflag, &p, &pending_newlines, &pending_spaces);
 	}
       else
 	{