chiark / gitweb /
Put _commodmap.tsv parsing all in Commods.pm
[ypp-sc-tools.db-test.git] / pctb / Commods.pm
index dee9fbed5ab45368da33416d9bf2fe20812b381c..6f3a177ee8870c8ca471521058c4b4d1b0d20728 100644 (file)
@@ -1,5 +1,6 @@
 
 package Commods;
+use IO::File;
 
 use strict;
 use warnings;
@@ -9,7 +10,8 @@ BEGIN {
     our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
     $VERSION     = 1.00;
     @ISA         = qw(Exporter);
-    @EXPORT      = qw(%oceans %commods &parse_masters);
+    @EXPORT      = qw(&parse_masters %oceans %commods
+                     &parse_pctb_commodmap %pctb_commodmap @pctb_commodmap);
     %EXPORT_TAGS = ( );
 
     @EXPORT_OK   = qw();
@@ -20,6 +22,8 @@ our %commods; # eg $commods{'Fine black cloth'}= $sources;
 # $sources = 's[l]b';
 #       's' = Special Circumstances; 'l' = local ; B = with Bleach
 
+our (%pctb_commodmap,@pctb_commodmap);
+
 my %colours; # eg $colours{'c'}{'black'}= $sources
 my @rawcm; # eg $rawcm[0]='fine rum'; $rawcm[1]='fine %c cloth'
 
@@ -78,4 +82,22 @@ sub parse_masters () {
     parse_master_master1('master-master.txt','s');
 }
 
+sub parse_pctb_commodmap () {
+    undef %pctb_commodmap;
+    foreach my $commod (keys %commods) { $commods{$commod} =~ s/b//; }
+
+    my $c= new IO::File '_commodmap.tsv' or die $!;
+    if (!$c) { $!==&ENOENT or die $!; return 0; }
+
+    while (<$c>) {
+       m/^(\S.*\S)\t(\d+)\n$/ or die "$_";
+       die if defined $pctb_commodmap{$1};  $pctb_commodmap{$1}= $2;
+       die if defined $pctb_commodmap[$2];  $pctb_commodmap[$2]= $1;
+       $commods{$1} .= 'b';
+    }
+    $c->error and die $!;
+    close $c or die $!;
+    return 1;
+}
+
 1;