Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Utility for Finding Constants hidden in 'C' Source Code

by pmonk4ever (Friar)
on Sep 22, 2007 at 01:09 UTC ( #640465=sourcecode: print w/ replies, xml ) Need Help??

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

Comment on Utility for Finding Constants hidden in 'C' Source Code
Download Code
Re: Utility for Finding Constants hidden in 'C' Source Code
by Anonymous Monk on Jan 10, 2008 at 17:40 UTC
    I will be highly thankful if u could please help me with the code written in C for finding the line numbers for lines containing constants in a C source file. I can be reached at debu_mckv@yahoo.co.in Thanks & Regards, DP

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://640465]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2014-08-30 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls