| Category: | Utility Code |
| Author/Contact Info | pmonk4ever |
| Description: | As a Test Engineer, I am tasked with finding items buried in C or Ada Source Code, in order to prove that the development team utilized certain customer required data constants. The resultant code inspection can take several hours in doing a manual search, and because I am interested in spending time in other pursuits, I wanted an automatic solution... This bit of magic was inspired with help from Monks Grandfather and scorpio17 and this is my final solution! My only unaccomplished bit to this code is I wish I could ignore the @Constants that are in commented lines...where the line begins with a #... My attempts to filter out the commented lines resulted in all source lines being filtered out... Therefore, here is final_search.pl: the miracle of searching & printing source path, filename, and source code line where used. If you don't provide a directory path on the command line, it will default to the current directory. Please feel free to make use of this fine utility! Thanks to all the Monks who offered solutions to this problem! UPDATE I ran into some problems with the initial version of this utility during an actual run in the destination directory. So the code has been updated with suggestions from thezip and tested successfully! Thanks also to Anno and ikegami and several others in the Chatterbox for your excellent suggestions! Cheers! :D pmonk4ever
|
#!/bin/perl -w
use strict;
use File::Find;
use Time::localtime;
use Data::Dumper;
package main;
#####################################################################
# final_search.pl - the miracle of searching & printing source path,
# filename, and source code line where used.
#
# If you don't provide a directory path on the command line,
# it will default to the current directory.
#
#####################################################################
my @Constants = (
"fee",
"fie",
"foe",
"fum" );
my $day_of_year = localtime(time())->yday;
my $year = localtime(time())->year + 1900;
my $path = $ARGV[0] || "."; # use the current directory or supplied
+path
my $count = 0;
my $fname1 = "";
my $fname2 = "";
my $line = "";
my $search_string = "";
print "\n <<<<< Constants Search Script >>>>> \n\n";
print " >>> Today is the $day_of_year 'th day of $year <<< \n\n";
sub wanted {
my $fname1 = $File::Find::name;
return unless $fname1 =~ /\.[ch]$/;
push(@files, $fname1);
}
find(\&wanted, $path);
# print Dumper(\@files); # Uncomment to check out the array
for $fname2 (@files) {
open (INFILE, "$fname2") or warn "can't open file: $fname2 : $! ";
while (<INFILE>) {
$line = $_;
for $search_string (@Constants) {
if ($line =~ /\b$search_string\b/) {
# print the path/filename:line number - the code line
print "$fname2:$. - $line \n";
}
}
}
close (INFILE);
++$count;
}
print "\n <<<>>> Searched $count files <<<>>> \n";
print "\n";
# END - package main
|
|
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Utility for Finding Constants hidden in 'C' Source Code
by Anonymous Monk on Jan 10, 2008 at 17:40 UTC |