chiark / gitweb /
marketdata file decoder
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 9 Jun 2009 11:48:17 +0000 (12:48 +0100)
committerIan Jackson <Ian.Jackson@eu.citrix.com>
Tue, 9 Jun 2009 11:48:17 +0000 (12:48 +0100)
pctb/yppsc-decode-marketdata [new file with mode: 0755]

diff --git a/pctb/yppsc-decode-marketdata b/pctb/yppsc-decode-marketdata
new file mode 100755 (executable)
index 0000000..969ada0
--- /dev/null
@@ -0,0 +1,81 @@
+#!/usr/bin/perl
+
+use IO::Handle;
+
+open CM, "commodmap" or die $!;
+while (<CM>) {
+    m/^(\S.*\S)\t(\d+)$/ or die;
+    $commodmap[$2]= $1;
+}
+die $! if CM->error;
+
+%stallkinds= qw(A Apothecary
+               D Distilling
+               F Furnishing
+               I Ironworking
+               S Shipbuilding
+               T Tailor
+               W Weaving);
+
+sub getline() {
+    $!=0; my $l= <STDIN>; die $! unless defined $l;
+    die $! if STDIN->error;
+    die unless chomp $l;
+#print STDERR "GOT LINE [$l]\n";
+    return $l;
+}
+
+sub getint() {
+    my $b;
+    my $r= read STDIN,$b,2; die $! if STDIN->error;
+    die unless $r==2;
+    my $v= scalar unpack "v", $b;
+#printf STDERR "GOT INT %d 0x%x\n", $v, $v;
+    return $v;
+}
+
+sub inmap($\@$) {
+    my ($what,$ary,$ix) = @_;
+    my $got= $ary->[$ix];
+    return $got if defined $got;
+    die "$what $ix ?";
+}
+    
+
+printf "# Version: \"%s\"\n", getline();
+$nstalls= getline()+0;
+
+while (@stalls < $nstalls) {
+    $_= getline();
+    if (s/\^[A-Z]$//) {
+       $kind= $1;
+       $sk= $stallkinds{$kind};
+       die "kind $kind in $_ ?" unless defined $sk;
+       $_ .= "'s $sk Stall";
+    }
+    push @stalls, $_;
+}
+unshift @stalls, undef;
+
+$|=1;
+
+foreach $bs qw(Buy Sell) {
+    $ncommods= getint();
+    for ($commodnum=0; $commodnum<$ncommods; $commodnum++) {
+       $commodix= getint();
+       $offers= getint();
+       for ($offernum=0; $offernum<$offers; $offernum++) {
+           $stallix= getint();
+           $price= getint();
+           $qty= getint();
+           printf("%s\t%s\t%s",
+                  $bs,
+                  inmap('commod',@commodmap,$commodix),
+                  inmap('stall',@stalls,$stallix)) or die $!;
+           if ($bs eq 'Sell') { print "\t\t" or die $!; }
+           printf("\t%d\t%d", $price, $qty) or die $!;
+           if ($bs eq 'Buy') { print "\t\t" or die $!; }
+           print "\n" or die $!;
+       }
+    }
+}