#!/usr/bin/perl # add a glossary item require "./cgi-lib.pl"; $DataFile = '../data/cgIndex.dat'; $OldDataFile = '../data/cgIndex.old.dat'; $SpecFile = '../data/qqq.dat'; $lsp = 'ZZ'; $fsp = 'QQ'; $ArialFont = 'FACE="Arial, Helvetical, sans-serif"'; &ReadParse(); $IN_ETitle = $in{'ETitle'}; $IN_CTitle = $in{'CTitle'}; $IN_Key = $in{'Key'}; $IN_Def = $in{'Def'}; # store special case: CTitle ends with a 'Q' # first get existing cases %Special = (); if (open(SPEC, $SpecFile)) { while ($SpecItem = ) { chomp $SpecItem; $Special{$SpecItem} = '1'; } close SPEC; } if ($IN_CTitle =~ /Q$/) { if (!defined($Special{$IN_Key})) { # add this case if (open(SPEC, ">>$SpecFile")) { print SPEC $IN_Key, "\n"; close SPEC; } } } print <<_ENDPRINT; Content-type: text/html Buddhist Door Glossary New Entry

Buddhist Door Glossary
New Entry

_ENDPRINT # build an index of keys for checking duplicates # also write to old data file $Backup = 0; if (open (BACKUP, ">$OldDataFile")) { $Backup = 1; } %ETitles = (); if (open (GLOS, $DataFile)) { while ($Line = ) { chomp $Line; ($ETitle, $Key, $CTitle, $Def) = split /$fsp/, $Line, 4; # special case for 'Contemplation', key 'contempl' # the last Chinese char ends with a 'Q' if (defined($Special{$Key})) { $CTitle .= 'Q'; $Def = substr($Def, 1); } # end special case $ETitles{$Key} = $ETitle; if ($Backup) { print BACKUP "$Line\n"; } } # while close GLOS; } else { print <<_ENDPRINT; Sorry, the glossary is not available at this time.
Please try later.
Return to main menu page.
_ENDPRINT exit; } # if open if ($Backup) { close BACKUP; } # verify input key # flag error if key contains non-word characters # or if key duplicates an existent key $ErrNonWord = 0; if ($IN_Key =~ /\W/) { $ErrNonWord = 1; } $ErrDuplKey = 0; if (defined $ETitles{$IN_Key}) { $ErrDuplKey = 1; } if ($ErrNonWord || $ErrDuplKey || defined $in{'Preview'}) { # preview print <<_ENDPRINT;

Title in English:
Title in Chinese:
Key:

_ENDPRINT if ($ErrNonWord) { print <<_ENDPRINT; *** "$IN_Key" contains non-alphanumeric characters. Please re-enter. *** _ENDPRINT } if ($ErrDuplKey) { print <<_ENDPRINT; *** "$IN_Key" is already used by "$ETitles{$IN_Key}". Please re-enter. *** _ENDPRINT } print <<_ENDPRINT;

Description:

$IN_ETitle $IN_CTitle
$IN_Def

_ENDPRINT if (!$ErrNonWord && !$ErrDuplKey) { print <<_ENDPRINT; _ENDPRINT } print <<_ENDPRINT;
_ENDPRINT exit; } # add mode # convert line breaks to $lsp $IN_Def =~ s/\r\n/\n/g; $IN_Def =~ s/\n/$lsp/g; $IN_Def .= "

\n"; if (open (GLOS, ">>$DataFile")) { $Line = join $fsp, $IN_ETitle, $IN_Key, $IN_CTitle, $IN_Def; print GLOS $Line; close GLOS; print <<_ENDPRINT; "$IN_ETitle $IN_CTitle" is successfully added to the glossary. _ENDPRINT } else { print <<_ENDPRINT; Sorry, the glossary is not available at this time.
Please try later.
_ENDPRINT } # if open print <<_ENDPRINT;

Add another glossary title.
Return to main menu page.
_ENDPRINT exit;