# Transform K&R C function definitions into ANSI equivalent.
#
# Author: Paul Marquess
# Version: 1.0
# Date: 3 October 2006
# TODO
#
# Asumes no function pointer parameters. unless they are typedefed.
# Assumes no literal strings that look like function definitions
# Assumes functions start at the beginning of a line
use strict;
use warnings;
local $/;
$_ = <>;
while (s/^
( # Start $1
( # Start $2
.*? # Minimal eat content
( ^ \w [\w\s\*]+ ) # $3 -- function name
\s* # optional whitespace
) # $2 - Matched up to before parameter list
\( \s* # Literal "(" + optional whitespace
( [^\)]+ ) # $4 - one or more anythings except ")"
\s* \) # optional whitespace surrounding a Literal ")"
) # Remember to $1
//xsom
)
{
my $all = $1 ;
my $prefix = $2;
my $param_list = $4 ;
my $params = $5;
$param_list =~ s/^\s+//;
$param_list =~ s/\s+$//;
my $i = 0 ;
split /\s*,\s*/, $param_list;
foreach my $p (@params)
{
if ($p =~ /,/)
{
$first =~ s/^\s*//;
my $type = $1 ;
}
else
{
$p =~ s/^\s+//;
}
}
@outParams ;
@outParams ;
print $prefix ;
print "{" ;
}
# Output any trailing code.
print ;
exit 0;
sub StripComments
{
# Strip C & C++ coments
# From the perlfaq
$_[0] =~
s{
/\* ## Start of /* ... */ comment
[^*]*\*+ ## Non-* followed by 1-or-more *'s
(
[^/*][^*]*\*+
)* ## 0-or-more things which don't start with /
## but do end with '*'
/ ## End of /* ... */ comment
| ## OR C++ Comment
// ## Start of C++ comment //
[^\n]* ## followed by 0-or-more non end of line characters
| ## OR various things which aren't comments:
(
" ## Start of " ... " string
(
\\. ## Escaped char
| ## OR
[^"\\] ## Non "\
)*
" ## End of " ... " string
| ## OR
' ## Start of ' ... ' string
(
\\. ## Escaped char
| ## OR
[^'\\] ## Non '\
)*
' ## End of ' ... ' string
| ## OR
. ## Anything other char
)
}{$2}gxs;
}