changes merged

Wed, 10 Jul 2013 13:54:15 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 10 Jul 2013 13:54:15 +0200
changeset 13
fe74bf2d5f27
parent 12
7ce5c4b51959 (diff)
parent 10
925172e535a9 (current diff)
child 14
b33629bf4b58

changes merged

src/c2html.c file | annotate | diff | comparison | revisions
--- a/Makefile	Fri Jun 21 13:32:31 2013 +0200
+++ b/Makefile	Wed Jul 10 13:54:15 2013 +0200
@@ -37,7 +37,7 @@
 	$(MKDIR) build
 	
 test: compile
-	./build/$(BIN) src/c2html.c > build/body.html
+	./build/$(BIN) $(ARGS) src/c2html.c > build/body.html
 	cat test/header.html build/body.html test/footer.html > build/code.html
 	
 clean:
--- a/src/c2html.c	Fri Jun 21 13:32:31 2013 +0200
+++ b/src/c2html.c	Wed Jul 10 13:54:15 2013 +0200
@@ -47,6 +47,11 @@
   "switch", "typedef", "union", "unsigned", "void", "volatile", "while", NULL
 };
 
+typedef struct {
+  char* outfilename;
+  char* infilename;
+  int highlight;
+} settings_t;
 
 typedef struct {
   size_t count;
@@ -308,7 +313,10 @@
 
 void printhelp() {
   printf("Formats source code using HTML.\n\nUsage:\n"
-      "  c2html [FILE...]"
+      "  c2html [Options] FILE\n\n"
+      " Options:\n"
+      "  -h                    Prints this help message\n"
+      "  -o <output>           Output file (if not specified, stdout is used)\n"
       "\n");
   
   
@@ -322,23 +330,63 @@
 
 int main(int argc, char** argv) {
   
-  if (argc == 1) {
+  settings_t settings;
+  settings.outfilename = NULL;
+  settings.highlight = 1;
+  
+  char optc;
+  while ((optc = getopt(argc, argv, "ho:p")) != -1) {
+    switch (optc) {
+      case 'o':
+        if (!(optarg[0] == '-' && optarg[1] == 0)) {
+          settings.outfilename = optarg;
+        }
+        break;
+      case 'p':
+        settings.highlight = 0;
+        break;
+      case 'h':
+        printhelp();
+        return 0;
+      default:
+        return 1;
+    }
+  }
+
+  if (optind != argc-1) {
     printhelp();
-    return 0;
+    return 1;
   } else {
+    settings.infilename = argv[optind];
     
-    inputfile_t *inputfile = readinput(argv[1]);
+    inputfile_t *inputfile = readinput(settings.infilename);
     if (inputfile) {
-      printf("<pre>\n");
-      char *line = (char*) malloc(inputfile->maxlinewidth*64);
+      FILE *fout;
+      if (settings.outfilename) {
+        fout = fopen(settings.outfilename, "w");
+      } else {
+        fout = stdout;
+      }
+      fprintf(fout, "<pre>\n");
+      char *line = (char*) malloc(inputfile->maxlinewidth
+          * (settings.highlight?64:0));
       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",
+        if (settings.highlight) {
+          parseline(inputfile->lines[i], line);
+        } else {
+          line = inputfile->lines[i];
+        }
+        fprintf(fout, "<span class=\"c2html-lineno\">%*d:</span> %s",
             lnw, i+1, line);
       }
       free(line);
-      printf("</pre>\n");
+      fprintf(fout, "</pre>\n");
+      
+      if (fout != stdout) {
+        fclose(fout);
+      }
+      
       freeinputfilebuffer(inputfile);
     }
   

mercurial