#!/usr/bin/perl # # This script provides search and display functions for maps # It should probably be split into three seperate files. # BEGIN { require "files.pl"; # import path info require "fields.pl"; # load form field associations } # # loadDatabase # Args: $comparefunc comparision function reference (takes entry hash returns bit vector) # $outputfunc output function reference (takes entry hash returns nothing) # # Read database file specified in $database # Parse entries and call &comparefunc # if comparefunc returns 1-bit add hash to return array # if comparefunc returns 2-bit call output function # sub loadDatabase { my ($databasefile, $comparefunc, $outputfunc) = @_; # Load args my @database; @database = (); # Return value (array of hash refs) # open database file or exit open( DATABASEFILE, "<$databasefile" ) or die( "Unable to open database '$databasefile'." ); # First line of database contains the field names, database uses | as field seperator my $fields; $fields = readline( *DATABASEFILE ); # First database line defines fields chomp( $fields ); # remove eol characters if( $fields =~ m/^\#/ ) # Line starts the # { $fields = substr( $fields, 1 ); # remove first character } # print $fields, "\n" if( $debug ); my @fields; @fields = split( /\|/, $fields ); # parse line into array of fields # print @fields, "\n" if( $debug ); my $line; while( $line = readline( *DATABASEFILE ) ) # Read every other line from database { chomp( $line ); # strip EOL my @line; @line = split( /\|/, $line ); # parse into array my $entry; $entry = {}; # Construct a new empty hash my $cnt; $cnt = 0; # Array index foreach $fn (@fields) # for each index field { $$entry{$fn} = $line[$cnt++]; # Add hash entry for field name -> database entry value # print "$fn -> $$entry{$fn} \n" if( $debug ); } # print $comparefunc if( $debug ); $cmp = &{$comparefunc}($entry); # Perform the compare function if( $cmp & 1 ) # Compare returns 1-bit { push( @database, $entry ); # Add entry to return array } if( $cmp & 2 ) # compare returns 2-bit { &{$outputfunc}($entry); # Perform output function } # print @line, "\n" if( $debug ); } return @database; # return array of hash refs that compare identified } # # Arg: Hash table for database entry # Prints the hash name value pairs. # sub showEntry(\%) { my ($entry) = @_; # Get args foreach $key (keys %$entry) # For each hash key { print "$key -> $$entry{$key} \n" if( $debug ); # Print name value pair } } # # Arg: Hash table for database entry # Prints the name. # sub showName(\%) { my ($entry) = @_; # Get args if( $$entry{$MAP_NAME_FIELD} ) { print "$$entry{$MAP_NAME_FIELD} \n" if( $debug ); # Print name } } # # Arg: Hash Table for database entry # Returns 2-bit | 1-bit # Indicates entry should be output and returned # sub compareTrue(\%) { my ($entry) = @_; # load args return 1 | 2; # return 1-bit | 2-bit } # # Creates the table for database entries. # sub showMapInfoHeader { print <<__END_HTML__;
| \n"; print '\n"; print " | \n"; print "
| ', "\n"; print '\n"; print " |
" if( $debug ); # preformat for debug information
print "CGIs = ", join( ",", $cgi->param() ), "\n" if( $debug );
$search = $cgi->param('search'); # Get form search info (if any)
$match = $cgi->param('match'); # Get form search info (if any)
# print "Search=$search to match $match\n";
if( $search ) # if search specified but not function found
{
$comparefunc = $searches{$search}; # lookup search function
if( !$comparefunc )
{
# print "Creating compare $search=$match...\n" if( $debug );
$comparefunc = sub # Create exact match search for $search db field
{
my ($entry) = @_;
return (lc($$entry{$search}) eq lc($match)) ? 1|2 : 0;
};
}
}
elsif( !($search) ) # if no search specified
{
# print "No Search. All show.\n" if( $debug );
$comparefunc = \&compareTrue; # Display all records
$search = 'all';
}
else
{
# print "Search function specified. $comparefunc\n";
}
@database = loadDatabase # Perform search and list
(
"$DATA_DIRECTORY/$database",
$comparefunc,
\&showName
);
print "" if( $debug );
# show count of records found
my $rec_count = $#database + 1;
print "There are $rec_count entries matching your search.
"; # get browse data my $startindex = $cgi->param('start'); # start index $startindex = 0 unless( $startindex ); my $count = $cgi->param('count'); # number of entries to display $count = 10 if( !($count) ); # reset count if not specified $count *= 2 if( $cgi->param('more') ); # increase size if more clicked $count = 50 if( $count > 50 ); # Limit count to 50 $startindex += $count if( $cgi->param('next') ); # Set to next page if next $startindex -= $count if( $cgi->param('prev') ); # set to prev page if prev $startindex = 0 if( $startindex < 0 ); # set start to 0 if negative my $endindex = $count + $startindex; # compute last scm index $endindex = $rec_count if( $endindex > $rec_count ); # set end to last if too many # $endindex = 50 if( $endindex > 50 ); # limit number of records in one search $startindex = $endindex if( $endindex < $startindex ); #clip start to end if( $startindex < $endindex ) { print "Showing ($startindex - $endindex)...