diff output.c @ 21:e3fab8f1b52c

strip comments.
author David A. Holland
date Mon, 20 Dec 2010 04:15:02 -0500
parents 40748b097655
children cef2dc916269
line wrap: on
line diff
--- a/output.c	Mon Dec 20 04:03:35 2010 -0500
+++ b/output.c	Mon Dec 20 04:15:02 2010 -0500
@@ -4,9 +4,11 @@
 
 #include "utils.h"
 #include "mode.h"
+#include "place.h"
 #include "output.h"
 
 static int outputfd = -1;
+static bool incomment = false;
 
 static
 void
@@ -19,8 +21,9 @@
 	}
 }
 
+static
 void
-output(const char *buf, size_t len)
+dowrite(const char *buf, size_t len)
 {
 	size_t done;
 	ssize_t result;
@@ -51,6 +54,51 @@
 }
 
 void
+output(const struct place *p, const char *buf, size_t len)
+{
+	size_t pos, start;
+	struct place p2;
+
+	start = 0;
+	for (pos = 0; pos < len - 1; pos++) {
+		if (buf[pos] == '/' && buf[pos+1] == '*') {
+			if (incomment && warns.nestcomment) {
+				p2 = *p;
+				p2.column += pos;
+				complain(p, "Warning: %c%c within comment",
+					 '/', '*');
+				if (mode.werror) {
+					complain_failed();
+				}
+			} else if (!incomment) {
+				if (pos > start) {
+					dowrite(buf + start, pos - start);
+				}
+				start = pos;
+				pos += 2;
+				incomment = true;
+				/* cancel out the loop's pos++ */
+				pos--;
+				continue;
+			}
+		} else if (buf[pos] == '*' && buf[pos+1] == '/') {
+			if (incomment) {
+				pos += 2;
+				if (mode.output_retain_comments) {
+					dowrite(buf + start, pos - start);
+				}
+				start = pos;
+				pos += 2;
+				incomment = false;
+				/* cancel out the loop's pos++ */
+				pos--;
+				continue;
+			}
+		}
+	}
+}
+
+void
 output_eof(void)
 {
 	if (outputfd >= 0) {