#!/usr/local/bin/perl # $Id: search-1.cgi,v 1.3 2006/02/04 07:11:40 68user Exp $ use strict; require 'jcode.pl'; my $start_time = times(); $|=1; my @keywords; # 検索対象となるキーワード # 引数解析 foreach ( split(/&/, $ENV{QUERY_STRING}) ){ my ($name, $value) = split(/=/, $_); if ( $name eq 'keyword' ){ $value =~ s/%([0-9a-fA-F][0-9a-fA-F])/pack("C", hex($1))/eg; &jcode::convert(\$value, 'euc'); # 前後の空白を削除 my $jisx0208_space = ' '; $value =~ s/^(\s|$jisx0208_space)+//; $value =~ s/(\s|$jisx0208_space)+$//; foreach my $keyword (split(/\++/, $value)){ push(@keywords, $keyword); } } } print "Content-type: text/html; charset=EUC-JP\n\n"; print qq(\n); print "

全文検索その1: index 関数で各キーワードごとに各行を検索

\n"; if ( scalar(@keywords) == 0 ){ print "検索キーワードが入力されていません。\n"; print "\n"; exit 1; } printf("

FreeBSD-users-jp を「%s」で検索します。

\n", escape(join(' ', @keywords))); my $freebsd_users_jp_url = 'http://home.jp.freebsd.org/cgi-bin/showmail/FreeBSD-users-jp'; # メールのファイル名を @files に格納 my $maildir = '../../freebsd-users-jp'; opendir(DIR, $maildir); my @files = grep(/^[0-9]+$/, readdir(DIR)); my $found_filenum = 0; # マッチしたファイル数 my $max_found_filenum = 100; # これ以上マッチしたら検索を打ち切る foreach my $filename (sort {$a <=> $b} @files){ my @match_line; # マッチした行を格納するための配列 my %already_found; # 発見済キーワードを登録しておくハッシュ my $all_found_flg = 0; # 全キーワードを発見したら 1 に open(IN, "$maildir/$filename"); LINELOOP: while (my $line=){ chomp $line; foreach my $keyword (@keywords){ # 狙いのキーワードがこのファイル内でみつかっておらず、 # なおかつこの行で検索対象文字列が見付かった if ( ! defined $already_found{$keyword} && index($line, $keyword) >= 0 ){ push(@match_line, $line); $already_found{$keyword} = 1; # 全部のキーワードが見付かったら、もうこのファイルを調べる必要はない if ( scalar(keys %already_found) == scalar(@keywords) ){ $all_found_flg = 1; last LINELOOP; } } } } close(IN); # 全てのキーワードが見付かった if ( $all_found_flg ){ print qq($filename
\n); foreach (@match_line){ printf("     %s
\n", escape($_)); } $found_filenum++; if ( $found_filenum == $max_found_filenum ){ last; } } } print "

\n"; print "$found_filenum 件見つかりました。\n"; if ( $found_filenum == $max_found_filenum ){ print "$max_found_filenum 件見つかったので、検索を打ち切りました。"; } print "

\n"; printf("ユーザモード CPU 消費時間: %.2f秒\n", times()-$start_time); print "\n"; exit 0; #----------------------------------------- sub escape { my ($str) = @_; $str =~ s/&/&/g; $str =~ s//>/g; $str =~ s/ / /g; return $str; }