csc230-autoformat/process.pl

73 lines
1.7 KiB
Perl
Raw Permalink Normal View History

#!/usr/bin/perl
use strict;
use autodie;
use warnings;
use File::Compare;
use File::Basename;
no warnings 'once';
foreach my $file (@ARGV) {
if (-e $file) {
my $data = process_file($file);
open(MYOUTPUTFILE, "+>$file.bak") or die "Error opening output file $!";
print MYOUTPUTFILE $data;
close(MYOUTPUTFILE);
if (compare($file, "$file.bak") != 0) {
print "Processed $file\n";
}
unlink $file;
rename "$file.bak", $file;
}
}
sub process_file {
my($filename) = "$_[0]";
open(MYINPUTFILE, "<$filename") or die "x $!";
my($data) = "";
while(<MYINPUTFILE>) {
s/\s*$//;
my($line) = $_;
if($line !~ /^\s*$/) {
$data = $data . "$line\n";
}
}
close(MYINPUTFILE);
chomp($data);
if ($data !~ /^\s*\/\*\*/) {
my $basename = basename($filename);
$data = "/**\n * \@file $basename\n * \@author Austen Adler (agadler)\n * TODO: Description\n */\n$data";
}
return $data;
}
#sub process_comment {
# # The data string
# my $data = $_[0];
# # Is the author line in there?
# my $author = 0;
# # Is the description there?
# my $desc = 0;
# # Is the first name there?
# my $fname = 0;
# # Are we still in the top comment area?
# my $incomment = 1;
# # If there is no top comment in the beginning, add the base comment
# if ($data !~ /^\s*\/\*\*/) {
# $data = "/**\n * \@file $filename\n * \@author Austen Adler (agadler)\n * TODO: Description\n */\n$data";
# $author = 1;
# $desc = 1;
# $fname = 1;
# } else {
# # Otherwise process an existing comment
# while ($incomment) {
# if (/\s*\*\//) {
# $incomment = 0;
# } elsif (/\s*\* \@author/) {
# } elsif (/\s*\* \@file $filename/) {
# } elsif (/\s*\* \@author/) {
# }
# }
#
# }
# return $data;
#}