chiark / gitweb /
Fix to make example2 work
[ypp-sc-tools.db-live.git] / pctb / yppsc-decode-marketdata
1 #!/usr/bin/perl
2
3 use IO::Handle;
4
5 $debug=1 if @ARGV;
6
7 open CM, "commodmap" or die $!;
8 while (<CM>) {
9     m/^(\S.*\S)\t(\d+)$/ or die;
10     $commodmap[$2]= $1;
11 }
12 die $! if CM->error;
13
14 %stallkinds= qw(A Apothecary
15                 D Distilling
16                 F Furnishing
17                 I Ironworking
18                 S Shipbuilding
19                 T Tailor
20                 W Weaving);
21
22 sub getline ($) {
23     my ($w)= @_;
24     $!=0; my $l= <STDIN>; die $! unless defined $l;
25     die $! if STDIN->error;
26     die unless chomp $l;
27     print "GOT FOR $w LINE [$l]\n"
28         if $debug;
29     return $l;
30 }
31
32 sub getint ($) {
33     my ($w)= @_;
34     my $b;
35     my $t= tell STDIN; $t>=0 or die $!;
36     my $r= read STDIN,$b,2; die $! if STDIN->error;
37     die unless $r==2;
38     my $v= scalar unpack "v", $b;
39     printf "GOT AT 0x%x INT FOR %s VALUE %d 0x%x\n", $t, $w, $v, $v
40         if $debug;
41     return $v;
42 }
43
44 sub inmap($\@$) {
45     my ($what,$ary,$ix) = @_;
46     my $got= $ary->[$ix];
47     return $got if defined $got;
48     die "$what $ix ?";
49 }
50     
51
52 printf "# Version: \"%s\"\n", getline("version");
53 $nstalls= getline("nstalls")+0;
54
55 while (@stalls < $nstalls) {
56     $_= getline("stall name ".(@stalls+1));
57     if (s/\^([A-Z])$//) {
58         $kind= $1;
59         $sk= $stallkinds{$kind};
60         die "kind $kind in $_ ?" unless defined $sk;
61         $_ .= "'s $sk Stall";
62     }
63     push @stalls, $_;
64 }
65 unshift @stalls, undef;
66
67 $|=1;
68
69 foreach $bs qw(Buy Sell) {
70     $alloffers_want= getint("Buy ncommods");
71     $alloffers_done=0;
72     while ($alloffers_done < $alloffers_want)  {
73         $commodix= getint("Buy $alloffers_done/$alloffers_want commodix");
74         $offers= getint("Buy $commodnum offers");
75         for ($offernum=0; $offernum<$offers; $offernum++) {
76             $stallix= getint("Buy $commodnum $offernum stallix");
77             $price= getint("Buy $commodnum $offernum price");
78             $qty= getint("Buy $commodnum $offernum qty");
79             printf("%s\t%s\t%s",
80                    $bs,
81                    inmap('commod',@commodmap,$commodix),
82                    inmap('stall',@stalls,$stallix)) or die $!;
83             if ($bs eq 'Sell') { print "\t\t" or die $!; }
84             printf("\t%d\t%d", $price, $qty) or die $!;
85             if ($bs eq 'Buy') { print "\t\t" or die $!; }
86             print "\n" or die $!;
87             $alloffers_done++;
88             die if $alloffers_done > $alloffers_want;
89         }
90     }
91 }
92
93 $r= read STDIN,$b,1;
94 STDIN->error and die $!;
95 STDIN->eof or die;
96 $b and die;