reformat spec openflow.h parser

This commit is contained in:
Brandon Heller committed 2008-12-01 04:14:54 -08:00
1 parent 84d3f43c7b
commit 7da34aa10c
1 file changed
+90 -80
+90 -80
View File
@@ -3,105 +3,115 @@
use strict;
sub handle_comment {
my ($comment) = @_;
my $next_line;
do {
$next_line = <INFILE>;
chomp $next_line;
$comment .= $next_line."\n";
} until ($next_line =~ m/\*\//);
my ($comment) = @_;
return $comment;
my $next_line;
do {
$next_line = <INFILE>;
chomp $next_line;
$comment .= $next_line . "\n";
} until ( $next_line =~ m/\*\// );
return $comment;
}
sub handle_define {
my ($var, $val) = @_;
open(OUTFILE, ">define/$var");
print OUTFILE $val;
close(OUTFILE);
my ( $var, $val ) = @_;
open( OUTFILE, ">define/$var" );
print OUTFILE $val;
close(OUTFILE);
}
sub handle_multiline {
my ( $dir, $filename, $line_orig, $last_comment) = @_;
open(OUTFILE, ">$dir/$filename");
#print OUTFILE "\\scriptsize\n";
print OUTFILE "\\begin{footnotesize}\n";
print OUTFILE "\\begin{verbatim}\n";
#print OUTFILE "\\begin{lstlisting}[frame=htb]{$filename}\n";
print OUTFILE $last_comment;
print OUTFILE $line_orig;
my $next_line;
do {
my ( $dir, $filename, $line_orig, $last_comment ) = @_;
$next_line = <INFILE>;
chomp $next_line;
open( OUTFILE, ">$dir/$filename" );
print OUTFILE $next_line."\n";
#print OUTFILE "\\scriptsize\n";
print OUTFILE "\\begin{footnotesize}\n";
print OUTFILE "\\begin{verbatim}\n";
} until ($next_line eq '};');
#print OUTFILE "\\begin{lstlisting}[frame=htb]{$filename}\n";
print OUTFILE $last_comment;
print OUTFILE $line_orig;
my $next_line;
do {
# add assertion line for structs
if ($dir eq 'struct') {
$next_line = <INFILE>;
print OUTFILE $next_line;
}
print OUTFILE "\\end{verbatim}\n";
#print OUTFILE "\\end{lstlisting}\n";
print OUTFILE "\\end{footnotesize}\n";
close(OUTFILE);
$next_line = <INFILE>;
chomp $next_line;
print OUTFILE $next_line . "\n";
} until ( $next_line eq '};' );
# add assertion line for structs
if ( $dir eq 'struct' ) {
$next_line = <INFILE>;
print OUTFILE $next_line;
}
print OUTFILE "\\end{verbatim}\n";
#print OUTFILE "\\end{lstlisting}\n";
print OUTFILE "\\end{footnotesize}\n";
close(OUTFILE);
}
#----------------------------------------
use File::Path;
foreach my $type ('enum', 'struct', 'define') {
if (-d $type) {
rmtree ($type);
}
mkdir $type;
foreach my $type ( 'enum', 'struct', 'define' ) {
if ( -d $type ) {
rmtree($type);
}
mkdir $type;
}
open(INFILE, "<../../include/openflow/openflow.h");
open( INFILE, "<../../include/openflow/openflow.h" );
my $last_comment;
while(<INFILE>)
{
# Good practice to store $_ value because
# subsequent operations may change it.
my($line) = $_;
my $line_orig = $line;
my @line_split = split ' ',$line;
if (not defined($line_split[0])) {
$last_comment = '';
}
# Handle single-line comment
elsif ($line =~ m/^\/\*.*\*\//) {
$last_comment = $line;
#print $last_comment;
}
# Handle multi-line comment
elsif ($line =~ m/^\/\*/) {
$last_comment = handle_comment($line_orig);
#print $last_comment;
}
# Handle define
elsif ($line_split[0] eq '#define') {
handle_define($line_split[1],$line_split[2]);
$last_comment = '';
}
# Handle enum
elsif ($line_split[0] eq 'enum') {
handle_multiline('enum', $line_split[1], $line_orig, $last_comment);
$last_comment = '';
}
# Handle struct
elsif ($line_split[0] eq 'struct') {
handle_multiline('struct', $line_split[1], $line_orig, $last_comment);
$last_comment = '';
}
while (<INFILE>) {
# Good practice to store $_ value because
# subsequent operations may change it.
my ($line) = $_;
my $line_orig = $line;
my @line_split = split ' ', $line;
if ( not defined( $line_split[0] ) ) {
$last_comment = '';
}
# Handle single-line comment
elsif ( $line =~ m/^\/\*.*\*\// ) {
$last_comment = $line;
#print $last_comment;
}
# Handle multi-line comment
elsif ( $line =~ m/^\/\*/ ) {
$last_comment = handle_comment($line_orig);
#print $last_comment;
}
# Handle define
elsif ( $line_split[0] eq '#define' ) {
handle_define( $line_split[1], $line_split[2] );
$last_comment = '';
}
# Handle enum
elsif ( $line_split[0] eq 'enum' ) {
handle_multiline( 'enum', $line_split[1], $line_orig, $last_comment );
$last_comment = '';
}
# Handle struct
elsif ( $line_split[0] eq 'struct' ) {
handle_multiline( 'struct', $line_split[1], $line_orig, $last_comment );
$last_comment = '';
}
}
print "completed\n";
print "completed\n";