Commits

christiang committed 963d6ad

Little tool to add comment headers to multiple c/c++ source files.

  • Participants
  • Parent commits c395c22

Comments (0)

Files changed (1)

+#!/usr/bin/perl
+
+###############################################################################
+#
+# AutoComment.pl
+# by
+# Christian Gruhl <christian@student.uni-kassel.de>
+# (c) 2012
+#
+# Use this tool to add headers to c/c++ source files.
+#
+###############################################################################
+
+use strict;
+use warnings;
+
+use Getopt::Std;
+use Cwd;
+
+my %opts;
+
+getopts('c:p:e:s:rh', \%opts);
+
+my $path = $opts{'p'};
+
+print "comments: ",$opts{'c'}," path: ",$path,"\n";
+
+if( $opts{'h'} ) {
+    &help();
+}
+
+my $editor = $opts{'e'} || "/usr/bin/vim";
+my $extensions = $opts{'s'} || "cpp|hpp|c|h";
+
+open TEMPLATE, "<", $opts{'c'} or die $!;
+
+my @commentTemplate = <TEMPLATE>;
+
+close TEMPLATE;
+
+opendir DIR,$opts{'p'};
+
+while ( my $entry = readdir(DIR) ) {
+
+    my $file = undef;
+    my @fileContent = undef;
+    if( $entry =~ /.*\.($extensions)$/ ) {
+        $file = $entry;
+        open SRCFILE, "<", $path."/".$file or next;
+        @fileContent = <SRCFILE>;
+        close SRCFILE;
+    } else {
+        next;
+    }
+
+    my $brief = "";
+    my $description = "";
+    
+    print "current file is: ", $path."/".$file,"\n";
+
+    print "display current source file? type 'yes' or type 'skip' to not comment this file : ";
+    my $read = <STDIN>;
+    $read = lc( $read );
+    if( $read =~ /^\w*(y|yes)\w*/ ) {
+        print "\nBEGIN: ",$file,"\n",@fileContent,"\nEOF\n\n";
+    } elsif ( $read =~ /^\w*(s|skip)\w*/ ) {
+        print "Skipping this file!\n";
+        next;
+    }
+
+    print "enter brief description [end with 'EOF' Ctrl+d] :\n";
+    print "-"x80,"\n\n";
+    
+    my $first = 1;
+    foreach my $input ( <STDIN> ) {
+        $input =~ s/\n//;
+        if( $first ) {
+            $first = 0;
+            if( $input eq "" ) {
+                last;
+            }
+            $brief .= "\\brief ".$input;
+        } else {
+            $brief .= "\n*        ".$input;
+        }
+    }
+    print "-"x80,"\n\n";
+    
+    print "enter detailed description [end with 'EOF' Ctrl+d] :\n";
+    
+    print "-"x80,"\n\n";
+    
+    $first = 1;
+    foreach my $input ( <STDIN> ) {
+       if( $first ) {
+            $first = 0;
+            if( $input eq "" ) {
+                last;
+            }
+            $description .= "\\details ".$input;
+        } else {
+            $description .= "*          ".$input;
+        }
+    }
+    
+    print "-"x80,"\n\n";
+    
+    my @commentHeader; 
+    foreach my $line ( @commentTemplate ) {
+        my $buf = $line;
+    
+        $buf =~ s/%FILE%/$file/;
+        $buf =~ s/%BRIEF%/$brief/;
+        $buf =~ s/%DESC%/$description/;
+        push( @commentHeader, $buf );
+    }
+
+    my @newContent = (@commentHeader, @fileContent );
+
+    print "Display altered file before writing? type 'yes' for display, type 'edit' for editing : ";
+    my $print = <STDIN>;
+    if( lc( $print ) =~ /^\w*(y|yes)\w*/ ) {
+        print @newContent;
+    } elsif( lc( $print ) =~ /^\w*(e|edit)\w*/ ) {
+        my $tmpfile = "/tmp/__".$file.".comment.pl-edit";
+        open TMPFILE, ">>", $tmpfile;
+        print TMPFILE @commentHeader;
+        print TMPFILE @fileContent;
+        close TMPFILE;
+        system( $editor, $tmpfile );
+        open TMPFILE, "<", $tmpfile;
+        @newContent = <TMPFILE>;
+        close TMPFILE;
+    }
+
+    print "write to file? : ";
+    my $print2file = <STDIN>;
+    if( lc( $print2file ) =~ /^\w*(y|yes)\w*/ ) {
+        open WRITEFILE, ">", $path."/".$file or do { warn "could not open"; next; };
+#        open WRITEFILE, ">", "/tmp/".$file or next;
+        print WRITEFILE @newContent;
+        close WRITEFILE;
+    }
+}
+
+sub help {
+    print "usage: AutoComment.pl -c commentTamplate -p path/to/src/files [-e myEditor] [-s fileExtension|anotherone] [-h]";
+    exit;
+}