#lib

sub error {
	$_ = shift;
	print "Content-type: text/html\n\n";
	print "Error:<br><hr><br>\n";
	print "$_";
	exit;
}

sub mysort {
	lc($b) <=> lc($a);
}

sub mysortrev {
	lc($a) <=> lc($b);
}

sub readparse {
	local (*in) = @_ if @_;
	local ($i, $key, $val);
	
	# Read in text
	if (&methget) {
		$in = $ENV{'QUERY_STRING'};
		} elsif (&methpost) {
		read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
	}
	
	@in = split(/[&;]/,$in);
	
	foreach $i (0 .. $#in) {
		# Convert plus's to spaces
		$in[$i] =~ s/\+/ /g;
		
		# Split into key and value.
		($key,$val) = split(/=/,$in[$i],2); # splits on the first =.
		
		# Convert %XX from hex numbers to alphanumeric
		$key =~ s/%(..)/pack("c",hex($1))/ge;
		$val =~ s/%(..)/pack("c",hex($1))/ge;
		
		# Associate key and value
		$in{$key} .= "\0" if (defined($in{$key})); # \0 is the multiple seperator
		$in{$key} .= $val;
		
	}
	
	return scalar(@in);
}

sub methget {
	return ($ENV{'REQUEST_METHOD'} eq "GET");
}

sub methpost {
	return ($ENV{'REQUEST_METHOD'} eq "POST");
}

1;