1
0
mirror of git://projects.qi-hardware.com/wernermisc.git synced 2024-12-18 20:45:00 +02:00
wernermisc/m1r4/dsv/bomshelf

96 lines
1.7 KiB
Plaintext
Raw Normal View History

#!/usr/bin/perl
#
# Source:
# http://downloads.qi-hardware.com/hardware/milkymist_one/bom/rc3/milkymist_one_bom_rc3.ods
# - save as CVS
# - edit to remove embedded newlines
#
sub csv
{
my $s = $_[0];
my @a = ();
while (1) {
if ($s =~ /^"/) {
die unless $s =~ /^"([^"]*)"(,\s*)?/;
push(@a, $1);
$s = $';
return @a unless length $2;
} elsif ($s =~ /,\s*/) {
push(@a, $`);
$s = $';
} else {
push(@a, $s);
return @a;
}
}
}
while (<>) {
chop;
@f = &csv($_);
next unless $f[0] =~ /^[0-9]+$/;
die "duplicate part $f[3]" if defined $cr{$f[3]};
$f[1] =~ s/\(DNP\)//g;
$cr{$f[3]} = $f[1];
$url{$f[3]} = $f[4];
$dsc{$f[3]} = $f[6];
for (&csv($f[1])) {
warn "\"$_\" used for $r{$_} and $f[3]" if defined $r{$_};
$r{$_} = $f[3];
}
}
open(F, "SUPPLEMENT") || die "open SUPPLEMENT: $!";
while (<F>) {
chop;
next if /^\s*#/;
next if /^\s*$/;
if (/^[Cc]:\s*/) {
die "unknown component reference $'" unless defined $r{$'};
$p = $r{$'};
next;
}
if (/^[Pp]:\s*/) {
$p = $';
next if defined $cr{$p};
@a = grep { $_ =~ /^$p/ } keys %cr;
die "key $p* is ambiguous" if scalar @a > 1;
if (!@a) {
@a = grep { $_ =~ /$p/ } keys %cr;
die "key *$p* is ambiguous" if scalar @a > 1;
die "key $p matches nothing" unless @a;
}
$p = $a[0];
next;
}
if (/^[Aa]:\s*/) {
$a{$p} .= "," if defined $a{$p};
$a{$p} .= $';
next;
}
if (/^[Dd]:\s*/) {
$url{$p} = $';
next;
}
die "don't recognize \"$_\"";
}
for (sort keys %cr) {
next if $url{$_} eq "";
print "# $dsc{$_}\n" unless $dsc{$_} eq "";
print "N: $_\n";
for $a (&csv($cr{$_})) {
print "A: $a\n";
}
if (defined $a{$_}) {
for $a (&csv($a{$_})) {
print "A: $a\n";
}
}
print "D: $url{$_}\n\n";
}