Reputation: 1471
I need to create a function pointer file. I am using perl for the same. My input is a list of function declarations. Below are some examples:
1.
char * cc_Hightor_ind(Closed* p_Closed);
to
char * (*cc_Hightor_ind)(Closed* p_Closed);
2.
char cc_Hightor_ind(Closed* p_Closed);
to
char (*cc_Hightor_ind)(Closed* p_Closed);
These are 2 usual cases where the function returns a pointer and value. I am unable to resolve on how to convert the function name to be preceded by (*.
My logic is on finding '_ind', I back-track to the start of the word and add (* . I am finding it hard to translate this to code. Please suggest the same. Currently I am using the regex to each datatype and adding adding (* to it which is a very lengthy process.
Thanks in advance
Upvotes: 0
Views: 253
Reputation: 91385
Update according to updated question:
while(<DATA>) {
chomp;
s/(\*?\s+)(\w+\s*\()/$1(*$2)/;
say;
}
__DATA__
char * cc_Hightor_ind(Closed* p_Closed);
char cc_Hightor_ind(Closed* p_Closed);
output:
char * (*cc_Hightor_ind)(Closed* p_Closed);
char (*cc_Hightor_ind)(Closed* p_Closed);
Upvotes: 1
Reputation: 1471
I have arrived at the following round-about solution :-(
my $line = "char * cc_Hightor_ind(Closed* p_Closed);";
# split with white-space as delimiter
my @splt = split (/\s/, $line);
$count = split(/\s/, $line);
#traverse through tokens
for ($i = 0; $i < $count; $i++) {
# If token has '_ind', then prepend '(*' and append ')' to it
if ((@splt[$i] =~ s/_ind/_ind\)/)) {
@splt[$i] = "\(* @splt[$i]\) ";
}
}
$line = join (" ", @splt);
print "$line\n";
#sample output
#D:\gsa\mytries\prl>getFptr.pl
#char * (* cc_Hightor_ind(Closed* p_Closed);
Upvotes: 0
Reputation: 20280
Sorry, I don't have time for a full answer, but here is some discussion about parsing C in Perl. http://www.perlmonks.org/?node_id=746341 Perhaps it can give you some ideas.
Upvotes: 1
Reputation: 126722
Akin to processing XML
with regular expressions, this will never be 100% correct unless you involve a proper C
parser. But you can write something that will be correct for most common situations, and perhaps always correct for a given sample of C code - especially if that code is auto-generated.
Try this for an initial attempt, which includes both code samples that you have posted. If you want us to help you more then please post a comprehensive example of what you are processing
use strict;
use warnings;
while (<DATA>) {
chomp;
(my $ptr = $_) =~ s/( \w+ \s* )\(/(*$1)(/x ;
print $ptr, "\n";
}
__DATA__
char * cc_Hightor_ind(Closed* p_Closed);
char cc_Hightor_ind(Closed* p_Closed);
OUTPUT
char * (*cc_Hightor_ind)(Closed* p_Closed);
char (*cc_Hightor_ind)(Closed* p_Closed);
Upvotes: 2