............
########### NEED TO PUT ARGUMENTS HERE AS COMMENT #########
eErrorT ChainCtrlInitChains(ChainCtrlT* pChainCtrl,
char* name,
int instance,
void* pOwner,
)
{
....
}
.........
我想提取并将其置于函数定义之上作为注释。有许多类似的函数定义。
open(my $FILE1, "< a.c") or die $!;
@arr = <$FILE1>;
foreach(@arr){
if($_ =~ /^ \S+ \s+ \S+ \s* \( (.+?) \) /xsmg) {
my $arg = $1;
my @arr = map /(\w+)$/, split /\W*?,\W*/, $arg;
print @temp = map ' * @param[in/out] '."$_\n", @arr
unless $_ =~ /;\s*$/;
}
}
当我使用$ str时,它有效,但是我不能将参数拼接为函数定义上方的注释。
$str = <$FILE1>;
答案 0 :(得分:2)
你走了:
use File::Copy;
open my $FILE,'<','a.c' or die "open failed: $!\n";
$file_slurp = do { local $/;<$FILE>};
$file_slurp =~ s{ ^ ( \w+ \s+ \w+ \s* \( (.+?) \) )}{&print_args($2,$1)}xmesg;
close($FILE) or die "Couldn't close file: $!\n";
copy "a.c","a.c.bak" or die "Copy failed: $!\n";
open my $NEW_FILE,'>','a.c' or die "Truncating a.c failed: $!\n";
print $NEW_FILE $file_slurp and unlink "a.c.bak";
sub print_args {
($args,$proto) = @_;
@comments = map { ' * @param[in/out] '."$_" } split /\s*(?:^|,)\s*/,$args;
return join "\n",(@comments,$proto)
}
首先通过删除unlink
来测试代码,以便将源的备份副本保留在磁盘上。当你确信它能做你想要的东西时,你可以放回取消链接,这样你的原始文件似乎就好了。