Added comments

This commit is contained in:
Austen Adler 2016-08-19 11:14:56 -04:00
parent 5e02c12a82
commit d04a039bd0
No known key found for this signature in database
GPG Key ID: 7ECEE590CCDFE3F1

View File

@ -5,35 +5,52 @@ use warnings;
use File::Compare; use File::Compare;
use File::Basename; use File::Basename;
no warnings 'once'; no warnings 'once';
# For every argument (file)
foreach my $file (@ARGV) { foreach my $file (@ARGV) {
# If it exists
if (-e $file) { if (-e $file) {
# Load the autoformatted data into $data
my $data = process_file($file); my $data = process_file($file);
open(MYOUTPUTFILE, "+>$file.bak") or die "Error opening output file $!"; open(MYOUTPUTFILE, "+>$file.bak") or die "Error opening output file $!";
# Write the data to $file.bak
#TODO: This could be done with no .bak
print MYOUTPUTFILE $data; print MYOUTPUTFILE $data;
close(MYOUTPUTFILE); close(MYOUTPUTFILE);
if (compare($file, "$file.bak") != 0) { if (compare($file, "$file.bak") != 0) {
# If there were changes, then tell the user
print "Processed $file\n"; print "Processed $file\n";
} }
# Replace the original file with this file
# Delete it
unlink $file; unlink $file;
# Overwrite the old one
rename "$file.bak", $file; rename "$file.bak", $file;
} }
} }
# Creates the automatically formatted file
sub process_file { sub process_file {
my($filename) = "$_[0]"; my($filename) = "$_[0]";
open(MYINPUTFILE, "<$filename") or die "x $!"; open(MYINPUTFILE, "<$filename") or die "x $!";
# The formatted file
my($data) = ""; my($data) = "";
# Loop through every line in the file
while(<MYINPUTFILE>) { while(<MYINPUTFILE>) {
# Get rid of trailing whitespace
s/\s*$//; s/\s*$//;
my($line) = $_; my($line) = $_;
# If it is not a whitespace only line, append it to $data
if($line !~ /^\s*$/) { if($line !~ /^\s*$/) {
$data = $data . "$line\n"; $data = $data . "$line\n";
} }
} }
close(MYINPUTFILE); close(MYINPUTFILE);
# Get rid of final newline
chomp($data); chomp($data);
if ($data !~ /^\s*\/\*\*/) { if ($data !~ /^\s*\/\*\*/) {
# Get the basename of the file, this prevents making the block comment say:
# @file src/folders/code.c
# And instead uses
# @file code.c
my $basename = basename($filename); my $basename = basename($filename);
$data = "/**\n * \@file $basename\n * \@author Austen Adler (agadler)\n * TODO: Description\n */\n$data"; $data = "/**\n * \@file $basename\n * \@author Austen Adler (agadler)\n * TODO: Description\n */\n$data";
} }