From d5e1adf060a23e6c739d0dd78fe9321fb0eb7582 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Mon, 24 Jan 2011 20:57:02 -0500 Subject: crude c comment highlighter --- highlight_comments.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 highlight_comments.c diff --git a/highlight_comments.c b/highlight_comments.c new file mode 100644 index 0000000..ad270f4 --- /dev/null +++ b/highlight_comments.c @@ -0,0 +1,93 @@ + +/* Here are all the header files */ +#include +#include + +/* Some defines */ +#define false 0 +#define true 1 + +/* Function definitions */ +void putc_html(char c); + + +int main(void) { + unsigned char c = ' ', last_c = ' '; /* TODO */ + int not_whitespace = false; + int do_inline = false; + int comment = false; + int index; + + printf("\n" + "
");
+
+  while((last_c = c) && (c = getc(stdin))) {
+    if(comment) {
+      if(last_c == '*' && c == '/') {
+        comment = false;
+        if(do_inline) {
+          printf("*/");
+        } else {
+          printf("
");
+        }
+        not_whitespace = true;
+        c = getc(stdin);
+        continue;
+      }
+      if(not_whitespace)
+        putc_html(last_c);
+      if(! (isspace(c) || c == '*'))
+        not_whitespace = true;
+      if(last_c == '\n') 
+        not_whitespace = false;
+    } else {
+      if(last_c == '/' && c == '*') {
+        comment = true;
+        if(not_whitespace) {
+          do_inline = true;
+          printf("/\x2A "); /* heh */
+        } else {
+          do_inline = false;
+          printf("
");
+        }
+        not_whitespace = false;
+        continue;
+      }
+      if(! isspace(last_c))
+        not_whitespace = true;
+      if(not_whitespace)
+        putc_html(last_c);
+      if(last_c == '\n' && c == '\n' && not_whitespace) {
+        not_whitespace = false;
+        putc('\n', stdout);
+      }
+    }
+
+    if(feof(stdin) || ferror(stdin)) break;
+  }
+  printf("
\n"); + return 0; +} + +/* This function puts some stuff! Great! */ +void putc_html(char c) { + switch(c) { + case '<': + printf("<"); + break; + case '>': + printf(">"); + break; + case '&': + printf("&"); + break; + default: + putc(c,stdout); + } +} + -- cgit v1.2.3