+ return dbr_filename(datadir(), $ocean);
+}
+
+sub dbw_connect ($) {
+ my ($ocean) = @_;
+ my $fn= dbw_filename($ocean);
+ return db_connect_core($fn);
+}
+
+sub to_json_shim ($) {
+ my ($obj) = @_;
+ # In JSON.pm 2.x, jsonToObj prints a warning to stderr which
+ # our callers don't like at all.
+ if ($JSON::VERSION >= 2.0) {
+ return to_json($obj);
+ } else {
+ return objToJson($obj);
+ }
+}
+
+sub to_json_protecttags ($) {
+ my ($v) = @_;
+ my $j= to_json_shim($v);
+ $j =~ s,/,\\/,g;
+ return $j;
+}
+
+sub meta_prettyprint_age ($$$) {
+ my ($age,$floor,$plus) = @_;
+ return <<END;
+ $age < 60 ? 'less than a minute' :
+ $age < 60*2 ? '1 minute' :
+ $age < 3600*2 ? $floor ($age/60) $plus' minutes' :
+ $age < 86400*2 ? $floor ($age/3600) $plus ' hours' :
+ $floor ($age/86400) $plus ' days';
+END
+};
+
+BEGIN { eval '
+ sub prettyprint_age ($) {
+ my ($age) = @_;
+ '.meta_prettyprint_age('$age','floor','.').'
+ };
+ 1;
+' or die "$@";
+}
+
+
+sub dbw_lookup_string ($$$$$$$$) { # => ( $emsg, @dbresults )
+ my ($each,
+ $sth, $stmt_nqs, $abbrev_initials, $maxambig,
+ $em_nomatch, $em_manyambig, $emf_ambiguous) = @_;
+
+ $each =~ s/^\s*//; $each =~ s/\s*$//; $each =~ s/\s+/ /g;
+ my %m;
+ my $results;
+ my @pats= ("$each", "$each \%", "$each\%", "\%$each\%");
+ if ($abbrev_initials) {
+ push @pats, join ' ', map { "$_%" } split //, $each;
+ }
+ foreach my $pat (@pats) {
+ $sth->execute(($pat) x $stmt_nqs);
+ $results= $sth->fetchall_arrayref();
+ last if @$results==1;
+ $m{ $_->[0] }=1 for @$results;
+ $results= undef;
+ }
+ if (!$results) {
+ if (!%m) {
+ return $em_nomatch;
+ } elsif (keys(%m) > $maxambig) {
+ return $em_manyambig;
+ } else {
+ return $emf_ambiguous->($each, join(', ', sort keys %m));
+ }
+ }
+ return (undef, @{ $results->[0] });
+}
+
+sub expected_error ($) {
+ my $r= { Emsg => $_[0] };
+ bless $r, 'CommodsWeb::ExpectedError';
+ die $r;
+}
+
+sub printable ($) { # printable($m) where $m is the Mason request object
+ my ($m) = @_;
+ my $a= scalar $m->caller_args(-1);
+ foreach my $t (qw(pdf ps html pdf2 ps2)) {
+ return $t if $a->{"printable_$t"};
+ }
+ return 0;
+}
+
+sub tr_datarow_s ($$) {
+ my ($m, $lineno) = @_;
+ $lineno &= 1;
+ if (!printable($m)) {
+ return "<tr class=\"datarow$lineno\">";
+ } else {
+ return "<tr bgcolor=\"".
+ ($lineno ? "#ffffff" : "#e3e3e3" ).
+ "\">";
+ }
+}
+
+sub tr_datarow ($$) {
+ my ($m, $lineno) = @_;
+ $m->print(tr_datarow_s($m, $lineno));
+}
+
+sub escerrq ($) {
+ return '"'.CGI::escapeHTML($_[0]).'"';
+ # Prettier qotes as below are not in HTML 3.2:
+# return '“'.CGI::escapeHTML($_[0]).'”';
+# return '‘'.CGI::escapeHTML($_[0]).'’';
+}
+
+package CommodsWeb::ExpectedError;
+
+sub emsg ($) {
+ my ($self) = @_;
+ return $self->{Emsg};