How to find a specific string in a .c file using the perl api of (Scitools) Understand?

300 views Asked by At

I'm trying to analyse a code base (many .c files) by writing Perl script using (Scitools) Understand's Perl API.

I'm aware that list of variables, functions etc. are easy to obtain for a given .c file.

Probably someone who has worked with Understand Perl API could point me in the right direction to search for a specific string that is present in many lines of a file? and possible return the line number?

1

There are 1 answers

0
ikegami On

Sounds like you simply want

  use Understand qw( );

  my $db = Understand::open("test.udb");

  # Find all 'File' entities that match test*.cpp
  for my $file_ent ($db->lookup("test*.cpp","File")) {
       my $file_contents = $file_ent->contents();
       printf "File %s matches", $file_ent->name
          if $file_contents =~ /pattern/;
  }