Ad-vic
Ad-vic

Reputation: 529

Output only function definition after checking

I have a function name "ChainCtrlBuildChain" as text in a file(FILE).

A c file(FILE1) with content

VideoChainT* ChainCtrlBuildChain(ChainCtrlT* pChainCtrl, char* pChainName, ChainDefT* pDef)
            {
          ...
            {
 ModTrace((ModT*) pChainCtrl, "ChainCtrlBuildChain: ERROR, chain init failure [chain: %4.4s inst: %d] [err: %d]\n",
                        ...
            }

My code:

my @Array=<FILE>;
my @Array1=<FILE1>;

foreach my $text (@Array1){
if (index($text, $Array[0]) !=-1)
{
print "$text \n";
}
}

Gives this output:

VideoChainT* ChainCtrlBuildChain(ChainCtrlT* pChainCtrl, char* pChainName, ChainDefT* pDef)
ModTrace((ModT*) pChainCtrl, "ChainCtrlBuildChain: ERROR, chain init failure [chain: %4.4s inst: %d] [err: %d]\n",

I want this instead:

VideoChainT* ChainCtrlBuildChain(ChainCtrlT* pChainCtrl, char* pChainName, ChainDefT* pDef)

I have to make it generalized as input files are by users and not fixed.

I was thinking

if (index($text, $Array[0]) != -1 && index('**WHAT SHOULD I WRITE SO THAT IT SKIPS ANY ARGUMENTS PRESENT AND JUST CHECKS WHETHER ITS A FUNCTION DEFINITION OR NOT**')!= -1) 

Upvotes: 0

Views: 71

Answers (1)

marderh
marderh

Reputation: 1256

In case you're content using your function name plus ( sufficient for a function definition this should work:

#!/usr/bin/perl

use warnings;
use strict;

my $function_file = 'functions.txt';
my $src_file = 'src.c';

open(my $ff,'<',$function_file) or die "Cant open $function_file: $!\n";
open(my $sf,'<',$src_file) or die "Cant open $src_file: $!\n";

my @array = <$ff>;
my @array2 = <$sf>;

close($ff);
close($sf);

foreach my $func (@array){
    chomp $func;
    foreach my $src_line (@array2){
        if ($src_line =~ /$func\(/){
            print $src_line;
        }
    }
}

BTW: you'll have less pain in your perl-life if you use 3-arg open!

Upvotes: 1

Related Questions