src/c2html.c

changeset 4
323f674931fe
parent 2
3bced1fe9193
child 5
412f1896874b
--- a/src/c2html.c	Fri May 24 13:57:51 2013 +0200
+++ b/src/c2html.c	Wed Jun 12 13:40:23 2013 +0200
@@ -32,13 +32,24 @@
 #include <string.h>
 #include <fcntl.h>
 #include <unistd.h>
-#include "c2html.h"
+#include <ctype.h>
+
+#define INPUTBUF_SIZE 2048
+
+
+typedef struct {
+  size_t count;
+  size_t capacity;
+  size_t maxlinewidth;
+  char** lines;
+} inputfile_t;
 
 inputfile_t *inputfilebuffer(size_t capacity) {
   inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t));
   inputfile->lines = (char**) malloc(capacity * sizeof(char*));
   inputfile->capacity = capacity;
   inputfile->count = 0;
+  inputfile->maxlinewidth = 0;
   
   return inputfile;
 }
@@ -52,6 +63,8 @@
     inputfile->lines = realloc(inputfile->lines, inputfile->capacity);
   }
   inputfile->lines[inputfile->count] = l;
+  inputfile->maxlinewidth =
+          width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth;
   inputfile->count++;
 }
 
@@ -70,15 +83,14 @@
   
   inputfile_t *inputfile = inputfilebuffer(512);
   
-  const size_t bufsize = 1024;
-  char buf[bufsize];
+  char buf[INPUTBUF_SIZE];
   ssize_t r;
   
-  size_t maxlinewidth = 80;
+  size_t maxlinewidth = 256;
   char *line = (char*) malloc(maxlinewidth);
   size_t col = 0;
   
-  while ((r = read(fd, buf, bufsize)) > 0) {
+  while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) {
     for (size_t i = 0 ; i < r ; i++) {
       if (col >= maxlinewidth-4) {
         maxlinewidth <<= 1;
@@ -89,12 +101,6 @@
         line[col] = 0;
         addline(inputfile, line, col);        
         col = 0;
-      } else if (buf[i] == '<') {
-        line[col++] = '&'; line[col++] = 'l';
-        line[col++] = 't'; line[col++] = ';';
-      } else if (buf[i] == '>') {
-        line[col++] = '&'; line[col++] = 'g';
-        line[col++] = 't'; line[col++] = ';';
       } else {
         line[col++] = buf[i];
       }
@@ -108,6 +114,29 @@
   return inputfile;
 }
 
+void parseline(char *src, char *dest) {
+  size_t sp = 0, dp = 0;
+  /* indent */
+  while (isspace(src[sp])) {
+    dest[dp++] = src[sp++];
+  }
+  for (char c = src[sp] ; c ; c=src[++sp]) {
+    switch (c) {
+      case '<':
+        memcpy(&(dest[dp]), "&lt;", 4);
+        dp += 4;
+        break;
+      case '>':
+        memcpy(&(dest[dp]), "&gt;", 4);
+        dp += 4;
+        break;
+      default:
+        dest[dp++] = c;
+    }
+  }
+  dest[dp] = 0;
+}
+
 void printhelp() {
   printf("Formats source code using HTML.\n\nUsage:\n"
       "  c2html [FILE...]"
@@ -116,7 +145,7 @@
   
 }
 
-int lnw(size_t lnc) {
+int lnint(size_t lnc) {
   int w = 1, p = 1;
   while ((p*=10) < lnc) w++;
   return w;
@@ -132,10 +161,14 @@
     inputfile_t *inputfile = readinput(argv[1]);
     if (inputfile) {
       printf("<pre>\n");
+      char *line = (char*) malloc(inputfile->maxlinewidth*64);
+      int lnw = lnint(inputfile->count);
       for (int i = 0 ; i < inputfile->count ; i++) {
+        parseline(inputfile->lines[i], line);
         printf("<span class=\"c2html-lineno\">%*d:</span> %s\n",
-            lnw(inputfile->count), i, inputfile->lines[i]);
+            lnw, i, line);
       }
+      free(line);
       printf("</pre>\n");
       freeinputfilebuffer(inputfile);
     }

mercurial