X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.db-test.git;a=blobdiff_plain;f=pctb%2FCommods.pm;fp=pctb%2FCommods.pm;h=7d9a0beb76dabe5bdedcdd69ced7ed2ea1b68351;hp=5eb9c7bba749cb1a09888c0b32c9bd9608cae499;hb=9d525e44bd9642a72eb7acd81bd9956b014e3872;hpb=dd3abc97940e71298a764e8c5c0d0ce94e052ca6 diff --git a/pctb/Commods.pm b/pctb/Commods.pm index 5eb9c7b..7d9a0be 100644 --- a/pctb/Commods.pm +++ b/pctb/Commods.pm @@ -13,8 +13,9 @@ BEGIN { @ISA = qw(Exporter); @EXPORT = qw(&parse_masters %oceans %commods %clients &parse_pctb_commodmap %pctb_commodmap @pctb_commodmap - &get_our_version + &get_our_version &check_tsv_line &pipethrough_prep &pipethrough_run + &pipethrough_run_along &pipethrough_run_finish &pipethrough_run_gzip &cgipostform); %EXPORT_TAGS = ( ); @@ -128,21 +129,33 @@ sub pipethrough_prep () { my $tf= IO::File::new_tmpfile() or die $!; return $tf; } - -sub pipethrough_run ($$$@) { + +sub pipethrough_run_along ($$$@) { my ($tf, $childprep, $cmd, @a) = @_; $tf->flush or die $!; $tf->seek(0,0) or die $!; - my $child= open GZ, "-|"; defined $child or die $!; + my $fh= new IO::File; + my $child= $fh->open("-|"); defined $child or die $!; if (!$child) { open STDIN, "<&", $tf; &$childprep() if defined $childprep; exec $cmd @a; die $!; } + return $fh; +} +sub pipethrough_run_finish ($) { + my ($fh)= @_; + $fh->error and die $!; + close $fh or die "$! $?"; die $? if $?; +} + +sub pipethrough_run ($$$@) { + my ($tf, $childprep, $cmd, @a) = @_; + my $pt= pipethrough_run_along($tf,$childprep,$cmd,@a); my $r; - { undef $/; $!=0; $r= ; } + { undef $/; $!=0; $r= <$pt>; } defined $r or die $!; - close GZ or die "$! $?"; die $? if $?; + pipethrough_run_finish($pt); return $r; } sub pipethrough_run_gzip ($) { @@ -186,4 +199,33 @@ sub cgipostform ($$$) { } } +our %check_tsv_done; + +sub check_tsv_line ($$) { + my ($l, $bad_data_callback) = @_; + my $bad_data= sub { &$bad_data_callback("bad data: line $.: $_[0]"); }; + + chomp($l) or &$bad_data('missing end-of-line'); + + $l !~ m/\P{IsPrint}/ or &$bad_data('nonprinting char(s)'); + $l !~ m/\\/ or &$bad_data('data contains backslashes'); + my @v= split /\t/, $l, -1; + @v==6 or &$bad_data('wrong number of fields'); + my ($commod,$stall) = @v; + + !keys %commods or + defined $commods{$commod} or + &$bad_data("unknown commodity \`$commod'"); + + $stall =~ m/^\p{IsUpper}|^[0-9]/ or &$bad_data("stall not capitalised"); + !exists $check_tsv_done{$commod,$stall} or &$bad_data("repeated data"); + $check_tsv_done{$commod,$stall}= 1; + foreach my $i (2..5) { + my $f= $v[$i]; + $f =~ m/^(|0|[1-9][0-9]{0,5}|\>1000)$/ or &$bad_data("bad field $i"); + ($i % 2) or ($f !~ m/\>/) or &$bad_data("> in field $i price"); + } + return @v; +} + 1;