Test

From Phyloinformatics
Jump to: navigation, search

<perl>#!/usr/bin/perl

  1. Simple Search Version 1.0 #
  2. Copyright 1996 Matt Wright mattw@scriptarchive.com #
  3. Created 12/16/95 Last Modified 12/16/95 #
  4. Scripts Archive at: http://www.scriptarchive.com/ #
  5. COPYRIGHT NOTICE #
  6. Copyright 1996 Matthew M. Wright All Rights Reserved. #
  7. #
  8. Simple Search may be used and modified free of charge by anyone so long as #
  9. this copyright notice and the comments above remain intact. By using this #
  10. code you agree to indemnify Matthew M. Wright from any liability that #
  11. might arise from it's use. #
  12. #
  13. Selling the code for this program without prior written consent is #
  14. expressly forbidden. In other words, please ask first before you try and #
  15. make money off of my program. #
  16. #
  17. Obtain permission before redistributing this software over the Internet or #
  18. in any other medium. In all cases copyright and header must remain intact.#
  19. Define Variables #

$basedir = '/home/scriptarchive/'; $baseurl = 'http://www.scriptarchive.com/'; @files = ('*.html'); $title = "Matt's Script Archive"; $title_url = 'http://www.scriptarchive.com/'; $search_url = 'http://www.scriptarchive.com/demos/search/search.html';

  1. Done #
  1. Parse Form Search Information

&parse_form;

  1. Get Files To Search Through

&get_files;

  1. Search the files

&search;

  1. Print Results of Search

&return_html;


sub parse_form {

  # Get the input
  read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  # Split the name-value pairs
  @pairs = split(/&/, $buffer);
  foreach $pair (@pairs) {
     ($name, $value) = split(/=/, $pair);
     $value =~ tr/+/ /;
     $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
     $FORM{$name} = $value;
  }

}

sub get_files {

  chdir($basedir);
  foreach $file (@files) {
     $ls = `ls $file`;
     @ls = split(/\s+/,$ls);
     foreach $temp_file (@ls) {
        if (-d $file) {
           $filename = "$file$temp_file";
           if (-T $filename) {
              push(@FILES,$filename);
           }
        }
        elsif (-T $temp_file) {
           push(@FILES,$temp_file);
        }
     }
  }

}

sub search {

  @terms = split(/\s+/, $FORM{'terms'});
  foreach $FILE (@FILES) {
     open(FILE,"$FILE");
     @LINES = <FILE>;
     close(FILE);
     $string = join(' ',@LINES);
     $string =~ s/\n//g;
     if ($FORM{'boolean'} eq 'AND') {
        foreach $term (@terms) {
           if ($FORM{'case'} eq 'Insensitive') {
              if (!($string =~ /$term/i)) {
                 $include{$FILE} = 'no';
 		  last;
              }
              else {
                 $include{$FILE} = 'yes';
              }
           }
           elsif ($FORM{'case'} eq 'Sensitive') {
              if (!($string =~ /$term/)) {
                 $include{$FILE} = 'no';
                 last;
              }
              else {
                 $include{$FILE} = 'yes';
              }
           }
        }
     }
     elsif ($FORM{'boolean'} eq 'OR') {
        foreach $term (@terms) {
           if ($FORM{'case'} eq 'Insensitive') {
              if ($string =~ /$term/i) {
                 $include{$FILE} = 'yes';
                 last;
              }
              else {
                 $include{$FILE} = 'no';
              }
           }
           elsif ($FORM{'case'} eq 'Sensitive') {
              if ($string =~ /$term/) {

$include{$FILE} = 'yes';

                 last;
              }
              else {
                 $include{$FILE} = 'no';
              }
           }
        }
     }
     if ($string =~ /<title>(.*)<\/title>/i) {
        $titles{$FILE} = "$1";
     }
     else {
        $titles{$FILE} = "$FILE";
     }
  }

}

sub return_html {

  print "Content-type: text/html\n\n";
  print "<html>\n <head>\n  <title>Results of Search</title>\n </head>\n";
print "<body>\n
\n

Results of Search in $title

\n
\n"; print "Below are the results of your Search in no particular order:


<p>\n"; print "
    \n"; foreach $key (keys %include) { if ($include{$key} eq 'yes') { print "
  • <a href=\"$baseurl$key\">$titles{$key}</a>\n"; } } print "
\n"; print "
\n";
  print "Search Information:<p>\n";
print "
    \n"; print "
  • Terms: "; $i = 0; foreach $term (@terms) { print "$term"; $i++; if (!($i == @terms)) { print ", "; } } print "\n"; print "
  • Boolean Used: $FORM{'boolean'}\n"; print "
  • Case $FORM{'case'}\n"; print "


<P>\n"; print "
    \n
  • <a href=\"$search_url\">Back to Search Page</a>\n"; print "
  • <a href=\"$title_url\">$title</a>\n"; print "
\n"; print "
\n";
  print "Search Script written by Matt Wright and can be found at <a href=\"http://www.scriptarchive.com/\">Matt's Script Archive</a>\n";
  print "</body>\n</html>\n";
}</perl>